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. |
… | |
… | |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #include "config.h" |
20 | #include "config.h" |
|
|
21 | #include "compat.h" |
|
|
22 | |
21 | #include <stdlib.h> |
23 | #include <stdlib.h> |
22 | |
24 | |
23 | #include "parse.h" |
25 | #include "parse.h" |
24 | #include "parse_routines.h" |
26 | #include "parse_routines.h" |
25 | #include "data.h" |
|
|
26 | |
27 | |
27 | psiconv_file_type_t psiconv_file_type(psiconv_buffer buf) |
28 | psiconv_file_type_t psiconv_file_type(psiconv_buffer buf,int *length, |
|
|
29 | psiconv_header_section *result) |
28 | { |
30 | { |
29 | psiconv_header_section header; |
31 | psiconv_header_section header; |
30 | psiconv_file_type_t res; |
32 | psiconv_file_type_t res; |
|
|
33 | int leng; |
31 | |
34 | |
32 | psiconv_parse_header_section(buf,0,0,NULL,&header); |
35 | if ((psiconv_parse_header_section(buf,0,0,&leng,&header))) |
|
|
36 | return psiconv_unknown_file; |
33 | res = header->file; |
37 | res = header->file; |
|
|
38 | if (result) |
|
|
39 | *result = header; |
|
|
40 | else |
34 | psiconv_free_header_section(header); |
41 | psiconv_free_header_section(header); |
|
|
42 | if (length) |
|
|
43 | *length = leng; |
35 | return res; |
44 | return res; |
36 | } |
45 | } |
37 | |
46 | |
38 | int psiconv_parse(const psiconv_buffer buf,psiconv_file *result) |
47 | int psiconv_parse(const psiconv_buffer buf,psiconv_file *result) |
39 | { |
48 | { |
40 | int res=0; |
49 | int res=0; |
41 | int lev=0; |
50 | int lev=0; |
42 | int off=0; |
51 | int off=0; |
|
|
52 | int leng; |
43 | |
53 | |
44 | (*result) = malloc(sizeof(**result)); |
54 | if (!((*result) = malloc(sizeof(**result)))) |
|
|
55 | goto ERROR1; |
45 | |
56 | |
46 | (*result)->type = psiconv_file_type(buf); |
57 | (*result)->type = psiconv_file_type(buf,&leng,NULL); |
47 | if ((*result)->type == psiconv_unknown_file) { |
58 | if ((*result)->type == psiconv_unknown_file) { |
48 | psiconv_warn(lev+1,off,"Unknown file type: can't parse!"); |
59 | psiconv_warn(lev+1,off,"Unknown file type: can't parse!"); |
49 | (*result)->file = NULL; |
60 | (*result)->file = NULL; |
50 | res = -1; |
|
|
51 | } else if ((*result)->type == psiconv_word_file) |
61 | } else if ((*result)->type == psiconv_word_file) |
52 | res = psiconv_parse_word_file(buf,lev+2,off, |
62 | res = psiconv_parse_word_file(buf,lev+2,leng, |
53 | (psiconv_word_f *)(&((*result)->file))); |
63 | (psiconv_word_f *)(&((*result)->file))); |
54 | else if ((*result)->type == psiconv_texted_file) |
64 | else if ((*result)->type == psiconv_texted_file) |
55 | res = psiconv_parse_texted_file(buf,lev+2,off, |
65 | res = psiconv_parse_texted_file(buf,lev+2,leng, |
56 | (psiconv_texted_f *)(&((*result)->file))); |
66 | (psiconv_texted_f *)(&((*result)->file))); |
57 | else if ((*result)->type == psiconv_mbm_file) |
67 | else if ((*result)->type == psiconv_mbm_file) |
58 | res = psiconv_parse_mbm_file(buf,lev+2,off, |
68 | res = psiconv_parse_mbm_file(buf,lev+2,leng, |
59 | (psiconv_mbm_f *)(&((*result)->file))); |
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 if ((*result)->type == psiconv_sheet_file) |
|
|
77 | res = psiconv_parse_sheet_file(buf,lev+2,leng, |
|
|
78 | (psiconv_sheet_f *)(&((*result)->file))); |
60 | else { |
79 | else { |
61 | psiconv_warn(lev+1,off,"Can't parse this file yet!"); |
80 | psiconv_warn(lev+1,off,"Can't parse this file yet!"); |
62 | (*result)->file = NULL; |
81 | (*result)->file = NULL; |
63 | } |
82 | } |
64 | res = -1; |
83 | if (res) |
|
|
84 | goto ERROR2; |
|
|
85 | return 0; |
65 | |
86 | |
|
|
87 | ERROR2: |
|
|
88 | free(*result); |
|
|
89 | ERROR1: |
|
|
90 | psiconv_warn(lev+1,off,"Reading of Psion File failed"); |
|
|
91 | if (res == 0) |
|
|
92 | return -PSICONV_E_NOMEM; |
|
|
93 | else |
|
|
94 | return res; |
|
|
95 | } |
|
|
96 | |
|
|
97 | int psiconv_parse_clipart_file(const psiconv_buffer buf,int lev, |
|
|
98 | psiconv_u32 off, psiconv_clipart_f *result) |
|
|
99 | { |
|
|
100 | int res=0; |
|
|
101 | int i; |
|
|
102 | psiconv_jumptable_section table; |
|
|
103 | psiconv_clipart_section clipart; |
|
|
104 | psiconv_u32 *entry; |
|
|
105 | |
|
|
106 | psiconv_progress(lev+1,off,"Going to read a clipart file"); |
|
|
107 | if (!((*result) = malloc(sizeof(**result)))) |
|
|
108 | goto ERROR1; |
|
|
109 | |
|
|
110 | psiconv_progress(lev+2,off,"Going to read the MBM jumptable"); |
|
|
111 | if ((res = psiconv_parse_jumptable_section(buf,lev+2,off, NULL,&table))) |
|
|
112 | goto ERROR2; |
|
|
113 | |
|
|
114 | psiconv_progress(lev+2,off,"Going to read the clipart sections"); |
|
|
115 | if (!((*result)->sections = psiconv_list_new(sizeof(*clipart)))) |
|
|
116 | goto ERROR3; |
|
|
117 | for (i = 0; i < psiconv_list_length(table); i ++) { |
|
|
118 | if (!(entry = psiconv_list_get(table,i))) |
|
|
119 | goto ERROR4; |
|
|
120 | psiconv_progress(lev+3,off,"Going to read clipart section %i",i); |
|
|
121 | if ((res = psiconv_parse_clipart_section(buf,lev+3,*entry,NULL,&clipart))) |
|
|
122 | goto ERROR4; |
|
|
123 | if ((res = psiconv_list_add((*result)->sections,clipart))) |
|
|
124 | goto ERROR5; |
|
|
125 | } |
|
|
126 | |
|
|
127 | psiconv_free_jumptable_section(table); |
|
|
128 | psiconv_progress(lev+1,off,"End of clipart file"); |
66 | return res; |
129 | return res; |
|
|
130 | ERROR5: |
|
|
131 | psiconv_free_clipart_section(clipart); |
|
|
132 | ERROR4: |
|
|
133 | for (i = 0; i < psiconv_list_length((*result)->sections); i++) { |
|
|
134 | if (!(clipart = psiconv_list_get((*result)->sections,i))) { |
|
|
135 | psiconv_warn(lev+1,off,"Massive memory corruption"); |
|
|
136 | goto ERROR3; |
|
|
137 | } |
|
|
138 | psiconv_free_clipart_section(clipart); |
|
|
139 | } |
|
|
140 | psiconv_list_free((*result)->sections); |
|
|
141 | ERROR3: |
|
|
142 | psiconv_free_jumptable_section(table); |
|
|
143 | ERROR2: |
|
|
144 | free(*result); |
|
|
145 | ERROR1: |
|
|
146 | psiconv_warn(lev+1,off,"Reading of Clipart File failed"); |
|
|
147 | if (res == 0) |
|
|
148 | return -PSICONV_E_NOMEM; |
|
|
149 | else |
|
|
150 | return res; |
67 | } |
151 | } |
68 | |
152 | |
69 | int psiconv_parse_mbm_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
153 | int psiconv_parse_mbm_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
70 | psiconv_mbm_f *result) |
154 | psiconv_mbm_f *result) |
71 | { |
155 | { |
72 | int res=0; |
156 | int res=0; |
73 | int i; |
157 | int i; |
74 | psiconv_mbm_jumptable_section table; |
158 | psiconv_jumptable_section table; |
75 | psiconv_paint_data_section paint; |
159 | psiconv_paint_data_section paint; |
76 | psiconv_header_section header; |
|
|
77 | psiconv_u32 *entry; |
160 | psiconv_u32 *entry; |
|
|
161 | psiconv_u32 sto; |
78 | |
162 | |
79 | psiconv_progress(lev+1,off,"Going to read a mbm file"); |
163 | psiconv_progress(lev+1,off,"Going to read a mbm file"); |
80 | *result = malloc(sizeof(**result)); |
164 | if (!(*result = malloc(sizeof(**result)))) |
|
|
165 | goto ERROR1; |
81 | |
166 | |
82 | psiconv_progress(lev+2,off, "Going to read the header section"); |
167 | 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); |
168 | sto = psiconv_read_u32(buf,lev+2,off,&res); |
|
|
169 | if (res) |
|
|
170 | goto ERROR2; |
|
|
171 | psiconv_debug(lev+2,off,"Offset: %08x",sto); |
84 | |
172 | |
85 | psiconv_progress(lev+2,off,"Going to read the MBM jumptable"); |
173 | psiconv_progress(lev+2,off,"Going to read the MBM jumptable"); |
86 | res |= psiconv_parse_mbm_jumptable_section(buf,lev+2, |
174 | if ((res = psiconv_parse_jumptable_section(buf,lev+2,sto, NULL,&table))) |
87 | header->section_table_offset, |
175 | goto ERROR2; |
88 | NULL,&table); |
|
|
89 | |
176 | |
|
|
177 | psiconv_progress(lev+2,off,"Going to read the picture sections"); |
90 | (*result)->sections = psiconv_list_new(sizeof(*paint)); |
178 | if (!((*result)->sections = psiconv_list_new(sizeof(*paint)))) |
|
|
179 | goto ERROR3; |
91 | for (i = 0; i < psiconv_list_length(table); i ++) { |
180 | for (i = 0; i < psiconv_list_length(table); i ++) { |
92 | entry = psiconv_list_get(table,i); |
181 | if (!(entry = psiconv_list_get(table,i))) |
|
|
182 | goto ERROR4; |
|
|
183 | psiconv_progress(lev+3,off,"Going to read picture section %i",i); |
93 | psiconv_parse_paint_data_section(buf,lev+2,*entry,NULL,&paint); |
184 | if ((res = psiconv_parse_paint_data_section(buf,lev+3,*entry,NULL, |
94 | psiconv_list_add(table,paint); |
185 | 0,&paint))) |
|
|
186 | goto ERROR4; |
|
|
187 | if ((res = psiconv_list_add((*result)->sections,paint))) |
|
|
188 | goto ERROR5; |
95 | } |
189 | } |
96 | |
190 | |
97 | psiconv_free_header_section(header); |
|
|
98 | psiconv_free_mbm_jumptable_section(table); |
191 | psiconv_free_jumptable_section(table); |
99 | psiconv_progress(lev+1,off,"End of mbm file"); |
192 | psiconv_progress(lev+1,off,"End of mbm file"); |
|
|
193 | return 0; |
|
|
194 | ERROR5: |
|
|
195 | psiconv_free_paint_data_section(paint); |
|
|
196 | ERROR4: |
|
|
197 | for (i = 0; i < psiconv_list_length((*result)->sections); i++) { |
|
|
198 | if (!(paint = psiconv_list_get((*result)->sections,i))) { |
|
|
199 | psiconv_warn(lev+1,off,"Massive memory corruption"); |
|
|
200 | goto ERROR3; |
|
|
201 | } |
|
|
202 | psiconv_free_paint_data_section(paint); |
|
|
203 | } |
|
|
204 | psiconv_list_free((*result)->sections); |
|
|
205 | ERROR3: |
|
|
206 | psiconv_free_jumptable_section(table); |
|
|
207 | ERROR2: |
|
|
208 | free(*result); |
|
|
209 | ERROR1: |
|
|
210 | psiconv_warn(lev+1,off,"Reading of MBM File failed"); |
|
|
211 | if (res == 0) |
|
|
212 | return -PSICONV_E_NOMEM; |
|
|
213 | else |
|
|
214 | return res; |
|
|
215 | } |
|
|
216 | |
|
|
217 | int psiconv_parse_sketch_file(const psiconv_buffer buf,int lev, |
|
|
218 | psiconv_u32 off, |
|
|
219 | psiconv_sketch_f *result) |
|
|
220 | { |
|
|
221 | psiconv_section_table_section table; |
|
|
222 | psiconv_application_id_section appl_id; |
|
|
223 | psiconv_u32 applid_sec = 0; |
|
|
224 | psiconv_u32 sketch_sec = 0; |
|
|
225 | psiconv_u32 sto; |
|
|
226 | psiconv_section_table_entry entry; |
|
|
227 | int i; |
|
|
228 | int res=0; |
|
|
229 | char *temp_str; |
|
|
230 | |
|
|
231 | psiconv_progress(lev+1,off,"Going to read a sketch file"); |
|
|
232 | if (!(*result = malloc(sizeof(**result)))) |
|
|
233 | goto ERROR1; |
|
|
234 | |
|
|
235 | psiconv_progress(lev+2,off, |
|
|
236 | "Going to read the offset of the section table section"); |
|
|
237 | sto = psiconv_read_u32(buf,lev+2,off,&res); |
|
|
238 | if (res) |
|
|
239 | goto ERROR2; |
|
|
240 | psiconv_debug(lev+2,off,"Offset: %08x",sto); |
|
|
241 | |
|
|
242 | psiconv_progress(lev+2,sto, "Going to read the section table section"); |
|
|
243 | if ((res = psiconv_parse_section_table_section(buf,lev+2,sto, NULL,&table))) |
|
|
244 | goto ERROR2; |
|
|
245 | |
|
|
246 | for (i = 0; i < psiconv_list_length(table); i ++) { |
|
|
247 | psiconv_progress(lev+2,sto, "Going to read entry %d",i); |
|
|
248 | if (!(entry = psiconv_list_get(table,i))) |
|
|
249 | goto ERROR3; |
|
|
250 | if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
|
|
251 | applid_sec = entry->offset; |
|
|
252 | psiconv_debug(lev+3,sto, |
|
|
253 | "Found the Application ID section at %08x",applid_sec); |
|
|
254 | } else if (entry->id == PSICONV_ID_SKETCH_SECTION) { |
|
|
255 | sketch_sec = entry->offset; |
|
|
256 | psiconv_debug(lev+3,sto, |
|
|
257 | "Found the Sketch section at %08x",sketch_sec); |
|
|
258 | } else { |
|
|
259 | psiconv_warn(lev+3,sto, |
|
|
260 | "Found unknown section in the Section Table (ignoring)"); |
|
|
261 | psiconv_debug(lev+3,sto, |
|
|
262 | "Section ID %08x, offset %08x",entry->id,entry->offset); |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | psiconv_progress(lev+2,sto, "Looking for the Application ID section"); |
|
|
267 | if (! applid_sec) { |
|
|
268 | psiconv_warn(lev+2,sto, |
|
|
269 | "Application ID section not found in the section table"); |
|
|
270 | res = -PSICONV_E_PARSE; |
|
|
271 | goto ERROR3; |
|
|
272 | } else { |
|
|
273 | psiconv_debug(lev+2,sto, |
|
|
274 | "Application ID section at offset %08x",applid_sec); |
|
|
275 | if ((res = psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
|
|
276 | &appl_id))) |
|
|
277 | goto ERROR3; |
|
|
278 | } |
|
|
279 | if ((appl_id->id != PSICONV_ID_SKETCH) || |
|
|
280 | strcmp(appl_id->name,"Paint.app")) { |
|
|
281 | psiconv_warn(lev+2,applid_sec, |
|
|
282 | "Application ID section contains unexpected data"); |
|
|
283 | psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
|
|
284 | PSICONV_ID_SKETCH,appl_id->id); |
|
|
285 | if (!(temp_str = psiconv_make_printable(appl_id->name))) |
|
|
286 | goto ERROR4; |
|
|
287 | psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
|
|
288 | "Paint.app",temp_str); |
|
|
289 | free(temp_str); |
|
|
290 | res = -PSICONV_E_PARSE; |
|
|
291 | goto ERROR4; |
|
|
292 | } |
|
|
293 | |
|
|
294 | psiconv_progress(lev+2,sto, "Looking for the Sketch section"); |
|
|
295 | if (! sketch_sec) { |
|
|
296 | psiconv_warn(lev+2,sto, |
|
|
297 | "Sketch section not found in the section table"); |
|
|
298 | } else { |
|
|
299 | psiconv_debug(lev+2,sto, |
|
|
300 | "Sketch section at offset %08x",applid_sec); |
|
|
301 | if ((res = psiconv_parse_sketch_section(buf,lev+2,sketch_sec,NULL,0, |
|
|
302 | &(*result)->sketch_sec))) |
|
|
303 | goto ERROR4; |
|
|
304 | } |
|
|
305 | |
|
|
306 | psiconv_free_application_id_section(appl_id); |
|
|
307 | psiconv_free_section_table_section(table); |
|
|
308 | |
|
|
309 | psiconv_progress(lev+1,off,"End of word file"); |
100 | return res; |
310 | return res; |
|
|
311 | |
|
|
312 | ERROR4: |
|
|
313 | psiconv_free_application_id_section(appl_id); |
|
|
314 | ERROR3: |
|
|
315 | free(table); |
|
|
316 | ERROR2: |
|
|
317 | free(*result); |
|
|
318 | ERROR1: |
|
|
319 | psiconv_warn(lev+1,off,"Reading of Scketch File failed"); |
|
|
320 | if (res == 0) |
|
|
321 | return -PSICONV_E_NOMEM; |
|
|
322 | else |
|
|
323 | return res; |
101 | } |
324 | } |
102 | |
325 | |
|
|
326 | |
103 | int psiconv_parse_texted_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
327 | int psiconv_parse_texted_file(const psiconv_buffer buf,int lev, |
|
|
328 | psiconv_u32 off, |
104 | psiconv_texted_f *result) |
329 | psiconv_texted_f *result) |
105 | { |
330 | { |
106 | int res=0; |
331 | int res=0; |
107 | psiconv_header_section header; |
|
|
108 | psiconv_section_table_section table; |
332 | psiconv_section_table_section table; |
109 | psiconv_application_id_section appl_id; |
333 | psiconv_application_id_section appl_id; |
110 | char *temp_str; |
334 | char *temp_str; |
111 | psiconv_character_layout base_char; |
335 | psiconv_character_layout base_char; |
112 | psiconv_paragraph_layout base_para; |
336 | psiconv_paragraph_layout base_para; |
113 | psiconv_u32 page_sec = 0; |
337 | psiconv_u32 page_sec = 0; |
114 | psiconv_u32 texted_sec = 0; |
338 | psiconv_u32 texted_sec = 0; |
115 | psiconv_u32 applid_sec = 0; |
339 | psiconv_u32 applid_sec = 0; |
|
|
340 | psiconv_u32 sto; |
116 | psiconv_section_table_entry entry; |
341 | psiconv_section_table_entry entry; |
117 | int i; |
342 | int i; |
118 | |
343 | |
119 | psiconv_progress(lev+1,off,"Going to read a texted file"); |
344 | psiconv_progress(lev+1,off,"Going to read a texted file"); |
120 | *result = malloc(sizeof(**result)); |
345 | if (!(*result = malloc(sizeof(**result)))) |
|
|
346 | goto ERROR1; |
121 | |
347 | |
122 | psiconv_progress(lev+2,off, "Going to read the header section"); |
348 | 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"); |
349 | "Going to read the offset of the section table section"); |
|
|
350 | sto = psiconv_read_u32(buf,lev+2,off,&res); |
|
|
351 | if (res) |
|
|
352 | goto ERROR2; |
|
|
353 | psiconv_debug(lev+2,off,"Offset: %08x",sto); |
|
|
354 | |
|
|
355 | psiconv_progress(lev+2,sto, "Going to read the section table section"); |
127 | res |= psiconv_parse_section_table_section(buf,lev+2, |
356 | if ((res = psiconv_parse_section_table_section(buf,lev+2,sto, NULL,&table))) |
128 | header->section_table_offset, |
357 | goto ERROR2; |
129 | NULL,&table); |
|
|
130 | |
358 | |
131 | for (i = 0; i < psiconv_list_length(table); i ++) { |
359 | for (i = 0; i < psiconv_list_length(table); i ++) { |
132 | psiconv_progress(lev+2,header->section_table_offset, |
360 | 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); |
361 | if (!(entry = psiconv_list_get(table,i))) |
|
|
362 | goto ERROR3; |
135 | if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
363 | if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
136 | applid_sec = entry->offset; |
364 | applid_sec = entry->offset; |
137 | psiconv_debug(lev+3,header->section_table_offset, |
365 | psiconv_debug(lev+3,sto, |
138 | "Found the Application ID section at %08x",applid_sec); |
366 | "Found the Application ID section at %08x",applid_sec); |
139 | } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) { |
367 | } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) { |
140 | page_sec = entry->offset; |
368 | page_sec = entry->offset; |
141 | psiconv_debug(lev+3,header->section_table_offset, |
369 | psiconv_debug(lev+3,sto, |
142 | "Found the Page Layout section at %08x",page_sec); |
370 | "Found the Page Layout section at %08x",page_sec); |
143 | } else if (entry->id == PSICONV_ID_TEXTED) { |
371 | } else if (entry->id == PSICONV_ID_TEXTED) { |
144 | texted_sec = entry->offset; |
372 | texted_sec = entry->offset; |
145 | psiconv_debug(lev+3,header->section_table_offset, |
373 | psiconv_debug(lev+3,sto, |
146 | "Found the TextEd section at %08x",texted_sec); |
374 | "Found the TextEd section at %08x",texted_sec); |
147 | } else { |
375 | } else { |
148 | psiconv_warn(lev+3,header->section_table_offset, |
376 | psiconv_warn(lev+3,sto, |
149 | "Found unknown section in the Section Table"); |
377 | "Found unknown section in the Section Table (ignoring)"); |
150 | psiconv_debug(lev+3,header->section_table_offset, |
378 | psiconv_debug(lev+3,sto, |
151 | "Section ID %08x, offset %08x",entry->id,entry->offset); |
379 | "Section ID %08x, offset %08x",entry->id,entry->offset); |
152 | res = -1; |
|
|
153 | } |
380 | } |
154 | } |
381 | } |
155 | |
382 | |
156 | psiconv_progress(lev+2,header->section_table_offset, |
383 | psiconv_progress(lev+2,sto, "Looking for the Application ID section"); |
157 | "Looking for the Application ID section"); |
|
|
158 | if (! applid_sec) { |
384 | if (! applid_sec) { |
159 | psiconv_warn(lev+2,header->section_table_offset, |
385 | psiconv_warn(lev+2,sto, |
160 | "Application ID section not found in the section table"); |
386 | "Application ID section not found in the section table"); |
161 | res = -1; |
387 | res = -PSICONV_E_PARSE; |
|
|
388 | goto ERROR3; |
162 | } else { |
389 | } else { |
163 | psiconv_debug(lev+2,header->section_table_offset, |
390 | psiconv_debug(lev+2,sto, |
164 | "Application ID section at offset %08x",applid_sec); |
391 | "Application ID section at offset %08x",applid_sec); |
165 | res |= psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
392 | if ((res = psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
166 | &appl_id); |
393 | &appl_id))) |
|
|
394 | goto ERROR3; |
167 | } |
395 | } |
168 | if ((appl_id->id != PSICONV_ID_TEXTED) || |
396 | if ((appl_id->id != PSICONV_ID_TEXTED) || |
169 | strcmp(appl_id->name,"TextEd.app")) { |
397 | strcmp(appl_id->name,"TextEd.app")) { |
170 | psiconv_warn(lev+2,applid_sec, |
398 | psiconv_warn(lev+2,applid_sec, |
171 | "Application ID section contains unexpected data"); |
399 | "Application ID section contains unexpected data"); |
172 | psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
400 | psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
173 | PSICONV_ID_TEXTED,appl_id->id); |
401 | PSICONV_ID_TEXTED,appl_id->id); |
174 | temp_str = psiconv_make_printable(appl_id->name); |
402 | if (!(temp_str = psiconv_make_printable(appl_id->name))) |
|
|
403 | goto ERROR4; |
175 | psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
404 | psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
176 | PSICONV_ID_TEXTED,temp_str); |
405 | "TextEd.app",temp_str); |
177 | free(temp_str); |
406 | free(temp_str); |
|
|
407 | res = -PSICONV_E_PARSE; |
|
|
408 | goto ERROR4; |
178 | } |
409 | } |
179 | |
410 | |
180 | psiconv_progress(lev+2,header->section_table_offset, |
411 | psiconv_progress(lev+2,sto, |
181 | "Looking for the Page layout section"); |
412 | "Looking for the Page layout section"); |
182 | if (! page_sec) { |
413 | if (! page_sec) { |
183 | psiconv_warn(lev+2,header->section_table_offset, |
414 | psiconv_warn(lev+2,sto, |
184 | "Page layout section not found in the section table"); |
415 | "Page layout section not found in the section table"); |
185 | (*result)->page_sec = NULL; |
416 | res = -PSICONV_E_PARSE; |
186 | res = -1; |
417 | goto ERROR4; |
187 | } else { |
418 | } else { |
188 | psiconv_debug(lev+2,header->section_table_offset, |
419 | psiconv_debug(lev+2,sto, |
189 | "Page layout section at offset %08x",page_sec); |
420 | "Page layout section at offset %08x",page_sec); |
190 | res |= psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL, |
421 | if ((res = psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL, |
191 | &(*result)->page_sec); |
422 | &(*result)->page_sec))) |
|
|
423 | goto ERROR4; |
192 | } |
424 | } |
193 | |
425 | |
194 | base_char = psiconv_basic_character_layout(); |
426 | if (!(base_char = psiconv_basic_character_layout())) |
|
|
427 | goto ERROR5; |
195 | base_para = psiconv_basic_paragraph_layout(); |
428 | if (!(base_para = psiconv_basic_paragraph_layout())) |
|
|
429 | goto ERROR6; |
196 | |
430 | |
197 | psiconv_progress(lev+2,header->section_table_offset, |
431 | psiconv_progress(lev+2,sto, |
198 | "Looking for the TextEd section"); |
432 | "Looking for the TextEd section"); |
199 | if (! texted_sec) { |
433 | if (! texted_sec) { |
200 | psiconv_warn(lev+2,header->section_table_offset, |
434 | psiconv_warn(lev+2,sto, |
201 | "TextEd section not found in the section table"); |
435 | "TextEd section not found in the section table"); |
202 | (*result)->texted_sec = NULL; |
436 | res = -PSICONV_E_PARSE; |
203 | res = -1; |
437 | goto ERROR7; |
204 | } else { |
438 | } else { |
205 | psiconv_debug(lev+2,header->section_table_offset, |
439 | 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, |
440 | if ((res = psiconv_parse_texted_section(buf,lev+2,texted_sec,NULL, |
208 | &(*result)->texted_sec, |
441 | &(*result)->texted_sec, |
209 | base_char,base_para); |
442 | base_char,base_para))) |
|
|
443 | goto ERROR7; |
210 | } |
444 | } |
211 | psiconv_free_character_layout(base_char); |
445 | psiconv_free_character_layout(base_char); |
212 | psiconv_free_paragraph_layout(base_para); |
446 | psiconv_free_paragraph_layout(base_para); |
213 | |
447 | |
214 | psiconv_free_application_id_section(appl_id); |
448 | psiconv_free_application_id_section(appl_id); |
215 | psiconv_free_header_section(header); |
|
|
216 | psiconv_free_section_table_section(table); |
449 | psiconv_free_section_table_section(table); |
217 | |
450 | |
218 | psiconv_progress(lev+1,off,"End of word file"); |
451 | psiconv_progress(lev+1,off,"End of TextEd file"); |
|
|
452 | return 0; |
|
|
453 | |
|
|
454 | ERROR7: |
|
|
455 | psiconv_free_paragraph_layout(base_para); |
|
|
456 | ERROR6: |
|
|
457 | psiconv_free_character_layout(base_char); |
|
|
458 | ERROR5: |
|
|
459 | psiconv_free_page_layout_section((*result)->page_sec); |
|
|
460 | ERROR4: |
|
|
461 | psiconv_free_application_id_section(appl_id); |
|
|
462 | ERROR3: |
|
|
463 | psiconv_free_section_table_section(table); |
|
|
464 | ERROR2: |
|
|
465 | free(*result); |
|
|
466 | ERROR1: |
|
|
467 | psiconv_warn(lev+1,off,"Reading of TextEd File failed"); |
|
|
468 | if (res == 0) |
|
|
469 | return -PSICONV_E_NOMEM; |
|
|
470 | else |
219 | return res; |
471 | return res; |
220 | } |
472 | } |
221 | |
473 | |
222 | int psiconv_parse_word_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
474 | int psiconv_parse_word_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
223 | psiconv_word_f *result) |
475 | psiconv_word_f *result) |
224 | { |
476 | { |
225 | int res=0; |
477 | int res=0; |
226 | psiconv_header_section header; |
|
|
227 | psiconv_section_table_section table; |
478 | psiconv_section_table_section table; |
228 | psiconv_application_id_section appl_id; |
479 | psiconv_application_id_section appl_id; |
229 | char *temp_str; |
480 | char *temp_str; |
230 | psiconv_u32 pwd_sec = 0; |
481 | psiconv_u32 pwd_sec = 0; |
231 | psiconv_u32 status_sec = 0; |
482 | psiconv_u32 status_sec = 0; |
… | |
… | |
233 | psiconv_u32 page_sec = 0; |
484 | psiconv_u32 page_sec = 0; |
234 | psiconv_u32 text_sec = 0; |
485 | psiconv_u32 text_sec = 0; |
235 | psiconv_u32 layout_sec = 0; |
486 | psiconv_u32 layout_sec = 0; |
236 | psiconv_u32 applid_sec = 0; |
487 | psiconv_u32 applid_sec = 0; |
237 | psiconv_section_table_entry entry; |
488 | psiconv_section_table_entry entry; |
|
|
489 | psiconv_u32 sto; |
238 | int i; |
490 | int i; |
239 | |
491 | |
240 | psiconv_progress(lev+1,off,"Going to read a word file"); |
492 | psiconv_progress(lev+1,off,"Going to read a word file"); |
241 | *result = malloc(sizeof(**result)); |
493 | if (!(*result = malloc(sizeof(**result)))) |
|
|
494 | goto ERROR1; |
242 | |
495 | |
243 | psiconv_progress(lev+2,off, "Going to read the header section"); |
496 | psiconv_progress(lev+2,off, |
244 | res |= psiconv_parse_header_section(buf,lev+2,off,NULL,&header); |
497 | "Going to read the offset of the section table section"); |
|
|
498 | sto = psiconv_read_u32(buf,lev+2,off,&res); |
|
|
499 | if (res) |
|
|
500 | goto ERROR2; |
|
|
501 | psiconv_debug(lev+2,off,"Offset: %08x",sto); |
245 | |
502 | |
246 | psiconv_progress(lev+2,header->section_table_offset, |
503 | psiconv_progress(lev+2,sto, |
247 | "Going to read the section table section"); |
504 | "Going to read the section table section"); |
248 | res |= psiconv_parse_section_table_section(buf,lev+2, |
505 | if ((res = psiconv_parse_section_table_section(buf,lev+2,sto, NULL,&table))) |
249 | header->section_table_offset, |
506 | goto ERROR2; |
250 | NULL,&table); |
|
|
251 | |
507 | |
252 | for (i = 0; i < psiconv_list_length(table); i ++) { |
508 | for (i = 0; i < psiconv_list_length(table); i ++) { |
253 | psiconv_progress(lev+2,header->section_table_offset, |
509 | 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); |
510 | if (!(entry = psiconv_list_get(table,i))) |
|
|
511 | goto ERROR3; |
256 | if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
512 | if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
257 | applid_sec = entry->offset; |
513 | applid_sec = entry->offset; |
258 | psiconv_debug(lev+3,header->section_table_offset, |
514 | psiconv_debug(lev+3,sto, |
259 | "Found the Application ID section at %08x",applid_sec); |
515 | "Found the Application ID section at %08x",applid_sec); |
260 | } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) { |
516 | } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) { |
261 | page_sec = entry->offset; |
517 | page_sec = entry->offset; |
262 | psiconv_debug(lev+3,header->section_table_offset, |
518 | psiconv_debug(lev+3,sto, |
263 | "Found the Page Layout section at %08x",page_sec); |
519 | "Found the Page Layout section at %08x",page_sec); |
264 | } else if (entry->id == PSICONV_ID_TEXT_SECTION) { |
520 | } else if (entry->id == PSICONV_ID_TEXT_SECTION) { |
265 | text_sec = entry->offset; |
521 | text_sec = entry->offset; |
266 | psiconv_debug(lev+3,header->section_table_offset, |
522 | 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) { |
523 | } else if (entry->id == PSICONV_ID_PASSWORD_SECTION) { |
269 | pwd_sec = entry->offset; |
524 | pwd_sec = entry->offset; |
270 | psiconv_debug(lev+3,header->section_table_offset, |
525 | psiconv_debug(lev+3,sto, |
271 | "Found the Password section at %08x",pwd_sec); |
526 | "Found the Password section at %08x",pwd_sec); |
272 | psiconv_warn(lev+3,header->section_table_offset, |
527 | psiconv_warn(lev+3,sto, |
273 | "Password section found - can't read encrypted data"); |
528 | "Password section found - can't read encrypted data"); |
274 | res = -1; |
529 | res = -PSICONV_E_PARSE; |
|
|
530 | goto ERROR3; |
275 | } else if (entry->id == PSICONV_ID_WORD_STATUS_SECTION) { |
531 | } else if (entry->id == PSICONV_ID_WORD_STATUS_SECTION) { |
276 | status_sec = entry->offset; |
532 | status_sec = entry->offset; |
277 | psiconv_debug(lev+3,header->section_table_offset, |
533 | psiconv_debug(lev+3,sto, |
278 | "Found the Word Status section at %08x",status_sec); |
534 | "Found the Word Status section at %08x",status_sec); |
279 | } else if (entry->id == PSICONV_ID_WORD_STYLES_SECTION) { |
535 | } else if (entry->id == PSICONV_ID_WORD_STYLES_SECTION) { |
280 | styles_sec = entry->offset; |
536 | styles_sec = entry->offset; |
281 | psiconv_debug(lev+3,header->section_table_offset, |
537 | psiconv_debug(lev+3,sto, |
282 | "Found the Word Styles section at %08x",styles_sec); |
538 | "Found the Word Styles section at %08x",styles_sec); |
283 | } else if (entry->id == PSICONV_ID_LAYOUT_SECTION) { |
539 | } else if (entry->id == PSICONV_ID_LAYOUT_SECTION) { |
284 | layout_sec = entry->offset; |
540 | layout_sec = entry->offset; |
285 | psiconv_debug(lev+3,header->section_table_offset, |
541 | psiconv_debug(lev+3,sto, |
286 | "Found the Layout section at %08x",layout_sec); |
542 | "Found the Layout section at %08x",layout_sec); |
287 | } else { |
543 | } else { |
288 | psiconv_warn(lev+3,header->section_table_offset, |
544 | psiconv_warn(lev+3,sto, |
289 | "Found unknown section in the Section Table"); |
545 | "Found unknown section in the Section Table (ignoring)"); |
290 | psiconv_debug(lev+3,header->section_table_offset, |
546 | psiconv_debug(lev+3,sto, |
291 | "Section ID %08x, offset %08x",entry->id,entry->offset); |
547 | "Section ID %08x, offset %08x",entry->id,entry->offset); |
292 | res = -1; |
|
|
293 | } |
548 | } |
294 | } |
549 | } |
295 | |
550 | |
296 | |
551 | |
297 | psiconv_progress(lev+2,header->section_table_offset, |
552 | psiconv_progress(lev+2,sto, |
298 | "Looking for the Status section"); |
553 | "Looking for the Status section"); |
299 | if (!status_sec) { |
554 | if (!status_sec) { |
300 | psiconv_warn(lev+2,header->section_table_offset, |
|
|
301 | "Status section not found in the section table"); |
555 | psiconv_warn(lev+2,sto, "Status section not found in the section table"); |
302 | res = -1; |
556 | res = -PSICONV_E_PARSE; |
|
|
557 | goto ERROR3; |
303 | } else { |
558 | } else { |
304 | psiconv_debug(lev+2,header->section_table_offset, |
559 | 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, |
560 | if ((res = psiconv_parse_word_status_section(buf,lev+2,status_sec,NULL, |
307 | &((*result)->status_sec)); |
561 | &((*result)->status_sec)))) |
|
|
562 | goto ERROR3; |
308 | } |
563 | } |
309 | |
564 | |
310 | psiconv_progress(lev+2,header->section_table_offset, |
565 | psiconv_progress(lev+2,sto, "Looking for the Application ID section"); |
311 | "Looking for the Application ID section"); |
|
|
312 | if (! applid_sec) { |
566 | if (! applid_sec) { |
313 | psiconv_warn(lev+2,header->section_table_offset, |
567 | psiconv_warn(lev+2,sto, |
314 | "Application ID section not found in the section table"); |
568 | "Application ID section not found in the section table"); |
315 | res = -1; |
569 | res = -PSICONV_E_PARSE; |
|
|
570 | goto ERROR4; |
316 | } else { |
571 | } else { |
317 | psiconv_debug(lev+2,header->section_table_offset, |
572 | psiconv_debug(lev+2,sto, |
318 | "Application ID section at offset %08x",applid_sec); |
573 | "Application ID section at offset %08x",applid_sec); |
319 | res |= psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
574 | if ((res = psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
320 | &appl_id); |
575 | &appl_id))) |
|
|
576 | goto ERROR4; |
321 | } |
577 | } |
322 | if ((appl_id->id != PSICONV_ID_WORD) || |
578 | if ((appl_id->id != PSICONV_ID_WORD) || |
323 | strcmp(appl_id->name,"Word.app")) { |
579 | strcmp(appl_id->name,"Word.app")) { |
324 | psiconv_warn(lev+2,applid_sec, |
580 | psiconv_warn(lev+2,applid_sec, |
325 | "Application ID section contains unexpected data"); |
581 | "Application ID section contains unexpected data"); |
326 | psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
582 | psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
327 | PSICONV_ID_WORD,appl_id->id); |
583 | PSICONV_ID_WORD,appl_id->id); |
328 | temp_str = psiconv_make_printable(appl_id->name); |
584 | if (!(temp_str = psiconv_make_printable(appl_id->name))) |
|
|
585 | goto ERROR5; |
329 | psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
586 | psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
330 | PSICONV_ID_WORD,temp_str); |
587 | "Word.app",temp_str); |
331 | free(temp_str); |
588 | free(temp_str); |
|
|
589 | res = -PSICONV_E_PARSE; |
|
|
590 | goto ERROR5; |
332 | } |
591 | } |
333 | |
592 | |
334 | psiconv_progress(lev+2,header->section_table_offset, |
593 | psiconv_progress(lev+2,sto, |
335 | "Looking for the Page layout section"); |
594 | "Looking for the Page layout section"); |
336 | if (! page_sec) { |
595 | if (! page_sec) { |
337 | psiconv_warn(lev+2,header->section_table_offset, |
596 | psiconv_warn(lev+2,sto, |
338 | "Page layout section not found in the section table"); |
597 | "Page layout section not found in the section table"); |
339 | (*result)->page_sec = NULL; |
598 | res = -PSICONV_E_PARSE; |
340 | res = -1; |
599 | goto ERROR5; |
341 | } else { |
600 | } else { |
342 | psiconv_debug(lev+2,header->section_table_offset, |
601 | psiconv_debug(lev+2,sto, |
343 | "Page layout section at offset %08x",page_sec); |
602 | "Page layout section at offset %08x",page_sec); |
344 | res |= psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL, |
603 | if ((res = psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL, |
345 | &(*result)->page_sec); |
604 | &(*result)->page_sec))) |
|
|
605 | goto ERROR5; |
346 | } |
606 | } |
347 | |
607 | |
348 | psiconv_progress(lev+2,header->section_table_offset, |
608 | psiconv_progress(lev+2,sto, |
349 | "Looking for the Word Style section"); |
609 | "Looking for the Word Style section"); |
350 | if (!styles_sec) { |
610 | if (!styles_sec) { |
351 | psiconv_warn(lev+2,header->section_table_offset, |
611 | psiconv_warn(lev+2,sto, |
352 | "Word styles section not found in the section table"); |
612 | "Word styles section not found in the section table"); |
353 | (*result)->styles_sec = NULL; |
613 | res = -PSICONV_E_PARSE; |
354 | res = -1; |
614 | goto ERROR6; |
355 | } else { |
615 | } else { |
356 | psiconv_debug(lev+2,header->section_table_offset, |
616 | psiconv_debug(lev+2,sto, |
357 | "Word styles section at offset %08x",styles_sec); |
617 | "Word styles section at offset %08x",styles_sec); |
358 | res |= psiconv_parse_word_styles_section(buf,lev+2,styles_sec,NULL, |
618 | if ((res = psiconv_parse_word_styles_section(buf,lev+2,styles_sec,NULL, |
359 | &(*result)->styles_sec); |
619 | &(*result)->styles_sec))) |
|
|
620 | goto ERROR6; |
360 | } |
621 | } |
361 | |
622 | |
362 | psiconv_progress(lev+2,header->section_table_offset, |
623 | psiconv_progress(lev+2,sto, |
363 | "Looking for the Text section"); |
624 | "Looking for the Text section"); |
364 | if (!text_sec) { |
625 | if (!text_sec) { |
365 | psiconv_warn(lev+2,header->section_table_offset, |
626 | psiconv_warn(lev+2,sto, "Text section not found in the section table"); |
366 | "Text section not found in the section table"); |
627 | res = -PSICONV_E_PARSE; |
367 | (*result)->paragraphs = NULL; |
628 | goto ERROR7; |
368 | res = -1; |
|
|
369 | } else { |
629 | } else { |
370 | psiconv_debug(lev+2,header->section_table_offset, |
630 | psiconv_debug(lev+2,sto, |
371 | "Text section at offset %08x",text_sec); |
631 | "Text section at offset %08x",text_sec); |
372 | res |= psiconv_parse_text_section(buf,lev+2,text_sec,NULL, |
632 | if ((res = psiconv_parse_text_section(buf,lev+2,text_sec,NULL, |
373 | &(*result)->paragraphs); |
633 | &(*result)->paragraphs))) |
|
|
634 | goto ERROR7; |
374 | } |
635 | } |
375 | |
636 | |
376 | if (((*result)->paragraphs) && ((*result)->styles_sec)) { |
637 | psiconv_progress(lev+2,sto, "Looking for the Layout section"); |
377 | psiconv_progress(lev+2,header->section_table_offset, |
|
|
378 | "Looking for the Layout section"); |
|
|
379 | if (!layout_sec) { |
638 | if (!layout_sec) { |
380 | psiconv_debug(lev+2,header->section_table_offset, |
639 | psiconv_debug(lev+2,sto, "No layout section today"); |
381 | "Layout section not found in the section table"); |
640 | } else { |
382 | res = -1; |
641 | psiconv_debug(lev+2,sto, |
|
|
642 | "Layout section at offset %08x",layout_sec); |
|
|
643 | if ((res = psiconv_parse_styled_layout_section(buf,lev+2,layout_sec,NULL, |
|
|
644 | (*result)->paragraphs, |
|
|
645 | (*result)->styles_sec))) |
|
|
646 | goto ERROR8; |
|
|
647 | } |
|
|
648 | |
|
|
649 | psiconv_free_application_id_section(appl_id); |
|
|
650 | psiconv_free_section_table_section(table); |
|
|
651 | |
|
|
652 | psiconv_progress(lev+1,off,"End of word file"); |
|
|
653 | return 0; |
|
|
654 | |
|
|
655 | |
|
|
656 | ERROR8: |
|
|
657 | psiconv_free_text_and_layout((*result)->paragraphs); |
|
|
658 | ERROR7: |
|
|
659 | psiconv_free_word_styles_section((*result)->styles_sec); |
|
|
660 | ERROR6: |
|
|
661 | psiconv_free_page_layout_section((*result)->page_sec); |
|
|
662 | ERROR5: |
|
|
663 | psiconv_free_application_id_section(appl_id); |
|
|
664 | ERROR4: |
|
|
665 | psiconv_free_word_status_section((*result)->status_sec); |
|
|
666 | ERROR3: |
|
|
667 | psiconv_free_section_table_section(table); |
|
|
668 | ERROR2: |
|
|
669 | free(*result); |
|
|
670 | ERROR1: |
|
|
671 | psiconv_warn(lev+1,off,"Reading of Word File failed"); |
|
|
672 | if (res == 0) |
|
|
673 | return -PSICONV_E_NOMEM; |
|
|
674 | else |
|
|
675 | return res; |
|
|
676 | } |
|
|
677 | |
|
|
678 | int psiconv_parse_sheet_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
|
|
679 | psiconv_sheet_f *result) |
|
|
680 | { |
|
|
681 | int res=0; |
|
|
682 | psiconv_section_table_section table; |
|
|
683 | psiconv_application_id_section appl_id; |
|
|
684 | char *temp_str; |
|
|
685 | psiconv_u32 pwd_sec = 0; |
|
|
686 | psiconv_u32 status_sec = 0; |
|
|
687 | psiconv_u32 page_sec = 0; |
|
|
688 | psiconv_u32 applid_sec = 0; |
|
|
689 | psiconv_section_table_entry entry; |
|
|
690 | psiconv_u32 sto; |
|
|
691 | int i; |
|
|
692 | |
|
|
693 | psiconv_progress(lev+1,off,"Going to read a sheet file"); |
|
|
694 | if (!(*result = malloc(sizeof(**result)))) |
|
|
695 | goto ERROR1; |
|
|
696 | |
|
|
697 | psiconv_progress(lev+2,off, |
|
|
698 | "Going to read the offset of the section table section"); |
|
|
699 | sto = psiconv_read_u32(buf,lev+2,off,&res); |
|
|
700 | if (res) |
|
|
701 | goto ERROR2; |
|
|
702 | psiconv_debug(lev+2,off,"Offset: %08x",sto); |
|
|
703 | |
|
|
704 | psiconv_progress(lev+2,sto, |
|
|
705 | "Going to read the section table section"); |
|
|
706 | if ((res = psiconv_parse_section_table_section(buf,lev+2,sto, NULL,&table))) |
|
|
707 | goto ERROR2; |
|
|
708 | |
|
|
709 | for (i = 0; i < psiconv_list_length(table); i ++) { |
|
|
710 | psiconv_progress(lev+2,sto, "Going to read entry %d",i); |
|
|
711 | if (!(entry = psiconv_list_get(table,i))) |
|
|
712 | goto ERROR3; |
|
|
713 | if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
|
|
714 | applid_sec = entry->offset; |
|
|
715 | psiconv_debug(lev+3,sto, |
|
|
716 | "Found the Application ID section at %08x",applid_sec); |
|
|
717 | } else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) { |
|
|
718 | page_sec = entry->offset; |
|
|
719 | psiconv_debug(lev+3,sto, |
|
|
720 | "Found the Page Layout section at %08x",page_sec); |
|
|
721 | } else if (entry->id == PSICONV_ID_PASSWORD_SECTION) { |
|
|
722 | pwd_sec = entry->offset; |
|
|
723 | psiconv_debug(lev+3,sto, |
|
|
724 | "Found the Password section at %08x",pwd_sec); |
|
|
725 | psiconv_warn(lev+3,sto, |
|
|
726 | "Password section found - can't read encrypted data"); |
|
|
727 | res = -PSICONV_E_PARSE; |
|
|
728 | goto ERROR3; |
|
|
729 | } else if (entry->id == PSICONV_ID_SHEET_STATUS_SECTION) { |
|
|
730 | status_sec = entry->offset; |
|
|
731 | psiconv_debug(lev+3,sto, |
|
|
732 | "Found the Sheet Status section at %08x",status_sec); |
383 | } else { |
733 | } else { |
384 | psiconv_debug(lev+2,header->section_table_offset, |
734 | psiconv_warn(lev+3,sto, |
385 | "Layout section at offset %08x",layout_sec); |
735 | "Found unknown section in the Section Table (ignoring)"); |
386 | res |= psiconv_parse_styled_layout_section(buf,lev+2,layout_sec,NULL, |
736 | psiconv_debug(lev+3,sto, |
387 | (*result)->paragraphs, |
737 | "Section ID %08x, offset %08x",entry->id,entry->offset); |
388 | (*result)->styles_sec); |
|
|
389 | } |
738 | } |
|
|
739 | } |
|
|
740 | |
|
|
741 | |
|
|
742 | psiconv_progress(lev+2,sto, |
|
|
743 | "Looking for the Status section"); |
|
|
744 | if (!status_sec) { |
|
|
745 | psiconv_warn(lev+2,sto, "Status section not found in the section table"); |
|
|
746 | res = -PSICONV_E_PARSE; |
|
|
747 | goto ERROR3; |
390 | } else |
748 | } else { |
391 | psiconv_debug(lev+2,header->section_table_offset, |
749 | psiconv_debug(lev+2,sto, "Status section at offset %08x",status_sec); |
392 | "Skipping search for Layout section, as either the " |
750 | if ((res = psiconv_parse_sheet_status_section(buf,lev+2,status_sec,NULL, |
393 | "text or the word styles section was not found"); |
751 | &((*result)->status_sec)))) |
|
|
752 | goto ERROR3; |
|
|
753 | } |
|
|
754 | |
|
|
755 | psiconv_progress(lev+2,sto, "Looking for the Application ID section"); |
|
|
756 | if (! applid_sec) { |
|
|
757 | psiconv_warn(lev+2,sto, |
|
|
758 | "Application ID section not found in the section table"); |
|
|
759 | res = -PSICONV_E_PARSE; |
|
|
760 | goto ERROR4; |
|
|
761 | } else { |
|
|
762 | psiconv_debug(lev+2,sto, |
|
|
763 | "Application ID section at offset %08x",applid_sec); |
|
|
764 | if ((res = psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
|
|
765 | &appl_id))) |
|
|
766 | goto ERROR4; |
|
|
767 | } |
|
|
768 | if ((appl_id->id != PSICONV_ID_SHEET) || |
|
|
769 | strcmp(appl_id->name,"Sheet.app")) { |
|
|
770 | psiconv_warn(lev+2,applid_sec, |
|
|
771 | "Application ID section contains unexpected data"); |
|
|
772 | psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
|
|
773 | PSICONV_ID_SHEET,appl_id->id); |
|
|
774 | if (!(temp_str = psiconv_make_printable(appl_id->name))) |
|
|
775 | goto ERROR5; |
|
|
776 | psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
|
|
777 | "Word.app",temp_str); |
|
|
778 | free(temp_str); |
|
|
779 | res = -PSICONV_E_PARSE; |
|
|
780 | goto ERROR5; |
|
|
781 | } |
|
|
782 | |
|
|
783 | psiconv_progress(lev+2,sto, |
|
|
784 | "Looking for the Page layout section"); |
|
|
785 | if (! page_sec) { |
|
|
786 | psiconv_warn(lev+2,sto, |
|
|
787 | "Page layout section not found in the section table"); |
|
|
788 | res = -PSICONV_E_PARSE; |
|
|
789 | goto ERROR5; |
|
|
790 | } else { |
|
|
791 | psiconv_debug(lev+2,sto, |
|
|
792 | "Page layout section at offset %08x",page_sec); |
|
|
793 | if ((res = psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL, |
|
|
794 | &(*result)->page_sec))) |
|
|
795 | goto ERROR5; |
|
|
796 | } |
394 | |
797 | |
395 | psiconv_free_application_id_section(appl_id); |
798 | psiconv_free_application_id_section(appl_id); |
396 | psiconv_free_header_section(header); |
|
|
397 | psiconv_free_section_table_section(table); |
799 | psiconv_free_section_table_section(table); |
398 | |
800 | |
399 | psiconv_progress(lev+1,off,"End of word file"); |
801 | psiconv_progress(lev+1,off,"End of Sheet file"); |
|
|
802 | return 0; |
|
|
803 | |
|
|
804 | |
|
|
805 | ERROR5: |
|
|
806 | psiconv_free_application_id_section(appl_id); |
|
|
807 | ERROR4: |
|
|
808 | psiconv_free_sheet_status_section((*result)->status_sec); |
|
|
809 | ERROR3: |
|
|
810 | psiconv_free_section_table_section(table); |
|
|
811 | ERROR2: |
|
|
812 | free(*result); |
|
|
813 | ERROR1: |
|
|
814 | psiconv_warn(lev+1,off,"Reading of Sheet File failed"); |
|
|
815 | if (res == 0) |
|
|
816 | return -PSICONV_E_NOMEM; |
|
|
817 | else |
400 | return res; |
818 | return res; |
401 | } |
819 | } |