/[public]/psiconv/trunk/lib/psiconv/parse_driver.c
ViewVC logotype

Annotation of /psiconv/trunk/lib/psiconv/parse_driver.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 351 - (hide annotations)
Wed Oct 22 19:53:40 2014 UTC (9 years, 5 months ago) by frodo
File MIME type: text/plain
File size: 31037 byte(s)
(Frodo) Update copyright year in all source files

1 frodo 2 /*
2     parse_driver.c - Part of psiconv, a PSION 5 file formats converter
3 frodo 351 Copyright (c) 1999-2014 Frodo Looijaard <frodo@frodo.looijaard.name>
4 frodo 2
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9    
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14    
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18     */
19    
20     #include "config.h"
21 frodo 71 #include "compat.h"
22    
23 frodo 2 #include <stdlib.h>
24 frodo 160 #include <string.h>
25 frodo 2
26     #include "parse.h"
27     #include "parse_routines.h"
28 frodo 184 #include "unicode.h"
29 frodo 2
30 frodo 142 #ifdef DMALLOC
31     #include <dmalloc.h>
32     #endif
33    
34 frodo 237 /* Compare whether application id names match.
35     Sought must be lower case; the comparison is case insensitive */
36     static psiconv_bool_t applid_matches(psiconv_string_t found,
37     const char *sought)
38     {
39     int i;
40     if (psiconv_unicode_strlen(found) != strlen(sought))
41     return psiconv_bool_false;
42     for (i = 0; i < strlen(sought); i++)
43     if ((found[i] != sought[i]) &&
44     ((sought[i] < 'a') || (sought[i] > 'z') ||
45     (found[i] != sought[i] + 'A' - 'a')))
46     return psiconv_bool_false;
47     return psiconv_bool_true;
48     }
49 frodo 142
50 frodo 168 psiconv_file_type_t psiconv_file_type(const psiconv_config config,
51     psiconv_buffer buf,int *length,
52 frodo 18 psiconv_header_section *result)
53 frodo 2 {
54     psiconv_header_section header;
55     psiconv_file_type_t res;
56 frodo 18 int leng;
57 frodo 2
58 frodo 168 if ((psiconv_parse_header_section(config,buf,0,0,&leng,&header)))
59 frodo 64 return psiconv_unknown_file;
60 frodo 2 res = header->file;
61 frodo 18 if (result)
62     *result = header;
63     else
64     psiconv_free_header_section(header);
65     if (length)
66     *length = leng;
67 frodo 2 return res;
68     }
69    
70 frodo 168 int psiconv_parse(const psiconv_config config,const psiconv_buffer buf,
71     psiconv_file *result)
72 frodo 2 {
73     int res=0;
74     int lev=0;
75     int off=0;
76 frodo 18 int leng;
77 frodo 2
78 frodo 64 if (!((*result) = malloc(sizeof(**result))))
79     goto ERROR1;
80 frodo 2
81 frodo 168 (*result)->type = psiconv_file_type(config,buf,&leng,NULL);
82 frodo 2 if ((*result)->type == psiconv_unknown_file) {
83 frodo 168 psiconv_warn(config,lev+1,off,"Unknown file type: can't parse!");
84 frodo 2 (*result)->file = NULL;
85     } else if ((*result)->type == psiconv_word_file)
86 frodo 168 res = psiconv_parse_word_file(config,buf,lev+2,leng,
87 frodo 2 (psiconv_word_f *)(&((*result)->file)));
88     else if ((*result)->type == psiconv_texted_file)
89 frodo 168 res = psiconv_parse_texted_file(config,buf,lev+2,leng,
90 frodo 2 (psiconv_texted_f *)(&((*result)->file)));
91 frodo 12 else if ((*result)->type == psiconv_mbm_file)
92 frodo 168 res = psiconv_parse_mbm_file(config,buf,lev+2,leng,
93 frodo 12 (psiconv_mbm_f *)(&((*result)->file)));
94 frodo 24 else if ((*result)->type == psiconv_sketch_file)
95 frodo 168 res = psiconv_parse_sketch_file(config,buf,lev+2,leng,
96 frodo 24 (psiconv_sketch_f *)(&((*result)->file)));
97 frodo 41 else if ((*result)->type == psiconv_clipart_file)
98 frodo 168 res = psiconv_parse_clipart_file(config,buf,lev+2,leng,
99 frodo 41 (psiconv_clipart_f *)(&((*result)->file)));
100 frodo 94 else if ((*result)->type == psiconv_sheet_file)
101 frodo 168 res = psiconv_parse_sheet_file(config,buf,lev+2,leng,
102 frodo 94 (psiconv_sheet_f *)(&((*result)->file)));
103 frodo 2 else {
104 frodo 168 psiconv_warn(config,lev+1,off,"Can't parse this file yet!");
105 frodo 2 (*result)->file = NULL;
106     }
107 frodo 64 if (res)
108     goto ERROR2;
109     return 0;
110 frodo 2
111 frodo 64 ERROR2:
112     free(*result);
113     ERROR1:
114 frodo 184 psiconv_error(config,lev+1,off,"Reading of Psion File failed");
115 frodo 64 if (res == 0)
116     return -PSICONV_E_NOMEM;
117     else
118     return res;
119 frodo 2 }
120    
121 frodo 168 int psiconv_parse_clipart_file(const psiconv_config config,
122     const psiconv_buffer buf,int lev,
123 frodo 41 psiconv_u32 off, psiconv_clipart_f *result)
124     {
125 frodo 42 int res=0;
126 frodo 43 int i;
127     psiconv_jumptable_section table;
128     psiconv_clipart_section clipart;
129     psiconv_u32 *entry;
130    
131 frodo 168 psiconv_progress(config,lev+1,off,"Going to read a clipart file");
132 frodo 64 if (!((*result) = malloc(sizeof(**result))))
133     goto ERROR1;
134 frodo 43
135 frodo 168 psiconv_progress(config,lev+2,off,"Going to read the MBM jumptable");
136     if ((res = psiconv_parse_jumptable_section(config,buf,lev+2,off, NULL,&table)))
137 frodo 64 goto ERROR2;
138 frodo 43
139 frodo 168 psiconv_progress(config,lev+2,off,"Going to read the clipart sections");
140 frodo 64 if (!((*result)->sections = psiconv_list_new(sizeof(*clipart))))
141     goto ERROR3;
142 frodo 43 for (i = 0; i < psiconv_list_length(table); i ++) {
143 frodo 64 if (!(entry = psiconv_list_get(table,i)))
144     goto ERROR4;
145 frodo 168 psiconv_progress(config,lev+3,off,"Going to read clipart section %i",i);
146     if ((res = psiconv_parse_clipart_section(config,buf,lev+3,*entry,NULL,&clipart)))
147 frodo 64 goto ERROR4;
148 frodo 66 if ((res = psiconv_list_add((*result)->sections,clipart)))
149 frodo 64 goto ERROR5;
150 frodo 208 free(clipart);
151 frodo 43 }
152    
153     psiconv_free_jumptable_section(table);
154 frodo 168 psiconv_progress(config,lev+1,off,"End of clipart file");
155 frodo 43 return res;
156 frodo 64 ERROR5:
157     psiconv_free_clipart_section(clipart);
158     ERROR4:
159     for (i = 0; i < psiconv_list_length((*result)->sections); i++) {
160     if (!(clipart = psiconv_list_get((*result)->sections,i))) {
161 frodo 233 psiconv_error(config,lev+1,off,"Data structure corruption");
162 frodo 64 goto ERROR3;
163     }
164     psiconv_free_clipart_section(clipart);
165     }
166     psiconv_list_free((*result)->sections);
167     ERROR3:
168     psiconv_free_jumptable_section(table);
169     ERROR2:
170     free(*result);
171     ERROR1:
172 frodo 184 psiconv_error(config,lev+1,off,"Reading of Clipart File failed");
173 frodo 64 if (res == 0)
174     return -PSICONV_E_NOMEM;
175     else
176     return res;
177 frodo 41 }
178    
179 frodo 168 int psiconv_parse_mbm_file(const psiconv_config config,
180     const psiconv_buffer buf,int lev, psiconv_u32 off,
181 frodo 12 psiconv_mbm_f *result)
182     {
183     int res=0;
184     int i;
185 frodo 42 psiconv_jumptable_section table;
186 frodo 12 psiconv_paint_data_section paint;
187     psiconv_u32 *entry;
188 frodo 18 psiconv_u32 sto;
189 frodo 12
190 frodo 168 psiconv_progress(config,lev+1,off,"Going to read a mbm file");
191 frodo 64 if (!(*result = malloc(sizeof(**result))))
192     goto ERROR1;
193 frodo 12
194 frodo 168 psiconv_progress(config,lev+2,off,"Going to read the offset of the MBM jumptable");
195     sto = psiconv_read_u32(config,buf,lev+2,off,&res);
196 frodo 64 if (res)
197     goto ERROR2;
198 frodo 168 psiconv_debug(config,lev+2,off,"Offset: %08x",sto);
199 frodo 12
200 frodo 168 psiconv_progress(config,lev+2,off,"Going to read the MBM jumptable");
201     if ((res = psiconv_parse_jumptable_section(config,buf,lev+2,sto, NULL,&table)))
202 frodo 64 goto ERROR2;
203 frodo 12
204 frodo 168 psiconv_progress(config,lev+2,off,"Going to read the picture sections");
205 frodo 64 if (!((*result)->sections = psiconv_list_new(sizeof(*paint))))
206     goto ERROR3;
207 frodo 12 for (i = 0; i < psiconv_list_length(table); i ++) {
208 frodo 64 if (!(entry = psiconv_list_get(table,i)))
209     goto ERROR4;
210 frodo 168 psiconv_progress(config,lev+3,off,"Going to read picture section %i",i);
211     if ((res = psiconv_parse_paint_data_section(config,buf,lev+3,*entry,NULL,
212 frodo 64 0,&paint)))
213     goto ERROR4;
214     if ((res = psiconv_list_add((*result)->sections,paint)))
215     goto ERROR5;
216 frodo 208 free(paint);
217 frodo 12 }
218    
219 frodo 42 psiconv_free_jumptable_section(table);
220 frodo 168 psiconv_progress(config,lev+1,off,"End of mbm file");
221 frodo 64 return 0;
222     ERROR5:
223     psiconv_free_paint_data_section(paint);
224     ERROR4:
225     for (i = 0; i < psiconv_list_length((*result)->sections); i++) {
226     if (!(paint = psiconv_list_get((*result)->sections,i))) {
227 frodo 233 psiconv_error(config,lev+1,off,"Data structure corruption");
228 frodo 64 goto ERROR3;
229     }
230     psiconv_free_paint_data_section(paint);
231     }
232     psiconv_list_free((*result)->sections);
233     ERROR3:
234     psiconv_free_jumptable_section(table);
235     ERROR2:
236     free(*result);
237     ERROR1:
238 frodo 184 psiconv_error(config,lev+1,off,"Reading of MBM File failed");
239 frodo 64 if (res == 0)
240     return -PSICONV_E_NOMEM;
241     else
242     return res;
243 frodo 12 }
244    
245 frodo 168 int psiconv_parse_sketch_file(const psiconv_config config,
246     const psiconv_buffer buf,int lev,
247 frodo 24 psiconv_u32 off,
248     psiconv_sketch_f *result)
249     {
250     psiconv_section_table_section table;
251     psiconv_application_id_section appl_id;
252     psiconv_u32 applid_sec = 0;
253     psiconv_u32 sketch_sec = 0;
254     psiconv_u32 sto;
255     psiconv_section_table_entry entry;
256     int i;
257     int res=0;
258     char *temp_str;
259    
260 frodo 168 psiconv_progress(config,lev+1,off,"Going to read a sketch file");
261 frodo 64 if (!(*result = malloc(sizeof(**result))))
262     goto ERROR1;
263 frodo 24
264 frodo 168 psiconv_progress(config,lev+2,off,
265 frodo 24 "Going to read the offset of the section table section");
266 frodo 168 sto = psiconv_read_u32(config,buf,lev+2,off,&res);
267 frodo 64 if (res)
268     goto ERROR2;
269 frodo 168 psiconv_debug(config,lev+2,off,"Offset: %08x",sto);
270 frodo 24
271 frodo 168 psiconv_progress(config,lev+2,sto, "Going to read the section table section");
272     if ((res = psiconv_parse_section_table_section(config,buf,lev+2,sto, NULL,&table)))
273 frodo 64 goto ERROR2;
274 frodo 24
275     for (i = 0; i < psiconv_list_length(table); i ++) {
276 frodo 168 psiconv_progress(config,lev+2,sto, "Going to read entry %d",i);
277 frodo 64 if (!(entry = psiconv_list_get(table,i)))
278     goto ERROR3;
279 frodo 24 if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
280     applid_sec = entry->offset;
281 frodo 168 psiconv_debug(config,lev+3,sto,
282 frodo 24 "Found the Application ID section at %08x",applid_sec);
283     } else if (entry->id == PSICONV_ID_SKETCH_SECTION) {
284     sketch_sec = entry->offset;
285 frodo 168 psiconv_debug(config,lev+3,sto,
286 frodo 24 "Found the Sketch section at %08x",sketch_sec);
287     } else {
288 frodo 168 psiconv_warn(config,lev+3,sto,
289 frodo 64 "Found unknown section in the Section Table (ignoring)");
290 frodo 168 psiconv_debug(config,lev+3,sto,
291 frodo 24 "Section ID %08x, offset %08x",entry->id,entry->offset);
292     }
293     }
294    
295 frodo 168 psiconv_progress(config,lev+2,sto, "Looking for the Application ID section");
296 frodo 24 if (! applid_sec) {
297 frodo 184 psiconv_error(config,lev+2,sto,
298 frodo 64 "Application ID section not found in the section table");
299     res = -PSICONV_E_PARSE;
300     goto ERROR3;
301 frodo 24 } else {
302 frodo 168 psiconv_debug(config,lev+2,sto,
303 frodo 24 "Application ID section at offset %08x",applid_sec);
304 frodo 168 if ((res = psiconv_parse_application_id_section(config,buf,lev+2,applid_sec,NULL,
305 frodo 64 &appl_id)))
306     goto ERROR3;
307 frodo 24 }
308     if ((appl_id->id != PSICONV_ID_SKETCH) ||
309 frodo 237 !applid_matches(appl_id->name,"paint.app")) {
310 frodo 168 psiconv_warn(config,lev+2,applid_sec,
311 frodo 24 "Application ID section contains unexpected data");
312 frodo 168 psiconv_debug(config,lev+2,applid_sec,"ID: %08x expected, %08x found",
313 frodo 24 PSICONV_ID_SKETCH,appl_id->id);
314 frodo 184 if (!(temp_str = psiconv_make_printable(config,appl_id->name)))
315 frodo 64 goto ERROR4;
316 frodo 168 psiconv_debug(config,lev+2,applid_sec,"Name: `%s' expected, `%s' found",
317 frodo 24 "Paint.app",temp_str);
318     free(temp_str);
319 frodo 64 res = -PSICONV_E_PARSE;
320     goto ERROR4;
321 frodo 24 }
322    
323 frodo 168 psiconv_progress(config,lev+2,sto, "Looking for the Sketch section");
324 frodo 24 if (! sketch_sec) {
325 frodo 168 psiconv_warn(config,lev+2,sto,
326 frodo 24 "Sketch section not found in the section table");
327     } else {
328 frodo 168 psiconv_debug(config,lev+2,sto,
329 frodo 24 "Sketch section at offset %08x",applid_sec);
330 frodo 168 if ((res = psiconv_parse_sketch_section(config,buf,lev+2,sketch_sec,NULL,
331 frodo 64 &(*result)->sketch_sec)))
332     goto ERROR4;
333 frodo 24 }
334    
335     psiconv_free_application_id_section(appl_id);
336     psiconv_free_section_table_section(table);
337    
338 frodo 170 psiconv_progress(config,lev+1,off,"End of sketch file");
339 frodo 24 return res;
340 frodo 64
341     ERROR4:
342     psiconv_free_application_id_section(appl_id);
343     ERROR3:
344     free(table);
345     ERROR2:
346     free(*result);
347     ERROR1:
348 frodo 184 psiconv_error(config,lev+1,off,"Reading of Sketch File failed");
349 frodo 64 if (res == 0)
350     return -PSICONV_E_NOMEM;
351     else
352     return res;
353 frodo 24 }
354    
355    
356 frodo 168 int psiconv_parse_texted_file(const psiconv_config config,
357     const psiconv_buffer buf,int lev,
358 frodo 18 psiconv_u32 off,
359 frodo 2 psiconv_texted_f *result)
360     {
361     int res=0;
362     psiconv_section_table_section table;
363     psiconv_application_id_section appl_id;
364     char *temp_str;
365     psiconv_character_layout base_char;
366     psiconv_paragraph_layout base_para;
367     psiconv_u32 page_sec = 0;
368     psiconv_u32 texted_sec = 0;
369     psiconv_u32 applid_sec = 0;
370 frodo 18 psiconv_u32 sto;
371 frodo 2 psiconv_section_table_entry entry;
372     int i;
373    
374 frodo 168 psiconv_progress(config,lev+1,off,"Going to read a texted file");
375 frodo 64 if (!(*result = malloc(sizeof(**result))))
376     goto ERROR1;
377 frodo 2
378 frodo 168 psiconv_progress(config,lev+2,off,
379 frodo 24 "Going to read the offset of the section table section");
380 frodo 168 sto = psiconv_read_u32(config,buf,lev+2,off,&res);
381 frodo 64 if (res)
382     goto ERROR2;
383 frodo 168 psiconv_debug(config,lev+2,off,"Offset: %08x",sto);
384 frodo 2
385 frodo 168 psiconv_progress(config,lev+2,sto, "Going to read the section table section");
386     if ((res = psiconv_parse_section_table_section(config,buf,lev+2,sto, NULL,&table)))
387 frodo 64 goto ERROR2;
388 frodo 2
389     for (i = 0; i < psiconv_list_length(table); i ++) {
390 frodo 168 psiconv_progress(config,lev+2,sto, "Going to read entry %d",i);
391 frodo 64 if (!(entry = psiconv_list_get(table,i)))
392     goto ERROR3;
393 frodo 2 if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
394     applid_sec = entry->offset;
395 frodo 168 psiconv_debug(config,lev+3,sto,
396 frodo 2 "Found the Application ID section at %08x",applid_sec);
397     } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) {
398     page_sec = entry->offset;
399 frodo 168 psiconv_debug(config,lev+3,sto,
400 frodo 2 "Found the Page Layout section at %08x",page_sec);
401     } else if (entry->id == PSICONV_ID_TEXTED) {
402     texted_sec = entry->offset;
403 frodo 168 psiconv_debug(config,lev+3,sto,
404 frodo 2 "Found the TextEd section at %08x",texted_sec);
405     } else {
406 frodo 168 psiconv_warn(config,lev+3,sto,
407 frodo 64 "Found unknown section in the Section Table (ignoring)");
408 frodo 168 psiconv_debug(config,lev+3,sto,
409 frodo 2 "Section ID %08x, offset %08x",entry->id,entry->offset);
410     }
411     }
412    
413 frodo 168 psiconv_progress(config,lev+2,sto, "Looking for the Application ID section");
414 frodo 2 if (! applid_sec) {
415 frodo 184 psiconv_error(config,lev+2,sto,
416 frodo 2 "Application ID section not found in the section table");
417 frodo 64 res = -PSICONV_E_PARSE;
418     goto ERROR3;
419 frodo 2 } else {
420 frodo 168 psiconv_debug(config,lev+2,sto,
421 frodo 2 "Application ID section at offset %08x",applid_sec);
422 frodo 168 if ((res = psiconv_parse_application_id_section(config,buf,lev+2,applid_sec,NULL,
423 frodo 64 &appl_id)))
424     goto ERROR3;
425 frodo 2 }
426 frodo 184
427 frodo 2 if ((appl_id->id != PSICONV_ID_TEXTED) ||
428 frodo 237 !applid_matches(appl_id->name,"texted.app")) {
429 frodo 168 psiconv_warn(config,lev+2,applid_sec,
430 frodo 2 "Application ID section contains unexpected data");
431 frodo 168 psiconv_debug(config,lev+2,applid_sec,"ID: %08x expected, %08x found",
432 frodo 2 PSICONV_ID_TEXTED,appl_id->id);
433 frodo 184 if (!(temp_str = psiconv_make_printable(config,appl_id->name)))
434 frodo 64 goto ERROR4;
435 frodo 168 psiconv_debug(config,lev+2,applid_sec,"Name: `%s' expected, `%s' found",
436 frodo 24 "TextEd.app",temp_str);
437 frodo 2 free(temp_str);
438 frodo 64 res = -PSICONV_E_PARSE;
439     goto ERROR4;
440 frodo 2 }
441    
442 frodo 168 psiconv_progress(config,lev+2,sto,
443 frodo 2 "Looking for the Page layout section");
444     if (! page_sec) {
445 frodo 184 psiconv_error(config,lev+2,sto,
446 frodo 2 "Page layout section not found in the section table");
447 frodo 64 res = -PSICONV_E_PARSE;
448     goto ERROR4;
449 frodo 2 } else {
450 frodo 168 psiconv_debug(config,lev+2,sto,
451 frodo 2 "Page layout section at offset %08x",page_sec);
452 frodo 168 if ((res = psiconv_parse_page_layout_section(config,buf,lev+2,page_sec,NULL,
453 frodo 64 &(*result)->page_sec)))
454     goto ERROR4;
455 frodo 2 }
456    
457 frodo 64 if (!(base_char = psiconv_basic_character_layout()))
458     goto ERROR5;
459     if (!(base_para = psiconv_basic_paragraph_layout()))
460     goto ERROR6;
461 frodo 2
462 frodo 168 psiconv_progress(config,lev+2,sto,
463 frodo 2 "Looking for the TextEd section");
464     if (! texted_sec) {
465 frodo 184 psiconv_error(config,lev+2,sto,
466 frodo 2 "TextEd section not found in the section table");
467 frodo 64 res = -PSICONV_E_PARSE;
468     goto ERROR7;
469 frodo 2 } else {
470 frodo 168 psiconv_debug(config,lev+2,sto, "TextEd section at offset %08x",texted_sec);
471     if ((res = psiconv_parse_texted_section(config,buf,lev+2,texted_sec,NULL,
472 frodo 2 &(*result)->texted_sec,
473 frodo 64 base_char,base_para)))
474     goto ERROR7;
475 frodo 2 }
476     psiconv_free_character_layout(base_char);
477     psiconv_free_paragraph_layout(base_para);
478    
479     psiconv_free_application_id_section(appl_id);
480     psiconv_free_section_table_section(table);
481    
482 frodo 168 psiconv_progress(config,lev+1,off,"End of TextEd file");
483 frodo 64 return 0;
484    
485     ERROR7:
486     psiconv_free_paragraph_layout(base_para);
487     ERROR6:
488     psiconv_free_character_layout(base_char);
489     ERROR5:
490     psiconv_free_page_layout_section((*result)->page_sec);
491     ERROR4:
492     psiconv_free_application_id_section(appl_id);
493     ERROR3:
494     psiconv_free_section_table_section(table);
495     ERROR2:
496     free(*result);
497     ERROR1:
498 frodo 184 psiconv_error(config,lev+1,off,"Reading of TextEd File failed");
499 frodo 64 if (res == 0)
500     return -PSICONV_E_NOMEM;
501     else
502     return res;
503 frodo 2 }
504    
505 frodo 168 int psiconv_parse_word_file(const psiconv_config config,
506     const psiconv_buffer buf,int lev, psiconv_u32 off,
507 frodo 2 psiconv_word_f *result)
508     {
509     int res=0;
510     psiconv_section_table_section table;
511     psiconv_application_id_section appl_id;
512     char *temp_str;
513     psiconv_u32 pwd_sec = 0;
514     psiconv_u32 status_sec = 0;
515     psiconv_u32 styles_sec = 0;
516     psiconv_u32 page_sec = 0;
517     psiconv_u32 text_sec = 0;
518     psiconv_u32 layout_sec = 0;
519     psiconv_u32 applid_sec = 0;
520     psiconv_section_table_entry entry;
521 frodo 18 psiconv_u32 sto;
522 frodo 2 int i;
523    
524 frodo 168 psiconv_progress(config,lev+1,off,"Going to read a word file");
525 frodo 64 if (!(*result = malloc(sizeof(**result))))
526     goto ERROR1;
527 frodo 2
528 frodo 168 psiconv_progress(config,lev+2,off,
529 frodo 24 "Going to read the offset of the section table section");
530 frodo 168 sto = psiconv_read_u32(config,buf,lev+2,off,&res);
531 frodo 64 if (res)
532     goto ERROR2;
533 frodo 168 psiconv_debug(config,lev+2,off,"Offset: %08x",sto);
534 frodo 2
535 frodo 168 psiconv_progress(config,lev+2,sto,
536 frodo 2 "Going to read the section table section");
537 frodo 168 if ((res = psiconv_parse_section_table_section(config,buf,lev+2,sto, NULL,&table)))
538 frodo 64 goto ERROR2;
539 frodo 2
540     for (i = 0; i < psiconv_list_length(table); i ++) {
541 frodo 168 psiconv_progress(config,lev+2,sto, "Going to read entry %d",i);
542 frodo 64 if (!(entry = psiconv_list_get(table,i)))
543     goto ERROR3;
544 frodo 2 if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
545     applid_sec = entry->offset;
546 frodo 168 psiconv_debug(config,lev+3,sto,
547 frodo 2 "Found the Application ID section at %08x",applid_sec);
548     } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) {
549     page_sec = entry->offset;
550 frodo 168 psiconv_debug(config,lev+3,sto,
551 frodo 2 "Found the Page Layout section at %08x",page_sec);
552     } else if (entry->id == PSICONV_ID_TEXT_SECTION) {
553     text_sec = entry->offset;
554 frodo 168 psiconv_debug(config,lev+3,sto, "Found the Text section at %08x",text_sec);
555 frodo 2 } else if (entry->id == PSICONV_ID_PASSWORD_SECTION) {
556     pwd_sec = entry->offset;
557 frodo 168 psiconv_debug(config,lev+3,sto,
558 frodo 2 "Found the Password section at %08x",pwd_sec);
559 frodo 184 psiconv_error(config,lev+3,sto,
560 frodo 2 "Password section found - can't read encrypted data");
561 frodo 64 res = -PSICONV_E_PARSE;
562     goto ERROR3;
563 frodo 2 } else if (entry->id == PSICONV_ID_WORD_STATUS_SECTION) {
564     status_sec = entry->offset;
565 frodo 168 psiconv_debug(config,lev+3,sto,
566 frodo 2 "Found the Word Status section at %08x",status_sec);
567     } else if (entry->id == PSICONV_ID_WORD_STYLES_SECTION) {
568     styles_sec = entry->offset;
569 frodo 168 psiconv_debug(config,lev+3,sto,
570 frodo 2 "Found the Word Styles section at %08x",styles_sec);
571     } else if (entry->id == PSICONV_ID_LAYOUT_SECTION) {
572     layout_sec = entry->offset;
573 frodo 168 psiconv_debug(config,lev+3,sto,
574 frodo 2 "Found the Layout section at %08x",layout_sec);
575     } else {
576 frodo 168 psiconv_warn(config,lev+3,sto,
577 frodo 64 "Found unknown section in the Section Table (ignoring)");
578 frodo 168 psiconv_debug(config,lev+3,sto,
579 frodo 2 "Section ID %08x, offset %08x",entry->id,entry->offset);
580     }
581     }
582    
583    
584 frodo 168 psiconv_progress(config,lev+2,sto,
585 frodo 2 "Looking for the Status section");
586     if (!status_sec) {
587 frodo 184 psiconv_error(config,lev+2,sto, "Status section not found in the section table");
588 frodo 64 res = -PSICONV_E_PARSE;
589     goto ERROR3;
590 frodo 2 } else {
591 frodo 168 psiconv_debug(config,lev+2,sto, "Status section at offset %08x",status_sec);
592     if ((res = psiconv_parse_word_status_section(config,buf,lev+2,status_sec,NULL,
593 frodo 64 &((*result)->status_sec))))
594     goto ERROR3;
595 frodo 2 }
596    
597 frodo 168 psiconv_progress(config,lev+2,sto, "Looking for the Application ID section");
598 frodo 2 if (! applid_sec) {
599 frodo 184 psiconv_error(config,lev+2,sto,
600 frodo 2 "Application ID section not found in the section table");
601 frodo 64 res = -PSICONV_E_PARSE;
602     goto ERROR4;
603 frodo 2 } else {
604 frodo 168 psiconv_debug(config,lev+2,sto,
605 frodo 2 "Application ID section at offset %08x",applid_sec);
606 frodo 168 if ((res = psiconv_parse_application_id_section(config,buf,lev+2,applid_sec,NULL,
607 frodo 64 &appl_id)))
608     goto ERROR4;
609 frodo 2 }
610     if ((appl_id->id != PSICONV_ID_WORD) ||
611 frodo 237 !applid_matches(appl_id->name,"word.app")) {
612 frodo 168 psiconv_warn(config,lev+2,applid_sec,
613 frodo 2 "Application ID section contains unexpected data");
614 frodo 168 psiconv_debug(config,lev+2,applid_sec,"ID: %08x expected, %08x found",
615 frodo 2 PSICONV_ID_WORD,appl_id->id);
616 frodo 184 if (!(temp_str = psiconv_make_printable(config,appl_id->name)))
617 frodo 64 goto ERROR5;
618 frodo 168 psiconv_debug(config,lev+2,applid_sec,"Name: `%s' expected, `%s' found",
619 frodo 24 "Word.app",temp_str);
620 frodo 2 free(temp_str);
621 frodo 64 res = -PSICONV_E_PARSE;
622     goto ERROR5;
623 frodo 2 }
624    
625 frodo 168 psiconv_progress(config,lev+2,sto,
626 frodo 2 "Looking for the Page layout section");
627     if (! page_sec) {
628 frodo 184 psiconv_error(config,lev+2,sto,
629 frodo 2 "Page layout section not found in the section table");
630 frodo 64 res = -PSICONV_E_PARSE;
631     goto ERROR5;
632 frodo 2 } else {
633 frodo 168 psiconv_debug(config,lev+2,sto,
634 frodo 2 "Page layout section at offset %08x",page_sec);
635 frodo 168 if ((res = psiconv_parse_page_layout_section(config,buf,lev+2,page_sec,NULL,
636 frodo 64 &(*result)->page_sec)))
637     goto ERROR5;
638 frodo 2 }
639    
640 frodo 168 psiconv_progress(config,lev+2,sto,
641 frodo 2 "Looking for the Word Style section");
642     if (!styles_sec) {
643 frodo 184 psiconv_error(config,lev+2,sto,
644 frodo 2 "Word styles section not found in the section table");
645 frodo 64 res = -PSICONV_E_PARSE;
646     goto ERROR6;
647 frodo 2 } else {
648 frodo 168 psiconv_debug(config,lev+2,sto,
649 frodo 2 "Word styles section at offset %08x",styles_sec);
650 frodo 168 if ((res = psiconv_parse_word_styles_section(config,buf,lev+2,styles_sec,NULL,
651 frodo 64 &(*result)->styles_sec)))
652     goto ERROR6;
653 frodo 2 }
654    
655 frodo 168 psiconv_progress(config,lev+2,sto,
656 frodo 2 "Looking for the Text section");
657     if (!text_sec) {
658 frodo 184 psiconv_error(config,lev+2,sto, "Text section not found in the section table");
659 frodo 64 res = -PSICONV_E_PARSE;
660     goto ERROR7;
661 frodo 2 } else {
662 frodo 168 psiconv_debug(config,lev+2,sto,
663 frodo 2 "Text section at offset %08x",text_sec);
664 frodo 168 if ((res = psiconv_parse_text_section(config,buf,lev+2,text_sec,NULL,
665 frodo 64 &(*result)->paragraphs)))
666     goto ERROR7;
667 frodo 2 }
668    
669 frodo 168 psiconv_progress(config,lev+2,sto, "Looking for the Layout section");
670 frodo 64 if (!layout_sec) {
671 frodo 168 psiconv_debug(config,lev+2,sto, "No layout section today");
672 frodo 64 } else {
673 frodo 168 psiconv_debug(config,lev+2,sto,
674 frodo 64 "Layout section at offset %08x",layout_sec);
675 frodo 168 if ((res = psiconv_parse_styled_layout_section(config,buf,lev+2,layout_sec,NULL,
676 frodo 64 (*result)->paragraphs,
677     (*result)->styles_sec)))
678     goto ERROR8;
679     }
680 frodo 2
681     psiconv_free_application_id_section(appl_id);
682     psiconv_free_section_table_section(table);
683    
684 frodo 168 psiconv_progress(config,lev+1,off,"End of word file");
685 frodo 70 return 0;
686 frodo 64
687    
688     ERROR8:
689     psiconv_free_text_and_layout((*result)->paragraphs);
690     ERROR7:
691     psiconv_free_word_styles_section((*result)->styles_sec);
692     ERROR6:
693     psiconv_free_page_layout_section((*result)->page_sec);
694     ERROR5:
695     psiconv_free_application_id_section(appl_id);
696     ERROR4:
697     psiconv_free_word_status_section((*result)->status_sec);
698     ERROR3:
699     psiconv_free_section_table_section(table);
700     ERROR2:
701     free(*result);
702     ERROR1:
703 frodo 184 psiconv_error(config,lev+1,off,"Reading of Word File failed");
704 frodo 64 if (res == 0)
705     return -PSICONV_E_NOMEM;
706     else
707     return res;
708 frodo 2 }
709 frodo 94
710 frodo 168 int psiconv_parse_sheet_file(const psiconv_config config,
711     const psiconv_buffer buf,int lev, psiconv_u32 off,
712     psiconv_sheet_f *result)
713 frodo 94 {
714     int res=0;
715     psiconv_section_table_section table;
716     psiconv_application_id_section appl_id;
717     char *temp_str;
718     psiconv_u32 pwd_sec = 0;
719     psiconv_u32 status_sec = 0;
720     psiconv_u32 page_sec = 0;
721     psiconv_u32 applid_sec = 0;
722 frodo 95 psiconv_u32 workbook_sec = 0;
723 frodo 94 psiconv_section_table_entry entry;
724     psiconv_u32 sto;
725     int i;
726    
727 frodo 168 psiconv_progress(config,lev+1,off,"Going to read a sheet file");
728 frodo 94 if (!(*result = malloc(sizeof(**result))))
729     goto ERROR1;
730    
731 frodo 168 psiconv_progress(config,lev+2,off,
732 frodo 94 "Going to read the offset of the section table section");
733 frodo 168 sto = psiconv_read_u32(config,buf,lev+2,off,&res);
734 frodo 94 if (res)
735     goto ERROR2;
736 frodo 168 psiconv_debug(config,lev+2,off,"Offset: %08x",sto);
737 frodo 94
738 frodo 168 psiconv_progress(config,lev+2,sto,
739 frodo 94 "Going to read the section table section");
740 frodo 168 if ((res = psiconv_parse_section_table_section(config,buf,lev+2,sto, NULL,&table)))
741 frodo 94 goto ERROR2;
742    
743     for (i = 0; i < psiconv_list_length(table); i ++) {
744 frodo 168 psiconv_progress(config,lev+2,sto, "Going to read entry %d",i);
745 frodo 94 if (!(entry = psiconv_list_get(table,i)))
746     goto ERROR3;
747     if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
748     applid_sec = entry->offset;
749 frodo 168 psiconv_debug(config,lev+3,sto,
750 frodo 94 "Found the Application ID section at %08x",applid_sec);
751     } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) {
752     page_sec = entry->offset;
753 frodo 168 psiconv_debug(config,lev+3,sto,
754 frodo 94 "Found the Page Layout section at %08x",page_sec);
755     } else if (entry->id == PSICONV_ID_PASSWORD_SECTION) {
756     pwd_sec = entry->offset;
757 frodo 168 psiconv_debug(config,lev+3,sto,
758 frodo 94 "Found the Password section at %08x",pwd_sec);
759 frodo 184 psiconv_error(config,lev+3,sto,
760 frodo 94 "Password section found - can't read encrypted data");
761     res = -PSICONV_E_PARSE;
762     goto ERROR3;
763 frodo 95 } else if (entry->id == PSICONV_ID_SHEET_WORKBOOK_SECTION) {
764     workbook_sec = entry->offset;
765 frodo 168 psiconv_debug(config,lev+3,sto,
766 frodo 95 "Found the Sheet Workbook section at %08x",workbook_sec);
767 frodo 94 } else if (entry->id == PSICONV_ID_SHEET_STATUS_SECTION) {
768     status_sec = entry->offset;
769 frodo 168 psiconv_debug(config,lev+3,sto,
770 frodo 94 "Found the Sheet Status section at %08x",status_sec);
771     } else {
772 frodo 168 psiconv_warn(config,lev+3,sto,
773 frodo 94 "Found unknown section in the Section Table (ignoring)");
774 frodo 168 psiconv_debug(config,lev+3,sto,
775 frodo 94 "Section ID %08x, offset %08x",entry->id,entry->offset);
776     }
777     }
778    
779    
780 frodo 168 psiconv_progress(config,lev+2,sto,
781 frodo 94 "Looking for the Status section");
782     if (!status_sec) {
783 frodo 184 psiconv_error(config,lev+2,sto, "Status section not found in the section table");
784 frodo 94 res = -PSICONV_E_PARSE;
785     goto ERROR3;
786     } else {
787 frodo 168 psiconv_debug(config,lev+2,sto, "Status section at offset %08x",status_sec);
788     if ((res = psiconv_parse_sheet_status_section(config,buf,lev+2,status_sec,NULL,
789 frodo 94 &((*result)->status_sec))))
790     goto ERROR3;
791     }
792    
793 frodo 168 psiconv_progress(config,lev+2,sto, "Looking for the Application ID section");
794 frodo 94 if (! applid_sec) {
795 frodo 184 psiconv_error(config,lev+2,sto,
796 frodo 94 "Application ID section not found in the section table");
797     res = -PSICONV_E_PARSE;
798     goto ERROR4;
799     } else {
800 frodo 168 psiconv_debug(config,lev+2,sto,
801 frodo 94 "Application ID section at offset %08x",applid_sec);
802 frodo 168 if ((res = psiconv_parse_application_id_section(config,buf,lev+2,applid_sec,NULL,
803 frodo 94 &appl_id)))
804     goto ERROR4;
805     }
806     if ((appl_id->id != PSICONV_ID_SHEET) ||
807 frodo 237 !applid_matches(appl_id->name,"sheet.app")) {
808 frodo 168 psiconv_warn(config,lev+2,applid_sec,
809 frodo 94 "Application ID section contains unexpected data");
810 frodo 168 psiconv_debug(config,lev+2,applid_sec,"ID: %08x expected, %08x found",
811 frodo 94 PSICONV_ID_SHEET,appl_id->id);
812 frodo 184 if (!(temp_str = psiconv_make_printable(config,appl_id->name)))
813 frodo 94 goto ERROR5;
814 frodo 168 psiconv_debug(config,lev+2,applid_sec,"Name: `%s' expected, `%s' found",
815 frodo 95 "Sheet.app",temp_str);
816 frodo 94 free(temp_str);
817     res = -PSICONV_E_PARSE;
818     goto ERROR5;
819     }
820    
821 frodo 168 psiconv_progress(config,lev+2,sto,
822 frodo 94 "Looking for the Page layout section");
823     if (! page_sec) {
824 frodo 184 psiconv_error(config,lev+2,sto,
825 frodo 94 "Page layout section not found in the section table");
826     res = -PSICONV_E_PARSE;
827     goto ERROR5;
828     } else {
829 frodo 168 psiconv_debug(config,lev+2,sto,
830 frodo 94 "Page layout section at offset %08x",page_sec);
831 frodo 168 if ((res = psiconv_parse_page_layout_section(config,buf,lev+2,page_sec,NULL,
832 frodo 94 &(*result)->page_sec)))
833     goto ERROR5;
834     }
835    
836 frodo 168 psiconv_progress(config,lev+2,sto,
837 frodo 95 "Looking for the Sheet Workbook section");
838     if (! workbook_sec) {
839 frodo 184 psiconv_error(config,lev+2,sto,
840 frodo 95 "Sheet workbook section not found in the section table");
841     res = -PSICONV_E_PARSE;
842     goto ERROR6;
843     } else {
844 frodo 168 psiconv_debug(config,lev+2,sto,
845 frodo 95 "Sheet workbook section at offset %08x",page_sec);
846 frodo 168 if ((res = psiconv_parse_sheet_workbook_section(config,buf,lev+2,workbook_sec,NULL,
847 frodo 95 &(*result)->workbook_sec)))
848     goto ERROR6;
849     }
850    
851 frodo 94 psiconv_free_application_id_section(appl_id);
852     psiconv_free_section_table_section(table);
853    
854 frodo 168 psiconv_progress(config,lev+1,off,"End of Sheet file");
855 frodo 94 return 0;
856    
857 frodo 95 ERROR6:
858     psiconv_free_page_layout_section((*result)->page_sec);
859 frodo 94 ERROR5:
860     psiconv_free_application_id_section(appl_id);
861     ERROR4:
862     psiconv_free_sheet_status_section((*result)->status_sec);
863     ERROR3:
864     psiconv_free_section_table_section(table);
865     ERROR2:
866     free(*result);
867     ERROR1:
868 frodo 184 psiconv_error(config,lev+1,off,"Reading of Sheet File failed");
869 frodo 94 if (res == 0)
870     return -PSICONV_E_NOMEM;
871     else
872     return res;
873     }

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26