1 | /* |
1 | /* |
2 | parse_formula.c - Part of psiconv, a PSION 5 file formats converter |
2 | parse_formula.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 2001 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 2001-2014 Frodo Looijaard <frodo@frodo.looijaard.name> |
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. |
… | |
… | |
291 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
291 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
292 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
292 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
293 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
293 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
294 | {psiconv_formula_unknown,0,"*UNKNOWN*"}}; |
294 | {psiconv_formula_unknown,0,"*UNKNOWN*"}}; |
295 | |
295 | |
296 | static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev, |
296 | static int psiconv_parse_sheet_ref(const psiconv_config config, |
|
|
297 | const psiconv_buffer buf,int lev, |
297 | psiconv_u32 off, int *length, |
298 | psiconv_u32 off, int *length, |
298 | psiconv_sheet_ref_t *result) |
299 | psiconv_sheet_ref_t *result) |
299 | { |
300 | { |
300 | int res; |
301 | int res; |
301 | psiconv_u16 temp; |
302 | psiconv_u16 temp; |
302 | |
303 | |
303 | psiconv_progress(lev+1,off,"Going to read a sheet ref"); |
304 | psiconv_progress(config,lev+1,off,"Going to read a sheet ref"); |
304 | psiconv_progress(lev+2,off,"Going to read the offset encoding"); |
305 | psiconv_progress(config,lev+2,off,"Going to read the offset encoding"); |
305 | temp = psiconv_read_u16(buf,lev+2,off,&res); |
306 | temp = psiconv_read_u16(config,buf,lev+2,off,&res); |
306 | if (res) { |
307 | if (res) { |
307 | if (length) |
308 | if (length) |
308 | *length = 0; |
309 | *length = 0; |
309 | return res; |
310 | return res; |
310 | } |
311 | } |
311 | psiconv_debug(lev+2,off,"Encoded word: %04x",temp); |
312 | psiconv_debug(config,lev+2,off,"Encoded word: %04x",temp); |
312 | result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false; |
313 | result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false; |
313 | result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1); |
314 | result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1); |
314 | psiconv_debug(lev+2,off,"Reference: %s offset %d", |
315 | psiconv_debug(config,lev+2,off,"Reference: %s offset %d", |
315 | result->absolute?"absolute":"relative",result->offset); |
316 | result->absolute?"absolute":"relative",result->offset); |
316 | if (length) |
317 | if (length) |
317 | *length = 2; |
318 | *length = 2; |
318 | return 0; |
319 | return 0; |
319 | } |
320 | } |
320 | |
321 | |
321 | static int psiconv_parse_sheet_cell_reference(const psiconv_buffer buf,int lev, |
322 | static int psiconv_parse_sheet_cell_reference(const psiconv_config config, |
|
|
323 | const psiconv_buffer buf,int lev, |
322 | psiconv_u32 off, int *length, |
324 | psiconv_u32 off, int *length, |
323 | psiconv_sheet_cell_reference_t *result) |
325 | psiconv_sheet_cell_reference_t *result) |
324 | { |
326 | { |
325 | int len = 0; |
327 | int len = 0; |
326 | int leng,res; |
328 | int leng,res; |
327 | psiconv_u8 temp; |
329 | psiconv_u8 temp; |
328 | |
330 | |
329 | psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference"); |
331 | psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell reference"); |
330 | psiconv_progress(lev+2,off+len,"Going to read the row reference"); |
332 | psiconv_progress(config,lev+2,off+len,"Going to read the row reference"); |
331 | if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->row))) |
333 | if ((res = psiconv_parse_sheet_ref(config,buf,lev+2,off+len,&leng,&result->row))) |
332 | goto ERROR; |
334 | goto ERROR; |
333 | len += leng; |
335 | len += leng; |
334 | psiconv_progress(lev+2,off+len,"Going to read the column reference"); |
336 | psiconv_progress(config,lev+2,off+len,"Going to read the column reference"); |
335 | if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->column))) |
337 | if ((res = psiconv_parse_sheet_ref(config,buf,lev+2,off+len,&leng,&result->column))) |
336 | goto ERROR; |
338 | goto ERROR; |
337 | len += leng; |
339 | len += leng; |
338 | |
340 | |
339 | psiconv_progress(lev+2,off+len, |
341 | psiconv_progress(config,lev+2,off+len, |
340 | "Going to read the trailing byte (%02x expected)",0); |
342 | "Going to read the trailing byte (%02x expected)",0); |
341 | temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
343 | temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
342 | if (res) |
344 | if (res) |
343 | goto ERROR; |
345 | goto ERROR; |
344 | if (temp != 0) { |
346 | if (temp != 0) { |
345 | psiconv_warn(lev+2,off+len,"Unknown byte in cell reference (ignored"); |
347 | psiconv_warn(config,lev+2,off+len,"Unknown byte in cell reference (ignored"); |
346 | psiconv_debug(lev+2,off+len,"Trailing byte: %02x",temp); |
348 | psiconv_debug(config,lev+2,off+len,"Trailing byte: %02x",temp); |
347 | } |
349 | } |
348 | len ++; |
350 | len ++; |
349 | psiconv_progress(lev,off+len-1, |
351 | psiconv_progress(config,lev,off+len-1, |
350 | "End of cell reference (total length: %08x)", len); |
352 | "End of cell reference (total length: %08x)", len); |
351 | if (length) |
353 | if (length) |
352 | *length = len; |
354 | *length = len; |
353 | return 0; |
355 | return 0; |
354 | ERROR: |
356 | ERROR: |
355 | if (length) |
357 | if (length) |
356 | *length = 0; |
358 | *length = 0; |
357 | return res; |
359 | return res; |
358 | } |
360 | } |
359 | |
361 | |
360 | static int psiconv_parse_sheet_cell_block(const psiconv_buffer buf,int lev, |
362 | static int psiconv_parse_sheet_cell_block(const psiconv_config config, |
|
|
363 | const psiconv_buffer buf,int lev, |
361 | psiconv_u32 off, int *length, |
364 | psiconv_u32 off, int *length, |
362 | psiconv_sheet_cell_block_t *result) |
365 | psiconv_sheet_cell_block_t *result) |
363 | { |
366 | { |
364 | int len = 0; |
367 | int len = 0; |
365 | int leng,res; |
368 | int leng,res; |
366 | |
369 | |
367 | psiconv_progress(lev+1,off+len,"Going to read a sheet cell block"); |
370 | psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell block"); |
368 | psiconv_progress(lev+2,off+len,"Going to read the first cell"); |
371 | psiconv_progress(config,lev+2,off+len,"Going to read the first cell"); |
369 | if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, |
372 | if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng, |
370 | &result->first))) |
373 | &result->first))) |
371 | goto ERROR; |
374 | goto ERROR; |
372 | len += leng; |
375 | len += leng; |
373 | psiconv_progress(lev+2,off+len,"Going to read the last cell"); |
376 | psiconv_progress(config,lev+2,off+len,"Going to read the last cell"); |
374 | if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, |
377 | if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng, |
375 | &result->last))) |
378 | &result->last))) |
376 | goto ERROR; |
379 | goto ERROR; |
377 | len += leng; |
380 | len += leng; |
378 | psiconv_progress(lev,off+len-1, |
381 | psiconv_progress(config,lev,off+len-1, |
379 | "End of cell block (total length: %08x)", len); |
382 | "End of cell block (total length: %08x)", len); |
380 | if (length) |
383 | if (length) |
381 | *length = len; |
384 | *length = len; |
382 | return 0; |
385 | return 0; |
383 | ERROR: |
386 | ERROR: |
384 | if (length) |
387 | if (length) |
385 | *length = 0; |
388 | *length = 0; |
386 | return res; |
389 | return res; |
387 | } |
390 | } |
388 | |
391 | |
389 | static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev, |
392 | static int psiconv_parse_formula_element_list(const psiconv_config config, |
|
|
393 | const psiconv_buffer buf, int lev, |
390 | psiconv_u32 off, int *length, |
394 | psiconv_u32 off, int *length, |
391 | psiconv_formula *result, |
395 | psiconv_formula *result, |
392 | psiconv_u32 maxlen) |
396 | psiconv_u32 maxlen) |
393 | { |
397 | { |
394 | int res=0; |
398 | int res=0; |
… | |
… | |
399 | psiconv_formula_list formula_stack; |
403 | psiconv_formula_list formula_stack; |
400 | psiconv_formula formula,subformula,subformula1,subformula2, |
404 | psiconv_formula formula,subformula,subformula1,subformula2, |
401 | subformula3,subformula4; |
405 | subformula3,subformula4; |
402 | psiconv_u16 temp,nr_of_subs; |
406 | psiconv_u16 temp,nr_of_subs; |
403 | |
407 | |
404 | psiconv_progress(lev+1,off,"Going to read a formula element list"); |
408 | psiconv_progress(config,lev+1,off,"Going to read a formula element list"); |
405 | if (!(*result = malloc(sizeof(**result)))) |
409 | if (!(*result = malloc(sizeof(**result)))) |
406 | goto ERROR1; |
410 | goto ERROR1; |
407 | if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
411 | if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
408 | goto ERROR2; |
412 | goto ERROR2; |
409 | if (!(formula = malloc(sizeof(*formula)))) |
413 | if (!(formula = malloc(sizeof(*formula)))) |
… | |
… | |
422 | if (!(subformula4 = malloc(sizeof(*subformula4)))) |
426 | if (!(subformula4 = malloc(sizeof(*subformula4)))) |
423 | goto ERROR7; |
427 | goto ERROR7; |
424 | subformula4->type = psiconv_formula_unknown; |
428 | subformula4->type = psiconv_formula_unknown; |
425 | |
429 | |
426 | while (!eof && len+off < maxlen) { |
430 | while (!eof && len+off < maxlen) { |
427 | psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); |
431 | psiconv_progress(config,lev+3,off+len,"Going to read a formula item marker"); |
428 | marker = psiconv_read_u8(buf,lev+2,off+len,&res); |
432 | marker = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
429 | if (res) |
433 | if (res) |
430 | goto ERROR8; |
434 | goto ERROR8; |
431 | psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, |
435 | psiconv_debug(config,lev+3,off+len,"Marker: %02x (%s)",marker, |
432 | formula_elements[marker].name); |
436 | formula_elements[marker].name); |
433 | len ++; |
437 | len ++; |
434 | |
438 | |
435 | if (formula_elements[marker].formula_type == psiconv_formula_unknown) { |
439 | if (formula_elements[marker].formula_type == psiconv_formula_unknown) { |
436 | psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); |
440 | psiconv_error(config,lev+3,off+len,"Unknown formula marker found!"); |
437 | goto ERROR8; |
441 | goto ERROR8; |
438 | } else if ((formula_elements[marker].formula_type == |
442 | } else if ((formula_elements[marker].formula_type == |
439 | psiconv_formula_mark_eof) || |
443 | psiconv_formula_mark_eof) || |
440 | (formula_elements[marker].formula_type == |
444 | (formula_elements[marker].formula_type == |
441 | psiconv_formula_mark_opend) || |
445 | psiconv_formula_mark_opend) || |
442 | (formula_elements[marker].formula_type == |
446 | (formula_elements[marker].formula_type == |
443 | psiconv_formula_mark_opsep)) { |
447 | psiconv_formula_mark_opsep)) { |
444 | len--; |
448 | len--; |
445 | psiconv_progress(lev+3,off+len,"End of this formula list"); |
449 | psiconv_progress(config,lev+3,off+len,"End of this formula list"); |
446 | eof = 1; |
450 | eof = 1; |
447 | } else if (formula_elements[marker].formula_type == |
451 | } else if (formula_elements[marker].formula_type == |
448 | psiconv_formula_dat_int) { |
452 | psiconv_formula_dat_int) { |
449 | psiconv_progress(lev+3,off+len,"Next item: an integer"); |
453 | psiconv_progress(config,lev+3,off+len,"Next item: an integer"); |
450 | formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); |
454 | formula->data.dat_int = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
451 | if (res) |
455 | if (res) |
452 | goto ERROR8; |
456 | goto ERROR8; |
453 | formula->type = formula_elements[marker].formula_type; |
457 | formula->type = formula_elements[marker].formula_type; |
454 | psiconv_debug(lev+3,off+len,"Value: %08x",formula->data.dat_int); |
458 | psiconv_debug(config,lev+3,off+len,"Value: %08x",formula->data.dat_int); |
455 | len += 4; |
459 | len += 4; |
456 | if ((res = psiconv_list_add(formula_stack,formula))) |
460 | if ((res = psiconv_list_add(formula_stack,formula))) |
457 | goto ERROR8; |
461 | goto ERROR8; |
458 | formula->type = psiconv_formula_unknown; |
462 | formula->type = psiconv_formula_unknown; |
459 | } else if (formula_elements[marker].formula_type == |
463 | } else if (formula_elements[marker].formula_type == |
460 | psiconv_formula_dat_float) { |
464 | psiconv_formula_dat_float) { |
461 | psiconv_progress(lev+3,off+len,"Next item: a float"); |
465 | psiconv_progress(config,lev+3,off+len,"Next item: a float"); |
462 | formula->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng, |
466 | formula->data.dat_float = psiconv_read_float(config,buf,lev+2,off+len,&leng, |
463 | &res); |
467 | &res); |
464 | if (res) |
468 | if (res) |
465 | goto ERROR8; |
469 | goto ERROR8; |
466 | formula->type = formula_elements[marker].formula_type; |
470 | formula->type = formula_elements[marker].formula_type; |
467 | psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float); |
471 | psiconv_debug(config,lev+3,off+len,"Value: %f",formula->data.dat_float); |
468 | len += leng; |
472 | len += leng; |
469 | if ((res = psiconv_list_add(formula_stack,formula))) |
473 | if ((res = psiconv_list_add(formula_stack,formula))) |
470 | goto ERROR8; |
474 | goto ERROR8; |
471 | formula->type = psiconv_formula_unknown; |
475 | formula->type = psiconv_formula_unknown; |
472 | } else if (formula_elements[marker].formula_type == |
476 | } else if (formula_elements[marker].formula_type == |
473 | psiconv_formula_dat_cellref) { |
477 | psiconv_formula_dat_cellref) { |
474 | psiconv_progress(lev+3,off+len,"Next item: a cell reference"); |
478 | psiconv_progress(config,lev+3,off+len,"Next item: a cell reference"); |
475 | if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, |
479 | if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng, |
476 | &formula->data.dat_cellref))) |
480 | &formula->data.dat_cellref))) |
477 | goto ERROR8; |
481 | goto ERROR8; |
478 | formula->type = formula_elements[marker].formula_type; |
482 | formula->type = formula_elements[marker].formula_type; |
479 | len += leng; |
483 | len += leng; |
480 | if ((res = psiconv_list_add(formula_stack,formula))) |
484 | if ((res = psiconv_list_add(formula_stack,formula))) |
… | |
… | |
482 | formula->type = psiconv_formula_unknown; |
486 | formula->type = psiconv_formula_unknown; |
483 | } else if ((formula_elements[marker].formula_type == |
487 | } else if ((formula_elements[marker].formula_type == |
484 | psiconv_formula_dat_cellblock) || |
488 | psiconv_formula_dat_cellblock) || |
485 | (formula_elements[marker].formula_type == |
489 | (formula_elements[marker].formula_type == |
486 | psiconv_formula_dat_vcellblock)) { |
490 | psiconv_formula_dat_vcellblock)) { |
487 | psiconv_progress(lev+3,off+len,"Next item: a cell block"); |
491 | psiconv_progress(config,lev+3,off+len,"Next item: a cell block"); |
488 | if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, |
492 | if ((res = psiconv_parse_sheet_cell_block(config,buf,lev+2,off+len,&leng, |
489 | &formula->data.dat_cellblock))) |
493 | &formula->data.dat_cellblock))) |
490 | goto ERROR8; |
494 | goto ERROR8; |
491 | formula->type = formula_elements[marker].formula_type; |
495 | formula->type = formula_elements[marker].formula_type; |
492 | len += leng; |
496 | len += leng; |
493 | if ((res = psiconv_list_add(formula_stack,formula))) |
497 | if ((res = psiconv_list_add(formula_stack,formula))) |
494 | goto ERROR8; |
498 | goto ERROR8; |
495 | formula->type = psiconv_formula_unknown; |
499 | formula->type = psiconv_formula_unknown; |
496 | } else if (formula_elements[marker].formula_type == |
500 | } else if (formula_elements[marker].formula_type == |
497 | psiconv_formula_dat_string) { |
501 | psiconv_formula_dat_string) { |
498 | psiconv_progress(lev+3,off+len,"Next item: a string"); |
502 | psiconv_progress(config,lev+3,off+len,"Next item: a string"); |
499 | formula->data.dat_string = |
503 | formula->data.dat_string = |
500 | psiconv_read_short_string(buf,lev+2,off+len,&leng,&res); |
504 | psiconv_read_short_string(config,buf,lev+2,off+len,&leng,&res); |
501 | if (res) |
505 | if (res) |
502 | goto ERROR8; |
506 | goto ERROR8; |
503 | formula->type = formula_elements[marker].formula_type; |
507 | formula->type = formula_elements[marker].formula_type; |
504 | len += leng; |
508 | len += leng; |
505 | if ((res = psiconv_list_add(formula_stack,formula))) |
509 | if ((res = psiconv_list_add(formula_stack,formula))) |
506 | goto ERROR8; |
510 | goto ERROR8; |
507 | formula->type = psiconv_formula_unknown; |
511 | formula->type = psiconv_formula_unknown; |
508 | } else if ((formula_elements[marker].formula_type == |
512 | } else if ((formula_elements[marker].formula_type == |
509 | psiconv_formula_dat_var)) { |
513 | psiconv_formula_dat_var)) { |
510 | psiconv_progress(lev+3,off+len,"Next item: a variable reference"); |
514 | psiconv_progress(config,lev+3,off+len,"Next item: a variable reference"); |
511 | formula->data.dat_variable = psiconv_read_u32(buf,lev+2,off+len,&res); |
515 | formula->data.dat_variable = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
512 | if (res) |
516 | if (res) |
513 | goto ERROR8; |
517 | goto ERROR8; |
514 | formula->type = formula_elements[marker].formula_type; |
518 | formula->type = formula_elements[marker].formula_type; |
515 | len += 4; |
519 | len += 4; |
516 | if ((res = psiconv_list_add(formula_stack,formula))) |
520 | if ((res = psiconv_list_add(formula_stack,formula))) |
517 | goto ERROR8; |
521 | goto ERROR8; |
518 | formula->type = psiconv_formula_unknown; |
522 | formula->type = psiconv_formula_unknown; |
519 | } else if (formula_elements[marker].number_of_args == -1) { |
523 | } else if (formula_elements[marker].number_of_args == -1) { |
520 | psiconv_progress(lev+3,off+len,"Going to parse a vararg function"); |
524 | psiconv_progress(config,lev+3,off+len,"Going to parse a vararg function"); |
521 | if (!(formula->data.fun_operands = |
525 | if (!(formula->data.fun_operands = |
522 | psiconv_list_new(sizeof(*formula)))) |
526 | psiconv_list_new(sizeof(*formula)))) |
523 | goto ERROR8; |
527 | goto ERROR8; |
524 | formula->type = formula_elements[marker].formula_type; |
528 | formula->type = formula_elements[marker].formula_type; |
525 | nr_of_subs = 0; |
529 | nr_of_subs = 0; |
526 | do { |
530 | do { |
527 | nr_of_subs ++; |
531 | nr_of_subs ++; |
528 | psiconv_progress(lev+4,off+len,"Going to read vararg argument %d", |
532 | psiconv_progress(config,lev+4,off+len,"Going to read vararg argument %d", |
529 | nr_of_subs); |
533 | nr_of_subs); |
530 | if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng, |
534 | if ((res = psiconv_parse_formula_element_list(config,buf,lev+4,off+len,&leng, |
531 | &subformula,maxlen))) |
535 | &subformula,maxlen))) |
532 | goto ERROR8; |
536 | goto ERROR8; |
533 | len += leng; |
537 | len += leng; |
534 | if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) { |
538 | if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) { |
535 | psiconv_free_formula(subformula); |
539 | psiconv_free_formula(subformula); |
536 | goto ERROR8; |
540 | goto ERROR8; |
537 | } |
541 | } |
538 | free(subformula); |
542 | free(subformula); |
539 | psiconv_progress(lev+4,off+len,"Going to read the next marker"); |
543 | psiconv_progress(config,lev+4,off+len,"Going to read the next marker"); |
540 | submarker = psiconv_read_u8(buf,lev+4,off+len,&res); |
544 | submarker = psiconv_read_u8(config,buf,lev+4,off+len,&res); |
541 | len ++; |
545 | len ++; |
542 | if (res) |
546 | if (res) |
543 | goto ERROR8; |
547 | goto ERROR8; |
544 | submarker2 = psiconv_read_u8(buf,lev+4,off+len,&res); |
548 | submarker2 = psiconv_read_u8(config,buf,lev+4,off+len,&res); |
545 | if (res) |
549 | if (res) |
546 | goto ERROR8; |
550 | goto ERROR8; |
547 | } while ((formula_elements[submarker].formula_type |
551 | } while ((formula_elements[submarker].formula_type |
548 | == psiconv_formula_mark_opsep) && |
552 | == psiconv_formula_mark_opsep) && |
549 | (formula_elements[submarker2].formula_type |
553 | (formula_elements[submarker2].formula_type |
… | |
… | |
555 | submarker=submarker2; |
559 | submarker=submarker2; |
556 | len++; |
560 | len++; |
557 | } |
561 | } |
558 | if (formula_elements[submarker].formula_type |
562 | if (formula_elements[submarker].formula_type |
559 | != psiconv_formula_mark_opend) { |
563 | != psiconv_formula_mark_opend) { |
560 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
564 | psiconv_error(config,lev+3,off+len,"Formula corrupted!"); |
561 | psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker); |
565 | psiconv_debug(config,lev+3,off+len,"Found unexpected marker %02x",submarker); |
562 | goto ERROR8; |
566 | goto ERROR8; |
563 | } |
567 | } |
564 | psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x", |
568 | psiconv_progress(config,lev+3,off+len,"Going to read the repeated marker %02x", |
565 | marker); |
569 | marker); |
566 | submarker = psiconv_read_u8(buf,lev+3,off+len,&res); |
570 | submarker = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
567 | if (res) |
571 | if (res) |
568 | goto ERROR8; |
572 | goto ERROR8; |
569 | if (submarker != marker) { |
573 | if (submarker != marker) { |
570 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
574 | psiconv_error(config,lev+3,off+len,"Formula corrupted!"); |
571 | psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x", |
575 | psiconv_debug(config,lev+3,off+len,"Expected marker %02x, found %02x", |
572 | marker,submarker); |
576 | marker,submarker); |
573 | goto ERROR8; |
577 | goto ERROR8; |
574 | } |
578 | } |
575 | len++; |
579 | len++; |
576 | psiconv_progress(lev+3,off+len, |
580 | psiconv_progress(config,lev+3,off+len, |
577 | "Going to read the number of arguments (%d expected)", |
581 | "Going to read the number of arguments (%d expected)", |
578 | nr_of_subs); |
582 | nr_of_subs); |
579 | temp = psiconv_read_u16(buf,lev+3,off+len,&res); |
583 | temp = psiconv_read_u16(config,buf,lev+3,off+len,&res); |
580 | if (res) |
584 | if (res) |
581 | goto ERROR8; |
585 | goto ERROR8; |
582 | if (temp != nr_of_subs) { |
586 | if (temp != nr_of_subs) { |
583 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
587 | psiconv_error(config,lev+3,off+len,"Formula corrupted!"); |
584 | psiconv_debug(lev+3,off+len, |
588 | psiconv_debug(config,lev+3,off+len, |
585 | "Read %d arguments, but formula says there are %d", |
589 | "Read %d arguments, but formula says there are %d", |
586 | nr_of_subs,temp); |
590 | nr_of_subs,temp); |
587 | goto ERROR8; |
591 | goto ERROR8; |
588 | } |
592 | } |
589 | len += 2; |
593 | len += 2; |
… | |
… | |
624 | subformula4->type = subformula3->type = subformula2->type = |
628 | subformula4->type = subformula3->type = subformula2->type = |
625 | subformula1->type = formula->type = psiconv_formula_unknown; |
629 | subformula1->type = formula->type = psiconv_formula_unknown; |
626 | } |
630 | } |
627 | } |
631 | } |
628 | if ((len+off > maxlen) || !eof) { |
632 | if ((len+off > maxlen) || !eof) { |
629 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
633 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
630 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
634 | psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x", |
631 | maxlen,len+off); |
635 | maxlen,len+off); |
632 | goto ERROR8; |
636 | goto ERROR8; |
633 | } |
637 | } |
634 | if ((psiconv_list_length(formula_stack)) != 1) { |
638 | if ((psiconv_list_length(formula_stack)) != 1) { |
635 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
639 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
636 | psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", |
640 | psiconv_debug(config,lev+2,off+len,"More than one item left on the stack (%d)", |
637 | psiconv_list_length(formula_stack)); |
641 | psiconv_list_length(formula_stack)); |
638 | goto ERROR8; |
642 | goto ERROR8; |
639 | } |
643 | } |
640 | if ((res = psiconv_list_pop(formula_stack,*result))) |
644 | if ((res = psiconv_list_pop(formula_stack,*result))) |
641 | goto ERROR8; |
645 | goto ERROR8; |
… | |
… | |
643 | free(formula); |
647 | free(formula); |
644 | |
648 | |
645 | if (length) |
649 | if (length) |
646 | *length = len; |
650 | *length = len; |
647 | |
651 | |
648 | psiconv_progress(lev,off+len-1, |
652 | psiconv_progress(config,lev,off+len-1, |
649 | "End of formula element list (total length: %08x)", len); |
653 | "End of formula element list (total length: %08x)", len); |
650 | return 0; |
654 | return 0; |
651 | |
655 | |
652 | ERROR8: |
656 | ERROR8: |
653 | psiconv_free_formula(subformula4); |
657 | psiconv_free_formula(subformula4); |
… | |
… | |
662 | ERROR3: |
666 | ERROR3: |
663 | psiconv_free_formula_list(formula_stack); |
667 | psiconv_free_formula_list(formula_stack); |
664 | ERROR2: |
668 | ERROR2: |
665 | free (*result); |
669 | free (*result); |
666 | ERROR1: |
670 | ERROR1: |
667 | psiconv_warn(lev+1,off,"Reading of formula element list failed"); |
671 | psiconv_error(config,lev+1,off,"Reading of formula element list failed"); |
668 | if (length) |
672 | if (length) |
669 | *length = 0; |
673 | *length = 0; |
670 | if (!res) |
674 | if (!res) |
671 | return -PSICONV_E_NOMEM; |
675 | return -PSICONV_E_NOMEM; |
672 | else |
676 | else |
… | |
… | |
674 | } |
678 | } |
675 | |
679 | |
676 | |
680 | |
677 | |
681 | |
678 | |
682 | |
679 | int psiconv_parse_formula(const psiconv_buffer buf, int lev, |
683 | int psiconv_parse_formula(const psiconv_config config, |
|
|
684 | const psiconv_buffer buf, int lev, |
680 | psiconv_u32 off, int *length, |
685 | psiconv_u32 off, int *length, |
681 | psiconv_formula *result) |
686 | psiconv_formula *result) |
682 | { |
687 | { |
683 | int res=0; |
688 | int res=0; |
684 | int len=0; |
689 | int len=0; |
685 | int leng; |
690 | int leng; |
686 | psiconv_u32 bytelen,formula_end; |
691 | psiconv_u32 bytelen,formula_end; |
687 | psiconv_u8 temp; |
692 | psiconv_u8 temp; |
688 | |
693 | |
689 | psiconv_progress(lev+1,off,"Going to read a formula"); |
694 | psiconv_progress(config,lev+1,off,"Going to read a formula"); |
690 | |
695 | |
691 | psiconv_progress(lev+2,off+len, |
696 | psiconv_progress(config,lev+2,off+len, |
692 | "Going to read the formula byte length"); |
697 | "Going to read the formula byte length"); |
693 | bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res); |
698 | bytelen = psiconv_read_S(config,buf,lev+2,off+len,&leng,&res); |
694 | if (res) |
699 | if (res) |
695 | goto ERROR1; |
700 | goto ERROR1; |
696 | psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen); |
701 | psiconv_debug(config,lev+2,off+len,"Formula byte length: %d",bytelen); |
697 | len += leng; |
702 | len += leng; |
698 | bytelen += len; |
703 | bytelen += len; |
699 | formula_end = off + bytelen; |
704 | formula_end = off + bytelen; |
700 | |
705 | |
701 | psiconv_progress(lev+2,off+len,"Going to read the formula elements list"); |
706 | psiconv_progress(config,lev+2,off+len,"Going to read the formula elements list"); |
702 | if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng, |
707 | if ((res = psiconv_parse_formula_element_list(config,buf,lev+2,off+len,&leng, |
703 | result,formula_end))) |
708 | result,formula_end))) |
704 | goto ERROR1; |
709 | goto ERROR1; |
705 | len += leng; |
710 | len += leng; |
706 | |
711 | |
707 | psiconv_progress(lev+2,off+len,"Going to read the eof marker"); |
712 | psiconv_progress(config,lev+2,off+len,"Going to read the eof marker"); |
708 | temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
713 | temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
709 | if (res) |
714 | if (res) |
710 | goto ERROR2; |
715 | goto ERROR2; |
711 | if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) { |
716 | if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) { |
712 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
717 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
713 | psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x", |
718 | psiconv_debug(config,lev+2,off+len,"Expected marker: %02x, found byte: %02x", |
714 | 0x15,temp); |
719 | 0x15,temp); |
715 | goto ERROR2; |
720 | goto ERROR2; |
716 | } |
721 | } |
717 | len ++; |
722 | len ++; |
718 | |
723 | |
719 | if (off+len != formula_end) { |
724 | if (off+len != formula_end) { |
720 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
725 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
721 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
726 | psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x", |
722 | formula_end,len+off); |
727 | formula_end,len+off); |
723 | goto ERROR2; |
728 | goto ERROR2; |
724 | } |
729 | } |
725 | |
730 | |
726 | if (length) |
731 | if (length) |
727 | *length = len; |
732 | *length = len; |
728 | |
733 | |
729 | psiconv_progress(lev,off+len-1, |
734 | psiconv_progress(config,lev,off+len-1, |
730 | "End of formula (total length: %08x)", len); |
735 | "End of formula (total length: %08x)", len); |
731 | return 0; |
736 | return 0; |
732 | |
737 | |
733 | ERROR2: |
738 | ERROR2: |
734 | psiconv_free_formula(*result); |
739 | psiconv_free_formula(*result); |
735 | ERROR1: |
740 | ERROR1: |
736 | psiconv_warn(lev+1,off,"Reading of formula failed"); |
741 | psiconv_error(config,lev+1,off,"Reading of formula failed"); |
737 | if (length) |
742 | if (length) |
738 | *length = 0; |
743 | *length = 0; |
739 | if (!res) |
744 | if (!res) |
740 | return -PSICONV_E_NOMEM; |
745 | return -PSICONV_E_NOMEM; |
741 | else |
746 | else |