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