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