| … | |
… | |
| 39 | int res=0; |
39 | int res=0; |
| 40 | int len=0; |
40 | int len=0; |
| 41 | psiconv_u32 temp; |
41 | psiconv_u32 temp; |
| 42 | |
42 | |
| 43 | psiconv_progress(lev+1,off+len,"Going to read the header section"); |
43 | psiconv_progress(lev+1,off+len,"Going to read the header section"); |
| 44 | (*result) = malloc(sizeof(**result)); |
44 | if (!((*result) = malloc(sizeof(**result)))) |
|
|
45 | goto ERROR1; |
| 45 | |
46 | |
| 46 | psiconv_progress(lev+2,off+len,"Going to read UID1 to UID3"); |
47 | psiconv_progress(lev+2,off+len,"Going to read UID1 to UID3"); |
| 47 | (*result)->uid1 = psiconv_read_u32(buf,lev+2,off+len); |
48 | (*result)->uid1 = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
49 | if (!res) |
|
|
50 | goto ERROR2; |
| 48 | psiconv_debug(lev+2,off+len,"UID1: %08x",(*result)->uid1); |
51 | psiconv_debug(lev+2,off+len,"UID1: %08x",(*result)->uid1); |
| 49 | if ((*result)->uid1 == PSICONV_ID_CLIPART) { |
52 | if ((*result)->uid1 == PSICONV_ID_CLIPART) { |
| 50 | /* That's all folks... */ |
53 | /* That's all folks... */ |
| 51 | (*result)->file = psiconv_clipart_file; |
54 | (*result)->file = psiconv_clipart_file; |
| 52 | (*result)->uid2 = 0; |
55 | (*result)->uid2 = 0; |
| … | |
… | |
| 57 | goto DONE; |
60 | goto DONE; |
| 58 | } |
61 | } |
| 59 | if ((*result)->uid1 != PSICONV_ID_PSION5) { |
62 | if ((*result)->uid1 != PSICONV_ID_PSION5) { |
| 60 | psiconv_warn(lev+2,off+len,"UID1 has unknown value. This is probably " |
63 | psiconv_warn(lev+2,off+len,"UID1 has unknown value. This is probably " |
| 61 | "not a (parsable) Psion 5 file"); |
64 | "not a (parsable) Psion 5 file"); |
| 62 | res = -1; |
65 | res = -PSICONV_E_PARSE; |
|
|
66 | goto ERROR2; |
| 63 | } |
67 | } |
| 64 | len += 4; |
68 | len += 4; |
| 65 | (*result)->uid2 = psiconv_read_u32(buf,lev+2,off+len); |
69 | (*result)->uid2 = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
70 | if (res) |
|
|
71 | goto ERROR2; |
| 66 | psiconv_debug(lev+2,off+len,"UID2: %08x",(*result)->uid2); |
72 | psiconv_debug(lev+2,off+len,"UID2: %08x",(*result)->uid2); |
| 67 | len += 4; |
73 | len += 4; |
| 68 | (*result)->uid3 = psiconv_read_u32(buf,lev+2,off+len); |
74 | (*result)->uid3 = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
75 | if (res) |
|
|
76 | goto ERROR2; |
| 69 | psiconv_debug(lev+2,off+len,"UID3: %08x",(*result)->uid3); |
77 | psiconv_debug(lev+2,off+len,"UID3: %08x",(*result)->uid3); |
| 70 | len += 4; |
78 | len += 4; |
| 71 | |
79 | |
| 72 | (*result)->file = psiconv_unknown_file; |
80 | (*result)->file = psiconv_unknown_file; |
| 73 | if ((*result)->uid1 == PSICONV_ID_PSION5) { |
81 | if ((*result)->uid1 == PSICONV_ID_PSION5) { |
| … | |
… | |
| 93 | psiconv_warn(lev+2,off+len,"Unknown file type"); |
101 | psiconv_warn(lev+2,off+len,"Unknown file type"); |
| 94 | (*result)->file = psiconv_unknown_file; |
102 | (*result)->file = psiconv_unknown_file; |
| 95 | } |
103 | } |
| 96 | |
104 | |
| 97 | psiconv_progress(lev+2,off+len,"Checking UID4"); |
105 | psiconv_progress(lev+2,off+len,"Checking UID4"); |
| 98 | temp = psiconv_read_u32(buf,lev+2,off+len); |
106 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
107 | if (res) |
|
|
108 | goto ERROR2; |
| 99 | if (temp == psiconv_checkuid((*result)->uid1,(*result)->uid2, |
109 | if (temp == psiconv_checkuid((*result)->uid1,(*result)->uid2, |
| 100 | (*result)->uid3)) |
110 | (*result)->uid3)) |
| 101 | psiconv_debug(lev+2,off+len,"Checksum %08x is correct",temp); |
111 | psiconv_debug(lev+2,off+len,"Checksum %08x is correct",temp); |
| 102 | else { |
112 | else { |
| 103 | psiconv_warn(lev+2,off+len,"Checksum failed, file corrupted!"); |
113 | psiconv_warn(lev+2,off+len,"Checksum failed, file corrupted!"); |
| 104 | psiconv_debug(lev+2,off+len,"Expected checksum %08x, found %08x", |
114 | psiconv_debug(lev+2,off+len,"Expected checksum %08x, found %08x", |
| 105 | psiconv_checkuid((*result)->uid1,(*result)->uid2, |
115 | psiconv_checkuid((*result)->uid1,(*result)->uid2, |
| 106 | (*result)->uid3),temp); |
116 | (*result)->uid3),temp); |
| 107 | res = -1; |
117 | res = -PSICONV_E_PARSE; |
|
|
118 | goto ERROR2; |
| 108 | } |
119 | } |
| 109 | len += 4; |
120 | len += 4; |
| 110 | |
121 | |
| 111 | DONE: |
122 | DONE: |
| 112 | if (length) |
123 | if (length) |
| … | |
… | |
| 114 | |
125 | |
| 115 | psiconv_progress(lev+1,off+len-1, |
126 | psiconv_progress(lev+1,off+len-1, |
| 116 | "End of Header Section (total length: %08x)",len); |
127 | "End of Header Section (total length: %08x)",len); |
| 117 | |
128 | |
| 118 | return res; |
129 | return res; |
|
|
130 | |
|
|
131 | ERROR2: |
|
|
132 | free(*result); |
|
|
133 | ERROR1: |
|
|
134 | psiconv_warn(lev+1,off,"Reading of Header Section failed"); |
|
|
135 | if (length) |
|
|
136 | *length = 0; |
|
|
137 | if (res == 0) |
|
|
138 | return -PSICONV_E_NOMEM; |
|
|
139 | else |
|
|
140 | return res; |
| 119 | } |
141 | } |
| 120 | |
142 | |
| 121 | int psiconv_parse_section_table_section(const psiconv_buffer buf, int lev, |
143 | int psiconv_parse_section_table_section(const psiconv_buffer buf, int lev, |
| 122 | psiconv_u32 off, int *length, |
144 | psiconv_u32 off, int *length, |
| 123 | psiconv_section_table_section *result) |
145 | psiconv_section_table_section *result) |
| … | |
… | |
| 128 | |
150 | |
| 129 | int i; |
151 | int i; |
| 130 | psiconv_u8 nr; |
152 | psiconv_u8 nr; |
| 131 | |
153 | |
| 132 | psiconv_progress(lev+1,off+len,"Going to read the section table section"); |
154 | psiconv_progress(lev+1,off+len,"Going to read the section table section"); |
| 133 | *result = psiconv_list_new(sizeof(*entry)); |
155 | if (!(*result = psiconv_list_new(sizeof(*entry)))) |
|
|
156 | goto ERROR1; |
| 134 | |
157 | |
| 135 | psiconv_progress(lev+2,off+len,"Going to read the section table length"); |
158 | psiconv_progress(lev+2,off+len,"Going to read the section table length"); |
| 136 | nr = psiconv_read_u8(buf,lev+2,off+len); |
159 | nr = psiconv_read_u8(buf,lev+2,off+len,&res); |
|
|
160 | if (res) |
|
|
161 | goto ERROR2; |
| 137 | psiconv_debug(lev+2,off+len,"Length: %08x",nr); |
162 | psiconv_debug(lev+2,off+len,"Length: %08x",nr); |
| 138 | if (nr & 0x01) { |
163 | if (nr & 0x01) { |
| 139 | psiconv_warn(lev+2,off+len, |
164 | psiconv_warn(lev+2,off+len, |
| 140 | "Section table length odd - ignoring last entry"); |
165 | "Section table length odd - ignoring last entry"); |
| 141 | res = -1; |
|
|
| 142 | } |
166 | } |
| 143 | len ++; |
167 | len ++; |
| 144 | |
168 | |
| 145 | psiconv_progress(lev+2,off+len,"Going to read the section table entries"); |
169 | psiconv_progress(lev+2,off+len,"Going to read the section table entries"); |
| 146 | entry = malloc(sizeof(*entry)); |
170 | entry = malloc(sizeof(*entry)); |
| 147 | for (i = 0; i < nr / 2; i++) { |
171 | for (i = 0; i < nr / 2; i++) { |
| 148 | entry->id = psiconv_read_u32(buf,lev+2,off + len); |
172 | entry->id = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
173 | if (res) |
|
|
174 | goto ERROR3; |
| 149 | psiconv_debug(lev+2,off + len,"Entry %d: ID = %08x",i,entry->id); |
175 | psiconv_debug(lev+2,off + len,"Entry %d: ID = %08x",i,entry->id); |
| 150 | len += 0x04; |
176 | len += 0x04; |
| 151 | entry->offset = psiconv_read_u32(buf,lev+2,off + len); |
177 | entry->offset = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
178 | if (res) |
|
|
179 | goto ERROR3; |
| 152 | psiconv_debug(lev+2,off +len,"Entry %d: Offset = %08x",i,entry->offset); |
180 | psiconv_debug(lev+2,off +len,"Entry %d: Offset = %08x",i,entry->offset); |
| 153 | len += 0x04; |
181 | len += 0x04; |
| 154 | psiconv_list_add(*result,entry); |
182 | if ((res=psiconv_list_add(*result,entry))) |
|
|
183 | goto ERROR3; |
| 155 | } |
184 | } |
| 156 | |
185 | |
| 157 | free(entry); |
186 | free(entry); |
| 158 | |
187 | |
| 159 | if (length) |
188 | if (length) |
| 160 | *length = len; |
189 | *length = len; |
| 161 | |
190 | |
| 162 | psiconv_progress(lev+1,off+len-1,"End of section table section " |
191 | psiconv_progress(lev+1,off+len-1,"End of section table section " |
| 163 | "(total length: %08x)", len); |
192 | "(total length: %08x)", len); |
| 164 | |
193 | |
|
|
194 | return 0; |
|
|
195 | ERROR3: |
|
|
196 | free(entry); |
|
|
197 | ERROR2: |
|
|
198 | psiconv_list_free(*result); |
|
|
199 | ERROR1: |
|
|
200 | psiconv_warn(lev+1,off,"Reading of Section Table Section failed"); |
|
|
201 | if (length) |
|
|
202 | *length = 0; |
|
|
203 | if (res == 0) |
|
|
204 | return -PSICONV_E_NOMEM; |
|
|
205 | else |
| 165 | return res; |
206 | return res; |
| 166 | } |
207 | } |
| 167 | |
208 | |
| 168 | int psiconv_parse_application_id_section(const psiconv_buffer buf, int lev, |
209 | int psiconv_parse_application_id_section(const psiconv_buffer buf, int lev, |
| 169 | psiconv_u32 off, int *length, |
210 | psiconv_u32 off, int *length, |
| 170 | psiconv_application_id_section *result) |
211 | psiconv_application_id_section *result) |
| … | |
… | |
| 172 | int res=0; |
213 | int res=0; |
| 173 | int len=0; |
214 | int len=0; |
| 174 | int leng; |
215 | int leng; |
| 175 | |
216 | |
| 176 | psiconv_progress(lev+1,off,"Going to read the application id section"); |
217 | psiconv_progress(lev+1,off,"Going to read the application id section"); |
| 177 | (*result) = malloc(sizeof(**result)); |
218 | if (!(*result = malloc(sizeof(**result)))) |
|
|
219 | goto ERROR1; |
| 178 | |
220 | |
| 179 | psiconv_progress(lev+2,off+len,"Going to read the type identifier"); |
221 | psiconv_progress(lev+2,off+len,"Going to read the type identifier"); |
| 180 | (*result)->id = psiconv_read_u32(buf,lev+2,off+len); |
222 | (*result)->id = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
223 | if (res) |
|
|
224 | goto ERROR2; |
| 181 | psiconv_debug(lev+2,off+len,"Identifier: %08x",(*result)->id); |
225 | psiconv_debug(lev+2,off+len,"Identifier: %08x",(*result)->id); |
| 182 | len += 4; |
226 | len += 4; |
| 183 | |
227 | |
| 184 | psiconv_progress(lev+2,off+len,"Going to read the application id string"); |
228 | psiconv_progress(lev+2,off+len,"Going to read the application id string"); |
| 185 | (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng); |
229 | (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res); |
|
|
230 | if (res) |
|
|
231 | goto ERROR2; |
| 186 | len += leng; |
232 | len += leng; |
| 187 | |
233 | |
| 188 | if (length) |
234 | if (length) |
| 189 | *length = len; |
235 | *length = len; |
| 190 | |
236 | |
| 191 | psiconv_progress(lev+1,off+len-1,"End of application id section " |
237 | psiconv_progress(lev+1,off+len-1,"End of application id section " |
| 192 | "(total length: %08x", len); |
238 | "(total length: %08x", len); |
| 193 | |
239 | |
| 194 | return res; |
240 | return res; |
|
|
241 | ERROR2: |
|
|
242 | free(*result); |
|
|
243 | ERROR1: |
|
|
244 | psiconv_warn(lev+1,off,"Reading of Application ID Section failed"); |
|
|
245 | if (length) |
|
|
246 | *length = 0; |
|
|
247 | if (res == 0) |
|
|
248 | return -PSICONV_E_NOMEM; |
|
|
249 | else |
|
|
250 | return res; |
| 195 | } |
251 | } |
| 196 | |
252 | |
| 197 | int psiconv_parse_text_section(const psiconv_buffer buf,int lev,psiconv_u32 off, |
253 | int psiconv_parse_text_section(const psiconv_buffer buf,int lev,psiconv_u32 off, |
| 198 | int *length,psiconv_text_and_layout *result) |
254 | int *length,psiconv_text_and_layout *result) |
| 199 | { |
255 | { |
| … | |
… | |
| 203 | |
259 | |
| 204 | psiconv_u32 text_len; |
260 | psiconv_u32 text_len; |
| 205 | psiconv_paragraph para; |
261 | psiconv_paragraph para; |
| 206 | |
262 | |
| 207 | int nr; |
263 | int nr; |
| 208 | int i,j,start,leng; |
264 | int i,j,start,leng,temp; |
| 209 | char *str_copy; |
265 | char *str_copy; |
| 210 | |
266 | |
| 211 | psiconv_progress(lev+1,off,"Going to parse the text section"); |
267 | psiconv_progress(lev+1,off,"Going to parse the text section"); |
| 212 | psiconv_progress(lev+2,off,"Reading the text length"); |
268 | psiconv_progress(lev+2,off,"Reading the text length"); |
|
|
269 | |
|
|
270 | if(!(*result = psiconv_list_new(sizeof(*para)))) |
|
|
271 | goto ERROR1; |
|
|
272 | if (!(para = malloc(sizeof(*para)))) |
|
|
273 | goto ERROR2; |
|
|
274 | |
| 213 | text_len = psiconv_read_X(buf,lev+2,off,&leng); |
275 | text_len = psiconv_read_X(buf,lev+2,off,&leng,&res); |
|
|
276 | if (!res) |
|
|
277 | goto ERROR3; |
| 214 | psiconv_debug(lev+2,off,"Length: %08x",text_len); |
278 | psiconv_debug(lev+2,off,"Length: %08x",text_len); |
| 215 | len += leng; |
279 | len += leng; |
| 216 | |
|
|
| 217 | *result = psiconv_list_new(sizeof(*para)); |
|
|
| 218 | para = malloc(sizeof(*para)); |
|
|
| 219 | |
280 | |
| 220 | psiconv_progress(lev+2,off+len,"Going to read all paragraph text"); |
281 | psiconv_progress(lev+2,off+len,"Going to read all paragraph text"); |
| 221 | nr = 0; |
282 | nr = 0; |
| 222 | start = 0; |
283 | start = 0; |
| 223 | for (i = 0; i < text_len; i++) |
284 | for (i = 0; i < text_len; i++) { |
| 224 | if (psiconv_read_u8(buf,lev+2,off+len+i) == 0x06) { |
285 | temp = psiconv_read_u8(buf,lev+2,off+len+i,&res); |
|
|
286 | if (res) |
|
|
287 | goto ERROR3; |
|
|
288 | if (temp == 0x06) { |
| 225 | para->text = malloc(i - start + 1); |
289 | if (!(para->text = malloc(i - start + 1))) |
|
|
290 | goto ERROR3; |
| 226 | for (j = 0; j < i - start; j++) |
291 | for (j = 0; j < i - start; j++) { |
| 227 | para->text[j] = psiconv_read_u8(buf,lev+1,off + len + start + j); |
292 | temp = psiconv_read_u8(buf,lev+1,off + len + start + j,&res); |
|
|
293 | if (res) |
|
|
294 | goto ERROR4; |
|
|
295 | para->text[j] = temp; |
|
|
296 | } |
| 228 | para->text[j] = 0; |
297 | para->text[j] = 0; |
| 229 | |
298 | |
| 230 | psiconv_list_add(*result,para); |
299 | if ((res = psiconv_list_add(*result,para))) |
|
|
300 | goto ERROR4; |
| 231 | |
301 | |
| 232 | str_copy = psiconv_make_printable(para->text); |
302 | if (!(str_copy = psiconv_make_printable(para->text))) |
|
|
303 | goto ERROR3; |
| 233 | psiconv_debug(lev+2,off+i+len,"Line %d: %d characters",nr, |
304 | psiconv_debug(lev+2,off+i+len,"Line %d: %d characters",nr, |
| 234 | strlen(str_copy) +1); |
305 | strlen(str_copy) +1); |
| 235 | psiconv_debug(lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
306 | psiconv_debug(lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
| 236 | free(str_copy); |
307 | free(str_copy); |
| 237 | |
308 | |
| 238 | start = i + 1; |
309 | start = i + 1; |
| 239 | nr ++; |
310 | nr ++; |
| 240 | } |
311 | } |
|
|
312 | } |
| 241 | |
313 | |
| 242 | if (start != text_len) { |
314 | if (start != text_len) { |
| 243 | res = -1; |
|
|
| 244 | psiconv_warn(lev+2,off+start+len, |
315 | psiconv_warn(lev+2,off+start+len, |
| 245 | "Last line does not end on EOL (%d characters left)", len - start); |
316 | "Last line does not end on EOL (%d characters left)", len - start); |
| 246 | para->text = malloc(text_len - start + 1); |
317 | if (!(para->text = malloc(text_len - start + 1))) |
|
|
318 | goto ERROR3; |
| 247 | for (j = 0; j < text_len - start; j++) |
319 | for (j = 0; j < text_len - start; j++) { |
| 248 | para->text[j] = psiconv_read_u8(buf,lev+2,off + start + j + len); |
320 | temp = psiconv_read_u8(buf,lev+2,off + start + j + len, &res); |
|
|
321 | if (res) |
|
|
322 | goto ERROR4; |
|
|
323 | para->text[j] = temp; |
|
|
324 | } |
| 249 | para->text[text_len - start] = 0; |
325 | para->text[text_len - start] = 0; |
| 250 | psiconv_list_add(*result,para); |
326 | if ((res = psiconv_list_add(*result,para))) |
|
|
327 | goto ERROR4; |
| 251 | str_copy = psiconv_make_printable(para->text); |
328 | if (!(str_copy = psiconv_make_printable(para->text))) |
|
|
329 | goto ERROR3; |
| 252 | psiconv_debug(lev+2,off+start+len,"Last line: %d characters",nr, |
330 | psiconv_debug(lev+2,off+start+len,"Last line: %d characters",nr, |
| 253 | strlen(str_copy)+1); |
331 | strlen(str_copy)+1); |
| 254 | psiconv_debug(lev+2,off+start+len,"Last line: `%s'",str_copy); |
332 | psiconv_debug(lev+2,off+start+len,"Last line: `%s'",str_copy); |
| 255 | free(str_copy); |
333 | free(str_copy); |
| 256 | } |
334 | } |
| 257 | |
335 | |
| 258 | free(para); |
336 | free(para); |
| 259 | |
337 | |
| 260 | /* Initialize the remaining parts of each paragraph */ |
338 | /* Initialize the remaining parts of each paragraph */ |
| 261 | for (i = 0; i < psiconv_list_length(*result); i ++) { |
339 | for (i = 0; i < psiconv_list_length(*result); i ++) { |
| 262 | para = psiconv_list_get(*result,i); |
340 | if (!(para = psiconv_list_get(*result,i))) { |
| 263 | para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout_s)); |
341 | psiconv_warn(lev+2,off+len,"Massive memory corruption"); |
| 264 | para->replacements = psiconv_list_new(sizeof(struct psiconv_replacement_s)); |
342 | goto ERROR2_0; |
|
|
343 | } |
|
|
344 | if (!(para->in_lines = psiconv_list_new(sizeof( |
|
|
345 | struct psiconv_in_line_layout_s)))) |
|
|
346 | goto ERROR2_0; |
|
|
347 | if (!(para->replacements = psiconv_list_new(sizeof( |
|
|
348 | struct psiconv_replacement_s)))) |
|
|
349 | goto ERROR2_1; |
|
|
350 | if (!(para->base_character = psiconv_basic_character_layout())) |
|
|
351 | goto ERROR2_2; |
|
|
352 | if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) |
|
|
353 | goto ERROR2_3; |
| 265 | para->base_style = 0; |
354 | para->base_style = 0; |
| 266 | para->base_character = psiconv_basic_character_layout(); |
|
|
| 267 | para->base_paragraph = psiconv_basic_paragraph_layout(); |
|
|
| 268 | } |
355 | } |
| 269 | |
356 | |
| 270 | |
357 | |
| 271 | len += text_len; |
358 | len += text_len; |
| 272 | |
359 | |
| … | |
… | |
| 275 | |
362 | |
| 276 | psiconv_progress(lev+1,off+len-1,"End of text section (total length: %08x", |
363 | psiconv_progress(lev+1,off+len-1,"End of text section (total length: %08x", |
| 277 | len); |
364 | len); |
| 278 | |
365 | |
| 279 | return res; |
366 | return res; |
|
|
367 | |
|
|
368 | ERROR2_3: |
|
|
369 | psiconv_free_character_layout(para->base_character); |
|
|
370 | ERROR2_2: |
|
|
371 | psiconv_list_free(para->replacements); |
|
|
372 | ERROR2_1: |
|
|
373 | psiconv_list_free(para->in_lines); |
|
|
374 | ERROR2_0: |
|
|
375 | for (j = 0; j < i; j++) { |
|
|
376 | if (!(para = psiconv_list_get(*result,j))) { |
|
|
377 | psiconv_warn(lev+1,off,"Massive memory corruption..."); |
|
|
378 | break; |
|
|
379 | } |
|
|
380 | psiconv_list_free(para->in_lines); |
|
|
381 | psiconv_list_free(para->replacements); |
|
|
382 | psiconv_free_character_layout(para->base_character); |
|
|
383 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
384 | } |
|
|
385 | goto ERROR2; |
|
|
386 | |
|
|
387 | ERROR4: |
|
|
388 | free(para->text); |
|
|
389 | ERROR3: |
|
|
390 | free(para); |
|
|
391 | ERROR2: |
|
|
392 | for (i = 0; i < psiconv_list_length(*result);i++) { |
|
|
393 | if (!(para = psiconv_list_get(*result,i))) { |
|
|
394 | psiconv_warn(lev+1,off,"Massive memory corruption..."); |
|
|
395 | break; |
|
|
396 | } |
|
|
397 | free(para->text); |
|
|
398 | } |
|
|
399 | psiconv_list_free(*result); |
|
|
400 | ERROR1: |
|
|
401 | psiconv_warn(lev+1,off,"Reading of Text Section failed"); |
|
|
402 | if (length) |
|
|
403 | *length = 0; |
|
|
404 | if (!res) |
|
|
405 | return -PSICONV_E_NOMEM; |
|
|
406 | else |
|
|
407 | return res; |
| 280 | } |
408 | } |
| 281 | |
409 | |
| 282 | /* First do a parse_text_section, or you will get into trouble here */ |
410 | /* First do a parse_text_section, or you will get into trouble here */ |
| 283 | int psiconv_parse_layout_section(const psiconv_buffer buf, |
411 | int psiconv_parse_layout_section(const psiconv_buffer buf, |
| 284 | int lev,psiconv_u32 off, |
412 | int lev,psiconv_u32 off, |
| … | |
… | |
| 304 | |
432 | |
| 305 | anon_style_list anon_styles; |
433 | anon_style_list anon_styles; |
| 306 | struct anon_style_s anon; |
434 | struct anon_style_s anon; |
| 307 | anon_style anon_ptr=NULL; |
435 | anon_style anon_ptr=NULL; |
| 308 | |
436 | |
|
|
437 | psiconv_character_layout temp_char; |
|
|
438 | psiconv_paragraph_layout temp_para; |
|
|
439 | psiconv_word_style temp_style; |
| 309 | psiconv_paragraph para; |
440 | psiconv_paragraph para; |
| 310 | struct psiconv_in_line_layout_s in_line; |
441 | struct psiconv_in_line_layout_s in_line; |
| 311 | |
442 | |
| 312 | int *inline_count; |
443 | int *inline_count; |
| 313 | |
444 | |
| 314 | |
445 | |
| 315 | psiconv_progress(lev+1,off,"Going to read the layout section"); |
446 | psiconv_progress(lev+1,off,"Going to read the layout section"); |
| 316 | |
447 | |
| 317 | psiconv_progress(lev+2,off,"Going to read the section type"); |
448 | psiconv_progress(lev+2,off,"Going to read the section type"); |
| 318 | temp = psiconv_read_u16(buf,lev+2,off+len); |
449 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
450 | if (res) |
|
|
451 | goto ERROR1; |
| 319 | psiconv_debug(lev+2,off+len,"Type: %02x",temp); |
452 | psiconv_debug(lev+2,off+len,"Type: %02x",temp); |
| 320 | parse_styles = with_styles; |
453 | parse_styles = with_styles; |
| 321 | if ((temp == 0x0001) && !with_styles) { |
454 | if ((temp == 0x0001) && !with_styles) { |
| 322 | psiconv_warn(lev+2,off+len,"Styleless layout section expected, " |
455 | psiconv_warn(lev+2,off+len,"Styleless layout section expected, " |
| 323 | "but styled section found!"); |
456 | "but styled section found!"); |
| 324 | parse_styles = 1; |
457 | parse_styles = 1; |
| 325 | res = -1; |
|
|
| 326 | } else if ((temp == 0x0000) && (with_styles)) { |
458 | } else if ((temp == 0x0000) && (with_styles)) { |
| 327 | psiconv_warn(lev+2,off+len,"Styled layout section expected, " |
459 | psiconv_warn(lev+2,off+len,"Styled layout section expected, " |
| 328 | "but styleless section found!"); |
460 | "but styleless section found!"); |
| 329 | parse_styles = 0; |
461 | parse_styles = 0; |
| 330 | res = -1; |
|
|
| 331 | } else if ((temp != 0x0000) && (temp != 0x0001)) { |
462 | } else if ((temp != 0x0000) && (temp != 0x0001)) { |
| 332 | psiconv_warn(lev+2,off+len, |
463 | psiconv_warn(lev+2,off+len, |
| 333 | "Layout section type indicator has unknown value!"); |
464 | "Layout section type indicator has unknown value!"); |
| 334 | res = -1; |
|
|
| 335 | } |
465 | } |
| 336 | len += 0x02; |
466 | len += 0x02; |
| 337 | |
467 | |
| 338 | psiconv_progress(lev+2,off+len,"Going to read paragraph type list"); |
468 | psiconv_progress(lev+2,off+len,"Going to read paragraph type list"); |
| 339 | anon_styles = psiconv_list_new(sizeof(anon)); |
469 | if (!(anon_styles = psiconv_list_new(sizeof(anon)))) |
|
|
470 | goto ERROR1; |
| 340 | psiconv_progress(lev+3,off+len,"Going to read paragraph type list length"); |
471 | psiconv_progress(lev+3,off+len,"Going to read paragraph type list length"); |
| 341 | nr = psiconv_read_u8(buf,lev+3,off+len); |
472 | nr = psiconv_read_u8(buf,lev+3,off+len,&res); |
|
|
473 | if (res) |
|
|
474 | goto ERROR2; |
| 342 | psiconv_debug(lev+3,off+len,"Length: %02x",nr); |
475 | psiconv_debug(lev+3,off+len,"Length: %02x",nr); |
| 343 | len ++; |
476 | len ++; |
| 344 | |
477 | |
| 345 | psiconv_progress(lev+3,off+len, |
478 | psiconv_progress(lev+3,off+len, |
| 346 | "Going to read the paragraph type list elements"); |
479 | "Going to read the paragraph type list elements"); |
| 347 | for (i = 0; i < nr; i ++) { |
480 | for (i = 0; i < nr; i ++) { |
| 348 | psiconv_progress(lev+3,off+len,"Element %d",i); |
481 | psiconv_progress(lev+3,off+len,"Element %d",i); |
| 349 | anon.nr = psiconv_read_u32(buf,lev+4,off+len); |
482 | anon.nr = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
483 | if (res) |
|
|
484 | goto ERROR3; |
| 350 | psiconv_debug(lev+4,off+len,"Number: %08x",anon.nr); |
485 | psiconv_debug(lev+4,off+len,"Number: %08x",anon.nr); |
| 351 | len += 0x04; |
486 | len += 0x04; |
| 352 | |
487 | |
| 353 | psiconv_progress(lev+4,off,"Going to determine the base style"); |
488 | psiconv_progress(lev+4,off,"Going to determine the base style"); |
| 354 | if (parse_styles) { |
489 | if (parse_styles) { |
|
|
490 | temp = psiconv_read_u32(buf,lev+4, off+len,&res); |
|
|
491 | if (res) |
|
|
492 | goto ERROR3; |
| 355 | anon.base_style = psiconv_read_u8(buf,lev+3, |
493 | anon.base_style = psiconv_read_u8(buf,lev+3, off+len+4+temp,&res); |
| 356 | off+len+4+psiconv_read_u32(buf,lev+4, |
494 | if (res) |
| 357 | off+len)); |
495 | goto ERROR3; |
| 358 | psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len), |
496 | psiconv_debug(lev+4,off+len+temp, |
| 359 | "Style indicator: %02x",anon.base_style); |
497 | "Style indicator: %02x",anon.base_style); |
| 360 | } else |
498 | } else |
| 361 | anon.base_style = 0; |
499 | anon.base_style = 0; |
|
|
500 | if (!(temp_style = psiconv_get_style(styles,anon.base_style))) { |
|
|
501 | psiconv_warn(lev+4,off,"Unknown Style referenced"); |
|
|
502 | if (!(temp_style = psiconv_get_style(styles,anon.base_style))) { |
|
|
503 | psiconv_warn(lev+4,off,"Base style unknown"); |
|
|
504 | goto ERROR3; |
|
|
505 | } |
|
|
506 | } |
| 362 | anon.paragraph = psiconv_clone_paragraph_layout(psiconv_get_style |
507 | if (!(anon.paragraph = psiconv_clone_paragraph_layout |
| 363 | (styles,anon.base_style)->paragraph); |
508 | (temp_style->paragraph))) |
|
|
509 | goto ERROR3; |
| 364 | anon.character = psiconv_clone_character_layout(psiconv_get_style |
510 | if (!(anon.character = psiconv_clone_character_layout |
| 365 | (styles,anon.base_style)->character); |
511 | (temp_style->character))) |
|
|
512 | goto ERROR3_1; |
| 366 | |
513 | |
| 367 | psiconv_progress(lev+4,off+len,"Going to read the paragraph layout"); |
514 | psiconv_progress(lev+4,off+len,"Going to read the paragraph layout"); |
| 368 | res |= psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
515 | if ((res = psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
| 369 | anon.paragraph); |
516 | anon.paragraph))) |
|
|
517 | goto ERROR3_2; |
| 370 | len += leng; |
518 | len += leng; |
| 371 | if (parse_styles) |
519 | if (parse_styles) |
| 372 | len ++; |
520 | len ++; |
| 373 | |
521 | |
| 374 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
522 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
| 375 | res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
523 | if ((res = psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
| 376 | anon.character); |
524 | anon.character))) |
|
|
525 | goto ERROR3_2; |
| 377 | len += leng; |
526 | len += leng; |
| 378 | psiconv_list_add(anon_styles,&anon); |
527 | if ((res = psiconv_list_add(anon_styles,&anon))) |
|
|
528 | goto ERROR3_2; |
| 379 | } |
529 | } |
| 380 | |
530 | |
| 381 | psiconv_progress(lev+2,off+len,"Going to parse the paragraph element list"); |
531 | psiconv_progress(lev+2,off+len,"Going to parse the paragraph element list"); |
| 382 | psiconv_progress(lev+3,off+len,"Going to read the number of paragraphs"); |
532 | psiconv_progress(lev+3,off+len,"Going to read the number of paragraphs"); |
| 383 | nr = psiconv_read_u32(buf,lev+3,off+len); |
533 | nr = psiconv_read_u32(buf,lev+3,off+len,&res); |
|
|
534 | if (res) |
|
|
535 | goto ERROR3; |
| 384 | if (nr != psiconv_list_length(result)) { |
536 | if (nr != psiconv_list_length(result)) { |
| 385 | psiconv_warn(lev+3,off+len, |
537 | psiconv_warn(lev+3,off+len, |
| 386 | "Number of text paragraphs and paragraph elements does not match"); |
538 | "Number of text paragraphs and paragraph elements does not match"); |
| 387 | psiconv_debug(lev+3,off+len, |
539 | psiconv_debug(lev+3,off+len, |
| 388 | "%d text paragraphs, %d paragraph elements", |
540 | "%d text paragraphs, %d paragraph elements", |
| 389 | psiconv_list_length(result),nr); |
541 | psiconv_list_length(result),nr); |
| 390 | } |
542 | } |
| 391 | psiconv_debug(lev+3,off+len,"Number of paragraphs: %d",nr); |
543 | psiconv_debug(lev+3,off+len,"Number of paragraphs: %d",nr); |
| 392 | len += 4; |
544 | len += 4; |
| 393 | inline_count = malloc(nr * sizeof(*inline_count)); |
545 | if (!(inline_count = malloc(nr * sizeof(*inline_count)))) |
|
|
546 | goto ERROR3; |
| 394 | |
547 | |
| 395 | psiconv_progress(lev+3,off+len,"Going to read the paragraph elements"); |
548 | psiconv_progress(lev+3,off+len,"Going to read the paragraph elements"); |
| 396 | for (i = 0; i < nr; i ++) { |
549 | for (i = 0; i < nr; i ++) { |
| 397 | psiconv_progress(lev+3,off+len,"Element %d",i); |
550 | psiconv_progress(lev+3,off+len,"Element %d",i); |
| 398 | if (i >= psiconv_list_length(result)) { |
551 | if (i >= psiconv_list_length(result)) { |
| 399 | psiconv_debug(lev+4,off+len,"Going to allocate a new element"); |
552 | psiconv_debug(lev+4,off+len,"Going to allocate a new element"); |
| 400 | para = malloc(sizeof(*para)); |
553 | if (!(para = malloc(sizeof(*para)))) |
| 401 | para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout_s)); |
554 | goto ERROR4; |
|
|
555 | if (!(para->in_lines = psiconv_list_new(sizeof( |
|
|
556 | struct psiconv_in_line_layout_s)))) |
|
|
557 | goto ERROR4_1; |
| 402 | para->base_style = 0; |
558 | para->base_style = 0; |
| 403 | para->base_character = psiconv_basic_character_layout(); |
559 | if (!(para->base_character = psiconv_basic_character_layout())) |
|
|
560 | goto ERROR4_2; |
| 404 | para->base_paragraph = psiconv_basic_paragraph_layout(); |
561 | if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) |
|
|
562 | goto ERROR4_3; |
|
|
563 | if ((res = psiconv_list_add(result,para))) |
|
|
564 | goto ERROR4_4; |
| 405 | free(para); |
565 | free(para); |
| 406 | } |
566 | } |
| 407 | para = psiconv_list_get(result,i); |
567 | if (!(para = psiconv_list_get(result,i))) |
|
|
568 | goto ERROR4; |
| 408 | |
569 | |
| 409 | psiconv_progress(lev+4,off+len,"Going to read the paragraph length"); |
570 | psiconv_progress(lev+4,off+len,"Going to read the paragraph length"); |
| 410 | temp = psiconv_read_u32(buf,lev+4,off+len); |
571 | temp = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
572 | if (!res) |
|
|
573 | goto ERROR4; |
| 411 | if (temp != strlen(para->text)+1) { |
574 | if (temp != strlen(para->text)+1) { |
| 412 | res = -1; |
|
|
| 413 | psiconv_warn(lev+4,off+len, |
575 | psiconv_warn(lev+4,off+len, |
| 414 | "Disagreement of the length of paragraph in layout section"); |
576 | "Disagreement of the length of paragraph in layout section"); |
| 415 | psiconv_debug(lev+4,off+len, |
577 | psiconv_debug(lev+4,off+len, |
| 416 | "Paragraph length: layout section says %d, counted %d", |
578 | "Paragraph length: layout section says %d, counted %d", |
| 417 | temp,strlen(para->text)+1); |
579 | temp,strlen(para->text)+1); |
| 418 | } else |
580 | } else |
| 419 | psiconv_debug(lev+4,off+len,"Paragraph length: %d",temp); |
581 | psiconv_debug(lev+4,off+len,"Paragraph length: %d",temp); |
| 420 | len += 4; |
582 | len += 4; |
| 421 | |
583 | |
| 422 | psiconv_progress(lev+4,off+len,"Going to read the paragraph type"); |
584 | psiconv_progress(lev+4,off+len,"Going to read the paragraph type"); |
| 423 | temp = psiconv_read_u8(buf,lev+4,off+len); |
585 | temp = psiconv_read_u8(buf,lev+4,off+len,&res); |
|
|
586 | if (!res) |
|
|
587 | goto ERROR4; |
| 424 | if (temp != 0x00) { |
588 | if (temp != 0x00) { |
| 425 | psiconv_debug(lev+4,off+len,"Type: %02x",temp); |
589 | psiconv_debug(lev+4,off+len,"Type: %02x",temp); |
| 426 | for (j = 0; j < psiconv_list_length(anon_styles); j++) { |
590 | for (j = 0; j < psiconv_list_length(anon_styles); j++) { |
| 427 | anon_ptr = psiconv_list_get(anon_styles,j); |
591 | if (!(anon_ptr = psiconv_list_get(anon_styles,j))) { |
|
|
592 | psiconv_warn(lev+4,off+len,"Massive memory curruption"); |
|
|
593 | goto ERROR4; |
|
|
594 | } |
| 428 | if (temp == anon_ptr->nr) |
595 | if (temp == anon_ptr->nr) |
| 429 | break; |
596 | break; |
| 430 | } |
597 | } |
| 431 | if (j == psiconv_list_length(anon_styles)) { |
598 | if (j == psiconv_list_length(anon_styles)) { |
| 432 | psiconv_warn(lev+4,off+len,"Layout section paragraph type unknown"); |
599 | psiconv_warn(lev+4,off+len,"Layout section paragraph type unknown"); |
| 433 | psiconv_debug(lev+4,off+len,"Unknown type - using base styles instead"); |
600 | psiconv_debug(lev+4,off+len,"Unknown type - using base styles instead"); |
| 434 | para->base_style = 0; |
601 | para->base_style = 0; |
|
|
602 | if (!(temp_style = psiconv_get_style(styles,0))) { |
|
|
603 | psiconv_warn(lev+4,off,"Base style unknown"); |
|
|
604 | goto ERROR4; |
|
|
605 | } |
|
|
606 | if (!(temp_para = psiconv_clone_paragraph_layout |
|
|
607 | (temp_style->paragraph))) |
|
|
608 | goto ERROR4; |
| 435 | psiconv_free_paragraph_layout(para->base_paragraph); |
609 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
610 | para->base_paragraph = temp_para; |
|
|
611 | |
|
|
612 | if (!(temp_char = psiconv_clone_character_layout |
|
|
613 | (temp_style->character))) |
|
|
614 | goto ERROR4; |
| 436 | psiconv_free_character_layout(para->base_character); |
615 | psiconv_free_character_layout(para->base_character); |
| 437 | para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style |
616 | para->base_character = temp_char; |
| 438 | (styles,0)->paragraph); |
|
|
| 439 | para->base_character = psiconv_clone_character_layout(psiconv_get_style |
|
|
| 440 | (styles,0)->character); |
|
|
| 441 | } else { |
617 | } else { |
| 442 | para->base_style = anon_ptr->base_style; |
618 | para->base_style = anon_ptr->base_style; |
|
|
619 | if (!(temp_para = psiconv_clone_paragraph_layout (anon_ptr->paragraph))) |
|
|
620 | goto ERROR4; |
| 443 | psiconv_free_paragraph_layout(para->base_paragraph); |
621 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
622 | para->base_paragraph = temp_para; |
|
|
623 | |
|
|
624 | if (!(temp_char = psiconv_clone_character_layout (anon_ptr->character))) |
|
|
625 | goto ERROR4; |
| 444 | psiconv_free_character_layout(para->base_character); |
626 | psiconv_free_character_layout(para->base_character); |
| 445 | para->base_paragraph = psiconv_clone_paragraph_layout |
627 | para->base_character = temp_char; |
| 446 | (anon_ptr->paragraph); |
|
|
| 447 | para->base_character = psiconv_clone_character_layout |
|
|
| 448 | (anon_ptr->character); |
|
|
| 449 | } |
628 | } |
| 450 | inline_count[i] = 0; |
629 | inline_count[i] = 0; |
| 451 | len += 0x01; |
630 | len += 0x01; |
| 452 | } else { |
631 | } else { |
| 453 | psiconv_debug(lev+4,off+len,"Type: %02x (not based on a paragraph type)" |
632 | psiconv_debug(lev+4,off+len,"Type: %02x (not based on a paragraph type)" |
| 454 | ,temp); |
633 | ,temp); |
| 455 | len += 0x01; |
634 | len += 0x01; |
| 456 | if (parse_styles) { |
635 | if (parse_styles) { |
| 457 | psiconv_progress(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len)+4, |
636 | temp = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
637 | if (res) |
|
|
638 | goto ERROR4; |
|
|
639 | psiconv_progress(lev+4,off+len+temp+4, |
| 458 | "Going to read the paragraph element base style"); |
640 | "Going to read the paragraph element base style"); |
| 459 | temp = psiconv_read_u8(buf,lev+4, |
641 | temp = psiconv_read_u8(buf,lev+4, off+len+temp+4,&res); |
| 460 | off+len+psiconv_read_u32(buf,lev+4,off+len)+4); |
642 | if (res) |
| 461 | psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len)+4, |
643 | goto ERROR4; |
| 462 | "Style: %02x",temp); |
644 | psiconv_debug(lev+4,off+len+temp+4, "Style: %02x",temp); |
| 463 | } else |
645 | } else |
| 464 | temp = 0x00; |
646 | temp = 0x00; |
|
|
647 | |
|
|
648 | if (!(temp_style = psiconv_get_style (styles,temp))) { |
|
|
649 | psiconv_warn(lev+4,off,"Unknown Style referenced"); |
|
|
650 | if (!(temp_style = psiconv_get_style(styles,0))) { |
|
|
651 | psiconv_warn(lev+4,off,"Base style unknown"); |
|
|
652 | goto ERROR4; |
|
|
653 | } |
|
|
654 | } |
|
|
655 | |
|
|
656 | if (!(temp_para = psiconv_clone_paragraph_layout(temp_style->paragraph))) |
|
|
657 | goto ERROR4; |
| 465 | psiconv_free_paragraph_layout(para->base_paragraph); |
658 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
659 | para->base_paragraph = temp_para; |
|
|
660 | |
|
|
661 | if (!(temp_char = psiconv_clone_character_layout(temp_style->character))) |
|
|
662 | goto ERROR4; |
| 466 | psiconv_free_character_layout(para->base_character); |
663 | psiconv_free_character_layout(para->base_character); |
| 467 | para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style |
664 | para->base_character = temp_char; |
| 468 | (styles,temp)->paragraph); |
665 | |
| 469 | para->base_character = psiconv_clone_character_layout(psiconv_get_style |
|
|
| 470 | (styles,temp)->character); |
|
|
| 471 | para->base_style = temp; |
666 | para->base_style = temp; |
| 472 | psiconv_progress(lev+4,off+len,"Going to read paragraph layout"); |
667 | psiconv_progress(lev+4,off+len,"Going to read paragraph layout"); |
| 473 | psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
668 | if (!(psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
| 474 | para->base_paragraph); |
669 | para->base_paragraph))) |
|
|
670 | goto ERROR4; |
| 475 | len += leng; |
671 | len += leng; |
| 476 | if (parse_styles) |
672 | if (parse_styles) |
| 477 | len += 1; |
673 | len += 1; |
| 478 | psiconv_progress(lev+4,off+len,"Going to read number of in-line " |
674 | psiconv_progress(lev+4,off+len,"Going to read number of in-line " |
| 479 | "layout elements"); |
675 | "layout elements"); |
| 480 | inline_count[i] = psiconv_read_u32(buf,lev+4,off+len); |
676 | inline_count[i] = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
677 | if (res) |
|
|
678 | goto ERROR4; |
| 481 | psiconv_debug(lev+4,off+len,"Nr: %08x",inline_count[i]); |
679 | psiconv_debug(lev+4,off+len,"Nr: %08x",inline_count[i]); |
| 482 | len += 4; |
680 | len += 4; |
| 483 | } |
681 | } |
| 484 | } |
682 | } |
| 485 | |
683 | |
| 486 | psiconv_progress(lev+2,off+len,"Going to read the text layout inline list"); |
684 | psiconv_progress(lev+2,off+len,"Going to read the text layout inline list"); |
| 487 | |
685 | |
| 488 | psiconv_progress(lev+3,off+len,"Going to read the number of elements"); |
686 | psiconv_progress(lev+3,off+len,"Going to read the number of elements"); |
| 489 | nr = psiconv_read_u32(buf,lev+3,off+len); |
687 | nr = psiconv_read_u32(buf,lev+3,off+len,&res); |
|
|
688 | if (res) |
|
|
689 | goto ERROR4; |
| 490 | psiconv_debug(lev+3,off,"Elements: %08x",nr); |
690 | psiconv_debug(lev+3,off,"Elements: %08x",nr); |
| 491 | len += 0x04; |
691 | len += 0x04; |
| 492 | |
692 | |
| 493 | psiconv_progress(lev+3,off+len, |
693 | psiconv_progress(lev+3,off+len, |
| 494 | "Going to read the text layout inline elements"); |
694 | "Going to read the text layout inline elements"); |
| 495 | total = 0; |
695 | total = 0; |
| 496 | for (i = 0; i < psiconv_list_length(result); i++) { |
696 | for (i = 0; i < psiconv_list_length(result); i++) { |
| 497 | para = psiconv_list_get(result,i); |
697 | if (!(para = psiconv_list_get(result,i))) { |
|
|
698 | psiconv_warn(lev+3,off+len,"Massive memory corruption"); |
|
|
699 | goto ERROR4; |
|
|
700 | } |
| 498 | line_length = -1; |
701 | line_length = -1; |
| 499 | for (j = 0; j < inline_count[i]; j++) { |
702 | for (j = 0; j < inline_count[i]; j++) { |
| 500 | psiconv_progress(lev+3,off+len,"Element %d: Paragraph %d, element %d", |
703 | psiconv_progress(lev+3,off+len,"Element %d: Paragraph %d, element %d", |
| 501 | total,i,j); |
704 | total,i,j); |
| 502 | if (total >= nr) { |
705 | if (total >= nr) { |
| 503 | psiconv_warn(lev+3,off+len, |
706 | psiconv_warn(lev+3,off+len, |
| 504 | "Layout section inlines: not enough element"); |
707 | "Layout section inlines: not enough element"); |
| 505 | res = -1; |
|
|
| 506 | psiconv_debug(lev+3,off+len,"Can't read element!"); |
708 | psiconv_debug(lev+3,off+len,"Can't read element!"); |
| 507 | } else { |
709 | } else { |
| 508 | total ++; |
710 | total ++; |
| 509 | in_line.layout = psiconv_clone_character_layout(para->base_character); |
711 | if (!(in_line.layout = psiconv_clone_character_layout |
|
|
712 | (para->base_character))) |
|
|
713 | goto ERROR4; |
| 510 | psiconv_progress(lev+4,off+len,"Going to read the element type"); |
714 | psiconv_progress(lev+4,off+len,"Going to read the element type"); |
| 511 | temp = psiconv_read_u8(buf,lev+4,len+off); |
715 | temp = psiconv_read_u8(buf,lev+4,len+off,&res); |
|
|
716 | if (res) |
|
|
717 | goto ERROR4; |
| 512 | len += 1; |
718 | len += 1; |
| 513 | psiconv_debug(lev+4,off,"Type: %02x",temp); |
719 | psiconv_debug(lev+4,off,"Type: %02x",temp); |
| 514 | psiconv_progress(lev+4,off, |
720 | psiconv_progress(lev+4,off, |
| 515 | "Going to read the number of characters it applies to"); |
721 | "Going to read the number of characters it applies to"); |
| 516 | in_line.length = psiconv_read_u32(buf,lev+4,len+off); |
722 | in_line.length = psiconv_read_u32(buf,lev+4,len+off,&res); |
|
|
723 | if (res) |
|
|
724 | goto ERROR4; |
| 517 | psiconv_debug(lev+4,off+len,"Length: %02x",in_line.length); |
725 | psiconv_debug(lev+4,off+len,"Length: %02x",in_line.length); |
| 518 | len += 4; |
726 | len += 4; |
| 519 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
727 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
| 520 | res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
728 | if ((res = psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
| 521 | in_line.layout); |
729 | in_line.layout))) |
|
|
730 | goto ERROR4; |
| 522 | len += leng; |
731 | len += leng; |
| 523 | |
732 | |
| 524 | if (temp == 0x01) { |
733 | if (temp == 0x01) { |
| 525 | psiconv_debug(lev+4,off+len,"Skipping object data"); |
734 | psiconv_debug(lev+4,off+len,"Skipping object data"); |
| 526 | len += 0x10; |
735 | len += 0x10; |
| 527 | } else if (temp != 0x00) { |
736 | } else if (temp != 0x00) { |
| 528 | psiconv_warn(lev+4,off+len,"Layout section unknown inline type"); |
737 | psiconv_warn(lev+4,off+len,"Layout section unknown inline type"); |
| 529 | res = -1; |
|
|
| 530 | } |
738 | } |
| 531 | if (line_length + in_line.length > strlen(para->text)) { |
739 | if (line_length + in_line.length > strlen(para->text)) { |
| 532 | psiconv_warn(lev+4,off+len, |
740 | psiconv_warn(lev+4,off+len, |
| 533 | "Layout section inlines: line length mismatch"); |
741 | "Layout section inlines: line length mismatch"); |
| 534 | res = -1; |
742 | res = -1; |
| 535 | in_line.length = strlen(para->text) - line_length; |
743 | in_line.length = strlen(para->text) - line_length; |
| 536 | } |
744 | } |
| 537 | line_length += in_line.length; |
745 | line_length += in_line.length; |
| 538 | psiconv_list_add(para->in_lines,&in_line); |
746 | if ((res = psiconv_list_add(para->in_lines,&in_line))) |
|
|
747 | goto ERROR4; |
| 539 | } |
748 | } |
| 540 | } |
749 | } |
| 541 | } |
750 | } |
| 542 | |
751 | |
| 543 | if (total != nr) { |
752 | if (total != nr) { |
| … | |
… | |
| 546 | } |
755 | } |
| 547 | |
756 | |
| 548 | free(inline_count); |
757 | free(inline_count); |
| 549 | |
758 | |
| 550 | for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { |
759 | for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { |
| 551 | anon_ptr = psiconv_list_get(anon_styles,i); |
760 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
|
|
761 | psiconv_warn(lev+4,off+len,"Massive memory corruption"); |
|
|
762 | goto ERROR2; |
|
|
763 | } |
| 552 | psiconv_free_character_layout(anon_ptr->character); |
764 | psiconv_free_character_layout(anon_ptr->character); |
| 553 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
765 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
| 554 | } |
766 | } |
| 555 | psiconv_list_free(anon_styles); |
767 | psiconv_list_free(anon_styles); |
| 556 | |
768 | |
| … | |
… | |
| 558 | *length = len; |
770 | *length = len; |
| 559 | |
771 | |
| 560 | psiconv_progress(lev+1,off+len-1,"End of layout section (total length: %08x", |
772 | psiconv_progress(lev+1,off+len-1,"End of layout section (total length: %08x", |
| 561 | len); |
773 | len); |
| 562 | |
774 | |
|
|
775 | return 0; |
|
|
776 | |
|
|
777 | ERROR4_4: |
|
|
778 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
779 | ERROR4_3: |
|
|
780 | psiconv_free_character_layout(para->base_character); |
|
|
781 | ERROR4_2: |
|
|
782 | psiconv_list_free(para->in_lines); |
|
|
783 | ERROR4_1: |
|
|
784 | free(para); |
|
|
785 | goto ERROR4; |
|
|
786 | |
|
|
787 | ERROR3_2: |
|
|
788 | psiconv_free_character_layout(anon.character); |
|
|
789 | ERROR3_1: |
|
|
790 | psiconv_free_paragraph_layout(anon.paragraph); |
|
|
791 | goto ERROR3; |
|
|
792 | |
|
|
793 | ERROR4: |
|
|
794 | free(inline_count); |
|
|
795 | ERROR3: |
|
|
796 | for (i = 0; i < psiconv_list_length(anon_styles); i++) { |
|
|
797 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
|
|
798 | psiconv_warn(lev+1,off,"Massive memory corruption"); |
|
|
799 | break; |
|
|
800 | } |
|
|
801 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
|
|
802 | psiconv_free_character_layout(anon_ptr->character); |
|
|
803 | } |
|
|
804 | |
|
|
805 | ERROR2: |
|
|
806 | psiconv_list_free(anon_styles); |
|
|
807 | ERROR1: |
|
|
808 | psiconv_warn(lev+1,off,"Reading of Layout Section failed"); |
|
|
809 | if (length) |
|
|
810 | *length = 0; |
|
|
811 | if (!res) |
|
|
812 | return -PSICONV_E_NOMEM; |
|
|
813 | else |
| 563 | return res; |
814 | return res; |
| 564 | } |
815 | } |
| 565 | |
816 | |
| 566 | int psiconv_parse_styled_layout_section(const psiconv_buffer buf, |
817 | int psiconv_parse_styled_layout_section(const psiconv_buffer buf, |
| 567 | int lev,psiconv_u32 off, |
818 | int lev,psiconv_u32 off, |
| 568 | int *length, |
819 | int *length, |
| … | |
… | |
| 577 | int *length, |
828 | int *length, |
| 578 | psiconv_text_and_layout result, |
829 | psiconv_text_and_layout result, |
| 579 | psiconv_character_layout base_char, |
830 | psiconv_character_layout base_char, |
| 580 | psiconv_paragraph_layout base_para) |
831 | psiconv_paragraph_layout base_para) |
| 581 | { |
832 | { |
| 582 | int res; |
833 | int res = 0; |
| 583 | psiconv_word_styles_section styles_section; |
834 | psiconv_word_styles_section styles_section; |
| 584 | |
835 | |
| 585 | styles_section = malloc(sizeof(*styles_section)); |
836 | if (!(styles_section = malloc(sizeof(*styles_section)))) |
|
|
837 | goto ERROR1; |
| 586 | styles_section->normal = malloc(sizeof(*styles_section->normal)); |
838 | if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) |
| 587 | styles_section->normal->character = psiconv_clone_character_layout(base_char); |
839 | goto ERROR2; |
| 588 | styles_section->normal->paragraph = psiconv_clone_paragraph_layout(base_para); |
840 | if (!(styles_section->normal->character = |
|
|
841 | psiconv_clone_character_layout(base_char))) |
|
|
842 | goto ERROR3; |
|
|
843 | if (!(styles_section->normal->paragraph = |
|
|
844 | psiconv_clone_paragraph_layout(base_para))) |
|
|
845 | goto ERROR4; |
| 589 | styles_section->normal->hotkey = 0; |
846 | styles_section->normal->hotkey = 0; |
| 590 | styles_section->normal->name = strdup(""); |
847 | if (!(styles_section->normal->name = strdup(""))) |
| 591 | styles_section->styles = psiconv_list_new(sizeof(struct psiconv_word_style_s)); |
848 | goto ERROR5; |
|
|
849 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
|
|
850 | struct psiconv_word_style_s)))) |
|
|
851 | goto ERROR6; |
| 592 | |
852 | |
| 593 | res = psiconv_parse_layout_section(buf,lev,off,length,result, |
853 | res = psiconv_parse_layout_section(buf,lev,off,length,result, |
| 594 | styles_section,0); |
854 | styles_section,0); |
| 595 | |
855 | |
| 596 | psiconv_free_word_styles_section(styles_section); |
856 | psiconv_free_word_styles_section(styles_section); |
| 597 | return res; |
857 | return res; |
|
|
858 | |
|
|
859 | ERROR6: |
|
|
860 | free(styles_section->normal->name); |
|
|
861 | ERROR5: |
|
|
862 | psiconv_free_paragraph_layout(styles_section->normal->paragraph); |
|
|
863 | ERROR4: |
|
|
864 | psiconv_free_character_layout(styles_section->normal->character); |
|
|
865 | ERROR3: |
|
|
866 | free(styles_section->normal); |
|
|
867 | ERROR2: |
|
|
868 | free(styles_section); |
|
|
869 | ERROR1: |
|
|
870 | psiconv_warn(lev+1,off,"Reading of Styleless Layout Section failed"); |
|
|
871 | if (length) |
|
|
872 | *length = 0; |
|
|
873 | if (!res) |
|
|
874 | return -PSICONV_E_NOMEM; |
|
|
875 | else |
|
|
876 | return res; |
| 598 | } |
877 | } |
| 599 | |
878 | |