/[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 45 - (show annotations)
Sat Dec 4 20:53:11 1999 UTC (24 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 19108 byte(s)
(Frodo) Clip Art files now working correctly.

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

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