/[public]/psiconv/trunk/lib/psiconv/parse_common.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/parse_common.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 41 Revision 233
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-2004 Frodo Looijaard <frodol@dds.nl>
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
20#include "config.h" 20#include "config.h"
21#include "compat.h" 21#include "compat.h"
22#include <stdlib.h> 22#include <stdlib.h>
23#include <string.h> 23#include <string.h>
24 24
25#include "data.h"
26#include "parse_routines.h" 25#include "parse_routines.h"
26#include "error.h"
27 27
28#ifdef DMALLOC
29#include <dmalloc.h>
30#endif
31
32
28static int psiconv_parse_layout_section(const psiconv_buffer buf, 33static int psiconv_parse_layout_section(const psiconv_config config,
34 const psiconv_buffer buf,
29 int lev,psiconv_u32 off, 35 int lev,psiconv_u32 off,
30 int *length, 36 int *length,
31 psiconv_text_and_layout result, 37 psiconv_text_and_layout result,
32 psiconv_word_styles_section styles, 38 psiconv_word_styles_section styles,
33 int with_styles); 39 int with_styles);
40static psiconv_file_type_t psiconv_determine_embedded_object_type
41 (const psiconv_config config,
42 const psiconv_buffer buf,int lev,
43 int *status);
34 44
35int psiconv_parse_header_section(const psiconv_buffer buf,int lev, 45int psiconv_parse_header_section(const psiconv_config config,
46 const psiconv_buffer buf,int lev,
36 psiconv_u32 off, int *length, 47 psiconv_u32 off, int *length,
37 psiconv_header_section *result) 48 psiconv_header_section *result)
38{ 49{
39 int res=0; 50 int res=0;
40 int len=0; 51 int len=0;
41 psiconv_u32 temp; 52 psiconv_u32 temp;
42 53
43 psiconv_progress(lev+1,off+len,"Going to read the header section"); 54 psiconv_progress(config,lev+1,off+len,"Going to read the header section");
44 (*result) = malloc(sizeof(**result)); 55 if (!((*result) = malloc(sizeof(**result))))
56 goto ERROR1;
45 57
46 psiconv_progress(lev+2,off+len,"Going to read UID1 to UID3"); 58 psiconv_progress(config,lev+2,off+len,"Going to read UID1 to UID3");
47 (*result)->uid1 = psiconv_read_u32(buf,lev+2,off+len); 59 (*result)->uid1 = psiconv_read_u32(config,buf,lev+2,off+len,&res);
60 if (res)
61 goto ERROR2;
48 psiconv_debug(lev+2,off+len,"UID1: %08x",(*result)->uid1); 62 psiconv_debug(config,lev+2,off+len,"UID1: %08x",(*result)->uid1);
49 if ((*result)->uid1 == PSICONV_ID_CLIPART) { 63 if ((*result)->uid1 == PSICONV_ID_CLIPART) {
50 /* That's all folks... */ 64 /* That's all folks... */
51 (*result)->file = psiconv_clipart_file; 65 (*result)->file = psiconv_clipart_file;
52 (*result)->uid2 = 0; 66 (*result)->uid2 = 0;
53 (*result)->uid3 = 0; 67 (*result)->uid3 = 0;
54 (*result)->checksum = 0; 68 (*result)->checksum = 0;
55 len += 4; 69 len += 4;
70 psiconv_debug(config,lev+2,off+len,"File is a Clipart file");
56 goto DONE; 71 goto DONE;
57 } 72 }
58 if ((*result)->uid1 != PSICONV_ID_PSION5) { 73 if ((*result)->uid1 != PSICONV_ID_PSION5) {
59 psiconv_warn(lev+2,off+len,"UID1 has unknown value. This is probably " 74 psiconv_error(config,lev+2,off+len,
75 "UID1 has unknown value. This is probably "
60 "not a (parsable) Psion 5 file"); 76 "not a (parsable) Psion 5 file");
61 res = -1; 77 res = -PSICONV_E_PARSE;
78 goto ERROR2;
62 } 79 }
63 len += 4; 80 len += 4;
64 (*result)->uid2 = psiconv_read_u32(buf,lev+2,off+len); 81 (*result)->uid2 = psiconv_read_u32(config,buf,lev+2,off+len,&res);
82 if (res)
83 goto ERROR2;
65 psiconv_debug(lev+2,off+len,"UID2: %08x",(*result)->uid2); 84 psiconv_debug(config,lev+2,off+len,"UID2: %08x",(*result)->uid2);
66 len += 4; 85 len += 4;
67 (*result)->uid3 = psiconv_read_u32(buf,lev+2,off+len); 86 (*result)->uid3 = psiconv_read_u32(config,buf,lev+2,off+len,&res);
87 if (res)
88 goto ERROR2;
68 psiconv_debug(lev+2,off+len,"UID3: %08x",(*result)->uid3); 89 psiconv_debug(config,lev+2,off+len,"UID3: %08x",(*result)->uid3);
69 len += 4; 90 len += 4;
70 91
71 (*result)->file = psiconv_unknown_file; 92 (*result)->file = psiconv_unknown_file;
72 if ((*result)->uid1 == PSICONV_ID_PSION5) { 93 if ((*result)->uid1 == PSICONV_ID_PSION5) {
73 if ((*result)->uid2 == PSICONV_ID_DATA_FILE) { 94 if ((*result)->uid2 == PSICONV_ID_DATA_FILE) {
74 if ((*result)->uid3 == PSICONV_ID_WORD) { 95 if ((*result)->uid3 == PSICONV_ID_WORD) {
75 (*result)->file = psiconv_word_file; 96 (*result)->file = psiconv_word_file;
76 psiconv_debug(lev+2,off+len,"File is a Word file"); 97 psiconv_debug(config,lev+2,off+len,"File is a Word file");
77 } else if ((*result)->uid3 == PSICONV_ID_TEXTED) { 98 } else if ((*result)->uid3 == PSICONV_ID_TEXTED) {
78 (*result)->file = psiconv_texted_file; 99 (*result)->file = psiconv_texted_file;
79 psiconv_debug(lev+2,off+len,"File is a TextEd file"); 100 psiconv_debug(config,lev+2,off+len,"File is a TextEd file");
80 } else if ((*result)->uid3 == PSICONV_ID_SKETCH) { 101 } else if ((*result)->uid3 == PSICONV_ID_SKETCH) {
81 (*result)->file = psiconv_sketch_file; 102 (*result)->file = psiconv_sketch_file;
82 psiconv_debug(lev+2,off+len,"File is a Sketch file"); 103 psiconv_debug(config,lev+2,off+len,"File is a Sketch file");
104 } else if ((*result)->uid3 == PSICONV_ID_SHEET) {
105 (*result)->file = psiconv_sheet_file;
106 psiconv_debug(config,lev+2,off+len,"File is a Sheet file");
83 } 107 }
84 } else if ((*result)->uid2 == PSICONV_ID_MBM_FILE) { 108 } else if ((*result)->uid2 == PSICONV_ID_MBM_FILE) {
85 (*result)->file = psiconv_mbm_file; 109 (*result)->file = psiconv_mbm_file;
86 if ((*result)->uid3 != 0x00) 110 if ((*result)->uid3 != 0x00)
87 psiconv_warn(lev+2,off+len,"UID3 set in MBM file?!?"); 111 psiconv_warn(config,lev+2,off+len,"UID3 set in MBM file?!?");
88 psiconv_debug(lev+2,off+len,"File is a MBM file"); 112 psiconv_debug(config,lev+2,off+len,"File is a MBM file");
89 } 113 }
90 } 114 }
91 if ((*result)->file == psiconv_unknown_file) { 115 if ((*result)->file == psiconv_unknown_file) {
92 psiconv_warn(lev+2,off+len,"Unknown file type"); 116 psiconv_warn(config,lev+2,off+len,"Unknown file type");
93 (*result)->file = psiconv_unknown_file; 117 (*result)->file = psiconv_unknown_file;
94 } 118 }
95 119
96 psiconv_progress(lev+2,off+len,"Checking UID4"); 120 psiconv_progress(config,lev+2,off+len,"Checking UID4");
97 temp = psiconv_read_u32(buf,lev+2,off+len); 121 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
122 if (res)
123 goto ERROR2;
98 if (temp == psiconv_checkuid((*result)->uid1,(*result)->uid2, 124 if (temp == psiconv_checkuid((*result)->uid1,(*result)->uid2,
99 (*result)->uid3)) 125 (*result)->uid3))
100 psiconv_debug(lev+2,off+len,"Checksum %08x is correct",temp); 126 psiconv_debug(config,lev+2,off+len,"Checksum %08x is correct",temp);
101 else { 127 else {
102 psiconv_warn(lev+2,off+len,"Checksum failed, file corrupted!"); 128 psiconv_error(config,lev+2,off+len,"Checksum failed, file corrupted!");
103 psiconv_debug(lev+2,off+len,"Expected checksum %08x, found %08x", 129 psiconv_debug(config,lev+2,off+len,"Expected checksum %08x, found %08x",
104 psiconv_checkuid((*result)->uid1,(*result)->uid2, 130 psiconv_checkuid((*result)->uid1,(*result)->uid2,
105 (*result)->uid3),temp); 131 (*result)->uid3),temp);
106 res = -1; 132 res = -PSICONV_E_PARSE;
133 goto ERROR2;
107 } 134 }
108 len += 4; 135 len += 4;
109 136
110DONE: 137DONE:
111 if (length) 138 if (length)
112 *length = len; 139 *length = len;
113 140
114 psiconv_progress(lev+1,off+len-1, 141 psiconv_progress(config,lev+1,off+len-1,
115 "End of Header Section (total length: %08x)",len); 142 "End of Header Section (total length: %08x)",len);
116 143
117 return res; 144 return res;
145
146ERROR2:
147 free(*result);
148ERROR1:
149 psiconv_error(config,lev+1,off,"Reading of Header Section failed");
150 if (length)
151 *length = 0;
152 if (res == 0)
153 return -PSICONV_E_NOMEM;
154 else
155 return res;
118} 156}
119 157
120int psiconv_parse_section_table_section(const psiconv_buffer buf, int lev, 158int psiconv_parse_section_table_section(const psiconv_config config,
159 const psiconv_buffer buf, int lev,
121 psiconv_u32 off, int *length, 160 psiconv_u32 off, int *length,
122 psiconv_section_table_section *result) 161 psiconv_section_table_section *result)
123{ 162{
124 int res=0; 163 int res=0;
125 int len=0; 164 int len=0;
126 psiconv_section_table_entry entry; 165 psiconv_section_table_entry entry;
127 166
128 int i; 167 int i;
129 psiconv_u8 nr; 168 psiconv_u8 nr;
130 169
131 psiconv_progress(lev+1,off+len,"Going to read the section table section"); 170 psiconv_progress(config,lev+1,off+len,"Going to read the section table section");
132 *result = psiconv_list_new(sizeof(*entry)); 171 if (!(*result = psiconv_list_new(sizeof(*entry))))
172 goto ERROR1;
133 173
134 psiconv_progress(lev+2,off+len,"Going to read the section table length"); 174 psiconv_progress(config,lev+2,off+len,"Going to read the section table length");
135 nr = psiconv_read_u8(buf,lev+2,off+len); 175 nr = psiconv_read_u8(config,buf,lev+2,off+len,&res);
176 if (res)
177 goto ERROR2;
136 psiconv_debug(lev+2,off+len,"Length: %08x",nr); 178 psiconv_debug(config,lev+2,off+len,"Length: %08x",nr);
137 if (nr & 0x01) { 179 if (nr & 0x01) {
138 psiconv_warn(lev+2,off+len, 180 psiconv_warn(config,lev+2,off+len,
139 "Section table length odd - ignoring last entry"); 181 "Section table length odd - ignoring last entry");
140 res = -1;
141 } 182 }
142 len ++; 183 len ++;
143 184
144 psiconv_progress(lev+2,off+len,"Going to read the section table entries"); 185 psiconv_progress(config,lev+2,off+len,"Going to read the section table entries");
145 entry = malloc(sizeof(*entry)); 186 entry = malloc(sizeof(*entry));
146 for (i = 0; i < nr / 2; i++) { 187 for (i = 0; i < nr / 2; i++) {
147 entry->id = psiconv_read_u32(buf,lev+2,off + len); 188 entry->id = psiconv_read_u32(config,buf,lev+2,off + len,&res);
189 if (res)
190 goto ERROR3;
148 psiconv_debug(lev+2,off + len,"Entry %d: ID = %08x",i,entry->id); 191 psiconv_debug(config,lev+2,off + len,"Entry %d: ID = %08x",i,entry->id);
149 len += 0x04; 192 len += 0x04;
150 entry->offset = psiconv_read_u32(buf,lev+2,off + len); 193 entry->offset = psiconv_read_u32(config,buf,lev+2,off + len,&res);
194 if (res)
195 goto ERROR3;
151 psiconv_debug(lev+2,off +len,"Entry %d: Offset = %08x",i,entry->offset); 196 psiconv_debug(config,lev+2,off +len,"Entry %d: Offset = %08x",i,entry->offset);
152 len += 0x04; 197 len += 0x04;
153 psiconv_list_add(*result,entry); 198 if ((res=psiconv_list_add(*result,entry)))
199 goto ERROR3;
154 } 200 }
155 201
156 free(entry); 202 free(entry);
157 203
158 if (length) 204 if (length)
159 *length = len; 205 *length = len;
160 206
161 psiconv_progress(lev+1,off+len-1,"End of section table section " 207 psiconv_progress(config,lev+1,off+len-1,"End of section table section "
162 "(total length: %08x)", len); 208 "(total length: %08x)", len);
163 209
210 return 0;
211ERROR3:
212 free(entry);
213ERROR2:
214 psiconv_list_free(*result);
215ERROR1:
216 psiconv_error(config,lev+1,off,"Reading of Section Table Section failed");
217 if (length)
218 *length = 0;
219 if (res == 0)
220 return -PSICONV_E_NOMEM;
221 else
164 return res; 222 return res;
165} 223}
166 224
167int psiconv_parse_application_id_section(const psiconv_buffer buf, int lev, 225int psiconv_parse_application_id_section(const psiconv_config config,
226 const psiconv_buffer buf, int lev,
168 psiconv_u32 off, int *length, 227 psiconv_u32 off, int *length,
169 psiconv_application_id_section *result) 228 psiconv_application_id_section *result)
170{ 229{
171 int res=0; 230 int res=0;
172 int len=0; 231 int len=0;
173 int leng; 232 int leng;
174 233
175 psiconv_progress(lev+1,off,"Going to read the application id section"); 234 psiconv_progress(config,lev+1,off,"Going to read the application id section");
176 (*result) = malloc(sizeof(**result)); 235 if (!(*result = malloc(sizeof(**result))))
236 goto ERROR1;
177 237
178 psiconv_progress(lev+2,off+len,"Going to read the type identifier"); 238 psiconv_progress(config,lev+2,off+len,"Going to read the type identifier");
179 (*result)->id = psiconv_read_u32(buf,lev+2,off+len); 239 (*result)->id = psiconv_read_u32(config,buf,lev+2,off+len,&res);
240 if (res)
241 goto ERROR2;
180 psiconv_debug(lev+2,off+len,"Identifier: %08x",(*result)->id); 242 psiconv_debug(config,lev+2,off+len,"Identifier: %08x",(*result)->id);
181 len += 4; 243 len += 4;
182 244
183 psiconv_progress(lev+2,off+len,"Going to read the application id string"); 245 psiconv_progress(config,lev+2,off+len,"Going to read the application id string");
184 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng); 246 (*result)->name = psiconv_read_string(config,buf,lev+2,off+len,&leng,&res);
247 if (res)
248 goto ERROR2;
185 len += leng; 249 len += leng;
186 250
187 if (length) 251 if (length)
188 *length = len; 252 *length = len;
189 253
190 psiconv_progress(lev+1,off+len-1,"End of application id section " 254 psiconv_progress(config,lev+1,off+len-1,"End of application id section "
191 "(total length: %08x", len); 255 "(total length: %08x", len);
192 256
193 return res; 257 return res;
258ERROR2:
259 free(*result);
260ERROR1:
261 psiconv_error(config,lev+1,off,"Reading of Application ID Section failed");
262 if (length)
263 *length = 0;
264 if (res == 0)
265 return -PSICONV_E_NOMEM;
266 else
267 return res;
194} 268}
195 269
196int psiconv_parse_text_section(const psiconv_buffer buf,int lev,psiconv_u32 off, 270int psiconv_parse_text_section(const psiconv_config config,
271 const psiconv_buffer buf,int lev,psiconv_u32 off,
197 int *length,psiconv_text_and_layout *result) 272 int *length,psiconv_text_and_layout *result)
198{ 273{
199 274
200 int res = 0; 275 int res = 0;
201 int len=0; 276 int len=0;
202 277
203 psiconv_u32 text_len; 278 psiconv_u32 text_len;
204 psiconv_paragraph para; 279 psiconv_paragraph para;
280 psiconv_ucs2 temp;
281 psiconv_list line;
282 psiconv_ucs2 *nextcharptr;
205 283
206 int nr; 284 int nr;
207 int i,j,start,leng; 285 int i,j,leng;
208 char *str_copy; 286 char *str_copy;
209 287
210 psiconv_progress(lev+1,off,"Going to parse the text section"); 288 psiconv_progress(config,lev+1,off,"Going to parse the text section");
289
290 if(!(*result = psiconv_list_new(sizeof(*para))))
291 goto ERROR1;
292 if (!(para = malloc(sizeof(*para))))
293 goto ERROR2;
294
211 psiconv_progress(lev+2,off,"Reading the text length"); 295 psiconv_progress(config,lev+2,off,"Reading the text length");
212 text_len = psiconv_read_X(buf,lev+2,off,&leng); 296 text_len = psiconv_read_X(config,buf,lev+2,off,&leng,&res);
297 if (res)
298 goto ERROR3;
213 psiconv_debug(lev+2,off,"Length: %08x",text_len); 299 psiconv_debug(config,lev+2,off,"Length: %08x",text_len);
214 len += leng; 300 len += leng;
215 301
216 *result = psiconv_list_new(sizeof(*para)); 302 if (!(line = psiconv_list_new(sizeof(psiconv_ucs2))))
217 para = malloc(sizeof(*para)); 303 goto ERROR3;
218 304
219 psiconv_progress(lev+2,off+len,"Going to read all paragraph text"); 305 i = 0;
220 nr = 0; 306 nr = 0;
221 start = 0; 307 while (i < text_len) {
222 for (i = 0; i < text_len; i++) 308 temp = psiconv_unicode_read_char(config,buf,lev+2,off+len+i,&leng,&res);
223 if (psiconv_read_u8(buf,lev+2,off+len+i) == 0x06) { 309 if (res)
224 para->text = malloc(i - start + 1); 310 goto ERROR4;
225 for (j = 0; j < i - start; j++) 311 if (i + leng > text_len) {
226 para->text[j] = psiconv_read_u8(buf,lev+1,off + len + start + j); 312 psiconv_error(config,lev+2,off+len+i,"Malformed text section");
313 res = PSICONV_E_PARSE;
314 goto ERROR4;
315 }
316 if ((temp == 0x06) || (i + leng == text_len)) {
317 if (!(para->text = malloc(sizeof(*(para->text)) *
318 (psiconv_list_length(line) + 1))))
319 goto ERROR4;
320 for (j = 0; j < psiconv_list_length(line); j++) {
321 if (!(nextcharptr = psiconv_list_get(line,j))) {
322 psiconv_error(config,lev+2,off+i+len,"Data structure corruption");
323 goto ERROR5;
324 }
325 para->text[j] = *nextcharptr;
326 }
227 para->text[j] = 0; 327 para->text[j] = 0;
228
229 psiconv_list_add(*result,para);
230 328
231 str_copy = psiconv_make_printable(para->text); 329 if (!(str_copy = psiconv_make_printable(config,para->text)))
330 goto ERROR5;
232 psiconv_debug(lev+2,off+i+len,"Line %d: %d characters",nr, 331 psiconv_debug(config,lev+2,off+i+len,"Line %d: %d characters",nr,
233 strlen(str_copy) +1); 332 strlen(str_copy) +1);
234 psiconv_debug(lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); 333 psiconv_debug(config,lev+2,off+i+len,"Line %d: `%s'",nr,str_copy);
235 free(str_copy); 334 free(str_copy);
335 i += leng;
236 336
237 start = i + 1; 337 if (!(para->in_lines = psiconv_list_new(sizeof(
338 struct psiconv_in_line_layout_s))))
339 goto ERROR5;
340 if (!(para->replacements = psiconv_list_new(sizeof(
341 struct psiconv_replacement_s))))
342 goto ERROR6;
343 if (!(para->base_character = psiconv_basic_character_layout()))
344 goto ERROR7;
345 if (!(para->base_paragraph = psiconv_basic_paragraph_layout()))
346 goto ERROR8;
347 para->base_style = 0;
348
349 if ((res = psiconv_list_add(*result,para)))
350 goto ERROR9;
351 psiconv_progress(config,lev+2,off+len+i,"Starting a new line");
352 psiconv_list_empty(line);
238 nr ++; 353 nr ++;
354 } else {
355 if ((res = psiconv_list_add(line,&temp)))
356 goto ERROR4;
357 i += leng;
239 } 358 }
240 359 }
241 if (start != text_len) { 360
242 res = -1; 361 psiconv_list_free(line);
243 psiconv_warn(lev+2,off+start+len,
244 "Last line does not end on EOL (%d characters left)", len - start);
245 para->text = malloc(text_len - start + 1);
246 for (j = 0; j < text_len - start; j++)
247 para->text[j] = psiconv_read_u8(buf,lev+2,off + start + j + len);
248 para->text[text_len - start] = 0;
249 psiconv_list_add(*result,para);
250 str_copy = psiconv_make_printable(para->text);
251 psiconv_debug(lev+2,off+start+len,"Last line: %d characters",nr,
252 strlen(str_copy)+1);
253 psiconv_debug(lev+2,off+start+len,"Last line: `%s'",str_copy);
254 free(str_copy);
255 }
256
257 free(para); 362 free(para);
258 363
259 /* Initialize the remaining parts of each paragraph */
260 for (i = 0; i < psiconv_list_length(*result); i ++) {
261 para = psiconv_list_get(*result,i);
262 para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout));
263 para->replacements = psiconv_list_new(sizeof(struct psiconv_replacement));
264 para->base_style = 0;
265 para->base_character = psiconv_basic_character_layout();
266 para->base_paragraph = psiconv_basic_paragraph_layout();
267 }
268
269
270 len += text_len; 364 len += text_len;
271 365
272 if (length) 366 if (length)
273 *length = len; 367 *length = len;
274 368
275 psiconv_progress(lev+1,off+len-1,"End of text section (total length: %08x", 369 psiconv_progress(config,lev+1,off+len-1,
276 len); 370 "End of text section (total length: %08x", len);
277 371
278 return res; 372 return res;
373
374ERROR9:
375 psiconv_free_paragraph_layout(para->base_paragraph);
376ERROR8:
377 psiconv_free_character_layout(para->base_character);
378ERROR7:
379 psiconv_list_free(para->replacements);
380ERROR6:
381 psiconv_list_free(para->in_lines);
382ERROR5:
383 free(para->text);
384ERROR4:
385 psiconv_list_free(line);
386ERROR3:
387 free(para);
388ERROR2:
389 psiconv_free_text_and_layout(*result);
390ERROR1:
391 psiconv_error(config,lev+1,off,"Reading of Text Section failed");
392 if (length)
393 *length = 0;
394 if (!res)
395 return -PSICONV_E_NOMEM;
396 else
397 return res;
279} 398}
280 399
281/* First do a parse_text_section, or you will get into trouble here */ 400/* First do a parse_text_section, or you will get into trouble here */
282int psiconv_parse_layout_section(const psiconv_buffer buf, 401int psiconv_parse_layout_section(const psiconv_config config,
402 const psiconv_buffer buf,
283 int lev,psiconv_u32 off, 403 int lev,psiconv_u32 off,
284 int *length, 404 int *length,
285 psiconv_text_and_layout result, 405 psiconv_text_and_layout result,
286 psiconv_word_styles_section styles, 406 psiconv_word_styles_section styles,
287 int with_styles) 407 int with_styles)
289 int res = 0; 409 int res = 0;
290 int len = 0; 410 int len = 0;
291 psiconv_u32 temp; 411 psiconv_u32 temp;
292 int parse_styles,nr,i,j,total,leng,line_length; 412 int parse_styles,nr,i,j,total,leng,line_length;
293 413
294 typedef struct anon_style 414 typedef struct anon_style_s
295 { 415 {
296 int nr; 416 int nr;
297 psiconv_s16 base_style; 417 psiconv_s16 base_style;
298 psiconv_character_layout character; 418 psiconv_character_layout character;
299 psiconv_paragraph_layout paragraph; 419 psiconv_paragraph_layout paragraph;
300 } *anon_style; 420 } *anon_style;
301 421
302 typedef psiconv_list anon_style_list; /* of struct anon_style */ 422 typedef psiconv_list anon_style_list; /* of struct anon_style */
303 423
304 anon_style_list anon_styles; 424 anon_style_list anon_styles;
305 struct anon_style anon; 425 struct anon_style_s anon;
306 anon_style anon_ptr=NULL; 426 anon_style anon_ptr=NULL;
307 427
428 psiconv_character_layout temp_char;
429 psiconv_paragraph_layout temp_para;
430 psiconv_word_style temp_style;
308 psiconv_paragraph para; 431 psiconv_paragraph para;
309 struct psiconv_in_line_layout in_line; 432 struct psiconv_in_line_layout_s in_line;
310 433
311 int *inline_count; 434 int *inline_count;
312 435
313 436
314 psiconv_progress(lev+1,off,"Going to read the layout section"); 437 psiconv_progress(config,lev+1,off,"Going to read the layout section");
315 438
316 psiconv_progress(lev+2,off,"Going to read the section type"); 439 psiconv_progress(config,lev+2,off,"Going to read the section type");
317 temp = psiconv_read_u16(buf,lev+2,off+len); 440 temp = psiconv_read_u16(config,buf,lev+2,off+len,&res);
441 if (res)
442 goto ERROR1;
318 psiconv_debug(lev+2,off+len,"Type: %02x",temp); 443 psiconv_debug(config,lev+2,off+len,"Type: %02x",temp);
319 parse_styles = with_styles; 444 parse_styles = with_styles;
320 if ((temp == 0x0001) && !with_styles) { 445 if ((temp == 0x0001) && !with_styles) {
321 psiconv_warn(lev+2,off+len,"Styleless layout section expected, " 446 psiconv_warn(config,lev+2,off+len,"Styleless layout section expected, "
322 "but styled section found!"); 447 "but styled section found!");
323 parse_styles = 1; 448 parse_styles = 1;
324 res = -1;
325 } else if ((temp == 0x0000) && (with_styles)) { 449 } else if ((temp == 0x0000) && (with_styles)) {
326 psiconv_warn(lev+2,off+len,"Styled layout section expected, " 450 psiconv_warn(config,lev+2,off+len,"Styled layout section expected, "
327 "but styleless section found!"); 451 "but styleless section found!");
328 parse_styles = 0; 452 parse_styles = 0;
329 res = -1;
330 } else if ((temp != 0x0000) && (temp != 0x0001)) { 453 } else if ((temp != 0x0000) && (temp != 0x0001)) {
331 psiconv_warn(lev+2,off+len, 454 psiconv_warn(config,lev+2,off+len,
332 "Layout section type indicator has unknown value!"); 455 "Layout section type indicator has unknown value!");
333 res = -1;
334 } 456 }
335 len += 0x02; 457 len += 0x02;
336 458
337 psiconv_progress(lev+2,off+len,"Going to read paragraph type list"); 459 psiconv_progress(config,lev+2,off+len,"Going to read paragraph type list");
338 anon_styles = psiconv_list_new(sizeof(anon)); 460 if (!(anon_styles = psiconv_list_new(sizeof(anon))))
461 goto ERROR1;
339 psiconv_progress(lev+3,off+len,"Going to read paragraph type list length"); 462 psiconv_progress(config,lev+3,off+len,"Going to read paragraph type list length");
340 nr = psiconv_read_u8(buf,lev+3,off+len); 463 nr = psiconv_read_u8(config,buf,lev+3,off+len,&res);
464 if (res)
465 goto ERROR2;
341 psiconv_debug(lev+3,off+len,"Length: %02x",nr); 466 psiconv_debug(config,lev+3,off+len,"Length: %02x",nr);
342 len ++; 467 len ++;
343 468
344 psiconv_progress(lev+3,off+len, 469 psiconv_progress(config,lev+3,off+len,
345 "Going to read the paragraph type list elements"); 470 "Going to read the paragraph type list elements");
346 for (i = 0; i < nr; i ++) { 471 for (i = 0; i < nr; i ++) {
347 psiconv_progress(lev+3,off+len,"Element %d",i); 472 psiconv_progress(config,lev+3,off+len,"Element %d",i);
348 anon.nr = psiconv_read_u32(buf,lev+4,off+len); 473 anon.nr = psiconv_read_u32(config,buf,lev+4,off+len,&res);
474 if (res)
475 goto ERROR3;
349 psiconv_debug(lev+4,off+len,"Number: %08x",anon.nr); 476 psiconv_debug(config,lev+4,off+len,"Number: %08x",anon.nr);
350 len += 0x04; 477 len += 0x04;
351 478
352 psiconv_progress(lev+4,off,"Going to determine the base style"); 479 psiconv_progress(config,lev+4,off,"Going to determine the base style");
353 if (parse_styles) { 480 if (parse_styles) {
481 temp = psiconv_read_u32(config,buf,lev+4, off+len,&res);
482 if (res)
483 goto ERROR3;
354 anon.base_style = psiconv_read_u8(buf,lev+3, 484 anon.base_style = psiconv_read_u8(config,buf,lev+3, off+len+4+temp,&res);
355 off+len+4+psiconv_read_u32(buf,lev+4, 485 if (res)
356 off+len)); 486 goto ERROR3;
357 psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len), 487 psiconv_debug(config,lev+4,off+len+temp,
358 "Style indicator: %02x",anon.base_style); 488 "Style indicator: %02x",anon.base_style);
359 } else 489 } else
360 anon.base_style = 0; 490 anon.base_style = 0;
491 if (!(temp_style = psiconv_get_style(styles,anon.base_style))) {
492 psiconv_warn(config,lev+4,off,"Unknown Style referenced");
493 if (!(temp_style = psiconv_get_style(styles,anon.base_style))) {
494 psiconv_warn(config,lev+4,off,"Base style unknown");
495 goto ERROR3;
496 }
497 }
361 anon.paragraph = psiconv_clone_paragraph_layout(psiconv_get_style 498 if (!(anon.paragraph = psiconv_clone_paragraph_layout
362 (styles,anon.base_style)->paragraph); 499 (temp_style->paragraph)))
500 goto ERROR3;
363 anon.character = psiconv_clone_character_layout(psiconv_get_style 501 if (!(anon.character = psiconv_clone_character_layout
364 (styles,anon.base_style)->character); 502 (temp_style->character)))
503 goto ERROR3_1;
365 504
366 psiconv_progress(lev+4,off+len,"Going to read the paragraph layout"); 505 psiconv_progress(config,lev+4,off+len,"Going to read the paragraph layout");
367 res |= psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, 506 if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+4,off+len,&leng,
368 anon.paragraph); 507 anon.paragraph)))
508 goto ERROR3_2;
369 len += leng; 509 len += leng;
370 if (parse_styles) 510 if (parse_styles)
371 len ++; 511 len ++;
372 512
373 psiconv_progress(lev+4,off+len,"Going to read the character layout"); 513 psiconv_progress(config,lev+4,off+len,"Going to read the character layout");
374 res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, 514 if ((res = psiconv_parse_character_layout_list(config,buf,lev+4,off+len,&leng,
375 anon.character); 515 anon.character)))
516 goto ERROR3_2;
376 len += leng; 517 len += leng;
377 psiconv_list_add(anon_styles,&anon); 518 if ((res = psiconv_list_add(anon_styles,&anon)))
519 goto ERROR3_2;
378 } 520 }
379 521
380 psiconv_progress(lev+2,off+len,"Going to parse the paragraph element list"); 522 psiconv_progress(config,lev+2,off+len,"Going to parse the paragraph element list");
381 psiconv_progress(lev+3,off+len,"Going to read the number of paragraphs"); 523 psiconv_progress(config,lev+3,off+len,"Going to read the number of paragraphs");
382 nr = psiconv_read_u32(buf,lev+3,off+len); 524 nr = psiconv_read_u32(config,buf,lev+3,off+len,&res);
525 if (res)
526 goto ERROR3;
383 if (nr != psiconv_list_length(result)) { 527 if (nr != psiconv_list_length(result)) {
384 psiconv_warn(lev+3,off+len, 528 psiconv_warn(config,lev+3,off+len,
385 "Number of text paragraphs and paragraph elements does not match"); 529 "Number of text paragraphs and paragraph elements does not match");
386 psiconv_debug(lev+3,off+len, 530 psiconv_debug(config,lev+3,off+len,
387 "%d text paragraphs, %d paragraph elements", 531 "%d text paragraphs, %d paragraph elements",
388 psiconv_list_length(result),nr); 532 psiconv_list_length(result),nr);
389 } 533 }
390 psiconv_debug(lev+3,off+len,"Number of paragraphs: %d",nr); 534 psiconv_debug(config,lev+3,off+len,"Number of paragraphs: %d",nr);
391 len += 4; 535 len += 4;
392 inline_count = malloc(nr * sizeof(*inline_count)); 536 if (!(inline_count = malloc(nr * sizeof(*inline_count))))
537 goto ERROR3;
393 538
394 psiconv_progress(lev+3,off+len,"Going to read the paragraph elements"); 539 psiconv_progress(config,lev+3,off+len,"Going to read the paragraph elements");
395 for (i = 0; i < nr; i ++) { 540 for (i = 0; i < nr; i ++) {
396 psiconv_progress(lev+3,off+len,"Element %d",i); 541 psiconv_progress(config,lev+3,off+len,"Element %d",i);
397 if (i >= psiconv_list_length(result)) { 542 if (i >= psiconv_list_length(result)) {
398 psiconv_debug(lev+4,off+len,"Going to allocate a new element"); 543 psiconv_debug(config,lev+4,off+len,"Going to allocate a new element");
399 para = malloc(sizeof(*para)); 544 if (!(para = malloc(sizeof(*para))))
400 para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout)); 545 goto ERROR4;
546 if (!(para->in_lines = psiconv_list_new(sizeof(
547 struct psiconv_in_line_layout_s))))
548 goto ERROR4_1;
401 para->base_style = 0; 549 para->base_style = 0;
402 para->base_character = psiconv_basic_character_layout(); 550 if (!(para->base_character = psiconv_basic_character_layout()))
551 goto ERROR4_2;
403 para->base_paragraph = psiconv_basic_paragraph_layout(); 552 if (!(para->base_paragraph = psiconv_basic_paragraph_layout()))
553 goto ERROR4_3;
554 if ((res = psiconv_list_add(result,para)))
555 goto ERROR4_4;
404 free(para); 556 free(para);
405 } 557 }
406 para = psiconv_list_get(result,i); 558 if (!(para = psiconv_list_get(result,i)))
559 goto ERROR4;
407 560
408 psiconv_progress(lev+4,off+len,"Going to read the paragraph length"); 561 psiconv_progress(config,lev+4,off+len,"Going to read the paragraph length");
409 temp = psiconv_read_u32(buf,lev+4,off+len); 562 temp = psiconv_read_u32(config,buf,lev+4,off+len,&res);
563 if (res)
564 goto ERROR4;
410 if (temp != strlen(para->text)+1) { 565 if (temp != psiconv_unicode_strlen(para->text)+1) {
411 res = -1;
412 psiconv_warn(lev+4,off+len, 566 psiconv_warn(config,lev+4,off+len,
413 "Disagreement of the length of paragraph in layout section"); 567 "Disagreement of the length of paragraph in layout section");
414 psiconv_debug(lev+4,off+len, 568 psiconv_debug(config,lev+4,off+len,
415 "Paragraph length: layout section says %d, counted %d", 569 "Paragraph length: layout section says %d, counted %d",
416 temp,strlen(para->text)+1); 570 temp,psiconv_unicode_strlen(para->text)+1);
417 } else 571 } else
418 psiconv_debug(lev+4,off+len,"Paragraph length: %d",temp); 572 psiconv_debug(config,lev+4,off+len,"Paragraph length: %d",temp);
419 len += 4; 573 len += 4;
420 574
421 psiconv_progress(lev+4,off+len,"Going to read the paragraph type"); 575 psiconv_progress(config,lev+4,off+len,"Going to read the paragraph type");
422 temp = psiconv_read_u8(buf,lev+4,off+len); 576 temp = psiconv_read_u8(config,buf,lev+4,off+len,&res);
577 if (res)
578 goto ERROR4;
423 if (temp != 0x00) { 579 if (temp != 0x00) {
424 psiconv_debug(lev+4,off+len,"Type: %02x",temp); 580 psiconv_debug(config,lev+4,off+len,"Type: %02x",temp);
425 for (j = 0; j < psiconv_list_length(anon_styles); j++) { 581 for (j = 0; j < psiconv_list_length(anon_styles); j++) {
426 anon_ptr = psiconv_list_get(anon_styles,j); 582 if (!(anon_ptr = psiconv_list_get(anon_styles,j))) {
583 psiconv_error(config,lev+4,off+len,"Data structure corruption");
584 goto ERROR4;
585 }
427 if (temp == anon_ptr->nr) 586 if (temp == anon_ptr->nr)
428 break; 587 break;
429 } 588 }
430 if (j == psiconv_list_length(anon_styles)) { 589 if (j == psiconv_list_length(anon_styles)) {
431 psiconv_warn(lev+4,off+len,"Layout section paragraph type unknown"); 590 psiconv_warn(config,lev+4,off+len,"Layout section paragraph type unknown");
432 psiconv_debug(lev+4,off+len,"Unknown type - using base styles instead"); 591 psiconv_debug(config,lev+4,off+len,"Unknown type - using base styles instead");
433 para->base_style = 0; 592 para->base_style = 0;
593 if (!(temp_style = psiconv_get_style(styles,0))) {
594 psiconv_error(config,lev+4,off,"Base style unknown");
595 goto ERROR4;
596 }
597 if (!(temp_para = psiconv_clone_paragraph_layout
598 (temp_style->paragraph)))
599 goto ERROR4;
434 psiconv_free_paragraph_layout(para->base_paragraph); 600 psiconv_free_paragraph_layout(para->base_paragraph);
601 para->base_paragraph = temp_para;
602
603 if (!(temp_char = psiconv_clone_character_layout
604 (temp_style->character)))
605 goto ERROR4;
435 psiconv_free_character_layout(para->base_character); 606 psiconv_free_character_layout(para->base_character);
436 para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style 607 para->base_character = temp_char;
437 (styles,0)->paragraph);
438 para->base_character = psiconv_clone_character_layout(psiconv_get_style
439 (styles,0)->character);
440 } else { 608 } else {
441 para->base_style = anon_ptr->base_style; 609 para->base_style = anon_ptr->base_style;
610 if (!(temp_para = psiconv_clone_paragraph_layout (anon_ptr->paragraph)))
611 goto ERROR4;
442 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 (anon_ptr->character)))
616 goto ERROR4;
443 psiconv_free_character_layout(para->base_character); 617 psiconv_free_character_layout(para->base_character);
444 para->base_paragraph = psiconv_clone_paragraph_layout 618 para->base_character = temp_char;
445 (anon_ptr->paragraph);
446 para->base_character = psiconv_clone_character_layout
447 (anon_ptr->character);
448 } 619 }
449 inline_count[i] = 0; 620 inline_count[i] = 0;
450 len += 0x01; 621 len += 0x01;
451 } else { 622 } else {
452 psiconv_debug(lev+4,off+len,"Type: %02x (not based on a paragraph type)" 623 psiconv_debug(config,lev+4,off+len,"Type: %02x (not based on a paragraph type)"
453 ,temp); 624 ,temp);
454 len += 0x01; 625 len += 0x01;
455 if (parse_styles) { 626 if (parse_styles) {
456 psiconv_progress(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len), 627 temp = psiconv_read_u32(config,buf,lev+4,off+len,&res);
628 if (res)
629 goto ERROR4;
630 psiconv_progress(config,lev+4,off+len+temp+4,
457 "Going to read the paragraph element base style"); 631 "Going to read the paragraph element base style");
458 temp = psiconv_read_u8(buf,lev+4, 632 temp = psiconv_read_u8(config,buf,lev+4, off+len+temp+4,&res);
459 off+len+psiconv_read_u32(buf,lev+4,off+len)); 633 if (res)
460 psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len), 634 goto ERROR4;
461 "Style: %02x",temp); 635 psiconv_debug(config,lev+4,off+len+temp+4, "Style: %02x",temp);
462 } else 636 } else
463 temp = 0x00; 637 temp = 0x00;
638
639 if (!(temp_style = psiconv_get_style (styles,temp))) {
640 psiconv_warn(config,lev+4,off,"Unknown Style referenced");
641 if (!(temp_style = psiconv_get_style(styles,0))) {
642 psiconv_error(config,lev+4,off,"Base style unknown");
643 goto ERROR4;
644 }
645 }
646
647 if (!(temp_para = psiconv_clone_paragraph_layout(temp_style->paragraph)))
648 goto ERROR4;
464 psiconv_free_paragraph_layout(para->base_paragraph); 649 psiconv_free_paragraph_layout(para->base_paragraph);
650 para->base_paragraph = temp_para;
651
652 if (!(temp_char = psiconv_clone_character_layout(temp_style->character)))
653 goto ERROR4;
465 psiconv_free_character_layout(para->base_character); 654 psiconv_free_character_layout(para->base_character);
466 para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style 655 para->base_character = temp_char;
467 (styles,temp)->paragraph); 656
468 para->base_character = psiconv_clone_character_layout(psiconv_get_style
469 (styles,temp)->character);
470 para->base_style = temp; 657 para->base_style = temp;
471 psiconv_progress(lev+4,off+len,"Going to read paragraph layout"); 658 psiconv_progress(config,lev+4,off+len,"Going to read paragraph layout");
472 psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng, 659 if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+4,off+len,&leng,
473 para->base_paragraph); 660 para->base_paragraph)))
661 goto ERROR4;
474 len += leng; 662 len += leng;
475 if (parse_styles) 663 if (parse_styles)
476 len += 1; 664 len += 1;
477 psiconv_progress(lev+4,off+len,"Going to read number of in-line " 665 psiconv_progress(config,lev+4,off+len,"Going to read number of in-line "
478 "layout elements"); 666 "layout elements");
479 inline_count[i] = psiconv_read_u32(buf,lev+4,off+len); 667 inline_count[i] = psiconv_read_u32(config,buf,lev+4,off+len,&res);
668 if (res)
669 goto ERROR4;
480 psiconv_debug(lev+4,off+len,"Nr: %08x",inline_count[i]); 670 psiconv_debug(config,lev+4,off+len,"Nr: %08x",inline_count[i]);
481 len += 4; 671 len += 4;
482 } 672 }
483 } 673 }
484 674
485 psiconv_progress(lev+2,off+len,"Going to read the text layout inline list"); 675 psiconv_progress(config,lev+2,off+len,"Going to read the text layout inline list");
486 676
487 psiconv_progress(lev+3,off+len,"Going to read the number of elements"); 677 psiconv_progress(config,lev+3,off+len,"Going to read the number of elements");
488 nr = psiconv_read_u32(buf,lev+3,off+len); 678 nr = psiconv_read_u32(config,buf,lev+3,off+len,&res);
679 if (res)
680 goto ERROR4;
489 psiconv_debug(lev+3,off,"Elements: %08x",nr); 681 psiconv_debug(config,lev+3,off+len,"Elements: %08x",nr);
490 len += 0x04; 682 len += 0x04;
491 683
492 psiconv_progress(lev+3,off+len, 684 psiconv_progress(config,lev+3,off+len,
493 "Going to read the text layout inline elements"); 685 "Going to read the text layout inline elements");
494 total = 0; 686 total = 0;
495 for (i = 0; i < psiconv_list_length(result); i++) { 687 for (i = 0; i < psiconv_list_length(result); i++) {
496 para = psiconv_list_get(result,i); 688 if (!(para = psiconv_list_get(result,i))) {
689 psiconv_error(config,lev+3,off+len,"Data structure corruption");
690 goto ERROR4;
691 }
497 line_length = -1; 692 line_length = -1;
498 for (j = 0; j < inline_count[i]; j++) { 693 for (j = 0; j < inline_count[i]; j++) {
499 psiconv_progress(lev+3,off+len,"Element %d: Paragraph %d, element %d", 694 psiconv_progress(config,lev+3,off+len,"Element %d: Paragraph %d, element %d",
500 total,i,j); 695 total,i,j);
501 if (total >= nr) { 696 if (total >= nr) {
502 psiconv_warn(lev+3,off+len, 697 psiconv_warn(config,lev+3,off+len,
503 "Layout section inlines: not enough element"); 698 "Layout section inlines: not enough element");
504 res = -1;
505 psiconv_debug(lev+3,off+len,"Can't read element!"); 699 psiconv_debug(config,lev+3,off+len,"Can't read element!");
506 } else { 700 } else {
507 total ++; 701 total ++;
702 in_line.object = NULL;
703 in_line.layout = NULL;
508 in_line.layout = psiconv_clone_character_layout(para->base_character); 704 if (!(in_line.layout = psiconv_clone_character_layout
705 (para->base_character)))
706 goto ERROR4;
509 psiconv_progress(lev+4,off+len,"Going to read the element type"); 707 psiconv_progress(config,lev+4,off+len,"Going to read the element type");
510 temp = psiconv_read_u8(buf,lev+4,len+off); 708 temp = psiconv_read_u8(config,buf,lev+4,len+off,&res);
709 if (res)
710 goto ERROR5;
511 len += 1; 711 len += 1;
512 psiconv_debug(lev+4,off,"Type: %02x",temp); 712 psiconv_debug(config,lev+4,off+len,"Type: %02x",temp);
513 psiconv_progress(lev+4,off, 713 psiconv_progress(config,lev+4,off+len,
514 "Going to read the number of characters it applies to"); 714 "Going to read the number of characters it applies to");
515 in_line.length = psiconv_read_u32(buf,lev+4,len+off); 715 in_line.length = psiconv_read_u32(config,buf,lev+4,len+off,&res);
716 if (res)
717 goto ERROR5;
516 psiconv_debug(lev+4,off+len,"Length: %02x",in_line.length); 718 psiconv_debug(config,lev+4,off+len,"Length: %02x",in_line.length);
517 len += 4; 719 len += 4;
518 psiconv_progress(lev+4,off+len,"Going to read the character layout"); 720 psiconv_progress(config,lev+4,off+len,"Going to read the character layout");
519 res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng, 721 if ((res = psiconv_parse_character_layout_list(config,buf,lev+4,off+len,&leng,
520 in_line.layout); 722 in_line.layout)))
723 goto ERROR5;
521 len += leng; 724 len += leng;
522 725
523 if (temp == 0x01) { 726 if (temp == 0x01) {
524 psiconv_debug(lev+4,off+len,"Skipping object data"); 727 psiconv_debug(config,lev+4,off+len,"Found an embedded object");
525 len += 0x10; 728 psiconv_progress(config,lev+4,off+len,"Going to read the object marker "
729 "(0x%08x expected)",PSICONV_ID_OBJECT);
730 temp = psiconv_read_u32(config,buf,lev+4,off+len,&res);
731 if (res)
732 goto ERROR5;
733 if (temp != PSICONV_ID_OBJECT) {
734 psiconv_warn(config,lev+4,off+len,"Unknown id marks embedded object");
735 psiconv_debug(config,lev+4,off+len,"Marker: read %08x, expected %08x",
736 temp,PSICONV_ID_OBJECT);
737 }
738 len += 4;
739 psiconv_progress(config,lev+4,off+len,
740 "Going to read the Embedded Object Section offset");
741 temp = psiconv_read_u32(config,buf,lev+4,off+len,&res);
742 if (res)
743 goto ERROR5;
744 psiconv_debug(config,lev+4,off+len, "Offset: %08x",temp);
745 len += 4;
746 psiconv_progress(config,lev+4,off+len,
747 "Going to parse the Embedded Object Section");
748 if ((res = psiconv_parse_embedded_object_section(config,buf,lev+4,temp,
749 NULL,&(in_line.object))))
750 goto ERROR5;
751 psiconv_progress(config,lev+4,off+len,
752 "Going to read the object width");
753 in_line.object_width = psiconv_read_length(config,buf,lev+4,off+len,
754 &leng,&res);
755 if (res)
756 goto ERROR5;
757 psiconv_debug(config,lev+4,off+len,"Object width: %f cm",
758 in_line.object_width);
759 len += leng;
760 psiconv_progress(config,lev+4,off+len,
761 "Going to read the object height");
762 in_line.object_height = psiconv_read_length(config,buf,lev+4,off+len,&leng,
763 &res);
764 if (res)
765 goto ERROR5;
766 psiconv_debug(config,lev+4,off+len,"Object height: %f cm",
767 in_line.object_height);
768 len += leng;
526 } else if (temp != 0x00) { 769 } else if (temp != 0x00) {
527 psiconv_warn(lev+4,off+len,"Layout section unknown inline type"); 770 psiconv_warn(config,lev+4,off+len,"Layout section unknown inline type");
528 res = -1;
529 } 771 }
530 if (line_length + in_line.length > strlen(para->text)) { 772 if (line_length + in_line.length > psiconv_unicode_strlen(para->text)) {
531 psiconv_warn(lev+4,off+len, 773 psiconv_warn(config,lev+4,off+len,
532 "Layout section inlines: line length mismatch"); 774 "Layout section inlines: line length mismatch");
533 res = -1; 775 res = -1;
534 in_line.length = strlen(para->text) - line_length; 776 in_line.length = psiconv_unicode_strlen(para->text) - line_length;
535 } 777 }
536 line_length += in_line.length; 778 line_length += in_line.length;
537 psiconv_list_add(para->in_lines,&in_line); 779 if ((res = psiconv_list_add(para->in_lines,&in_line)))
780 goto ERROR5;
538 } 781 }
539 } 782 }
540 } 783 }
541 784
542 if (total != nr) { 785 if (total != nr) {
543 psiconv_warn(lev+4,off+len, 786 psiconv_warn(config,lev+4,off+len,
544 "Layout section too many inlines, skipping remaining"); 787 "Layout section too many inlines, skipping remaining");
545 } 788 }
546 789
547 free(inline_count); 790 free(inline_count);
548 791
549 for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { 792 for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) {
550 anon_ptr = psiconv_list_get(anon_styles,i); 793 if (!(anon_ptr = psiconv_list_get(anon_styles,i))) {
794 psiconv_error(config,lev+4,off+len,"Data structure corruption");
795 goto ERROR2;
796 }
551 psiconv_free_character_layout(anon_ptr->character); 797 psiconv_free_character_layout(anon_ptr->character);
552 psiconv_free_paragraph_layout(anon_ptr->paragraph); 798 psiconv_free_paragraph_layout(anon_ptr->paragraph);
553 } 799 }
554 psiconv_list_free(anon_styles); 800 psiconv_list_free(anon_styles);
555 801
556 if (length) 802 if (length)
557 *length = len; 803 *length = len;
558 804
559 psiconv_progress(lev+1,off+len-1,"End of layout section (total length: %08x", 805 psiconv_progress(config,lev+1,off+len-1,"End of layout section (total length: %08x)",
560 len); 806 len);
561 807
808 return 0;
809
810ERROR4_4:
811 psiconv_free_paragraph_layout(para->base_paragraph);
812ERROR4_3:
813 psiconv_free_character_layout(para->base_character);
814ERROR4_2:
815 psiconv_list_free(para->in_lines);
816ERROR4_1:
817 free(para);
818 goto ERROR4;
819
820ERROR3_2:
821 psiconv_free_character_layout(anon.character);
822ERROR3_1:
823 psiconv_free_paragraph_layout(anon.paragraph);
824 goto ERROR3;
825
826ERROR5:
827 if (in_line.layout)
828 psiconv_free_character_layout(in_line.layout);
829 if (in_line.object)
830 psiconv_free_embedded_object_section(in_line.object);
831ERROR4:
832 free(inline_count);
833ERROR3:
834 for (i = 0; i < psiconv_list_length(anon_styles); i++) {
835 if (!(anon_ptr = psiconv_list_get(anon_styles,i))) {
836 psiconv_error(config,lev+1,off,"Data structure corruption");
837 break;
838 }
839 psiconv_free_paragraph_layout(anon_ptr->paragraph);
840 psiconv_free_character_layout(anon_ptr->character);
841 }
842
843ERROR2:
844 psiconv_list_free(anon_styles);
845ERROR1:
846 psiconv_error(config,lev+1,off,"Reading of Layout Section failed");
847 if (length)
848 *length = 0;
849 if (!res)
850 return -PSICONV_E_NOMEM;
851 else
562 return res; 852 return res;
563} 853}
564 854
565int psiconv_parse_styled_layout_section(const psiconv_buffer buf, 855int psiconv_parse_styled_layout_section(const psiconv_config config,
856 const psiconv_buffer buf,
566 int lev,psiconv_u32 off, 857 int lev,psiconv_u32 off,
567 int *length, 858 int *length,
568 psiconv_text_and_layout result, 859 psiconv_text_and_layout result,
569 psiconv_word_styles_section styles) 860 psiconv_word_styles_section styles)
570{ 861{
571 return psiconv_parse_layout_section(buf,lev,off,length,result,styles,1); 862 return psiconv_parse_layout_section(config,buf,lev,off,length,result,styles,1);
572} 863}
573 864
574int psiconv_parse_styleless_layout_section(const psiconv_buffer buf, 865int psiconv_parse_styleless_layout_section(const psiconv_config config,
866 const psiconv_buffer buf,
575 int lev,psiconv_u32 off, 867 int lev,psiconv_u32 off,
576 int *length, 868 int *length,
577 psiconv_text_and_layout result, 869 psiconv_text_and_layout result,
578 psiconv_character_layout base_char, 870 const psiconv_character_layout base_char,
579 psiconv_paragraph_layout base_para) 871 const psiconv_paragraph_layout base_para)
580{ 872{
581 int res; 873 int res = 0;
582 psiconv_word_styles_section styles_section; 874 psiconv_word_styles_section styles_section;
583 875
584 styles_section = malloc(sizeof(*styles_section)); 876 if (!(styles_section = malloc(sizeof(*styles_section))))
877 goto ERROR1;
585 styles_section->normal = malloc(sizeof(*styles_section->normal)); 878 if (!(styles_section->normal = malloc(sizeof(*styles_section->normal))))
586 styles_section->normal->character = psiconv_clone_character_layout(base_char); 879 goto ERROR2;
587 styles_section->normal->paragraph = psiconv_clone_paragraph_layout(base_para); 880 if (!(styles_section->normal->character =
881 psiconv_clone_character_layout(base_char)))
882 goto ERROR3;
883 if (!(styles_section->normal->paragraph =
884 psiconv_clone_paragraph_layout(base_para)))
885 goto ERROR4;
588 styles_section->normal->hotkey = 0; 886 styles_section->normal->hotkey = 0;
589 styles_section->normal->name = strdup(""); 887
888 if (!(styles_section->normal->name = psiconv_unicode_empty_string()))
889 goto ERROR5;
590 styles_section->styles = psiconv_list_new(sizeof(struct psiconv_word_style)); 890 if (!(styles_section->styles = psiconv_list_new(sizeof(
891 struct psiconv_word_style_s))))
892 goto ERROR6;
591 893
592 res = psiconv_parse_layout_section(buf,lev,off,length,result, 894 res = psiconv_parse_layout_section(config,buf,lev,off,length,result,
593 styles_section,0); 895 styles_section,0);
594 896
595 psiconv_free_word_styles_section(styles_section); 897 psiconv_free_word_styles_section(styles_section);
596 return res; 898 return res;
899
900ERROR6:
901 free(styles_section->normal->name);
902ERROR5:
903 psiconv_free_paragraph_layout(styles_section->normal->paragraph);
904ERROR4:
905 psiconv_free_character_layout(styles_section->normal->character);
906ERROR3:
907 free(styles_section->normal);
908ERROR2:
909 free(styles_section);
910ERROR1:
911 psiconv_error(config,lev+1,off,"Reading of Styleless Layout Section failed");
912 if (length)
913 *length = 0;
914 if (!res)
915 return -PSICONV_E_NOMEM;
916 else
917 return res;
597} 918}
598 919
920int psiconv_parse_embedded_object_section(const psiconv_config config,
921 const psiconv_buffer buf, int lev,
922 psiconv_u32 off, int *length,
923 psiconv_embedded_object_section *result)
924{
925 int res=0;
926 int len=0;
927 int leng,i;
928 psiconv_section_table_section table;
929 psiconv_section_table_entry entry;
930 psiconv_u32 icon_sec=0,display_sec=0,table_sec=0;
931 psiconv_buffer subbuf;
932
933 psiconv_progress(config,lev+1,off+len,"Going to read an Embedded Object");
934 if (!(*result = malloc(sizeof(**result))))
935 goto ERROR1;
936
937 psiconv_progress(config,lev+2,off+len,"Going to read the Embedded Object Section");
938 psiconv_parse_section_table_section(config,buf,lev+2,off+len,&leng,&table);
939 len += leng;
940
941 for (i = 0; i < psiconv_list_length(table); i++) {
942 psiconv_progress(config,lev+2,off+len,"Going to read entry %d",i);
943 if (!(entry = psiconv_list_get(table,i)))
944 goto ERROR2;
945 if (entry->id == PSICONV_ID_OBJECT_DISPLAY_SECTION) {
946 display_sec = entry->offset;
947 psiconv_debug(config,lev+3,off+len,"Found the Object Display Section at %08x",
948 display_sec);
949 } else if (entry->id == PSICONV_ID_OBJECT_ICON_SECTION) {
950 icon_sec = entry->offset;
951 psiconv_debug(config,lev+3,off+len,"Found the Object Icon Section at %08x",
952 icon_sec);
953 } else if (entry->id == PSICONV_ID_OBJECT_SECTION_TABLE_SECTION) {
954 table_sec = entry->offset;
955 psiconv_debug(config,lev+3,off+len,"Found the Object Section Table Section at %08x",
956 table_sec);
957 } else {
958 psiconv_warn(config,lev+3,off+len,
959 "Found unknown section in the Object Display Section "
960 "(ignoring)");
961 psiconv_debug(config,lev+3,off+len,"Section ID %08x, offset %08x",
962 entry->id, entry->offset);
963 }
964 }
965
966 psiconv_progress(config,lev+2,off+len,"Looking for the Object Display Section");
967 if (!icon_sec) {
968 psiconv_warn(config,lev+2,off+len,"Object Display Section not found");
969 (*result)->display = NULL;
970 } else {
971 psiconv_debug(config,lev+2,off+len,"Object Display Section at offset %08x",
972 display_sec);
973 if ((res = psiconv_parse_object_display_section(config,buf,lev+2,display_sec,NULL,
974 &(*result)->display)))
975 goto ERROR2;
976 }
977
978 psiconv_progress(config,lev+2,off+len,"Looking for the Object Icon Section");
979 if (!icon_sec) {
980 psiconv_warn(config,lev+2,off+len,"Object Icon Section not found");
981 (*result)->icon = NULL;
982 } else {
983 psiconv_debug(config,lev+2,off+len,"Object Icon Section at offset %08x",icon_sec);
984 if ((res = psiconv_parse_object_icon_section(config,buf,lev+2,icon_sec,NULL,
985 &(*result)->icon)))
986 goto ERROR3;
987 }
988
989 psiconv_progress(config,lev+2,off+len,
990 "Looking for the Section Table Offset Section");
991 if (!table_sec) {
992 psiconv_warn(config,lev+2,off+len,
993 "Embedded Section Table Offset Section not found");
994 (*result)->object = NULL;
995 } else {
996 psiconv_progress(config,lev+2,off+len,
997 "Extracting object: add %08x to all following offsets",
998 table_sec);
999 /* We can't determine the length of the object, so we just take it all */
1000 if ((res = psiconv_buffer_subbuffer(&subbuf,buf,table_sec,
1001 psiconv_buffer_length(buf)-table_sec)))
1002 goto ERROR4;
1003
1004 if (!((*result)->object = malloc(sizeof(*(*result)->object))))
1005 goto ERROR5;
1006
1007 /* We need to find the file type, but we don't have a normal header */
1008 /* So we try to find the Application ID Section and hope for the best */
1009 psiconv_progress(config,lev+3,0,"Trying to determine the file type");
1010 (*result)->object->type = psiconv_determine_embedded_object_type
1011 (config,subbuf,lev+3,&res);
1012 switch ((*result)->object->type) {
1013 case psiconv_word_file:
1014 if ((res = psiconv_parse_word_file(config,subbuf,lev+3,0,
1015 ((psiconv_word_f *) &(*result)->object->file))))
1016 goto ERROR6;
1017 break;
1018 case psiconv_texted_file:
1019 if ((res = psiconv_parse_texted_file(config,subbuf,lev+3,0,
1020 ((psiconv_texted_f *) &(*result)->object->file))))
1021 goto ERROR6;
1022 break;
1023 case psiconv_sheet_file:
1024 if ((res = psiconv_parse_sheet_file(config,subbuf,lev+3,0,
1025 ((psiconv_sheet_f *) &(*result)->object->file))))
1026 goto ERROR6;
1027 break;
1028 case psiconv_sketch_file:
1029 if ((res = psiconv_parse_sketch_file(config,subbuf,lev+3,0,
1030 ((psiconv_sketch_f *) &(*result)->object->file))))
1031 goto ERROR6;
1032 break;
1033 default:
1034 psiconv_warn(config,lev+3,0,"Can't parse embedded object (still continuing)");
1035 (*result)->object->file = NULL;
1036 }
1037 }
1038
1039 psiconv_buffer_free(subbuf);
1040 psiconv_free_section_table_section(table);
1041
1042 if (length)
1043 *length = len;
1044
1045 psiconv_progress(config,lev+1,off+len-1,"End of Embedded Object Section "
1046 "(total length: %08x)",len);
1047
1048 return res;
1049
1050
1051ERROR6:
1052 free((*result)->object);
1053ERROR5:
1054 psiconv_buffer_free(subbuf);
1055ERROR4:
1056 psiconv_free_object_icon_section((*result)->icon);
1057ERROR3:
1058 psiconv_free_object_display_section((*result)->display);
1059ERROR2:
1060 psiconv_free_section_table_section(table);
1061ERROR1:
1062 psiconv_error(config,lev+1,off,"Reading Embedded Object failed");
1063
1064 if (length)
1065 *length = 0;
1066
1067 if (res == 0)
1068 return -PSICONV_E_NOMEM;
1069 else
1070 return res;
1071}
1072
1073psiconv_file_type_t psiconv_determine_embedded_object_type
1074 (const psiconv_config config,
1075 const psiconv_buffer buf,int lev,
1076 int *status)
1077{
1078 psiconv_u32 off;
1079 psiconv_section_table_section table;
1080 int res,i;
1081 psiconv_file_type_t file_type = psiconv_unknown_file;
1082 psiconv_section_table_entry entry;
1083 psiconv_application_id_section applid;
1084
1085 psiconv_progress(config,lev+1,0,"Going to determine embedded object file type");
1086 psiconv_progress(config,lev+2,0,"Going to read the Section Table Offset Section");
1087 off = psiconv_read_u32(config,buf,lev,0,&res);
1088 if (res)
1089 goto ERROR1;
1090 psiconv_debug(config,lev+2,0,"Offset: %08x",off);
1091
1092 psiconv_progress(config,lev+2,off,"Going to read the Section Table Section");
1093 if ((res = psiconv_parse_section_table_section(config,buf,lev+2,off,NULL,&table)))
1094 goto ERROR1;
1095
1096 psiconv_progress(config,lev+2,off,"Going to search the Section Table Section "
1097 "for the Application ID Section");
1098 for (i=0; i < psiconv_list_length(table); i++) {
1099 psiconv_progress(config,lev+3,off,"Going to read entry %d",i);
1100 if (!(entry = psiconv_list_get(table,i)))
1101 goto ERROR2;
1102 if (entry->id == PSICONV_ID_APPL_ID_SECTION) {
1103 psiconv_progress(config,lev+3,off,
1104 "Found the Application ID Section at offset %08x",
1105 entry->offset);
1106 off = entry->offset;
1107 break;
1108 }
1109 }
1110 if (i == psiconv_list_length(table)) {
1111 psiconv_error(config,lev+2,off,"No Application ID Section found");
1112 res = PSICONV_E_PARSE;
1113 goto ERROR2;
1114 }
1115
1116 psiconv_progress(config,lev+2,off,"Going to read the Application ID Section");
1117 if ((res = psiconv_parse_application_id_section(config,buf,lev+2,off,NULL,&applid)))
1118 goto ERROR2;
1119
1120 switch (applid->id) {
1121 case PSICONV_ID_WORD: file_type = psiconv_word_file;
1122 psiconv_debug(config,lev+2,off,"Found a Word file");
1123 break;
1124 case PSICONV_ID_TEXTED: file_type = psiconv_texted_file;
1125 psiconv_debug(config,lev+2,off,"Found a TextEd file");
1126 break;
1127 case PSICONV_ID_SKETCH: file_type = psiconv_sketch_file;
1128 psiconv_debug(config,lev+2,off,"Found a Sketch file");
1129 break;
1130 case PSICONV_ID_SHEET: file_type = psiconv_sheet_file;
1131 psiconv_debug(config,lev+2,off,"Found a Sheet file");
1132 break;
1133 default: psiconv_warn(config,lev+2,off,"Found an unknown file type");
1134 psiconv_debug(config,lev+2,off,"Found ID %08x",applid->id);
1135 }
1136
1137ERROR2:
1138 psiconv_free_application_id_section(applid);
1139ERROR1:
1140 psiconv_free_section_table_section(table);
1141 if (status)
1142 *status = res;
1143 return file_type;
1144
1145}
1146
1147int psiconv_parse_object_display_section(const psiconv_config config,
1148 const psiconv_buffer buf,int lev,
1149 psiconv_u32 off, int *length,
1150 psiconv_object_display_section *result)
1151{
1152 int res = 0;
1153 int len = 0;
1154 int leng;
1155 psiconv_u32 temp;
1156
1157 psiconv_progress(config,lev+1,off,"Going to read the Object Display Section");
1158 if (!(*result = malloc(sizeof(**result))))
1159 goto ERROR1;
1160
1161 psiconv_progress(config,lev+2,off+len,"Going to read the display as icon flag "
1162 "(expecting 0x00 or 0x01)");
1163 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1164 if (res)
1165 goto ERROR2;
1166 if (temp == 0x00) {
1167 (*result)->show_icon = psiconv_bool_true;
1168 psiconv_debug(config,lev+2,off+len,"Displayed as icon");
1169 } else if (temp == 0x01) {
1170 (*result)->show_icon = psiconv_bool_false;
1171 psiconv_debug(config,lev+2,off+len,"Displayed as full document");
1172 } else {
1173 psiconv_warn(config,lev+2,off+len,"Unknown Object Display Section Icon Flag");
1174 psiconv_debug(config,lev+2,off+len,"Icon flag found: %02x",temp);
1175 /* Improvise */
1176 (*result)->show_icon = (temp & 0x01?psiconv_bool_false:psiconv_bool_true);
1177 }
1178 len ++;
1179
1180 psiconv_progress(config,lev+2,off+len,"Going to read the display width");
1181 (*result)->width = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
1182 if (res)
1183 goto ERROR2;
1184 psiconv_debug(config,lev+2,off+len,"Display width: %f cm",(*result)->width);
1185 len += leng;
1186
1187 psiconv_progress(config,lev+2,off+len,"Going to read the display height");
1188 (*result)->height = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
1189 if (res)
1190 goto ERROR2;
1191 psiconv_debug(config,lev+2,off+len,"Display length: %f cm",(*result)->height);
1192 len += leng;
1193
1194 psiconv_progress(config,lev+2,off+len,"Going to read unknown long (%08x expected)",
1195 0);
1196 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
1197 if (temp != 0) {
1198 psiconv_warn(config,lev+2,off+len,"Unknown Object Display Section final long");
1199 psiconv_debug(config,lev+2,off+len,"Long read: %08x",temp);
1200 }
1201 len += 4;
1202
1203 if (length)
1204 *length = len;
1205
1206 psiconv_progress(config,lev+1,off+len-1,"End of Object Display Section "
1207 "(total length: %08x",len);
1208 return res;
1209
1210ERROR2:
1211 free(*result);
1212ERROR1:
1213 psiconv_error(config,lev+1,off+len,"Reading of Object Display Section failed");
1214 if (length)
1215 *length=0;
1216 if (!res)
1217 return -PSICONV_E_NOMEM;
1218 else
1219 return res;
1220}
1221
1222int psiconv_parse_object_icon_section(const psiconv_config config,
1223 const psiconv_buffer buf,int lev,
1224 psiconv_u32 off, int *length,
1225 psiconv_object_icon_section *result)
1226{
1227 int res = 0;
1228 int len = 0;
1229 int leng;
1230
1231 psiconv_progress(config,lev+1,off,"Going to read the Object Icon Section");
1232 if (!(*result = malloc(sizeof(**result))))
1233 goto ERROR1;
1234
1235 psiconv_progress(config,lev+2,off+len,"Going to read the icon name");
1236 (*result)->icon_name = psiconv_read_string(config,buf,lev+2,off+len,&leng,&res);
1237 if (res)
1238 goto ERROR2;
1239 psiconv_debug(config,lev+2,off+len,"Icon name: %s",(*result)->icon_name);
1240 len += leng;
1241
1242 psiconv_progress(config,lev+2,off+len,"Going to read the icon width");
1243 (*result)->icon_width = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
1244 if (res)
1245 goto ERROR3;
1246 psiconv_debug(config,lev+2,off+len,"Icon width: %f cm",(*result)->icon_width);
1247 len += leng;
1248
1249 psiconv_progress(config,lev+2,off+len,"Going to read the icon height");
1250 (*result)->icon_height = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
1251 if (res)
1252 goto ERROR3;
1253 psiconv_debug(config,lev+2,off+len,"Icon length: %f cm",(*result)->icon_height);
1254 len += leng;
1255
1256 if (length)
1257 *length = len;
1258
1259 psiconv_progress(config,lev+1,off+len-1,"End of Object Icon Section"
1260 "(total length: %08x",len);
1261 return res;
1262
1263ERROR3:
1264 free((*result)->icon_name);
1265ERROR2:
1266 free(*result);
1267ERROR1:
1268 psiconv_error(config,lev+1,off+len,"Reading of Object Icon Section failed");
1269 if (length)
1270 *length=0;
1271 if (!res)
1272 return -PSICONV_E_NOMEM;
1273 else
1274 return res;
1275}

Legend:
Removed from v.41  
changed lines
  Added in v.233

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26