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