~ubuntu-branches/ubuntu/saucy/libwpd/saucy

« back to all changes in this revision

Viewing changes to src/lib/WP1Heuristics.cpp

  • Committer: Package Import Robot
  • Author(s): Rene Engelhard
  • Date: 2011-11-29 23:31:13 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20111129233113-xdtwca9h0y6wdxst
Tags: 0.9.4-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
1
2
/* libwpd
2
3
 * Copyright (C) 2003 William Lachance (wrlach@gmail.com)
3
4
 * Copyright (C) 2003 Marc Maurer (uwog@uwog.net)
4
5
 * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
5
 
 *  
 
6
 *
6
7
 * This library is free software; you can redistribute it and/or
7
8
 * modify it under the terms of the GNU Library General Public
8
9
 * License as published by the Free Software Foundation; either
20
21
 * For further information visit http://libwpd.sourceforge.net
21
22
 */
22
23
 
23
 
/* "This product is not manufactured, approved, or supported by 
 
24
/* "This product is not manufactured, approved, or supported by
24
25
 * Corel Corporation or Corel Corporation Limited."
25
26
 */
26
27
 
39
40
        try
40
41
        {
41
42
                if (readU8(input, 0) == 0xFE && readU8(input, 0) == 0xFF &&
42
 
                        readU8(input, 0) == 0x61 && readU8(input, 0) == 0x61)
 
43
                        readU8(input, 0) == 0x61 && readU8(input, 0) == 0x61)
43
44
                {
44
45
                        encryption = new WPXEncryption(password, 6);
45
46
                        if (readU16(input, 0, true) == encryption->getCheckSum())
72
73
        try
73
74
        {
74
75
                if (readU8(input, 0) == 0xFE && readU8(input, 0) == 0xFF &&
75
 
                        readU8(input, 0) == 0x61 && readU8(input, 0) == 0x61)
 
76
                        readU8(input, 0) == 0x61 && readU8(input, 0) == 0x61)
76
77
                {
77
78
                        if (password)
78
79
                        {
89
90
                                        return WPD_CONFIDENCE_SUPPORTED_ENCRYPTION;
90
91
                        }
91
92
                }
92
 
                
 
93
 
93
94
                input->seek(0, WPX_SEEK_SET);
94
95
                if (password && encryption)
95
 
                        input->seek(6, WPX_SEEK_SET);   
 
96
                        input->seek(6, WPX_SEEK_SET);
96
97
 
97
98
                int functionGroupCount = 0;
98
 
        
 
99
 
99
100
                WPD_DEBUG_MSG(("WP1Heuristics::isWP1FileFormat()\n"));
100
 
        
 
101
 
101
102
                while (!input->atEOS())
102
103
                {
103
104
                        uint8_t readVal = readU8(input, encryption);
104
105
 
105
106
                        WPD_DEBUG_MSG(("WP1Heuristics, Offset 0x%.8x, value 0x%.2x (%c)\n", (unsigned int)input->tell() - 1, readVal, readVal));
106
 
                
 
107
 
107
108
                        if (readVal < (uint8_t)0x20)
108
109
                        {
109
110
                                // line breaks et al, skip
110
111
                        }
111
112
                        else if (readVal >= (uint8_t)0x20 && readVal <= (uint8_t)0x7F)
112
113
                        {
113
 
                                // normal ASCII characters, skip                        
 
114
                                // normal ASCII characters, skip
114
115
                        }
115
116
                        else if (readVal >= (uint8_t)0x80 && readVal <= (uint8_t)0xBF)
116
117
                        {
124
125
                                // special codes that should not be found as separate functions
125
126
                                return WPD_CONFIDENCE_NONE;
126
127
                        }
127
 
                        else 
 
128
                        else
128
129
                        {
129
130
                                // multi character function group
130
131
                                // check that the size constrains are valid, and that every group_member
131
132
                                // is properly closed at the right place
132
 
                
 
133
 
133
134
                                if (WP1_FUNCTION_GROUP_SIZE[readVal-0xC0] == -1)
134
135
                                {
135
136
                                        // variable length function group
136
137
 
137
138
                                        // We are checking following structure:
138
139
                                        //   <function code>{function length}...{function length}<function code>
139
 
                                        //   that we observed in variable length WP1 functions 
140
 
                                
 
140
                                        //   that we observed in variable length WP1 functions
 
141
 
141
142
                                        unsigned long functionLength = readU32(input, encryption, true);
142
143
                                        if (functionLength > ((std::numeric_limits<uint32_t>::max)() / 2))
143
144
                                        {
163
164
                                                        delete encryption;
164
165
                                                return WPD_CONFIDENCE_NONE;
165
166
                                        }
166
 
                                        
 
167
 
167
168
                                        uint8_t closingGate = 0;
168
169
                                        if (!input->atEOS())
169
170
                                        {
190
191
                                else
191
192
                                {
192
193
                                        // fixed length function group
193
 
                                
 
194
 
194
195
                                        // seek to the position where the closing gate should be
195
196
                                        int res = input->seek(WP1_FUNCTION_GROUP_SIZE[readVal-0xC0]-2, WPX_SEEK_CUR);
196
197
                                        // when passed the complete file, we should be able to do that
209
210
                                                        delete encryption;
210
211
                                                return WPD_CONFIDENCE_NONE;
211
212
                                        }
212
 
                                
 
213
 
213
214
                                        functionGroupCount++;
214
215
                                }
215
216
                        }
216
 
                }       
 
217
                }
217
218
 
218
219
                /* When we get here, the document is in a format that we *could* import properly.
219
220
                However, if we didn't encounter a single WP1 function group) we need to be more carefull:
239
240
                return WPD_CONFIDENCE_NONE;
240
241
        }
241
242
}
 
243
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */