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