1 |
/* |
2 |
parse_sheet.c - Part of psiconv, a PSION 5 file formats converter |
3 |
Copyright (c) 2001 Frodo Looijaard <frodol@dds.nl> |
4 |
|
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 |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License |
16 |
along with this program; if not, write to the Free Software |
17 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
*/ |
19 |
|
20 |
#include "config.h" |
21 |
#include "compat.h" |
22 |
|
23 |
#include <stdlib.h> |
24 |
|
25 |
#include "parse_routines.h" |
26 |
#include "error.h" |
27 |
|
28 |
static psiconv_sheet_cell_layout psiconv_basic_cell_layout(void) |
29 |
{ |
30 |
psiconv_sheet_cell_layout result; |
31 |
if (!(result = malloc(sizeof(*result)))) |
32 |
goto ERROR1; |
33 |
if (!(result->character = psiconv_basic_character_layout())) |
34 |
goto ERROR2; |
35 |
if (!(result->paragraph = psiconv_basic_paragraph_layout())) |
36 |
goto ERROR3; |
37 |
if (!(result->numberformat = malloc(sizeof(*result->numberformat)))) |
38 |
goto ERROR4; |
39 |
result->numberformat->code = psiconv_numberformat_general; |
40 |
result->numberformat->decimal = 2; |
41 |
return result; |
42 |
ERROR4: |
43 |
psiconv_free_paragraph_layout(result->paragraph); |
44 |
ERROR3: |
45 |
psiconv_free_character_layout(result->character); |
46 |
ERROR2: |
47 |
free(result); |
48 |
ERROR1: |
49 |
return NULL; |
50 |
} |
51 |
|
52 |
static psiconv_sheet_cell_layout psiconv_clone_cell_layout |
53 |
(psiconv_sheet_cell_layout original) |
54 |
{ |
55 |
psiconv_sheet_cell_layout result; |
56 |
if (!(result = malloc(sizeof(*result)))) |
57 |
goto ERROR1; |
58 |
if (!(result->character = |
59 |
psiconv_clone_character_layout(original->character))) |
60 |
goto ERROR2; |
61 |
if (!(result->paragraph = |
62 |
psiconv_clone_paragraph_layout(original->paragraph))) |
63 |
goto ERROR3; |
64 |
if (!(result->numberformat = malloc(sizeof(*result->numberformat)))) |
65 |
goto ERROR4; |
66 |
result->numberformat->code = original->numberformat->code; |
67 |
result->numberformat->decimal = original->numberformat->decimal; |
68 |
return result; |
69 |
ERROR4: |
70 |
psiconv_free_paragraph_layout(result->paragraph); |
71 |
ERROR3: |
72 |
psiconv_free_character_layout(result->character); |
73 |
ERROR2: |
74 |
free(result); |
75 |
ERROR1: |
76 |
return NULL; |
77 |
} |
78 |
|
79 |
int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev, |
80 |
psiconv_u32 off, int *length, |
81 |
psiconv_sheet_numberformat result) |
82 |
{ |
83 |
int res=0; |
84 |
int len=0; |
85 |
psiconv_u8 temp; |
86 |
|
87 |
psiconv_progress(lev+1,off,"Going to read a sheet numberformat"); |
88 |
|
89 |
psiconv_progress(lev+2,off+len, |
90 |
"Going to read the initial byte (%02x expected)",0x02); |
91 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
92 |
if (res) |
93 |
goto ERROR1; |
94 |
if (temp != 0x02) { |
95 |
psiconv_warn(lev+2,off+len, |
96 |
"Sheet numberformat initial byte unknown value (ignored)"); |
97 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
98 |
} |
99 |
len ++; |
100 |
|
101 |
psiconv_progress(lev+2,off+len, "Going to read the code byte"); |
102 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
103 |
if (res) |
104 |
goto ERROR1; |
105 |
psiconv_debug(lev+2,off+len,"Code: %02x",temp); |
106 |
if (temp == 0x00) |
107 |
result->code = psiconv_numberformat_general; |
108 |
else if (temp == 0x02) |
109 |
result->code = psiconv_numberformat_fixeddecimal; |
110 |
else if (temp == 0x04) |
111 |
result->code = psiconv_numberformat_scientific; |
112 |
else if (temp == 0x06) |
113 |
result->code = psiconv_numberformat_currency; |
114 |
else if (temp == 0x08) |
115 |
result->code = psiconv_numberformat_percent; |
116 |
else if (temp == 0x0A) |
117 |
result->code = psiconv_numberformat_triads; |
118 |
else if (temp == 0x0C) |
119 |
result->code = psiconv_numberformat_boolean; |
120 |
else if (temp == 0x0E) |
121 |
result->code = psiconv_numberformat_text; |
122 |
else if (temp == 0x10) |
123 |
result->code = psiconv_numberformat_date_dmm; |
124 |
else if (temp == 0x12) |
125 |
result->code = psiconv_numberformat_date_mmd; |
126 |
else if (temp == 0x14) |
127 |
result->code = psiconv_numberformat_date_ddmmyy; |
128 |
else if (temp == 0x16) |
129 |
result->code = psiconv_numberformat_date_mmddyy; |
130 |
else if (temp == 0x18) |
131 |
result->code = psiconv_numberformat_date_yymmdd; |
132 |
else if (temp == 0x1A) |
133 |
result->code = psiconv_numberformat_date_dmmm; |
134 |
else if (temp == 0x1C) |
135 |
result->code = psiconv_numberformat_date_dmmmyy; |
136 |
else if (temp == 0x1E) |
137 |
result->code = psiconv_numberformat_date_ddmmmyy; |
138 |
else if (temp == 0x20) |
139 |
result->code = psiconv_numberformat_date_mmm; |
140 |
else if (temp == 0x22) |
141 |
result->code = psiconv_numberformat_date_monthname; |
142 |
else if (temp == 0x24) |
143 |
result->code = psiconv_numberformat_date_mmmyy; |
144 |
else if (temp == 0x26) |
145 |
result->code = psiconv_numberformat_date_monthnameyy; |
146 |
else if (temp == 0x28) |
147 |
result->code = psiconv_numberformat_date_monthnamedyyyy; |
148 |
else if (temp == 0x2A) |
149 |
result->code = psiconv_numberformat_datetime_ddmmyyyyhhii; |
150 |
else if (temp == 0x2C) |
151 |
result->code = psiconv_numberformat_datetime_ddmmyyyyHHii; |
152 |
else if (temp == 0x2E) |
153 |
result->code = psiconv_numberformat_datetime_mmddyyyyhhii; |
154 |
else if (temp == 0x30) |
155 |
result->code = psiconv_numberformat_datetime_mmddyyyyHHii; |
156 |
else if (temp == 0x32) |
157 |
result->code = psiconv_numberformat_datetime_yyyymmddhhii; |
158 |
else if (temp == 0x34) |
159 |
result->code = psiconv_numberformat_datetime_yyyymmddHHii; |
160 |
else if (temp == 0x36) |
161 |
result->code = psiconv_numberformat_time_hhii; |
162 |
else if (temp == 0x38) |
163 |
result->code = psiconv_numberformat_time_hhiiss; |
164 |
else if (temp == 0x3A) |
165 |
result->code = psiconv_numberformat_time_HHii; |
166 |
else if (temp == 0x3C) |
167 |
result->code = psiconv_numberformat_time_HHiiss; |
168 |
else { |
169 |
psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)"); |
170 |
result->code = psiconv_numberformat_general; |
171 |
} |
172 |
len ++; |
173 |
|
174 |
psiconv_progress(lev+2,off+len, "Going to read the number of decimals"); |
175 |
result->decimal = psiconv_read_u8(buf,lev+2,off+len,&res) >> 1; |
176 |
if (res) |
177 |
goto ERROR1; |
178 |
psiconv_debug(lev+2,off+len,"Decimals: %d",result->decimal); |
179 |
len ++; |
180 |
|
181 |
if (length) |
182 |
*length = len; |
183 |
|
184 |
psiconv_progress(lev,off+len-1, |
185 |
"End of sheet number format (total length: %08x)", len); |
186 |
return 0; |
187 |
|
188 |
ERROR1: |
189 |
psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed"); |
190 |
if (length) |
191 |
*length = 0; |
192 |
if (!res) |
193 |
return -PSICONV_E_NOMEM; |
194 |
else |
195 |
return res; |
196 |
} |
197 |
|
198 |
int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev, |
199 |
psiconv_u32 off, int *length, |
200 |
psiconv_sheet_status_section *result) |
201 |
{ |
202 |
int res=0; |
203 |
int len=0; |
204 |
psiconv_u32 temp; |
205 |
int leng; |
206 |
|
207 |
psiconv_progress(lev+1,off,"Going to read the sheet status section"); |
208 |
if (!(*result = malloc(sizeof(**result)))) |
209 |
goto ERROR1; |
210 |
|
211 |
psiconv_progress(lev+2,off+len, |
212 |
"Going to read the initial byte (%02x expected)",0x02); |
213 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
214 |
if (res) |
215 |
goto ERROR2; |
216 |
if (temp != 0x02) { |
217 |
psiconv_warn(lev+2,off+len, |
218 |
"Sheet status section initial byte unknown value (ignored)"); |
219 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
220 |
} |
221 |
len ++; |
222 |
|
223 |
psiconv_progress(lev+2,off+len, |
224 |
"Going to read the cursor row"); |
225 |
(*result)->cursor_row = psiconv_read_u32(buf,lev+2,off + len,&res); |
226 |
if (res) |
227 |
goto ERROR2; |
228 |
psiconv_debug(lev+2,off+len,"Cursor row: %08x", |
229 |
(*result)->cursor_row); |
230 |
len += 0x04; |
231 |
|
232 |
psiconv_progress(lev+2,off+len, |
233 |
"Going to read the cursor column"); |
234 |
(*result)->cursor_column = psiconv_read_u32(buf,lev+2,off + len,&res); |
235 |
if (res) |
236 |
goto ERROR2; |
237 |
psiconv_debug(lev+2,off+len,"Cursor column: %08x", |
238 |
(*result)->cursor_column); |
239 |
len += 0x04; |
240 |
|
241 |
psiconv_progress(lev+2,off+len,"Going to read initially display graph"); |
242 |
if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, |
243 |
&(*result)->show_graph))) |
244 |
goto ERROR2; |
245 |
len += leng; |
246 |
|
247 |
psiconv_progress(lev+2,off+len, |
248 |
"Going to read the toolbar status byte"); |
249 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
250 |
if (res) |
251 |
goto ERROR2; |
252 |
|
253 |
(*result)->show_side_sheet_toolbar = temp&0x01 ? psiconv_bool_true : |
254 |
psiconv_bool_false; |
255 |
psiconv_debug(lev+2,off+len,"Show side sheet toolbar: %02x", |
256 |
(*result)->show_side_sheet_toolbar); |
257 |
(*result)->show_top_sheet_toolbar = temp&0x02 ? psiconv_bool_true : |
258 |
psiconv_bool_false; |
259 |
psiconv_debug(lev+2,off+len,"Show top sheet toolbar: %02x", |
260 |
(*result)->show_top_sheet_toolbar); |
261 |
(*result)->show_side_graph_toolbar = temp&0x04 ? psiconv_bool_true : |
262 |
psiconv_bool_false; |
263 |
psiconv_debug(lev+2,off+len,"Show side graph toolbar: %02x", |
264 |
(*result)->show_side_graph_toolbar); |
265 |
(*result)->show_top_graph_toolbar = temp&0x08 ? psiconv_bool_true : |
266 |
psiconv_bool_false; |
267 |
psiconv_debug(lev+2,off+len,"Show top graph toolbar: %02x", |
268 |
(*result)->show_top_graph_toolbar); |
269 |
if (temp & 0xf0) { |
270 |
psiconv_warn(lev+2,off+len,"Sheet status section toolbar byte " |
271 |
"flags contains unknown flags (ignored)"); |
272 |
psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0); |
273 |
} |
274 |
len ++; |
275 |
|
276 |
psiconv_progress(lev+2,off+len, |
277 |
"Going to read the scrollbar status byte"); |
278 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
279 |
if (res) |
280 |
goto ERROR2; |
281 |
if ((temp & 0x03) == 0x03) { |
282 |
psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " |
283 |
"flags contains unknown flags (ignored)"); |
284 |
psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x03); |
285 |
} |
286 |
(*result)->show_horizontal_scrollbar = (temp&0x03) == 1? psiconv_triple_off : |
287 |
(temp&0x03) == 2? psiconv_triple_auto: |
288 |
psiconv_triple_on; |
289 |
psiconv_debug(lev+2,off+len,"Show horizontal scrollbar: %02x", |
290 |
(*result)->show_horizontal_scrollbar); |
291 |
if ((temp & 0x0c) == 0x0c) { |
292 |
psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " |
293 |
"flags contains unknown flags (ignored)"); |
294 |
psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x0c); |
295 |
} |
296 |
(*result)->show_vertical_scrollbar = (temp&0x0c) ==0x04? psiconv_triple_off: |
297 |
(temp&0x0c) ==0x08? psiconv_triple_auto: |
298 |
psiconv_triple_on; |
299 |
psiconv_debug(lev+2,off+len,"Show vertical scrollbar: %02x", |
300 |
(*result)->show_vertical_scrollbar); |
301 |
if (temp & 0xf0) { |
302 |
psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " |
303 |
"flags contains unknown flags (ignored)"); |
304 |
psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0); |
305 |
} |
306 |
len ++; |
307 |
|
308 |
psiconv_progress(lev+2,off+len, |
309 |
"Going to read an unknown byte (%02x expected)",0x00); |
310 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
311 |
if (res) |
312 |
goto ERROR2; |
313 |
if (temp != 0x00) { |
314 |
psiconv_warn(lev+2,off+len, |
315 |
"Sheet status section unknown byte unknown value (ignored)"); |
316 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
317 |
} |
318 |
len ++; |
319 |
|
320 |
psiconv_progress(lev+2,off+len,"Going to read sheet display size"); |
321 |
(*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); |
322 |
if (res) |
323 |
goto ERROR2; |
324 |
psiconv_debug(lev+2,off+len,"Sheet display size: %08x", |
325 |
(*result)->sheet_display_size); |
326 |
len += 0x04; |
327 |
|
328 |
psiconv_progress(lev+2,off+len,"Going to read graph display size"); |
329 |
(*result)->graph_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); |
330 |
if (res) |
331 |
goto ERROR2; |
332 |
psiconv_debug(lev+2,off+len,"Graph display size: %08x", |
333 |
(*result)->graph_display_size); |
334 |
len += 0x04; |
335 |
|
336 |
if (length) |
337 |
*length = len; |
338 |
|
339 |
psiconv_progress(lev,off+len-1, |
340 |
"End of sheet status section (total length: %08x)", len); |
341 |
return 0; |
342 |
|
343 |
ERROR2: |
344 |
free (*result); |
345 |
ERROR1: |
346 |
psiconv_warn(lev+1,off,"Reading of Sheet Status Section failed"); |
347 |
if (length) |
348 |
*length = 0; |
349 |
if (!res) |
350 |
return -PSICONV_E_NOMEM; |
351 |
else |
352 |
return res; |
353 |
} |
354 |
|
355 |
int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev, |
356 |
psiconv_u32 off, int *length, |
357 |
psiconv_sheet_workbook_section *result) |
358 |
{ |
359 |
int res=0; |
360 |
psiconv_u32 temp,formulas_off,worksheets_off; |
361 |
int len=0; |
362 |
|
363 |
psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); |
364 |
if (!(*result = malloc(sizeof(**result)))) |
365 |
goto ERROR1; |
366 |
|
367 |
psiconv_progress(lev+2,off+len, |
368 |
"Going to read the initial byte (%02x expected)",0x04); |
369 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
370 |
if (res) |
371 |
goto ERROR2; |
372 |
if (temp != 0x04) { |
373 |
psiconv_warn(lev+2,off+len, |
374 |
"Sheet workbook section initial byte unknown value (ignored)"); |
375 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
376 |
} |
377 |
len ++; |
378 |
|
379 |
psiconv_progress(lev+2,off+len, |
380 |
"Going to read the offset of the 1st ??? Section"); |
381 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
382 |
if (res) |
383 |
goto ERROR2; |
384 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
385 |
len += 4; |
386 |
|
387 |
psiconv_progress(lev+2,off+len, |
388 |
"Going to read the offset of the Formulas List"); |
389 |
formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res); |
390 |
if (res) |
391 |
goto ERROR2; |
392 |
psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off); |
393 |
len += 4; |
394 |
|
395 |
psiconv_progress(lev+2,off+len, |
396 |
"Going to read the offset of the Worksheet List"); |
397 |
worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res); |
398 |
if (res) |
399 |
goto ERROR2; |
400 |
psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off); |
401 |
len += 4; |
402 |
|
403 |
psiconv_progress(lev+2,off+len, |
404 |
"Going to read the offset of the 4th ??? Section"); |
405 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
406 |
if (res) |
407 |
goto ERROR2; |
408 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
409 |
len += 4; |
410 |
|
411 |
psiconv_progress(lev+2,off+len,"Going to read the formulas list"); |
412 |
if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL, |
413 |
&(*result)->formulas))) |
414 |
goto ERROR2; |
415 |
|
416 |
psiconv_progress(lev+2,off+len,"Going to read the worksheet list"); |
417 |
if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off, |
418 |
NULL,&(*result)->worksheets))) |
419 |
goto ERROR2; |
420 |
|
421 |
|
422 |
if (length) |
423 |
*length = len; |
424 |
|
425 |
psiconv_progress(lev,off+len-1, |
426 |
"End of sheet workbook section (total length: %08x)", len); |
427 |
return 0; |
428 |
|
429 |
ERROR2: |
430 |
free (*result); |
431 |
ERROR1: |
432 |
psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed"); |
433 |
if (length) |
434 |
*length = 0; |
435 |
if (!res) |
436 |
return -PSICONV_E_NOMEM; |
437 |
else |
438 |
return res; |
439 |
} |
440 |
|
441 |
int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev, |
442 |
psiconv_u32 off, int *length, |
443 |
psiconv_formula_list *result) |
444 |
{ |
445 |
int res=0; |
446 |
int len=0; |
447 |
psiconv_u32 temp; |
448 |
psiconv_formula formula; |
449 |
psiconv_u32 listlen,i; |
450 |
int leng; |
451 |
|
452 |
psiconv_progress(lev+1,off,"Going to read the sheet formula table"); |
453 |
if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
454 |
goto ERROR1; |
455 |
|
456 |
psiconv_progress(lev+2,off+len, |
457 |
"Going to read the initial byte (%02x expected)",0x02); |
458 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
459 |
if (res) |
460 |
goto ERROR2; |
461 |
if (temp != 0x02) { |
462 |
psiconv_warn(lev+2,off+len, |
463 |
"Sheet formula table initial byte unknown value (ignored)"); |
464 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
465 |
} |
466 |
len ++; |
467 |
|
468 |
psiconv_progress(lev+2,off+len, |
469 |
"Going to read the number of formulas"); |
470 |
listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); |
471 |
if (res) |
472 |
goto ERROR2; |
473 |
psiconv_debug(lev+2,off+len,"Number of formulas: %d",listlen); |
474 |
len += leng; |
475 |
|
476 |
psiconv_progress(lev+2,off+len,"Going to read all formulas"); |
477 |
for (i = 0; i < listlen; i++) { |
478 |
psiconv_progress(lev+3,off+len,"Going to read formula %d",i); |
479 |
if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula))) |
480 |
goto ERROR2; |
481 |
if ((res = psiconv_list_add(*result,formula))) |
482 |
goto ERROR3; |
483 |
len += leng; |
484 |
} |
485 |
|
486 |
if (length) |
487 |
*length = len; |
488 |
|
489 |
psiconv_progress(lev,off+len-1, |
490 |
"End of sheet formula table (total length: %08x)", len); |
491 |
return 0; |
492 |
|
493 |
ERROR3: |
494 |
psiconv_free_formula(formula); |
495 |
ERROR2: |
496 |
psiconv_list_free(*result); |
497 |
ERROR1: |
498 |
psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); |
499 |
if (length) |
500 |
*length = 0; |
501 |
if (!res) |
502 |
return -PSICONV_E_NOMEM; |
503 |
else |
504 |
return res; |
505 |
} |
506 |
|
507 |
int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, |
508 |
psiconv_u32 off, int *length, |
509 |
psiconv_sheet_cell *result, |
510 |
const psiconv_sheet_cell_layout default_layout) |
511 |
{ |
512 |
int res=0; |
513 |
int len=0; |
514 |
psiconv_u32 temp; |
515 |
psiconv_bool_t has_layout; |
516 |
int leng; |
517 |
|
518 |
psiconv_progress(lev+1,off,"Going to read a sheet cell structure"); |
519 |
if (!(*result = malloc(sizeof(**result)))) |
520 |
goto ERROR1; |
521 |
|
522 |
(*result)->layout = NULL; |
523 |
(*result)->type = psiconv_cell_blank; |
524 |
|
525 |
psiconv_progress(lev+2,off+len,"Going to read the cell position"); |
526 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
527 |
if (res) |
528 |
goto ERROR2; |
529 |
len ++; |
530 |
temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 8; |
531 |
if (res) |
532 |
goto ERROR2; |
533 |
len ++; |
534 |
temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 16; |
535 |
if (res) |
536 |
goto ERROR2; |
537 |
len ++; |
538 |
(*result)->column = (temp >> 2) & 0xFF; |
539 |
(*result)->row = (temp >> 10) & 0x3FFF; |
540 |
psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x", |
541 |
(*result)->column,(*result)->row); |
542 |
if (temp & 0x03) { |
543 |
psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)"); |
544 |
psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03); |
545 |
} |
546 |
|
547 |
psiconv_progress(lev+2,off+len,"Going to read the cell type"); |
548 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
549 |
if (res) |
550 |
goto ERROR2; |
551 |
len ++; |
552 |
(*result)->type = (temp >> 5) & 0x07; |
553 |
(*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false; |
554 |
has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false; |
555 |
|
556 |
psiconv_progress(lev+2,off+len,"Going to read the cell value"); |
557 |
if ((*result)->type == psiconv_cell_blank) { |
558 |
psiconv_debug(lev+2,off+len,"Cell type is blank: no value given."); |
559 |
} else if ((*result)->type == psiconv_cell_int) { |
560 |
psiconv_progress(lev+2,off+len,"Going to read an integer"); |
561 |
(*result)->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); |
562 |
if (res) |
563 |
goto ERROR2; |
564 |
len += 4; |
565 |
psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int); |
566 |
|
567 |
} else if ((*result)->type == psiconv_cell_bool) { |
568 |
psiconv_progress(lev+2,off+len,"Going to read a boolean"); |
569 |
if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, |
570 |
&(*result)->data.dat_bool))) |
571 |
goto ERROR2; |
572 |
psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp); |
573 |
(*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false; |
574 |
len += leng; |
575 |
} else if ((*result)->type == psiconv_cell_error) { |
576 |
psiconv_progress(lev+2,off+len,"Going to read the error code"); |
577 |
temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
578 |
if (res) |
579 |
goto ERROR2; |
580 |
if (temp == 0) |
581 |
(*result)->data.dat_error = psiconv_sheet_error_none; |
582 |
else if (temp == 1) |
583 |
(*result)->data.dat_error = psiconv_sheet_error_null; |
584 |
else if (temp == 2) |
585 |
(*result)->data.dat_error = psiconv_sheet_error_divzero; |
586 |
else if (temp == 3) |
587 |
(*result)->data.dat_error = psiconv_sheet_error_value; |
588 |
else if (temp == 4) |
589 |
(*result)->data.dat_error = psiconv_sheet_error_reference; |
590 |
else if (temp == 5) |
591 |
(*result)->data.dat_error = psiconv_sheet_error_name; |
592 |
else if (temp == 6) |
593 |
(*result)->data.dat_error = psiconv_sheet_error_number; |
594 |
else if (temp == 7) |
595 |
(*result)->data.dat_error = psiconv_sheet_error_notavail; |
596 |
else { |
597 |
psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)"); |
598 |
psiconv_debug(lev+2,off+len,"Error code: %04x",temp); |
599 |
(*result)->data.dat_error = psiconv_sheet_error_none; |
600 |
} |
601 |
psiconv_debug(lev+2,off+len,"Cell contents: %04x", |
602 |
(*result)->data.dat_error); |
603 |
len += 2; |
604 |
} else if ((*result)->type == psiconv_cell_float) { |
605 |
psiconv_progress(lev+2,off+len,"Going to read a float"); |
606 |
(*result)->data.dat_float = |
607 |
psiconv_read_float(buf,lev+2,off+len,&leng,&res); |
608 |
if (res) |
609 |
goto ERROR2; |
610 |
psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float); |
611 |
len += leng; |
612 |
} else if ((*result)->type == psiconv_cell_string) { |
613 |
psiconv_progress(lev+2,off+len,"Going to read a string"); |
614 |
(*result)->data.dat_string = |
615 |
psiconv_read_string(buf,lev+2,off+len,&leng,&res); |
616 |
if (res) |
617 |
goto ERROR2; |
618 |
psiconv_debug(lev+2,off+len,"Cell contents: `%s'", |
619 |
(*result)->data.dat_string); |
620 |
len += leng; |
621 |
} else { |
622 |
psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); |
623 |
res = PSICONV_E_PARSE; |
624 |
goto ERROR2; |
625 |
} |
626 |
|
627 |
if (!((*result)->layout = psiconv_clone_cell_layout(default_layout))) |
628 |
goto ERROR2; |
629 |
if (has_layout) { |
630 |
if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len, |
631 |
&leng,(*result)->layout))) |
632 |
goto ERROR2; |
633 |
len += leng; |
634 |
} |
635 |
|
636 |
if ((*result)->calculated) { |
637 |
psiconv_progress(lev+2,off+len,"Going to read the cell formula reference"); |
638 |
temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res); |
639 |
if (res) |
640 |
goto ERROR2; |
641 |
psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp); |
642 |
len += leng; |
643 |
(*result)->ref_formula = temp; |
644 |
} |
645 |
|
646 |
if (length) |
647 |
*length = len; |
648 |
|
649 |
psiconv_progress(lev,off+len-1, |
650 |
"End of sheet cell structure (total length: %08x)", len); |
651 |
return 0; |
652 |
|
653 |
ERROR2: |
654 |
psiconv_free_sheet_cell(*result); |
655 |
ERROR1: |
656 |
psiconv_warn(lev+1,off,"Reading of Sheet Cell Structure failed"); |
657 |
if (length) |
658 |
*length = 0; |
659 |
if (!res) |
660 |
return -PSICONV_E_NOMEM; |
661 |
else |
662 |
return res; |
663 |
} |
664 |
|
665 |
int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, |
666 |
psiconv_u32 off, int *length, |
667 |
psiconv_sheet_cell_list *result, |
668 |
const psiconv_sheet_cell_layout default_layout) |
669 |
{ |
670 |
int res=0; |
671 |
int len=0; |
672 |
psiconv_u32 temp; |
673 |
psiconv_sheet_cell cell; |
674 |
psiconv_u32 listlen,i; |
675 |
int leng; |
676 |
|
677 |
psiconv_progress(lev+1,off,"Going to read the sheet cell list"); |
678 |
if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s)))) |
679 |
goto ERROR1; |
680 |
|
681 |
psiconv_progress(lev+2,off+len, |
682 |
"Going to read the initial byte (%02x expected)",0x02); |
683 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
684 |
if (res) |
685 |
goto ERROR2; |
686 |
if (temp != 0x02) { |
687 |
psiconv_warn(lev+2,off+len, |
688 |
"Sheet cell list initial byte unknown value (ignored)"); |
689 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
690 |
} |
691 |
len ++; |
692 |
|
693 |
psiconv_progress(lev+2,off+len, |
694 |
"Going to read the initial byte (%02x expected)",0x00); |
695 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
696 |
if (res) |
697 |
goto ERROR2; |
698 |
if (temp != 0x00) { |
699 |
psiconv_warn(lev+2,off+len, |
700 |
"Sheet cell list initial byte unknown value (ignored)"); |
701 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
702 |
} |
703 |
len ++; |
704 |
|
705 |
psiconv_progress(lev+2,off+len, |
706 |
"Going to read the number of defined cells"); |
707 |
listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); |
708 |
if (res) |
709 |
goto ERROR2; |
710 |
psiconv_debug(lev+2,off+len,"Number of defined cells: %d",listlen); |
711 |
len += leng; |
712 |
|
713 |
psiconv_progress(lev+2,off+len,"Going to read all cells"); |
714 |
for (i = 0; i < listlen; i++) { |
715 |
psiconv_progress(lev+3,off+len,"Going to read cell %d",i); |
716 |
if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell, |
717 |
default_layout))) |
718 |
goto ERROR2; |
719 |
if ((res = psiconv_list_add(*result,cell))) |
720 |
goto ERROR3; |
721 |
free(cell); |
722 |
len += leng; |
723 |
} |
724 |
|
725 |
if (length) |
726 |
*length = len; |
727 |
|
728 |
psiconv_progress(lev,off+len-1, |
729 |
"End of sheet cell list (total length: %08x)", len); |
730 |
return 0; |
731 |
|
732 |
ERROR3: |
733 |
psiconv_free_sheet_cell(cell); |
734 |
ERROR2: |
735 |
psiconv_free_sheet_cell_list(*result); |
736 |
ERROR1: |
737 |
psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed"); |
738 |
if (length) |
739 |
*length = 0; |
740 |
if (!res) |
741 |
return -PSICONV_E_NOMEM; |
742 |
else |
743 |
return res; |
744 |
} |
745 |
|
746 |
|
747 |
int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev, |
748 |
psiconv_u32 off, int *length, |
749 |
psiconv_sheet_worksheet_list *result) |
750 |
{ |
751 |
psiconv_sheet_worksheet worksheet; |
752 |
int res=0; |
753 |
int len=0; |
754 |
psiconv_u8 temp; |
755 |
psiconv_u32 offset; |
756 |
int leng,i,nr; |
757 |
|
758 |
psiconv_progress(lev+1,off,"Going to read the worksheet list"); |
759 |
if (!(*result = psiconv_list_new(sizeof(*worksheet)))) |
760 |
goto ERROR1; |
761 |
|
762 |
psiconv_progress(lev+2,off+len, |
763 |
"Going to read the initial bytes (%02x expected)",0x02); |
764 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
765 |
if (res) |
766 |
goto ERROR2; |
767 |
if (temp != 0x02) { |
768 |
psiconv_warn(lev+2,off+len, |
769 |
"Sheet worksheet list initial byte unknown value (ignored)"); |
770 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
771 |
} |
772 |
len ++; |
773 |
|
774 |
psiconv_progress(lev+2,off+len,"Going to read the list length"); |
775 |
nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res); |
776 |
if (res) |
777 |
goto ERROR2; |
778 |
psiconv_debug(lev+2,off+len,"Length: %02x",nr); |
779 |
len += leng; |
780 |
|
781 |
psiconv_progress(lev+2,off+len,"Going to read the list"); |
782 |
for (i=0 ; i < nr; i++) { |
783 |
psiconv_progress(lev+3,off+len,"Going to read element %d",i); |
784 |
psiconv_progress(lev+4,off+len, |
785 |
"Going to read the initial byte (%02x expected)",0x00); |
786 |
temp = psiconv_read_u8(buf,lev+4,off+len,&res); |
787 |
if (res) |
788 |
goto ERROR2; |
789 |
if (temp != 0x00) { |
790 |
psiconv_warn(lev+4,off+len, |
791 |
"Sheet worksheet element initial byte unknown value (ignored)"); |
792 |
psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp); |
793 |
} |
794 |
len ++; |
795 |
|
796 |
psiconv_progress(lev+4,off+len,"Going to read the worksheet offset"); |
797 |
offset = psiconv_read_u32(buf,lev+2,off+len,&res); |
798 |
if (res) |
799 |
goto ERROR2; |
800 |
psiconv_debug(lev+4,off+len,"Offset: %08x",offset); |
801 |
len += 4; |
802 |
|
803 |
if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL, |
804 |
&worksheet))) |
805 |
goto ERROR2; |
806 |
if ((res = psiconv_list_add(*result,worksheet))) |
807 |
goto ERROR3; |
808 |
free(worksheet); |
809 |
} |
810 |
|
811 |
if (length) |
812 |
*length = len; |
813 |
|
814 |
psiconv_progress(lev,off+len-1, |
815 |
"End of worksheet list (total length: %08x)", len); |
816 |
|
817 |
return 0; |
818 |
|
819 |
ERROR3: |
820 |
psiconv_free_sheet_worksheet(worksheet); |
821 |
ERROR2: |
822 |
psiconv_free_sheet_worksheet_list(*result); |
823 |
ERROR1: |
824 |
psiconv_warn(lev+1,off,"Reading of worksheet list failed"); |
825 |
if (length) |
826 |
*length = 0; |
827 |
if (!res) |
828 |
return -PSICONV_E_NOMEM; |
829 |
else |
830 |
return res; |
831 |
} |
832 |
|
833 |
int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev, |
834 |
psiconv_u32 off, int *length, |
835 |
psiconv_sheet_cell_layout result) |
836 |
|
837 |
{ |
838 |
int res=0; |
839 |
int len=0; |
840 |
int leng; |
841 |
psiconv_u8 temp; |
842 |
|
843 |
psiconv_progress(lev+1,off,"Going to read a sheet cell layout"); |
844 |
|
845 |
psiconv_progress(lev+2,off+len, |
846 |
"Going to read the first byte (%02x expected)",0x02); |
847 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
848 |
if (res) |
849 |
goto ERROR1; |
850 |
if (temp != 0x02) { |
851 |
psiconv_warn(lev+2,off+len, |
852 |
"Worksheet section initial byte unknown value (ignored)"); |
853 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
854 |
} |
855 |
len ++; |
856 |
|
857 |
psiconv_progress(lev+2,off+len,"Going to read the default formats flag"); |
858 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
859 |
if (res) |
860 |
goto ERROR1; |
861 |
len ++; |
862 |
|
863 |
if (temp & 0x01) { |
864 |
psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes"); |
865 |
if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, |
866 |
result->paragraph))) |
867 |
goto ERROR1; |
868 |
len += leng; |
869 |
} |
870 |
|
871 |
if (temp & 0x02) { |
872 |
psiconv_progress(lev+3,off+len,"Going to read the default character codes"); |
873 |
if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, |
874 |
result->character))) |
875 |
goto ERROR1; |
876 |
len += leng; |
877 |
} |
878 |
|
879 |
if (temp & 0x04) { |
880 |
psiconv_progress(lev+3,off+len, "Going to read the default number format"); |
881 |
psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng, |
882 |
result->numberformat); |
883 |
len += leng; |
884 |
} |
885 |
|
886 |
if (length) |
887 |
*length = len; |
888 |
|
889 |
psiconv_progress(lev,off+len-1, |
890 |
"End of sheet cell layout (total length: %08x)", len); |
891 |
|
892 |
return 0; |
893 |
|
894 |
ERROR1: |
895 |
psiconv_warn(lev+1,off,"Reading of sheet cell layout failed"); |
896 |
if (length) |
897 |
*length = 0; |
898 |
if (!res) |
899 |
return -PSICONV_E_NOMEM; |
900 |
else |
901 |
return res; |
902 |
} |
903 |
|
904 |
|
905 |
int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev, |
906 |
psiconv_u32 off, int *length, |
907 |
psiconv_sheet_worksheet *result) |
908 |
{ |
909 |
int res=0; |
910 |
psiconv_u32 temp,cells_off,grid_off; |
911 |
int len=0; |
912 |
int leng; |
913 |
|
914 |
psiconv_progress(lev+1,off,"Going to read the sheet worksheet section"); |
915 |
if (!(*result = malloc(sizeof(**result)))) |
916 |
goto ERROR1; |
917 |
|
918 |
psiconv_progress(lev+2,off+len, |
919 |
"Going to read the initial bytes (%02x expected)",0x04); |
920 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
921 |
if (res) |
922 |
goto ERROR2; |
923 |
if (temp != 0x04) { |
924 |
psiconv_warn(lev+2,off+len, |
925 |
"Worksheet section initial byte unknown value (ignored)"); |
926 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
927 |
} |
928 |
len ++; |
929 |
|
930 |
psiconv_progress(lev+2,off+len, "Going to read the flags byte"); |
931 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
932 |
if (res) |
933 |
goto ERROR2; |
934 |
psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp); |
935 |
(*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false; |
936 |
if (temp & 0xfe) { |
937 |
psiconv_warn(lev+2,off+len, |
938 |
"Worksheet section flags byte unknown bits (ignored)"); |
939 |
} |
940 |
len ++; |
941 |
|
942 |
psiconv_progress(lev+2,off+len,"Going to read the default cell layout"); |
943 |
if (!((*result)->default_layout = psiconv_basic_cell_layout())) |
944 |
goto ERROR2; |
945 |
if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng, |
946 |
(*result)->default_layout))) |
947 |
goto ERROR3; |
948 |
len += leng; |
949 |
|
950 |
psiconv_progress(lev+2,off+len, |
951 |
"Going to read the offset of the 1st ??? Section"); |
952 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
953 |
if (res) |
954 |
goto ERROR3; |
955 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
956 |
len += 4; |
957 |
|
958 |
psiconv_progress(lev+2,off+len, |
959 |
"Going to read the offset of the 2nd ??? Section"); |
960 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
961 |
if (res) |
962 |
goto ERROR3; |
963 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
964 |
len += 4; |
965 |
|
966 |
psiconv_progress(lev+2,off+len, |
967 |
"Going to read the offset of the Cells List"); |
968 |
cells_off = psiconv_read_u32(buf,lev+2,off+len,&res); |
969 |
if (res) |
970 |
goto ERROR3; |
971 |
psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off); |
972 |
len += 4; |
973 |
|
974 |
psiconv_progress(lev+2,off+len, |
975 |
"Going to read the offset of the Grid Section"); |
976 |
grid_off = psiconv_read_u32(buf,lev+2,off+len,&res); |
977 |
if (res) |
978 |
goto ERROR3; |
979 |
psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off); |
980 |
len += 4; |
981 |
|
982 |
psiconv_progress(lev+2,off+len, |
983 |
"Going to read the offset of the 3rd ??? Section"); |
984 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
985 |
if (res) |
986 |
goto ERROR3; |
987 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
988 |
len += 4; |
989 |
|
990 |
psiconv_progress(lev+2,off+len,"Going to read the cells list"); |
991 |
if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL, |
992 |
&(*result)->cells, |
993 |
(*result)->default_layout))) |
994 |
goto ERROR3; |
995 |
|
996 |
|
997 |
/* TODO: parse grid section */ |
998 |
|
999 |
if (length) |
1000 |
*length = len; |
1001 |
|
1002 |
psiconv_progress(lev,off+len-1, |
1003 |
"End of sheet worksheet section (total length: %08x)", len); |
1004 |
return 0; |
1005 |
|
1006 |
ERROR3: |
1007 |
psiconv_free_sheet_cell_layout((*result)->default_layout); |
1008 |
ERROR2: |
1009 |
free (*result); |
1010 |
ERROR1: |
1011 |
psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); |
1012 |
if (length) |
1013 |
*length = 0; |
1014 |
if (!res) |
1015 |
return -PSICONV_E_NOMEM; |
1016 |
else |
1017 |
return res; |
1018 |
} |
1019 |
|
1020 |
|