1 | /* |
1 | /* |
2 | parse_common.c - Part of psiconv, a PSION 5 file formats converter |
2 | parse_common.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. |
… | |
… | |
20 | #include "config.h" |
20 | #include "config.h" |
21 | #include "compat.h" |
21 | #include "compat.h" |
22 | #include <stdlib.h> |
22 | #include <stdlib.h> |
23 | #include <string.h> |
23 | #include <string.h> |
24 | |
24 | |
25 | #include "data.h" |
|
|
26 | #include "parse_routines.h" |
25 | #include "parse_routines.h" |
|
|
26 | #include "error.h" |
27 | |
27 | |
28 | static int psiconv_parse_layout_section(const psiconv_buffer buf, |
28 | static int psiconv_parse_layout_section(const psiconv_buffer buf, |
29 | int lev,psiconv_u32 off, |
29 | int lev,psiconv_u32 off, |
30 | int *length, |
30 | int *length, |
31 | psiconv_text_and_layout result, |
31 | psiconv_text_and_layout result, |
… | |
… | |
39 | int res=0; |
39 | int res=0; |
40 | int len=0; |
40 | int len=0; |
41 | psiconv_u32 temp; |
41 | psiconv_u32 temp; |
42 | |
42 | |
43 | psiconv_progress(lev+1,off+len,"Going to read the header section"); |
43 | psiconv_progress(lev+1,off+len,"Going to read the header section"); |
44 | (*result) = malloc(sizeof(**result)); |
44 | if (!((*result) = malloc(sizeof(**result)))) |
|
|
45 | goto ERROR1; |
45 | |
46 | |
46 | psiconv_progress(lev+2,off+len,"Going to read UID1 to UID3"); |
47 | psiconv_progress(lev+2,off+len,"Going to read UID1 to UID3"); |
47 | (*result)->uid1 = psiconv_read_u32(buf,lev+2,off+len); |
48 | (*result)->uid1 = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
49 | if (res) |
|
|
50 | goto ERROR2; |
48 | psiconv_debug(lev+2,off+len,"UID1: %08x",(*result)->uid1); |
51 | psiconv_debug(lev+2,off+len,"UID1: %08x",(*result)->uid1); |
|
|
52 | if ((*result)->uid1 == PSICONV_ID_CLIPART) { |
|
|
53 | /* That's all folks... */ |
|
|
54 | (*result)->file = psiconv_clipart_file; |
|
|
55 | (*result)->uid2 = 0; |
|
|
56 | (*result)->uid3 = 0; |
|
|
57 | (*result)->checksum = 0; |
|
|
58 | len += 4; |
|
|
59 | psiconv_debug(lev+2,off+len,"File is a Clipart file"); |
|
|
60 | goto DONE; |
|
|
61 | } |
49 | if ((*result)->uid1 != PSICONV_ID_PSION5) { |
62 | if ((*result)->uid1 != PSICONV_ID_PSION5) { |
50 | psiconv_warn(lev+2,off+len,"UID1 has unknown value. This is probably " |
63 | psiconv_warn(lev+2,off+len,"UID1 has unknown value. This is probably " |
51 | "not a (parsable) Psion 5 file"); |
64 | "not a (parsable) Psion 5 file"); |
52 | res = -1; |
65 | res = -PSICONV_E_PARSE; |
|
|
66 | goto ERROR2; |
53 | } |
67 | } |
54 | len += 4; |
68 | len += 4; |
55 | (*result)->uid2 = psiconv_read_u32(buf,lev+2,off+len); |
69 | (*result)->uid2 = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
70 | if (res) |
|
|
71 | goto ERROR2; |
56 | psiconv_debug(lev+2,off+len,"UID2: %08x",(*result)->uid2); |
72 | psiconv_debug(lev+2,off+len,"UID2: %08x",(*result)->uid2); |
57 | len += 4; |
73 | len += 4; |
58 | (*result)->uid3 = psiconv_read_u32(buf,lev+2,off+len); |
74 | (*result)->uid3 = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
75 | if (res) |
|
|
76 | goto ERROR2; |
59 | psiconv_debug(lev+2,off+len,"UID3: %08x",(*result)->uid3); |
77 | psiconv_debug(lev+2,off+len,"UID3: %08x",(*result)->uid3); |
60 | len += 4; |
78 | len += 4; |
61 | |
79 | |
62 | (*result)->file = psiconv_unknown_file; |
80 | (*result)->file = psiconv_unknown_file; |
63 | if ((*result)->uid1 == PSICONV_ID_PSION5) { |
81 | if ((*result)->uid1 == PSICONV_ID_PSION5) { |
… | |
… | |
83 | psiconv_warn(lev+2,off+len,"Unknown file type"); |
101 | psiconv_warn(lev+2,off+len,"Unknown file type"); |
84 | (*result)->file = psiconv_unknown_file; |
102 | (*result)->file = psiconv_unknown_file; |
85 | } |
103 | } |
86 | |
104 | |
87 | psiconv_progress(lev+2,off+len,"Checking UID4"); |
105 | psiconv_progress(lev+2,off+len,"Checking UID4"); |
88 | temp = psiconv_read_u32(buf,lev+2,off+len); |
106 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
107 | if (res) |
|
|
108 | goto ERROR2; |
89 | if (temp == psiconv_checkuid((*result)->uid1,(*result)->uid2, |
109 | if (temp == psiconv_checkuid((*result)->uid1,(*result)->uid2, |
90 | (*result)->uid3)) |
110 | (*result)->uid3)) |
91 | psiconv_debug(lev+2,off+len,"Checksum %08x is correct",temp); |
111 | psiconv_debug(lev+2,off+len,"Checksum %08x is correct",temp); |
92 | else { |
112 | else { |
93 | psiconv_warn(lev+2,off+len,"Checksum failed, file corrupted!"); |
113 | psiconv_warn(lev+2,off+len,"Checksum failed, file corrupted!"); |
94 | psiconv_debug(lev+2,off+len,"Expected checksum %08x, found %08x", |
114 | psiconv_debug(lev+2,off+len,"Expected checksum %08x, found %08x", |
95 | psiconv_checkuid((*result)->uid1,(*result)->uid2, |
115 | psiconv_checkuid((*result)->uid1,(*result)->uid2, |
96 | (*result)->uid3),temp); |
116 | (*result)->uid3),temp); |
97 | res = -1; |
117 | res = -PSICONV_E_PARSE; |
|
|
118 | goto ERROR2; |
98 | } |
119 | } |
99 | len += 4; |
120 | len += 4; |
100 | |
121 | |
|
|
122 | DONE: |
101 | if (length) |
123 | if (length) |
102 | *length = len; |
124 | *length = len; |
103 | |
125 | |
104 | psiconv_progress(lev+1,off+len-1, |
126 | psiconv_progress(lev+1,off+len-1, |
105 | "End of Header Section (total length: %08x)",len); |
127 | "End of Header Section (total length: %08x)",len); |
106 | |
128 | |
107 | return res; |
129 | return res; |
|
|
130 | |
|
|
131 | ERROR2: |
|
|
132 | free(*result); |
|
|
133 | ERROR1: |
|
|
134 | psiconv_warn(lev+1,off,"Reading of Header Section failed"); |
|
|
135 | if (length) |
|
|
136 | *length = 0; |
|
|
137 | if (res == 0) |
|
|
138 | return -PSICONV_E_NOMEM; |
|
|
139 | else |
|
|
140 | return res; |
108 | } |
141 | } |
109 | |
142 | |
110 | int psiconv_parse_section_table_section(const psiconv_buffer buf, int lev, |
143 | int psiconv_parse_section_table_section(const psiconv_buffer buf, int lev, |
111 | psiconv_u32 off, int *length, |
144 | psiconv_u32 off, int *length, |
112 | psiconv_section_table_section *result) |
145 | psiconv_section_table_section *result) |
… | |
… | |
117 | |
150 | |
118 | int i; |
151 | int i; |
119 | psiconv_u8 nr; |
152 | psiconv_u8 nr; |
120 | |
153 | |
121 | psiconv_progress(lev+1,off+len,"Going to read the section table section"); |
154 | psiconv_progress(lev+1,off+len,"Going to read the section table section"); |
122 | *result = psiconv_list_new(sizeof(*entry)); |
155 | if (!(*result = psiconv_list_new(sizeof(*entry)))) |
|
|
156 | goto ERROR1; |
123 | |
157 | |
124 | psiconv_progress(lev+2,off+len,"Going to read the section table length"); |
158 | psiconv_progress(lev+2,off+len,"Going to read the section table length"); |
125 | nr = psiconv_read_u8(buf,lev+2,off+len); |
159 | nr = psiconv_read_u8(buf,lev+2,off+len,&res); |
|
|
160 | if (res) |
|
|
161 | goto ERROR2; |
126 | psiconv_debug(lev+2,off+len,"Length: %08x",nr); |
162 | psiconv_debug(lev+2,off+len,"Length: %08x",nr); |
127 | if (nr & 0x01) { |
163 | if (nr & 0x01) { |
128 | psiconv_warn(lev+2,off+len, |
164 | psiconv_warn(lev+2,off+len, |
129 | "Section table length odd - ignoring last entry"); |
165 | "Section table length odd - ignoring last entry"); |
130 | res = -1; |
|
|
131 | } |
166 | } |
132 | len ++; |
167 | len ++; |
133 | |
168 | |
134 | psiconv_progress(lev+2,off+len,"Going to read the section table entries"); |
169 | psiconv_progress(lev+2,off+len,"Going to read the section table entries"); |
135 | entry = malloc(sizeof(*entry)); |
170 | entry = malloc(sizeof(*entry)); |
136 | for (i = 0; i < nr / 2; i++) { |
171 | for (i = 0; i < nr / 2; i++) { |
137 | entry->id = psiconv_read_u32(buf,lev+2,off + len); |
172 | entry->id = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
173 | if (res) |
|
|
174 | goto ERROR3; |
138 | psiconv_debug(lev+2,off + len,"Entry %d: ID = %08x",i,entry->id); |
175 | psiconv_debug(lev+2,off + len,"Entry %d: ID = %08x",i,entry->id); |
139 | len += 0x04; |
176 | len += 0x04; |
140 | entry->offset = psiconv_read_u32(buf,lev+2,off + len); |
177 | entry->offset = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
178 | if (res) |
|
|
179 | goto ERROR3; |
141 | psiconv_debug(lev+2,off +len,"Entry %d: Offset = %08x",i,entry->offset); |
180 | psiconv_debug(lev+2,off +len,"Entry %d: Offset = %08x",i,entry->offset); |
142 | len += 0x04; |
181 | len += 0x04; |
143 | psiconv_list_add(*result,entry); |
182 | if ((res=psiconv_list_add(*result,entry))) |
|
|
183 | goto ERROR3; |
144 | } |
184 | } |
145 | |
185 | |
146 | free(entry); |
186 | free(entry); |
147 | |
187 | |
148 | if (length) |
188 | if (length) |
149 | *length = len; |
189 | *length = len; |
150 | |
190 | |
151 | psiconv_progress(lev+1,off+len-1,"End of section table section " |
191 | psiconv_progress(lev+1,off+len-1,"End of section table section " |
152 | "(total length: %08x)", len); |
192 | "(total length: %08x)", len); |
153 | |
193 | |
|
|
194 | return 0; |
|
|
195 | ERROR3: |
|
|
196 | free(entry); |
|
|
197 | ERROR2: |
|
|
198 | psiconv_list_free(*result); |
|
|
199 | ERROR1: |
|
|
200 | psiconv_warn(lev+1,off,"Reading of Section Table Section failed"); |
|
|
201 | if (length) |
|
|
202 | *length = 0; |
|
|
203 | if (res == 0) |
|
|
204 | return -PSICONV_E_NOMEM; |
|
|
205 | else |
154 | return res; |
206 | return res; |
155 | } |
207 | } |
156 | |
208 | |
157 | int psiconv_parse_application_id_section(const psiconv_buffer buf, int lev, |
209 | int psiconv_parse_application_id_section(const psiconv_buffer buf, int lev, |
158 | psiconv_u32 off, int *length, |
210 | psiconv_u32 off, int *length, |
159 | psiconv_application_id_section *result) |
211 | psiconv_application_id_section *result) |
… | |
… | |
161 | int res=0; |
213 | int res=0; |
162 | int len=0; |
214 | int len=0; |
163 | int leng; |
215 | int leng; |
164 | |
216 | |
165 | psiconv_progress(lev+1,off,"Going to read the application id section"); |
217 | psiconv_progress(lev+1,off,"Going to read the application id section"); |
166 | (*result) = malloc(sizeof(**result)); |
218 | if (!(*result = malloc(sizeof(**result)))) |
|
|
219 | goto ERROR1; |
167 | |
220 | |
168 | psiconv_progress(lev+2,off+len,"Going to read the type identifier"); |
221 | psiconv_progress(lev+2,off+len,"Going to read the type identifier"); |
169 | (*result)->id = psiconv_read_u32(buf,lev+2,off+len); |
222 | (*result)->id = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
223 | if (res) |
|
|
224 | goto ERROR2; |
170 | psiconv_debug(lev+2,off+len,"Identifier: %08x",(*result)->id); |
225 | psiconv_debug(lev+2,off+len,"Identifier: %08x",(*result)->id); |
171 | len += 4; |
226 | len += 4; |
172 | |
227 | |
173 | psiconv_progress(lev+2,off+len,"Going to read the application id string"); |
228 | psiconv_progress(lev+2,off+len,"Going to read the application id string"); |
174 | (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng); |
229 | (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res); |
|
|
230 | if (res) |
|
|
231 | goto ERROR2; |
175 | len += leng; |
232 | len += leng; |
176 | |
233 | |
177 | if (length) |
234 | if (length) |
178 | *length = len; |
235 | *length = len; |
179 | |
236 | |
180 | psiconv_progress(lev+1,off+len-1,"End of application id section " |
237 | psiconv_progress(lev+1,off+len-1,"End of application id section " |
181 | "(total length: %08x", len); |
238 | "(total length: %08x", len); |
182 | |
239 | |
183 | return res; |
240 | return res; |
|
|
241 | ERROR2: |
|
|
242 | free(*result); |
|
|
243 | ERROR1: |
|
|
244 | psiconv_warn(lev+1,off,"Reading of Application ID Section failed"); |
|
|
245 | if (length) |
|
|
246 | *length = 0; |
|
|
247 | if (res == 0) |
|
|
248 | return -PSICONV_E_NOMEM; |
|
|
249 | else |
|
|
250 | return res; |
184 | } |
251 | } |
185 | |
252 | |
186 | int psiconv_parse_text_section(const psiconv_buffer buf,int lev,psiconv_u32 off, |
253 | int psiconv_parse_text_section(const psiconv_buffer buf,int lev,psiconv_u32 off, |
187 | int *length,psiconv_text_and_layout *result) |
254 | int *length,psiconv_text_and_layout *result) |
188 | { |
255 | { |
… | |
… | |
192 | |
259 | |
193 | psiconv_u32 text_len; |
260 | psiconv_u32 text_len; |
194 | psiconv_paragraph para; |
261 | psiconv_paragraph para; |
195 | |
262 | |
196 | int nr; |
263 | int nr; |
197 | int i,j,start,leng; |
264 | int i,j,start,leng,temp; |
198 | char *str_copy; |
265 | char *str_copy; |
199 | |
266 | |
200 | psiconv_progress(lev+1,off,"Going to parse the text section"); |
267 | psiconv_progress(lev+1,off,"Going to parse the text section"); |
201 | psiconv_progress(lev+2,off,"Reading the text length"); |
268 | psiconv_progress(lev+2,off,"Reading the text length"); |
|
|
269 | |
|
|
270 | if(!(*result = psiconv_list_new(sizeof(*para)))) |
|
|
271 | goto ERROR1; |
|
|
272 | if (!(para = malloc(sizeof(*para)))) |
|
|
273 | goto ERROR2; |
|
|
274 | |
202 | text_len = psiconv_read_X(buf,lev+2,off,&leng); |
275 | text_len = psiconv_read_X(buf,lev+2,off,&leng,&res); |
|
|
276 | if (res) |
|
|
277 | goto ERROR3; |
203 | psiconv_debug(lev+2,off,"Length: %08x",text_len); |
278 | psiconv_debug(lev+2,off,"Length: %08x",text_len); |
204 | len += leng; |
279 | len += leng; |
205 | |
|
|
206 | *result = psiconv_list_new(sizeof(*para)); |
|
|
207 | para = malloc(sizeof(*para)); |
|
|
208 | |
280 | |
209 | psiconv_progress(lev+2,off+len,"Going to read all paragraph text"); |
281 | psiconv_progress(lev+2,off+len,"Going to read all paragraph text"); |
210 | nr = 0; |
282 | nr = 0; |
211 | start = 0; |
283 | start = 0; |
212 | for (i = 0; i < text_len; i++) |
284 | for (i = 0; i < text_len; i++) { |
213 | if (psiconv_read_u8(buf,lev+2,off+len+i) == 0x06) { |
285 | temp = psiconv_read_u8(buf,lev+2,off+len+i,&res); |
|
|
286 | if (res) |
|
|
287 | goto ERROR3; |
|
|
288 | if (temp == 0x06) { |
214 | para->text = malloc(i - start + 1); |
289 | if (!(para->text = malloc(i - start + 1))) |
|
|
290 | goto ERROR3; |
215 | for (j = 0; j < i - start; j++) |
291 | for (j = 0; j < i - start; j++) { |
216 | para->text[j] = psiconv_read_u8(buf,lev+1,off + len + start + j); |
292 | temp = psiconv_read_u8(buf,lev+1,off + len + start + j,&res); |
|
|
293 | if (res) |
|
|
294 | goto ERROR4; |
|
|
295 | para->text[j] = temp; |
|
|
296 | } |
217 | para->text[j] = 0; |
297 | para->text[j] = 0; |
218 | |
298 | |
219 | psiconv_list_add(*result,para); |
299 | if ((res = psiconv_list_add(*result,para))) |
|
|
300 | goto ERROR4; |
220 | |
301 | |
221 | str_copy = psiconv_make_printable(para->text); |
302 | if (!(str_copy = psiconv_make_printable(para->text))) |
|
|
303 | goto ERROR3; |
222 | psiconv_debug(lev+2,off+i+len,"Line %d: %d characters",nr, |
304 | psiconv_debug(lev+2,off+i+len,"Line %d: %d characters",nr, |
223 | strlen(str_copy) +1); |
305 | strlen(str_copy) +1); |
224 | psiconv_debug(lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
306 | psiconv_debug(lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
225 | free(str_copy); |
307 | free(str_copy); |
226 | |
308 | |
227 | start = i + 1; |
309 | start = i + 1; |
228 | nr ++; |
310 | nr ++; |
229 | } |
311 | } |
|
|
312 | } |
230 | |
313 | |
231 | if (start != text_len) { |
314 | if (start != text_len) { |
232 | res = -1; |
|
|
233 | psiconv_warn(lev+2,off+start+len, |
315 | psiconv_warn(lev+2,off+start+len, |
234 | "Last line does not end on EOL (%d characters left)", len - start); |
316 | "Last line does not end on EOL (%d characters left)", len - start); |
235 | para->text = malloc(text_len - start + 1); |
317 | if (!(para->text = malloc(text_len - start + 1))) |
|
|
318 | goto ERROR3; |
236 | for (j = 0; j < text_len - start; j++) |
319 | for (j = 0; j < text_len - start; j++) { |
237 | para->text[j] = psiconv_read_u8(buf,lev+2,off + start + j + len); |
320 | temp = psiconv_read_u8(buf,lev+2,off + start + j + len, &res); |
|
|
321 | if (res) |
|
|
322 | goto ERROR4; |
|
|
323 | para->text[j] = temp; |
|
|
324 | } |
238 | para->text[text_len - start] = 0; |
325 | para->text[text_len - start] = 0; |
239 | psiconv_list_add(*result,para); |
326 | if ((res = psiconv_list_add(*result,para))) |
|
|
327 | goto ERROR4; |
240 | str_copy = psiconv_make_printable(para->text); |
328 | if (!(str_copy = psiconv_make_printable(para->text))) |
|
|
329 | goto ERROR3; |
241 | psiconv_debug(lev+2,off+start+len,"Last line: %d characters",nr, |
330 | psiconv_debug(lev+2,off+start+len,"Last line: %d characters",nr, |
242 | strlen(str_copy)+1); |
331 | strlen(str_copy)+1); |
243 | psiconv_debug(lev+2,off+start+len,"Last line: `%s'",str_copy); |
332 | psiconv_debug(lev+2,off+start+len,"Last line: `%s'",str_copy); |
244 | free(str_copy); |
333 | free(str_copy); |
245 | } |
334 | } |
246 | |
335 | |
247 | free(para); |
336 | free(para); |
248 | |
337 | |
249 | /* Initialize the remaining parts of each paragraph */ |
338 | /* Initialize the remaining parts of each paragraph */ |
250 | for (i = 0; i < psiconv_list_length(*result); i ++) { |
339 | for (i = 0; i < psiconv_list_length(*result); i ++) { |
251 | para = psiconv_list_get(*result,i); |
340 | if (!(para = psiconv_list_get(*result,i))) { |
252 | para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout)); |
341 | psiconv_warn(lev+2,off+len,"Massive memory corruption"); |
253 | para->replacements = psiconv_list_new(sizeof(struct psiconv_replacement)); |
342 | goto ERROR2_0; |
|
|
343 | } |
|
|
344 | if (!(para->in_lines = psiconv_list_new(sizeof( |
|
|
345 | struct psiconv_in_line_layout_s)))) |
|
|
346 | goto ERROR2_0; |
|
|
347 | if (!(para->replacements = psiconv_list_new(sizeof( |
|
|
348 | struct psiconv_replacement_s)))) |
|
|
349 | goto ERROR2_1; |
|
|
350 | if (!(para->base_character = psiconv_basic_character_layout())) |
|
|
351 | goto ERROR2_2; |
|
|
352 | if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) |
|
|
353 | goto ERROR2_3; |
254 | para->base_style = 0; |
354 | para->base_style = 0; |
255 | para->base_character = psiconv_basic_character_layout(); |
|
|
256 | para->base_paragraph = psiconv_basic_paragraph_layout(); |
|
|
257 | } |
355 | } |
258 | |
356 | |
259 | |
357 | |
260 | len += text_len; |
358 | len += text_len; |
261 | |
359 | |
… | |
… | |
264 | |
362 | |
265 | psiconv_progress(lev+1,off+len-1,"End of text section (total length: %08x", |
363 | psiconv_progress(lev+1,off+len-1,"End of text section (total length: %08x", |
266 | len); |
364 | len); |
267 | |
365 | |
268 | return res; |
366 | return res; |
|
|
367 | |
|
|
368 | ERROR2_3: |
|
|
369 | psiconv_free_character_layout(para->base_character); |
|
|
370 | ERROR2_2: |
|
|
371 | psiconv_list_free(para->replacements); |
|
|
372 | ERROR2_1: |
|
|
373 | psiconv_list_free(para->in_lines); |
|
|
374 | ERROR2_0: |
|
|
375 | for (j = 0; j < i; j++) { |
|
|
376 | if (!(para = psiconv_list_get(*result,j))) { |
|
|
377 | psiconv_warn(lev+1,off,"Massive memory corruption..."); |
|
|
378 | break; |
|
|
379 | } |
|
|
380 | psiconv_list_free(para->in_lines); |
|
|
381 | psiconv_list_free(para->replacements); |
|
|
382 | psiconv_free_character_layout(para->base_character); |
|
|
383 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
384 | } |
|
|
385 | goto ERROR2; |
|
|
386 | |
|
|
387 | ERROR4: |
|
|
388 | free(para->text); |
|
|
389 | ERROR3: |
|
|
390 | free(para); |
|
|
391 | ERROR2: |
|
|
392 | for (i = 0; i < psiconv_list_length(*result);i++) { |
|
|
393 | if (!(para = psiconv_list_get(*result,i))) { |
|
|
394 | psiconv_warn(lev+1,off,"Massive memory corruption..."); |
|
|
395 | break; |
|
|
396 | } |
|
|
397 | free(para->text); |
|
|
398 | } |
|
|
399 | psiconv_list_free(*result); |
|
|
400 | ERROR1: |
|
|
401 | psiconv_warn(lev+1,off,"Reading of Text Section failed"); |
|
|
402 | if (length) |
|
|
403 | *length = 0; |
|
|
404 | if (!res) |
|
|
405 | return -PSICONV_E_NOMEM; |
|
|
406 | else |
|
|
407 | return res; |
269 | } |
408 | } |
270 | |
409 | |
271 | /* First do a parse_text_section, or you will get into trouble here */ |
410 | /* First do a parse_text_section, or you will get into trouble here */ |
272 | int psiconv_parse_layout_section(const psiconv_buffer buf, |
411 | int psiconv_parse_layout_section(const psiconv_buffer buf, |
273 | int lev,psiconv_u32 off, |
412 | int lev,psiconv_u32 off, |
… | |
… | |
279 | int res = 0; |
418 | int res = 0; |
280 | int len = 0; |
419 | int len = 0; |
281 | psiconv_u32 temp; |
420 | psiconv_u32 temp; |
282 | int parse_styles,nr,i,j,total,leng,line_length; |
421 | int parse_styles,nr,i,j,total,leng,line_length; |
283 | |
422 | |
284 | typedef struct anon_style |
423 | typedef struct anon_style_s |
285 | { |
424 | { |
286 | int nr; |
425 | int nr; |
287 | psiconv_s16 base_style; |
426 | psiconv_s16 base_style; |
288 | psiconv_character_layout character; |
427 | psiconv_character_layout character; |
289 | psiconv_paragraph_layout paragraph; |
428 | psiconv_paragraph_layout paragraph; |
290 | } *anon_style; |
429 | } *anon_style; |
291 | |
430 | |
292 | typedef psiconv_list anon_style_list; /* of struct anon_style */ |
431 | typedef psiconv_list anon_style_list; /* of struct anon_style */ |
293 | |
432 | |
294 | anon_style_list anon_styles; |
433 | anon_style_list anon_styles; |
295 | struct anon_style anon; |
434 | struct anon_style_s anon; |
296 | anon_style anon_ptr=NULL; |
435 | anon_style anon_ptr=NULL; |
297 | |
436 | |
|
|
437 | psiconv_character_layout temp_char; |
|
|
438 | psiconv_paragraph_layout temp_para; |
|
|
439 | psiconv_word_style temp_style; |
298 | psiconv_paragraph para; |
440 | psiconv_paragraph para; |
299 | struct psiconv_in_line_layout in_line; |
441 | struct psiconv_in_line_layout_s in_line; |
300 | |
442 | |
301 | int *inline_count; |
443 | int *inline_count; |
302 | |
444 | |
303 | |
445 | |
304 | psiconv_progress(lev+1,off,"Going to read the layout section"); |
446 | psiconv_progress(lev+1,off,"Going to read the layout section"); |
305 | |
447 | |
306 | psiconv_progress(lev+2,off,"Going to read the section type"); |
448 | psiconv_progress(lev+2,off,"Going to read the section type"); |
307 | temp = psiconv_read_u16(buf,lev+2,off+len); |
449 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
450 | if (res) |
|
|
451 | goto ERROR1; |
308 | psiconv_debug(lev+2,off+len,"Type: %02x",temp); |
452 | psiconv_debug(lev+2,off+len,"Type: %02x",temp); |
309 | parse_styles = with_styles; |
453 | parse_styles = with_styles; |
310 | if ((temp == 0x0001) && !with_styles) { |
454 | if ((temp == 0x0001) && !with_styles) { |
311 | psiconv_warn(lev+2,off+len,"Styleless layout section expected, " |
455 | psiconv_warn(lev+2,off+len,"Styleless layout section expected, " |
312 | "but styled section found!"); |
456 | "but styled section found!"); |
313 | parse_styles = 1; |
457 | parse_styles = 1; |
314 | res = -1; |
|
|
315 | } else if ((temp == 0x0000) && (with_styles)) { |
458 | } else if ((temp == 0x0000) && (with_styles)) { |
316 | psiconv_warn(lev+2,off+len,"Styled layout section expected, " |
459 | psiconv_warn(lev+2,off+len,"Styled layout section expected, " |
317 | "but styleless section found!"); |
460 | "but styleless section found!"); |
318 | parse_styles = 0; |
461 | parse_styles = 0; |
319 | res = -1; |
|
|
320 | } else if ((temp != 0x0000) && (temp != 0x0001)) { |
462 | } else if ((temp != 0x0000) && (temp != 0x0001)) { |
321 | psiconv_warn(lev+2,off+len, |
463 | psiconv_warn(lev+2,off+len, |
322 | "Layout section type indicator has unknown value!"); |
464 | "Layout section type indicator has unknown value!"); |
323 | res = -1; |
|
|
324 | } |
465 | } |
325 | len += 0x02; |
466 | len += 0x02; |
326 | |
467 | |
327 | psiconv_progress(lev+2,off+len,"Going to read paragraph type list"); |
468 | psiconv_progress(lev+2,off+len,"Going to read paragraph type list"); |
328 | anon_styles = psiconv_list_new(sizeof(anon)); |
469 | if (!(anon_styles = psiconv_list_new(sizeof(anon)))) |
|
|
470 | goto ERROR1; |
329 | psiconv_progress(lev+3,off+len,"Going to read paragraph type list length"); |
471 | psiconv_progress(lev+3,off+len,"Going to read paragraph type list length"); |
330 | nr = psiconv_read_u8(buf,lev+3,off+len); |
472 | nr = psiconv_read_u8(buf,lev+3,off+len,&res); |
|
|
473 | if (res) |
|
|
474 | goto ERROR2; |
331 | psiconv_debug(lev+3,off+len,"Length: %02x",nr); |
475 | psiconv_debug(lev+3,off+len,"Length: %02x",nr); |
332 | len ++; |
476 | len ++; |
333 | |
477 | |
334 | psiconv_progress(lev+3,off+len, |
478 | psiconv_progress(lev+3,off+len, |
335 | "Going to read the paragraph type list elements"); |
479 | "Going to read the paragraph type list elements"); |
336 | for (i = 0; i < nr; i ++) { |
480 | for (i = 0; i < nr; i ++) { |
337 | psiconv_progress(lev+3,off+len,"Element %d",i); |
481 | psiconv_progress(lev+3,off+len,"Element %d",i); |
338 | anon.nr = psiconv_read_u32(buf,lev+4,off+len); |
482 | anon.nr = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
483 | if (res) |
|
|
484 | goto ERROR3; |
339 | psiconv_debug(lev+4,off+len,"Number: %08x",anon.nr); |
485 | psiconv_debug(lev+4,off+len,"Number: %08x",anon.nr); |
340 | len += 0x04; |
486 | len += 0x04; |
341 | |
487 | |
342 | psiconv_progress(lev+4,off,"Going to determine the base style"); |
488 | psiconv_progress(lev+4,off,"Going to determine the base style"); |
343 | if (parse_styles) { |
489 | if (parse_styles) { |
|
|
490 | temp = psiconv_read_u32(buf,lev+4, off+len,&res); |
|
|
491 | if (res) |
|
|
492 | goto ERROR3; |
344 | anon.base_style = psiconv_read_u8(buf,lev+3, |
493 | anon.base_style = psiconv_read_u8(buf,lev+3, off+len+4+temp,&res); |
345 | off+len+4+psiconv_read_u32(buf,lev+4, |
494 | if (res) |
346 | off+len)); |
495 | goto ERROR3; |
347 | psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len), |
496 | psiconv_debug(lev+4,off+len+temp, |
348 | "Style indicator: %02x",anon.base_style); |
497 | "Style indicator: %02x",anon.base_style); |
349 | } else |
498 | } else |
350 | anon.base_style = 0; |
499 | anon.base_style = 0; |
|
|
500 | if (!(temp_style = psiconv_get_style(styles,anon.base_style))) { |
|
|
501 | psiconv_warn(lev+4,off,"Unknown Style referenced"); |
|
|
502 | if (!(temp_style = psiconv_get_style(styles,anon.base_style))) { |
|
|
503 | psiconv_warn(lev+4,off,"Base style unknown"); |
|
|
504 | goto ERROR3; |
|
|
505 | } |
|
|
506 | } |
351 | anon.paragraph = psiconv_clone_paragraph_layout(psiconv_get_style |
507 | if (!(anon.paragraph = psiconv_clone_paragraph_layout |
352 | (styles,anon.base_style)->paragraph); |
508 | (temp_style->paragraph))) |
|
|
509 | goto ERROR3; |
353 | anon.character = psiconv_clone_character_layout(psiconv_get_style |
510 | if (!(anon.character = psiconv_clone_character_layout |
354 | (styles,anon.base_style)->character); |
511 | (temp_style->character))) |
|
|
512 | goto ERROR3_1; |
355 | |
513 | |
356 | psiconv_progress(lev+4,off+len,"Going to read the paragraph layout"); |
514 | psiconv_progress(lev+4,off+len,"Going to read the paragraph layout"); |
357 | res |= psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
515 | if ((res = psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
358 | anon.paragraph); |
516 | anon.paragraph))) |
|
|
517 | goto ERROR3_2; |
359 | len += leng; |
518 | len += leng; |
360 | if (parse_styles) |
519 | if (parse_styles) |
361 | len ++; |
520 | len ++; |
362 | |
521 | |
363 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
522 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
364 | res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
523 | if ((res = psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
365 | anon.character); |
524 | anon.character))) |
|
|
525 | goto ERROR3_2; |
366 | len += leng; |
526 | len += leng; |
367 | psiconv_list_add(anon_styles,&anon); |
527 | if ((res = psiconv_list_add(anon_styles,&anon))) |
|
|
528 | goto ERROR3_2; |
368 | } |
529 | } |
369 | |
530 | |
370 | psiconv_progress(lev+2,off+len,"Going to parse the paragraph element list"); |
531 | psiconv_progress(lev+2,off+len,"Going to parse the paragraph element list"); |
371 | psiconv_progress(lev+3,off+len,"Going to read the number of paragraphs"); |
532 | psiconv_progress(lev+3,off+len,"Going to read the number of paragraphs"); |
372 | nr = psiconv_read_u32(buf,lev+3,off+len); |
533 | nr = psiconv_read_u32(buf,lev+3,off+len,&res); |
|
|
534 | if (res) |
|
|
535 | goto ERROR3; |
373 | if (nr != psiconv_list_length(result)) { |
536 | if (nr != psiconv_list_length(result)) { |
374 | psiconv_warn(lev+3,off+len, |
537 | psiconv_warn(lev+3,off+len, |
375 | "Number of text paragraphs and paragraph elements does not match"); |
538 | "Number of text paragraphs and paragraph elements does not match"); |
376 | psiconv_debug(lev+3,off+len, |
539 | psiconv_debug(lev+3,off+len, |
377 | "%d text paragraphs, %d paragraph elements", |
540 | "%d text paragraphs, %d paragraph elements", |
378 | psiconv_list_length(result),nr); |
541 | psiconv_list_length(result),nr); |
379 | } |
542 | } |
380 | psiconv_debug(lev+3,off+len,"Number of paragraphs: %d",nr); |
543 | psiconv_debug(lev+3,off+len,"Number of paragraphs: %d",nr); |
381 | len += 4; |
544 | len += 4; |
382 | inline_count = malloc(nr * sizeof(*inline_count)); |
545 | if (!(inline_count = malloc(nr * sizeof(*inline_count)))) |
|
|
546 | goto ERROR3; |
383 | |
547 | |
384 | psiconv_progress(lev+3,off+len,"Going to read the paragraph elements"); |
548 | psiconv_progress(lev+3,off+len,"Going to read the paragraph elements"); |
385 | for (i = 0; i < nr; i ++) { |
549 | for (i = 0; i < nr; i ++) { |
386 | psiconv_progress(lev+3,off+len,"Element %d",i); |
550 | psiconv_progress(lev+3,off+len,"Element %d",i); |
387 | if (i >= psiconv_list_length(result)) { |
551 | if (i >= psiconv_list_length(result)) { |
388 | psiconv_debug(lev+4,off+len,"Going to allocate a new element"); |
552 | psiconv_debug(lev+4,off+len,"Going to allocate a new element"); |
389 | para = malloc(sizeof(*para)); |
553 | if (!(para = malloc(sizeof(*para)))) |
390 | para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout)); |
554 | goto ERROR4; |
|
|
555 | if (!(para->in_lines = psiconv_list_new(sizeof( |
|
|
556 | struct psiconv_in_line_layout_s)))) |
|
|
557 | goto ERROR4_1; |
391 | para->base_style = 0; |
558 | para->base_style = 0; |
392 | para->base_character = psiconv_basic_character_layout(); |
559 | if (!(para->base_character = psiconv_basic_character_layout())) |
|
|
560 | goto ERROR4_2; |
393 | para->base_paragraph = psiconv_basic_paragraph_layout(); |
561 | if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) |
|
|
562 | goto ERROR4_3; |
|
|
563 | if ((res = psiconv_list_add(result,para))) |
|
|
564 | goto ERROR4_4; |
394 | free(para); |
565 | free(para); |
395 | } |
566 | } |
396 | para = psiconv_list_get(result,i); |
567 | if (!(para = psiconv_list_get(result,i))) |
|
|
568 | goto ERROR4; |
397 | |
569 | |
398 | psiconv_progress(lev+4,off+len,"Going to read the paragraph length"); |
570 | psiconv_progress(lev+4,off+len,"Going to read the paragraph length"); |
399 | temp = psiconv_read_u32(buf,lev+4,off+len); |
571 | temp = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
572 | if (res) |
|
|
573 | goto ERROR4; |
400 | if (temp != strlen(para->text)+1) { |
574 | if (temp != strlen(para->text)+1) { |
401 | res = -1; |
|
|
402 | psiconv_warn(lev+4,off+len, |
575 | psiconv_warn(lev+4,off+len, |
403 | "Disagreement of the length of paragraph in layout section"); |
576 | "Disagreement of the length of paragraph in layout section"); |
404 | psiconv_debug(lev+4,off+len, |
577 | psiconv_debug(lev+4,off+len, |
405 | "Paragraph length: layout section says %d, counted %d", |
578 | "Paragraph length: layout section says %d, counted %d", |
406 | temp,strlen(para->text)+1); |
579 | temp,strlen(para->text)+1); |
407 | } else |
580 | } else |
408 | psiconv_debug(lev+4,off+len,"Paragraph length: %d",temp); |
581 | psiconv_debug(lev+4,off+len,"Paragraph length: %d",temp); |
409 | len += 4; |
582 | len += 4; |
410 | |
583 | |
411 | psiconv_progress(lev+4,off+len,"Going to read the paragraph type"); |
584 | psiconv_progress(lev+4,off+len,"Going to read the paragraph type"); |
412 | temp = psiconv_read_u8(buf,lev+4,off+len); |
585 | temp = psiconv_read_u8(buf,lev+4,off+len,&res); |
|
|
586 | if (res) |
|
|
587 | goto ERROR4; |
413 | if (temp != 0x00) { |
588 | if (temp != 0x00) { |
414 | psiconv_debug(lev+4,off+len,"Type: %02x",temp); |
589 | psiconv_debug(lev+4,off+len,"Type: %02x",temp); |
415 | for (j = 0; j < psiconv_list_length(anon_styles); j++) { |
590 | for (j = 0; j < psiconv_list_length(anon_styles); j++) { |
416 | anon_ptr = psiconv_list_get(anon_styles,j); |
591 | if (!(anon_ptr = psiconv_list_get(anon_styles,j))) { |
|
|
592 | psiconv_warn(lev+4,off+len,"Massive memory curruption"); |
|
|
593 | goto ERROR4; |
|
|
594 | } |
417 | if (temp == anon_ptr->nr) |
595 | if (temp == anon_ptr->nr) |
418 | break; |
596 | break; |
419 | } |
597 | } |
420 | if (j == psiconv_list_length(anon_styles)) { |
598 | if (j == psiconv_list_length(anon_styles)) { |
421 | psiconv_warn(lev+4,off+len,"Layout section paragraph type unknown"); |
599 | psiconv_warn(lev+4,off+len,"Layout section paragraph type unknown"); |
422 | psiconv_debug(lev+4,off+len,"Unknown type - using base styles instead"); |
600 | psiconv_debug(lev+4,off+len,"Unknown type - using base styles instead"); |
423 | para->base_style = 0; |
601 | para->base_style = 0; |
|
|
602 | if (!(temp_style = psiconv_get_style(styles,0))) { |
|
|
603 | psiconv_warn(lev+4,off,"Base style unknown"); |
|
|
604 | goto ERROR4; |
|
|
605 | } |
|
|
606 | if (!(temp_para = psiconv_clone_paragraph_layout |
|
|
607 | (temp_style->paragraph))) |
|
|
608 | goto ERROR4; |
424 | psiconv_free_paragraph_layout(para->base_paragraph); |
609 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
610 | para->base_paragraph = temp_para; |
|
|
611 | |
|
|
612 | if (!(temp_char = psiconv_clone_character_layout |
|
|
613 | (temp_style->character))) |
|
|
614 | goto ERROR4; |
425 | psiconv_free_character_layout(para->base_character); |
615 | psiconv_free_character_layout(para->base_character); |
426 | para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style |
616 | para->base_character = temp_char; |
427 | (styles,0)->paragraph); |
|
|
428 | para->base_character = psiconv_clone_character_layout(psiconv_get_style |
|
|
429 | (styles,0)->character); |
|
|
430 | } else { |
617 | } else { |
431 | para->base_style = anon_ptr->base_style; |
618 | para->base_style = anon_ptr->base_style; |
|
|
619 | if (!(temp_para = psiconv_clone_paragraph_layout (anon_ptr->paragraph))) |
|
|
620 | goto ERROR4; |
432 | psiconv_free_paragraph_layout(para->base_paragraph); |
621 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
622 | para->base_paragraph = temp_para; |
|
|
623 | |
|
|
624 | if (!(temp_char = psiconv_clone_character_layout (anon_ptr->character))) |
|
|
625 | goto ERROR4; |
433 | psiconv_free_character_layout(para->base_character); |
626 | psiconv_free_character_layout(para->base_character); |
434 | para->base_paragraph = psiconv_clone_paragraph_layout |
627 | para->base_character = temp_char; |
435 | (anon_ptr->paragraph); |
|
|
436 | para->base_character = psiconv_clone_character_layout |
|
|
437 | (anon_ptr->character); |
|
|
438 | } |
628 | } |
439 | inline_count[i] = 0; |
629 | inline_count[i] = 0; |
440 | len += 0x01; |
630 | len += 0x01; |
441 | } else { |
631 | } else { |
442 | psiconv_debug(lev+4,off+len,"Type: %02x (not based on a paragraph type)" |
632 | psiconv_debug(lev+4,off+len,"Type: %02x (not based on a paragraph type)" |
443 | ,temp); |
633 | ,temp); |
444 | len += 0x01; |
634 | len += 0x01; |
445 | if (parse_styles) { |
635 | if (parse_styles) { |
446 | psiconv_progress(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len), |
636 | temp = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
637 | if (res) |
|
|
638 | goto ERROR4; |
|
|
639 | psiconv_progress(lev+4,off+len+temp+4, |
447 | "Going to read the paragraph element base style"); |
640 | "Going to read the paragraph element base style"); |
448 | temp = psiconv_read_u8(buf,lev+4, |
641 | temp = psiconv_read_u8(buf,lev+4, off+len+temp+4,&res); |
449 | off+len+psiconv_read_u32(buf,lev+4,off+len)); |
642 | if (res) |
450 | psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len), |
643 | goto ERROR4; |
451 | "Style: %02x",temp); |
644 | psiconv_debug(lev+4,off+len+temp+4, "Style: %02x",temp); |
452 | } else |
645 | } else |
453 | temp = 0x00; |
646 | temp = 0x00; |
|
|
647 | |
|
|
648 | if (!(temp_style = psiconv_get_style (styles,temp))) { |
|
|
649 | psiconv_warn(lev+4,off,"Unknown Style referenced"); |
|
|
650 | if (!(temp_style = psiconv_get_style(styles,0))) { |
|
|
651 | psiconv_warn(lev+4,off,"Base style unknown"); |
|
|
652 | goto ERROR4; |
|
|
653 | } |
|
|
654 | } |
|
|
655 | |
|
|
656 | if (!(temp_para = psiconv_clone_paragraph_layout(temp_style->paragraph))) |
|
|
657 | goto ERROR4; |
454 | psiconv_free_paragraph_layout(para->base_paragraph); |
658 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
659 | para->base_paragraph = temp_para; |
|
|
660 | |
|
|
661 | if (!(temp_char = psiconv_clone_character_layout(temp_style->character))) |
|
|
662 | goto ERROR4; |
455 | psiconv_free_character_layout(para->base_character); |
663 | psiconv_free_character_layout(para->base_character); |
456 | para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style |
664 | para->base_character = temp_char; |
457 | (styles,temp)->paragraph); |
665 | |
458 | para->base_character = psiconv_clone_character_layout(psiconv_get_style |
|
|
459 | (styles,temp)->character); |
|
|
460 | para->base_style = temp; |
666 | para->base_style = temp; |
461 | psiconv_progress(lev+4,off+len,"Going to read paragraph layout"); |
667 | psiconv_progress(lev+4,off+len,"Going to read paragraph layout"); |
462 | psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
668 | if ((res = psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, |
463 | para->base_paragraph); |
669 | para->base_paragraph))) |
|
|
670 | goto ERROR4; |
464 | len += leng; |
671 | len += leng; |
465 | if (parse_styles) |
672 | if (parse_styles) |
466 | len += 1; |
673 | len += 1; |
467 | psiconv_progress(lev+4,off+len,"Going to read number of in-line " |
674 | psiconv_progress(lev+4,off+len,"Going to read number of in-line " |
468 | "layout elements"); |
675 | "layout elements"); |
469 | inline_count[i] = psiconv_read_u32(buf,lev+4,off+len); |
676 | inline_count[i] = psiconv_read_u32(buf,lev+4,off+len,&res); |
|
|
677 | if (res) |
|
|
678 | goto ERROR4; |
470 | psiconv_debug(lev+4,off+len,"Nr: %08x",inline_count[i]); |
679 | psiconv_debug(lev+4,off+len,"Nr: %08x",inline_count[i]); |
471 | len += 4; |
680 | len += 4; |
472 | } |
681 | } |
473 | } |
682 | } |
474 | |
683 | |
475 | psiconv_progress(lev+2,off+len,"Going to read the text layout inline list"); |
684 | psiconv_progress(lev+2,off+len,"Going to read the text layout inline list"); |
476 | |
685 | |
477 | psiconv_progress(lev+3,off+len,"Going to read the number of elements"); |
686 | psiconv_progress(lev+3,off+len,"Going to read the number of elements"); |
478 | nr = psiconv_read_u32(buf,lev+3,off+len); |
687 | nr = psiconv_read_u32(buf,lev+3,off+len,&res); |
|
|
688 | if (res) |
|
|
689 | goto ERROR4; |
479 | psiconv_debug(lev+3,off,"Elements: %08x",nr); |
690 | psiconv_debug(lev+3,off+len,"Elements: %08x",nr); |
480 | len += 0x04; |
691 | len += 0x04; |
481 | |
692 | |
482 | psiconv_progress(lev+3,off+len, |
693 | psiconv_progress(lev+3,off+len, |
483 | "Going to read the text layout inline elements"); |
694 | "Going to read the text layout inline elements"); |
484 | total = 0; |
695 | total = 0; |
485 | for (i = 0; i < psiconv_list_length(result); i++) { |
696 | for (i = 0; i < psiconv_list_length(result); i++) { |
486 | para = psiconv_list_get(result,i); |
697 | if (!(para = psiconv_list_get(result,i))) { |
|
|
698 | psiconv_warn(lev+3,off+len,"Massive memory corruption"); |
|
|
699 | goto ERROR4; |
|
|
700 | } |
487 | line_length = -1; |
701 | line_length = -1; |
488 | for (j = 0; j < inline_count[i]; j++) { |
702 | for (j = 0; j < inline_count[i]; j++) { |
489 | psiconv_progress(lev+3,off+len,"Element %d: Paragraph %d, element %d", |
703 | psiconv_progress(lev+3,off+len,"Element %d: Paragraph %d, element %d", |
490 | total,i,j); |
704 | total,i,j); |
491 | if (total >= nr) { |
705 | if (total >= nr) { |
492 | psiconv_warn(lev+3,off+len, |
706 | psiconv_warn(lev+3,off+len, |
493 | "Layout section inlines: not enough element"); |
707 | "Layout section inlines: not enough element"); |
494 | res = -1; |
|
|
495 | psiconv_debug(lev+3,off+len,"Can't read element!"); |
708 | psiconv_debug(lev+3,off+len,"Can't read element!"); |
496 | } else { |
709 | } else { |
497 | total ++; |
710 | total ++; |
498 | in_line.layout = psiconv_clone_character_layout(para->base_character); |
711 | if (!(in_line.layout = psiconv_clone_character_layout |
|
|
712 | (para->base_character))) |
|
|
713 | goto ERROR4; |
499 | psiconv_progress(lev+4,off+len,"Going to read the element type"); |
714 | psiconv_progress(lev+4,off+len,"Going to read the element type"); |
500 | temp = psiconv_read_u8(buf,lev+4,len+off); |
715 | temp = psiconv_read_u8(buf,lev+4,len+off,&res); |
|
|
716 | if (res) |
|
|
717 | goto ERROR4; |
501 | len += 1; |
718 | len += 1; |
502 | psiconv_debug(lev+4,off,"Type: %02x",temp); |
719 | psiconv_debug(lev+4,off+len,"Type: %02x",temp); |
503 | psiconv_progress(lev+4,off, |
720 | psiconv_progress(lev+4,off+len, |
504 | "Going to read the number of characters it applies to"); |
721 | "Going to read the number of characters it applies to"); |
505 | in_line.length = psiconv_read_u32(buf,lev+4,len+off); |
722 | in_line.length = psiconv_read_u32(buf,lev+4,len+off,&res); |
|
|
723 | if (res) |
|
|
724 | goto ERROR4; |
506 | psiconv_debug(lev+4,off+len,"Length: %02x",in_line.length); |
725 | psiconv_debug(lev+4,off+len,"Length: %02x",in_line.length); |
507 | len += 4; |
726 | len += 4; |
508 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
727 | psiconv_progress(lev+4,off+len,"Going to read the character layout"); |
509 | res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
728 | if ((res = psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, |
510 | in_line.layout); |
729 | in_line.layout))) |
|
|
730 | goto ERROR4; |
511 | len += leng; |
731 | len += leng; |
512 | |
732 | |
513 | if (temp == 0x01) { |
733 | if (temp == 0x01) { |
514 | psiconv_debug(lev+4,off+len,"Skipping object data"); |
734 | psiconv_debug(lev+4,off+len,"Skipping object data"); |
515 | len += 0x10; |
735 | len += 0x10; |
516 | } else if (temp != 0x00) { |
736 | } else if (temp != 0x00) { |
517 | psiconv_warn(lev+4,off+len,"Layout section unknown inline type"); |
737 | psiconv_warn(lev+4,off+len,"Layout section unknown inline type"); |
518 | res = -1; |
|
|
519 | } |
738 | } |
520 | if (line_length + in_line.length > strlen(para->text)) { |
739 | if (line_length + in_line.length > strlen(para->text)) { |
521 | psiconv_warn(lev+4,off+len, |
740 | psiconv_warn(lev+4,off+len, |
522 | "Layout section inlines: line length mismatch"); |
741 | "Layout section inlines: line length mismatch"); |
523 | res = -1; |
742 | res = -1; |
524 | in_line.length = strlen(para->text) - line_length; |
743 | in_line.length = strlen(para->text) - line_length; |
525 | } |
744 | } |
526 | line_length += in_line.length; |
745 | line_length += in_line.length; |
527 | psiconv_list_add(para->in_lines,&in_line); |
746 | if ((res = psiconv_list_add(para->in_lines,&in_line))) |
|
|
747 | goto ERROR4; |
528 | } |
748 | } |
529 | } |
749 | } |
530 | } |
750 | } |
531 | |
751 | |
532 | if (total != nr) { |
752 | if (total != nr) { |
… | |
… | |
535 | } |
755 | } |
536 | |
756 | |
537 | free(inline_count); |
757 | free(inline_count); |
538 | |
758 | |
539 | for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { |
759 | for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { |
540 | anon_ptr = psiconv_list_get(anon_styles,i); |
760 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
|
|
761 | psiconv_warn(lev+4,off+len,"Massive memory corruption"); |
|
|
762 | goto ERROR2; |
|
|
763 | } |
541 | psiconv_free_character_layout(anon_ptr->character); |
764 | psiconv_free_character_layout(anon_ptr->character); |
542 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
765 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
543 | } |
766 | } |
544 | psiconv_list_free(anon_styles); |
767 | psiconv_list_free(anon_styles); |
545 | |
768 | |
546 | if (length) |
769 | if (length) |
547 | *length = len; |
770 | *length = len; |
548 | |
771 | |
549 | psiconv_progress(lev+1,off+len-1,"End of layout section (total length: %08x", |
772 | psiconv_progress(lev+1,off+len-1,"End of layout section (total length: %08x)", |
550 | len); |
773 | len); |
551 | |
774 | |
|
|
775 | return 0; |
|
|
776 | |
|
|
777 | ERROR4_4: |
|
|
778 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
779 | ERROR4_3: |
|
|
780 | psiconv_free_character_layout(para->base_character); |
|
|
781 | ERROR4_2: |
|
|
782 | psiconv_list_free(para->in_lines); |
|
|
783 | ERROR4_1: |
|
|
784 | free(para); |
|
|
785 | goto ERROR4; |
|
|
786 | |
|
|
787 | ERROR3_2: |
|
|
788 | psiconv_free_character_layout(anon.character); |
|
|
789 | ERROR3_1: |
|
|
790 | psiconv_free_paragraph_layout(anon.paragraph); |
|
|
791 | goto ERROR3; |
|
|
792 | |
|
|
793 | ERROR4: |
|
|
794 | free(inline_count); |
|
|
795 | ERROR3: |
|
|
796 | for (i = 0; i < psiconv_list_length(anon_styles); i++) { |
|
|
797 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
|
|
798 | psiconv_warn(lev+1,off,"Massive memory corruption"); |
|
|
799 | break; |
|
|
800 | } |
|
|
801 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
|
|
802 | psiconv_free_character_layout(anon_ptr->character); |
|
|
803 | } |
|
|
804 | |
|
|
805 | ERROR2: |
|
|
806 | psiconv_list_free(anon_styles); |
|
|
807 | ERROR1: |
|
|
808 | psiconv_warn(lev+1,off,"Reading of Layout Section failed"); |
|
|
809 | if (length) |
|
|
810 | *length = 0; |
|
|
811 | if (!res) |
|
|
812 | return -PSICONV_E_NOMEM; |
|
|
813 | else |
552 | return res; |
814 | return res; |
553 | } |
815 | } |
554 | |
816 | |
555 | int psiconv_parse_styled_layout_section(const psiconv_buffer buf, |
817 | int psiconv_parse_styled_layout_section(const psiconv_buffer buf, |
556 | int lev,psiconv_u32 off, |
818 | int lev,psiconv_u32 off, |
557 | int *length, |
819 | int *length, |
… | |
… | |
566 | int *length, |
828 | int *length, |
567 | psiconv_text_and_layout result, |
829 | psiconv_text_and_layout result, |
568 | psiconv_character_layout base_char, |
830 | psiconv_character_layout base_char, |
569 | psiconv_paragraph_layout base_para) |
831 | psiconv_paragraph_layout base_para) |
570 | { |
832 | { |
571 | int res; |
833 | int res = 0; |
572 | psiconv_word_styles_section styles_section; |
834 | psiconv_word_styles_section styles_section; |
573 | |
835 | |
574 | styles_section = malloc(sizeof(*styles_section)); |
836 | if (!(styles_section = malloc(sizeof(*styles_section)))) |
|
|
837 | goto ERROR1; |
575 | styles_section->normal = malloc(sizeof(*styles_section->normal)); |
838 | if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) |
576 | styles_section->normal->character = psiconv_clone_character_layout(base_char); |
839 | goto ERROR2; |
577 | styles_section->normal->paragraph = psiconv_clone_paragraph_layout(base_para); |
840 | if (!(styles_section->normal->character = |
|
|
841 | psiconv_clone_character_layout(base_char))) |
|
|
842 | goto ERROR3; |
|
|
843 | if (!(styles_section->normal->paragraph = |
|
|
844 | psiconv_clone_paragraph_layout(base_para))) |
|
|
845 | goto ERROR4; |
578 | styles_section->normal->hotkey = 0; |
846 | styles_section->normal->hotkey = 0; |
579 | styles_section->normal->name = strdup(""); |
847 | if (!(styles_section->normal->name = strdup(""))) |
|
|
848 | goto ERROR5; |
580 | styles_section->styles = psiconv_list_new(sizeof(struct psiconv_word_style)); |
849 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
|
|
850 | struct psiconv_word_style_s)))) |
|
|
851 | goto ERROR6; |
581 | |
852 | |
582 | res = psiconv_parse_layout_section(buf,lev,off,length,result, |
853 | res = psiconv_parse_layout_section(buf,lev,off,length,result, |
583 | styles_section,0); |
854 | styles_section,0); |
584 | |
855 | |
585 | psiconv_free_word_styles_section(styles_section); |
856 | psiconv_free_word_styles_section(styles_section); |
586 | return res; |
857 | return res; |
|
|
858 | |
|
|
859 | ERROR6: |
|
|
860 | free(styles_section->normal->name); |
|
|
861 | ERROR5: |
|
|
862 | psiconv_free_paragraph_layout(styles_section->normal->paragraph); |
|
|
863 | ERROR4: |
|
|
864 | psiconv_free_character_layout(styles_section->normal->character); |
|
|
865 | ERROR3: |
|
|
866 | free(styles_section->normal); |
|
|
867 | ERROR2: |
|
|
868 | free(styles_section); |
|
|
869 | ERROR1: |
|
|
870 | psiconv_warn(lev+1,off,"Reading of Styleless Layout Section failed"); |
|
|
871 | if (length) |
|
|
872 | *length = 0; |
|
|
873 | if (!res) |
|
|
874 | return -PSICONV_E_NOMEM; |
|
|
875 | else |
|
|
876 | return res; |
587 | } |
877 | } |
588 | |
878 | |