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