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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 60 - (show annotations)
Sun Dec 10 20:27:27 2000 UTC (19 years, 5 months ago) by frodo
File MIME type: text/plain
File size: 19152 byte(s)
(Frodo) psiconv_verbosity was undefined!

1 /*
2 parse_driver.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
4
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 #include <stdlib.h>
22
23 #include "parse.h"
24 #include "parse_routines.h"
25 #include "data.h"
26
27 int psiconv_verbosity = PSICONV_VERB_WARN;
28
29 psiconv_file_type_t psiconv_file_type(psiconv_buffer buf,int *length,
30 psiconv_header_section *result)
31 {
32 psiconv_header_section header;
33 psiconv_file_type_t res;
34 int leng;
35
36 psiconv_parse_header_section(buf,0,0,&leng,&header);
37 res = header->file;
38 if (result)
39 *result = header;
40 else
41 psiconv_free_header_section(header);
42 if (length)
43 *length = leng;
44 return res;
45 }
46
47 int psiconv_parse(const psiconv_buffer buf,psiconv_file *result)
48 {
49 int res=0;
50 int lev=0;
51 int off=0;
52 int leng;
53
54 (*result) = malloc(sizeof(**result));
55
56 (*result)->type = psiconv_file_type(buf,&leng,NULL);
57 if ((*result)->type == psiconv_unknown_file) {
58 psiconv_warn(lev+1,off,"Unknown file type: can't parse!");
59 (*result)->file = NULL;
60 res = -1;
61 } else if ((*result)->type == psiconv_word_file)
62 res = psiconv_parse_word_file(buf,lev+2,leng,
63 (psiconv_word_f *)(&((*result)->file)));
64 else if ((*result)->type == psiconv_texted_file)
65 res = psiconv_parse_texted_file(buf,lev+2,leng,
66 (psiconv_texted_f *)(&((*result)->file)));
67 else if ((*result)->type == psiconv_mbm_file)
68 res = psiconv_parse_mbm_file(buf,lev+2,leng,
69 (psiconv_mbm_f *)(&((*result)->file)));
70 else if ((*result)->type == psiconv_sketch_file)
71 res = psiconv_parse_sketch_file(buf,lev+2,leng,
72 (psiconv_sketch_f *)(&((*result)->file)));
73 else if ((*result)->type == psiconv_clipart_file)
74 res = psiconv_parse_clipart_file(buf,lev+2,leng,
75 (psiconv_clipart_f *)(&((*result)->file)));
76 else {
77 psiconv_warn(lev+1,off,"Can't parse this file yet!");
78 (*result)->file = NULL;
79 }
80 res = -1;
81
82 return res;
83 }
84
85 int psiconv_parse_clipart_file(const psiconv_buffer buf,int lev,
86 psiconv_u32 off, psiconv_clipart_f *result)
87 {
88 int res=0;
89 int i;
90 psiconv_jumptable_section table;
91 psiconv_clipart_section clipart;
92 psiconv_u32 *entry;
93
94 psiconv_progress(lev+1,off,"Going to read a clipart file");
95 (*result) = malloc(sizeof(**result));
96
97 psiconv_progress(lev+2,off,"Going to read the MBM jumptable");
98 res |= psiconv_parse_jumptable_section(buf,lev+2,off, NULL,&table);
99
100 psiconv_progress(lev+2,off,"Going to read the clipart sections");
101 (*result)->sections = psiconv_list_new(sizeof(*clipart));
102 for (i = 0; i < psiconv_list_length(table); i ++) {
103 entry = psiconv_list_get(table,i);
104 psiconv_progress(lev+3,off,"Going to read clipart section %i",i);
105 psiconv_parse_clipart_section(buf,lev+3,*entry,NULL,&clipart);
106 psiconv_list_add((*result)->sections,clipart);
107 }
108
109 psiconv_free_jumptable_section(table);
110 psiconv_progress(lev+1,off,"End of clipart file");
111 return res;
112 }
113
114 int psiconv_parse_mbm_file(const psiconv_buffer buf,int lev, psiconv_u32 off,
115 psiconv_mbm_f *result)
116 {
117 int res=0;
118 int i;
119 psiconv_jumptable_section table;
120 psiconv_paint_data_section paint;
121 psiconv_u32 *entry;
122 psiconv_u32 sto;
123
124 psiconv_progress(lev+1,off,"Going to read a mbm file");
125 *result = malloc(sizeof(**result));
126
127 psiconv_progress(lev+2,off,"Going to read the offset of the MBM jumptable");
128 sto = psiconv_read_u32(buf,lev+2,off);
129 psiconv_debug(lev+2,off,"Offset: %08x",sto);
130
131 psiconv_progress(lev+2,off,"Going to read the MBM jumptable");
132 res |= psiconv_parse_jumptable_section(buf,lev+2,sto, NULL,&table);
133
134 psiconv_progress(lev+2,off,"Going to read the picture sections");
135 (*result)->sections = psiconv_list_new(sizeof(*paint));
136 for (i = 0; i < psiconv_list_length(table); i ++) {
137 entry = psiconv_list_get(table,i);
138 psiconv_progress(lev+3,off,"Going to read picture section %i",i);
139 psiconv_parse_paint_data_section(buf,lev+3,*entry,NULL,0,&paint);
140 psiconv_list_add((*result)->sections,paint);
141 }
142
143 psiconv_free_jumptable_section(table);
144 psiconv_progress(lev+1,off,"End of mbm file");
145 return res;
146 }
147
148 int psiconv_parse_sketch_file(const psiconv_buffer buf,int lev,
149 psiconv_u32 off,
150 psiconv_sketch_f *result)
151 {
152 psiconv_section_table_section table;
153 psiconv_application_id_section appl_id;
154 psiconv_u32 applid_sec = 0;
155 psiconv_u32 sketch_sec = 0;
156 psiconv_u32 sto;
157 psiconv_section_table_entry entry;
158 int i;
159 int res=0;
160 char *temp_str;
161
162 psiconv_progress(lev+1,off,"Going to read a sketch file");
163 *result = malloc(sizeof(**result));
164
165 psiconv_progress(lev+2,off,
166 "Going to read the offset of the section table section");
167 sto = psiconv_read_u32(buf,lev+2,off);
168 psiconv_debug(lev+2,off,"Offset: %08x",sto);
169
170 psiconv_progress(lev+2,sto, "Going to read the section table section");
171 res |= psiconv_parse_section_table_section(buf,lev+2,sto, NULL,&table);
172
173 for (i = 0; i < psiconv_list_length(table); i ++) {
174 psiconv_progress(lev+2,sto, "Going to read entry %d",i);
175 entry = psiconv_list_get(table,i);
176 if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
177 applid_sec = entry->offset;
178 psiconv_debug(lev+3,sto,
179 "Found the Application ID section at %08x",applid_sec);
180 } else if (entry->id == PSICONV_ID_SKETCH_SECTION) {
181 sketch_sec = entry->offset;
182 psiconv_debug(lev+3,sto,
183 "Found the Sketch section at %08x",sketch_sec);
184 } else {
185 psiconv_warn(lev+3,sto,
186 "Found unknown section in the Section Table");
187 psiconv_debug(lev+3,sto,
188 "Section ID %08x, offset %08x",entry->id,entry->offset);
189 res = -1;
190 }
191 }
192
193 psiconv_progress(lev+2,sto, "Looking for the Application ID section");
194 if (! applid_sec) {
195 psiconv_warn(lev+2,sto,
196 "Application ID section not found in the section table");
197 res = -1;
198 } else {
199 psiconv_debug(lev+2,sto,
200 "Application ID section at offset %08x",applid_sec);
201 res |= psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL,
202 &appl_id);
203 }
204 if ((appl_id->id != PSICONV_ID_SKETCH) ||
205 strcmp(appl_id->name,"Paint.app")) {
206 psiconv_warn(lev+2,applid_sec,
207 "Application ID section contains unexpected data");
208 psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found",
209 PSICONV_ID_SKETCH,appl_id->id);
210 temp_str = psiconv_make_printable(appl_id->name);
211 psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found",
212 "Paint.app",temp_str);
213 free(temp_str);
214 }
215
216 psiconv_progress(lev+2,sto, "Looking for the Sketch section");
217 if (! sketch_sec) {
218 psiconv_warn(lev+2,sto,
219 "Sketch section not found in the section table");
220 res = -1;
221 } else {
222 psiconv_debug(lev+2,sto,
223 "Sketch section at offset %08x",applid_sec);
224 res |= psiconv_parse_sketch_section(buf,lev+2,sketch_sec,NULL,0,
225 &(*result)->sketch_sec);
226 }
227
228 psiconv_free_application_id_section(appl_id);
229 psiconv_free_section_table_section(table);
230
231 psiconv_progress(lev+1,off,"End of word file");
232 return res;
233 }
234
235
236
237 int psiconv_parse_texted_file(const psiconv_buffer buf,int lev,
238 psiconv_u32 off,
239 psiconv_texted_f *result)
240 {
241 int res=0;
242 psiconv_section_table_section table;
243 psiconv_application_id_section appl_id;
244 char *temp_str;
245 psiconv_character_layout base_char;
246 psiconv_paragraph_layout base_para;
247 psiconv_u32 page_sec = 0;
248 psiconv_u32 texted_sec = 0;
249 psiconv_u32 applid_sec = 0;
250 psiconv_u32 sto;
251 psiconv_section_table_entry entry;
252 int i;
253
254 psiconv_progress(lev+1,off,"Going to read a texted file");
255 *result = malloc(sizeof(**result));
256
257 psiconv_progress(lev+2,off,
258 "Going to read the offset of the section table section");
259 sto = psiconv_read_u32(buf,lev+2,off);
260 psiconv_debug(lev+2,off,"Offset: %08x",sto);
261
262 psiconv_progress(lev+2,sto, "Going to read the section table section");
263 res |= psiconv_parse_section_table_section(buf,lev+2,sto, NULL,&table);
264
265 for (i = 0; i < psiconv_list_length(table); i ++) {
266 psiconv_progress(lev+2,sto, "Going to read entry %d",i);
267 entry = psiconv_list_get(table,i);
268 if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
269 applid_sec = entry->offset;
270 psiconv_debug(lev+3,sto,
271 "Found the Application ID section at %08x",applid_sec);
272 } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) {
273 page_sec = entry->offset;
274 psiconv_debug(lev+3,sto,
275 "Found the Page Layout section at %08x",page_sec);
276 } else if (entry->id == PSICONV_ID_TEXTED) {
277 texted_sec = entry->offset;
278 psiconv_debug(lev+3,sto,
279 "Found the TextEd section at %08x",texted_sec);
280 } else {
281 psiconv_warn(lev+3,sto,
282 "Found unknown section in the Section Table");
283 psiconv_debug(lev+3,sto,
284 "Section ID %08x, offset %08x",entry->id,entry->offset);
285 res = -1;
286 }
287 }
288
289 psiconv_progress(lev+2,sto, "Looking for the Application ID section");
290 if (! applid_sec) {
291 psiconv_warn(lev+2,sto,
292 "Application ID section not found in the section table");
293 res = -1;
294 } else {
295 psiconv_debug(lev+2,sto,
296 "Application ID section at offset %08x",applid_sec);
297 res |= psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL,
298 &appl_id);
299 }
300 if ((appl_id->id != PSICONV_ID_TEXTED) ||
301 strcmp(appl_id->name,"TextEd.app")) {
302 psiconv_warn(lev+2,applid_sec,
303 "Application ID section contains unexpected data");
304 psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found",
305 PSICONV_ID_TEXTED,appl_id->id);
306 temp_str = psiconv_make_printable(appl_id->name);
307 psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found",
308 "TextEd.app",temp_str);
309 free(temp_str);
310 }
311
312 psiconv_progress(lev+2,sto,
313 "Looking for the Page layout section");
314 if (! page_sec) {
315 psiconv_warn(lev+2,sto,
316 "Page layout section not found in the section table");
317 (*result)->page_sec = NULL;
318 res = -1;
319 } else {
320 psiconv_debug(lev+2,sto,
321 "Page layout section at offset %08x",page_sec);
322 res |= psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL,
323 &(*result)->page_sec);
324 }
325
326 base_char = psiconv_basic_character_layout();
327 base_para = psiconv_basic_paragraph_layout();
328
329 psiconv_progress(lev+2,sto,
330 "Looking for the TextEd section");
331 if (! texted_sec) {
332 psiconv_warn(lev+2,sto,
333 "TextEd section not found in the section table");
334 (*result)->texted_sec = NULL;
335 res = -1;
336 } else {
337 psiconv_debug(lev+2,sto, "TextEd section at offset %08x",texted_sec);
338 res |= psiconv_parse_texted_section(buf,lev+2,texted_sec,NULL,
339 &(*result)->texted_sec,
340 base_char,base_para);
341 }
342 psiconv_free_character_layout(base_char);
343 psiconv_free_paragraph_layout(base_para);
344
345 psiconv_free_application_id_section(appl_id);
346 psiconv_free_section_table_section(table);
347
348 psiconv_progress(lev+1,off,"End of word file");
349 return res;
350 }
351
352 int psiconv_parse_word_file(const psiconv_buffer buf,int lev, psiconv_u32 off,
353 psiconv_word_f *result)
354 {
355 int res=0;
356 psiconv_section_table_section table;
357 psiconv_application_id_section appl_id;
358 char *temp_str;
359 psiconv_u32 pwd_sec = 0;
360 psiconv_u32 status_sec = 0;
361 psiconv_u32 styles_sec = 0;
362 psiconv_u32 page_sec = 0;
363 psiconv_u32 text_sec = 0;
364 psiconv_u32 layout_sec = 0;
365 psiconv_u32 applid_sec = 0;
366 psiconv_section_table_entry entry;
367 psiconv_u32 sto;
368 int i;
369
370 psiconv_progress(lev+1,off,"Going to read a word file");
371 *result = malloc(sizeof(**result));
372
373 psiconv_progress(lev+2,off,
374 "Going to read the offset of the section table section");
375 sto = psiconv_read_u32(buf,lev+2,off);
376 psiconv_debug(lev+2,off,"Offset: %08x",sto);
377
378 psiconv_progress(lev+2,sto,
379 "Going to read the section table section");
380 res |= psiconv_parse_section_table_section(buf,lev+2,sto, NULL,&table);
381
382 for (i = 0; i < psiconv_list_length(table); i ++) {
383 psiconv_progress(lev+2,sto, "Going to read entry %d",i);
384 entry = psiconv_list_get(table,i);
385 if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
386 applid_sec = entry->offset;
387 psiconv_debug(lev+3,sto,
388 "Found the Application ID section at %08x",applid_sec);
389 } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) {
390 page_sec = entry->offset;
391 psiconv_debug(lev+3,sto,
392 "Found the Page Layout section at %08x",page_sec);
393 } else if (entry->id == PSICONV_ID_TEXT_SECTION) {
394 text_sec = entry->offset;
395 psiconv_debug(lev+3,sto, "Found the Text section at %08x",text_sec);
396 } else if (entry->id == PSICONV_ID_PASSWORD_SECTION) {
397 pwd_sec = entry->offset;
398 psiconv_debug(lev+3,sto,
399 "Found the Password section at %08x",pwd_sec);
400 psiconv_warn(lev+3,sto,
401 "Password section found - can't read encrypted data");
402 res = -1;
403 } else if (entry->id == PSICONV_ID_WORD_STATUS_SECTION) {
404 status_sec = entry->offset;
405 psiconv_debug(lev+3,sto,
406 "Found the Word Status section at %08x",status_sec);
407 } else if (entry->id == PSICONV_ID_WORD_STYLES_SECTION) {
408 styles_sec = entry->offset;
409 psiconv_debug(lev+3,sto,
410 "Found the Word Styles section at %08x",styles_sec);
411 } else if (entry->id == PSICONV_ID_LAYOUT_SECTION) {
412 layout_sec = entry->offset;
413 psiconv_debug(lev+3,sto,
414 "Found the Layout section at %08x",layout_sec);
415 } else {
416 psiconv_warn(lev+3,sto,
417 "Found unknown section in the Section Table");
418 psiconv_debug(lev+3,sto,
419 "Section ID %08x, offset %08x",entry->id,entry->offset);
420 res = -1;
421 }
422 }
423
424
425 psiconv_progress(lev+2,sto,
426 "Looking for the Status section");
427 if (!status_sec) {
428 psiconv_warn(lev+2,sto, "Status section not found in the section table");
429 res = -1;
430 } else {
431 psiconv_debug(lev+2,sto, "Status section at offset %08x",status_sec);
432 res |= psiconv_parse_word_status_section(buf,lev+2,status_sec,NULL,
433 &((*result)->status_sec));
434 }
435
436 psiconv_progress(lev+2,sto, "Looking for the Application ID section");
437 if (! applid_sec) {
438 psiconv_warn(lev+2,sto,
439 "Application ID section not found in the section table");
440 res = -1;
441 } else {
442 psiconv_debug(lev+2,sto,
443 "Application ID section at offset %08x",applid_sec);
444 res |= psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL,
445 &appl_id);
446 }
447 if ((appl_id->id != PSICONV_ID_WORD) ||
448 strcmp(appl_id->name,"Word.app")) {
449 psiconv_warn(lev+2,applid_sec,
450 "Application ID section contains unexpected data");
451 psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found",
452 PSICONV_ID_WORD,appl_id->id);
453 temp_str = psiconv_make_printable(appl_id->name);
454 psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found",
455 "Word.app",temp_str);
456 free(temp_str);
457 }
458
459 psiconv_progress(lev+2,sto,
460 "Looking for the Page layout section");
461 if (! page_sec) {
462 psiconv_warn(lev+2,sto,
463 "Page layout section not found in the section table");
464 (*result)->page_sec = NULL;
465 res = -1;
466 } else {
467 psiconv_debug(lev+2,sto,
468 "Page layout section at offset %08x",page_sec);
469 res |= psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL,
470 &(*result)->page_sec);
471 }
472
473 psiconv_progress(lev+2,sto,
474 "Looking for the Word Style section");
475 if (!styles_sec) {
476 psiconv_warn(lev+2,sto,
477 "Word styles section not found in the section table");
478 (*result)->styles_sec = NULL;
479 res = -1;
480 } else {
481 psiconv_debug(lev+2,sto,
482 "Word styles section at offset %08x",styles_sec);
483 res |= psiconv_parse_word_styles_section(buf,lev+2,styles_sec,NULL,
484 &(*result)->styles_sec);
485 }
486
487 psiconv_progress(lev+2,sto,
488 "Looking for the Text section");
489 if (!text_sec) {
490 psiconv_warn(lev+2,sto, "Text section not found in the section table");
491 (*result)->paragraphs = NULL;
492 res = -1;
493 } else {
494 psiconv_debug(lev+2,sto,
495 "Text section at offset %08x",text_sec);
496 res |= psiconv_parse_text_section(buf,lev+2,text_sec,NULL,
497 &(*result)->paragraphs);
498 }
499
500 if (((*result)->paragraphs) && ((*result)->styles_sec)) {
501 psiconv_progress(lev+2,sto,
502 "Looking for the Layout section");
503 if (!layout_sec) {
504 psiconv_debug(lev+2,sto,
505 "Layout section not found in the section table");
506 res = -1;
507 } else {
508 psiconv_debug(lev+2,sto,
509 "Layout section at offset %08x",layout_sec);
510 res |= psiconv_parse_styled_layout_section(buf,lev+2,layout_sec,NULL,
511 (*result)->paragraphs,
512 (*result)->styles_sec);
513 }
514 } else
515 psiconv_debug(lev+2,sto,
516 "Skipping search for Layout section, as either the "
517 "text or the word styles section was not found");
518
519 psiconv_free_application_id_section(appl_id);
520 psiconv_free_section_table_section(table);
521
522 psiconv_progress(lev+1,off,"End of word file");
523 return res;
524 }

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