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

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

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

Revision 103 Revision 142
22 22
23#include <stdlib.h> 23#include <stdlib.h>
24 24
25#include "parse_routines.h" 25#include "parse_routines.h"
26#include "error.h" 26#include "error.h"
27
28#ifdef DMALLOC
29#include <dmalloc.h>
30#endif
31
27 32
28struct formula_element 33struct formula_element
29{ 34{
30 psiconv_formula_type_t formula_type; 35 psiconv_formula_type_t formula_type;
31 int number_of_args; 36 int number_of_args;
46 {psiconv_formula_op_mul,2,"*"}, 51 {psiconv_formula_op_mul,2,"*"},
47 {psiconv_formula_op_div,2,"/"}, 52 {psiconv_formula_op_div,2,"/"},
48 {psiconv_formula_op_pow,2,"^"}, 53 {psiconv_formula_op_pow,2,"^"},
49 {psiconv_formula_op_pos,1,"+"}, 54 {psiconv_formula_op_pos,1,"+"},
50 {psiconv_formula_op_neg,1,"-"}, 55 {psiconv_formula_op_neg,1,"-"},
51 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 56 {psiconv_formula_op_not,1,"NOT"},
52 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 57 {psiconv_formula_op_and,2,"AND"},
53 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 10 */ 58 {psiconv_formula_op_or,2,"OR"}, /* 10 */
54 {psiconv_formula_op_con,2,"&"}, 59 {psiconv_formula_op_con,2,"&"},
55 {psiconv_formula_op_bra,1,"{}"}, 60 {psiconv_formula_op_bra,1,"()"},
56 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 61 {psiconv_formula_unknown,0,"*UNKNOWN*"},
57 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 62 {psiconv_formula_unknown,0,"*UNKNOWN*"},
58 {psiconv_formula_mark_eof,0,"End of formula"}, 63 {psiconv_formula_mark_eof,0,"End of formula"},
59 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 64 {psiconv_formula_unknown,0,"*UNKNOWN*"},
60 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 65 {psiconv_formula_unknown,0,"*UNKNOWN*"},
284 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 289 {psiconv_formula_unknown,0,"*UNKNOWN*"},
285 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 290 {psiconv_formula_unknown,0,"*UNKNOWN*"},
286 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 291 {psiconv_formula_unknown,0,"*UNKNOWN*"},
287 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 292 {psiconv_formula_unknown,0,"*UNKNOWN*"},
288 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 293 {psiconv_formula_unknown,0,"*UNKNOWN*"},
289 {psiconv_formula_unknown,0,"*UNKNOWN*"},
290 {psiconv_formula_unknown,0,"*UNKNOWN*"},
291 {psiconv_formula_unknown,0,"*UNKNOWN*"},
292 {psiconv_formula_unknown,0,"*UNKNOWN*"}}; 294 {psiconv_formula_unknown,0,"*UNKNOWN*"}};
293
294static psiconv_string_t psiconv_read_sheet_string(const psiconv_buffer buf,
295 int lev,
296 psiconv_u32 off,int *length, int *status)
297{
298 int stringlen,i,len,localstatus;
299 psiconv_string_t result;
300 char *res_copy;
301
302 psiconv_progress(lev+1,off,"Going to read a sheet string");
303
304 stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus);
305 if (localstatus)
306 goto ERROR1;
307 psiconv_debug(lev+2,off,"Length: %i",stringlen);
308 len = 1;
309
310 result = malloc(stringlen + 1);
311 if (!result)
312 goto ERROR1;
313 for (i = 0; (i < stringlen) && !localstatus; i++)
314 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus);
315 if (localstatus)
316 goto ERROR2;
317 result[stringlen] = 0;
318 len += stringlen;
319
320 res_copy = psiconv_make_printable(result);
321 if (!res_copy)
322 goto ERROR2;
323 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
324 free(res_copy);
325
326 if (length)
327 *length = len;
328
329 if (status)
330 *status = 0;
331
332 psiconv_progress(lev+1,off+len-1,"End of sheet string (total length: %08x)",
333 len);
334
335 return result;
336
337
338ERROR2:
339 free(result);
340ERROR1:
341 psiconv_warn(lev+1,off,"Reading of sheet string failed");
342 if (status)
343 *status = localstatus;
344 if (length)
345 *length = 0;
346 return NULL;
347}
348
349 295
350static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev, 296static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev,
351 psiconv_u32 off, int *length, 297 psiconv_u32 off, int *length,
352 psiconv_sheet_ref_t *result) 298 psiconv_sheet_ref_t *result)
353{ 299{
438 if (length) 384 if (length)
439 *length = 0; 385 *length = 0;
440 return res; 386 return res;
441} 387}
442 388
443int psiconv_parse_formula(const psiconv_buffer buf, int lev, 389static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev,
444 psiconv_u32 off, int *length, 390 psiconv_u32 off, int *length,
445 psiconv_formula *result) 391 psiconv_formula *result,
392 psiconv_u32 maxlen)
446{ 393{
447 int res=0; 394 int res=0;
448 int len=0; 395 int len=0;
449 int leng; 396 int leng;
450 int eof = 0; 397 int eof = 0;
451 psiconv_u8 marker; 398 psiconv_u8 marker,submarker,submarker2;
452 psiconv_u32 bytelen;
453 psiconv_formula_list formula_stack; 399 psiconv_formula_list formula_stack;
454 psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; 400 psiconv_formula formula,subformula,subformula1,subformula2,
401 subformula3,subformula4;
402 psiconv_u16 temp,nr_of_subs;
455 403
456 psiconv_progress(lev+1,off,"Going to read a formula"); 404 psiconv_progress(lev+1,off,"Going to read a formula element list");
457 if (!(*result = malloc(sizeof(**result)))) 405 if (!(*result = malloc(sizeof(**result))))
458 goto ERROR1; 406 goto ERROR1;
459 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 407 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
460 goto ERROR2; 408 goto ERROR2;
461 if (!(formula = malloc(sizeof(*formula)))) 409 if (!(formula = malloc(sizeof(*formula))))
473 subformula3->type = psiconv_formula_unknown; 421 subformula3->type = psiconv_formula_unknown;
474 if (!(subformula4 = malloc(sizeof(*subformula4)))) 422 if (!(subformula4 = malloc(sizeof(*subformula4))))
475 goto ERROR7; 423 goto ERROR7;
476 subformula4->type = psiconv_formula_unknown; 424 subformula4->type = psiconv_formula_unknown;
477 425
478 psiconv_progress(lev+2,off+len,
479 "Going to read the formula byte length");
480 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
481 if (res)
482 goto ERROR8;
483 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
484 len += leng;
485 bytelen += len;
486
487 psiconv_progress(lev+2,off+len,"Going to read the formula items");
488 while (!eof && len < bytelen) { 426 while (!eof && len+off < maxlen) {
489 psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); 427 psiconv_progress(lev+3,off+len,"Going to read a formula item marker");
490 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 428 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
491 if (res) 429 if (res)
492 goto ERROR8; 430 goto ERROR8;
493 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 431 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
495 len ++; 433 len ++;
496 434
497 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 435 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
498 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 436 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
499 goto ERROR8; 437 goto ERROR8;
500 } else if (formula_elements[marker].formula_type == 438 } else if ((formula_elements[marker].formula_type ==
501 psiconv_formula_mark_eof) { 439 psiconv_formula_mark_eof) ||
440 (formula_elements[marker].formula_type ==
441 psiconv_formula_mark_opend) ||
442 (formula_elements[marker].formula_type ==
443 psiconv_formula_mark_opsep)) {
444 len--;
502 psiconv_progress(lev+3,off+len,"End of formula"); 445 psiconv_progress(lev+3,off+len,"End of this formula list");
503 eof = 1; 446 eof = 1;
504 } else if (formula_elements[marker].formula_type == 447 } else if (formula_elements[marker].formula_type ==
505 psiconv_formula_dat_int) { 448 psiconv_formula_dat_int) {
506 psiconv_progress(lev+3,off+len,"Next item: an integer"); 449 psiconv_progress(lev+3,off+len,"Next item: an integer");
507 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); 450 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
535 formula->type = formula_elements[marker].formula_type; 478 formula->type = formula_elements[marker].formula_type;
536 len += leng; 479 len += leng;
537 if ((res = psiconv_list_add(formula_stack,formula))) 480 if ((res = psiconv_list_add(formula_stack,formula)))
538 goto ERROR8; 481 goto ERROR8;
539 formula->type = psiconv_formula_unknown; 482 formula->type = psiconv_formula_unknown;
540 } else if (formula_elements[marker].formula_type == 483 } else if ((formula_elements[marker].formula_type ==
484 psiconv_formula_dat_cellblock) ||
485 (formula_elements[marker].formula_type ==
541 psiconv_formula_dat_cellblock) { 486 psiconv_formula_dat_vcellblock)) {
542 psiconv_progress(lev+3,off+len,"Next item: a cell block"); 487 psiconv_progress(lev+3,off+len,"Next item: a cell block");
543 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, 488 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
544 &formula->data.dat_cellblock))) 489 &formula->data.dat_cellblock)))
545 goto ERROR8; 490 goto ERROR8;
546 formula->type = formula_elements[marker].formula_type; 491 formula->type = formula_elements[marker].formula_type;
550 formula->type = psiconv_formula_unknown; 495 formula->type = psiconv_formula_unknown;
551 } else if (formula_elements[marker].formula_type == 496 } else if (formula_elements[marker].formula_type ==
552 psiconv_formula_dat_string) { 497 psiconv_formula_dat_string) {
553 psiconv_progress(lev+3,off+len,"Next item: a string"); 498 psiconv_progress(lev+3,off+len,"Next item: a string");
554 formula->data.dat_string = 499 formula->data.dat_string =
555 psiconv_read_sheet_string(buf,lev+2,off+len,&leng,&res); 500 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res);
556 if (res) 501 if (res)
557 goto ERROR8; 502 goto ERROR8;
558 formula->type = formula_elements[marker].formula_type; 503 formula->type = formula_elements[marker].formula_type;
559 len += leng; 504 len += leng;
560 if ((res = psiconv_list_add(formula_stack,formula))) 505 if ((res = psiconv_list_add(formula_stack,formula)))
561 goto ERROR8; 506 goto ERROR8;
562 formula->type = psiconv_formula_unknown; 507 formula->type = psiconv_formula_unknown;
563 } else if ((formula_elements[marker].formula_type == 508 } else if ((formula_elements[marker].formula_type ==
564 psiconv_formula_dat_var) || 509 psiconv_formula_dat_var)) {
565 (formula_elements[marker].formula_type == 510 psiconv_progress(lev+3,off+len,"Next item: a variable reference");
566 psiconv_formula_dat_string) || 511 formula->data.dat_variable = psiconv_read_u32(buf,lev+2,off+len,&res);
567 (formula_elements[marker].formula_type == 512 if (res)
568 psiconv_formula_dat_vcellblock) ||
569 (formula_elements[marker].formula_type ==
570 psiconv_formula_mark_opsep) ||
571 (formula_elements[marker].formula_type ==
572 psiconv_formula_mark_opend)) {
573 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!");
574 goto ERROR8; 513 goto ERROR8;
514 formula->type = formula_elements[marker].formula_type;
515 len += 4;
516 if ((res = psiconv_list_add(formula_stack,formula)))
517 goto ERROR8;
518 formula->type = psiconv_formula_unknown;
575 } else if (formula_elements[marker].number_of_args == -1) { 519 } else if (formula_elements[marker].number_of_args == -1) {
576 psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); 520 psiconv_progress(lev+3,off+len,"Going to parse a vararg function");
521 if (!(formula->data.fun_operands =
522 psiconv_list_new(sizeof(*formula))))
523 goto ERROR8;
524 formula->type = formula_elements[marker].formula_type;
525 nr_of_subs = 0;
526 do {
527 nr_of_subs ++;
528 psiconv_progress(lev+4,off+len,"Going to read vararg argument %d",
529 nr_of_subs);
530 if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng,
531 &subformula,maxlen)))
532 goto ERROR8;
533 len += leng;
534 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) {
535 psiconv_free_formula(subformula);
536 goto ERROR8;
537 }
538 free(subformula);
539 psiconv_progress(lev+4,off+len,"Going to read the next marker");
540 submarker = psiconv_read_u8(buf,lev+4,off+len,&res);
541 len ++;
542 if (res)
543 goto ERROR8;
544 submarker2 = psiconv_read_u8(buf,lev+4,off+len,&res);
545 if (res)
546 goto ERROR8;
547 } while ((formula_elements[submarker].formula_type
548 == psiconv_formula_mark_opsep) &&
549 (formula_elements[submarker2].formula_type
550 != psiconv_formula_mark_opend));
551 if ((formula_elements[submarker].formula_type ==
552 psiconv_formula_mark_opsep) &&
553 (formula_elements[submarker2].formula_type ==
554 psiconv_formula_mark_opend)) {
555 submarker=submarker2;
556 len++;
557 }
558 if (formula_elements[submarker].formula_type
559 != psiconv_formula_mark_opend) {
560 psiconv_warn(lev+3,off+len,"Formula corrupted!");
561 psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker);
577 goto ERROR8; 562 goto ERROR8;
563 }
564 psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x",
565 marker);
566 submarker = psiconv_read_u8(buf,lev+3,off+len,&res);
567 if (res)
568 goto ERROR8;
569 if (submarker != marker) {
570 psiconv_warn(lev+3,off+len,"Formula corrupted!");
571 psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x",
572 marker,submarker);
573 goto ERROR8;
574 }
575 len++;
576 psiconv_progress(lev+3,off+len,
577 "Going to read the number of arguments (%d expected)",
578 nr_of_subs);
579 temp = psiconv_read_u16(buf,lev+3,off+len,&res);
580 if (res)
581 goto ERROR8;
582 if (temp != nr_of_subs) {
583 psiconv_warn(lev+3,off+len,"Formula corrupted!");
584 psiconv_debug(lev+3,off+len,
585 "Read %d arguments, but formula says there are %d",
586 nr_of_subs,temp);
587 goto ERROR8;
588 }
589 len += 2;
590 if ((res = psiconv_list_add(formula_stack,formula)))
591 goto ERROR8;
592 formula->type = psiconv_formula_unknown;
578 } else { 593 } else {
579 if (formula_elements[marker].number_of_args > 0) 594 if (formula_elements[marker].number_of_args > 0)
580 if ((res = psiconv_list_pop(formula_stack,subformula1))) 595 if ((res = psiconv_list_pop(formula_stack,subformula1)))
581 goto ERROR8; 596 goto ERROR8;
582 if (formula_elements[marker].number_of_args > 1) 597 if (formula_elements[marker].number_of_args > 1)
608 goto ERROR8; 623 goto ERROR8;
609 subformula4->type = subformula3->type = subformula2->type = 624 subformula4->type = subformula3->type = subformula2->type =
610 subformula1->type = formula->type = psiconv_formula_unknown; 625 subformula1->type = formula->type = psiconv_formula_unknown;
611 } 626 }
612 } 627 }
613 if ((len != bytelen) || !eof) { 628 if ((len+off > maxlen) || !eof) {
614 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 629 psiconv_warn(lev+2,off+len,"Formula corrupted!");
615 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", 630 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
616 bytelen,len); 631 maxlen,len+off);
617 goto ERROR8; 632 goto ERROR8;
618 } 633 }
619 if ((psiconv_list_length(formula_stack)) != 1) { 634 if ((psiconv_list_length(formula_stack)) != 1) {
620 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 635 psiconv_warn(lev+2,off+len,"Formula corrupted!");
621 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", 636 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)",
629 644
630 if (length) 645 if (length)
631 *length = len; 646 *length = len;
632 647
633 psiconv_progress(lev,off+len-1, 648 psiconv_progress(lev,off+len-1,
634 "End of formula (total length: %08x)", len); 649 "End of formula element list (total length: %08x)", len);
635 return 0; 650 return 0;
636 651
637ERROR8: 652ERROR8:
638 psiconv_free_formula(subformula4); 653 psiconv_free_formula(subformula4);
639ERROR7: 654ERROR7:
647ERROR3: 662ERROR3:
648 psiconv_free_formula_list(formula_stack); 663 psiconv_free_formula_list(formula_stack);
649ERROR2: 664ERROR2:
650 free (*result); 665 free (*result);
651ERROR1: 666ERROR1:
652 psiconv_warn(lev+1,off,"Reading of formula failed"); 667 psiconv_warn(lev+1,off,"Reading of formula element list failed");
653 if (length) 668 if (length)
654 *length = 0; 669 *length = 0;
655 if (!res) 670 if (!res)
656 return -PSICONV_E_NOMEM; 671 return -PSICONV_E_NOMEM;
657 else 672 else
658 return res; 673 return res;
659} 674}
660 675
676
677
678
679int psiconv_parse_formula(const psiconv_buffer buf, int lev,
680 psiconv_u32 off, int *length,
681 psiconv_formula *result)
682{
683 int res=0;
684 int len=0;
685 int leng;
686 psiconv_u32 bytelen,formula_end;
687 psiconv_u8 temp;
688
689 psiconv_progress(lev+1,off,"Going to read a formula");
690
691 psiconv_progress(lev+2,off+len,
692 "Going to read the formula byte length");
693 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
694 if (res)
695 goto ERROR1;
696 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
697 len += leng;
698 bytelen += len;
699 formula_end = off + bytelen;
700
701 psiconv_progress(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,
703 result,formula_end)))
704 goto ERROR1;
705 len += leng;
706
707 psiconv_progress(lev+2,off+len,"Going to read the eof marker");
708 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
709 if (res)
710 goto ERROR2;
711 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) {
712 psiconv_warn(lev+2,off+len,"Formula corrupted!");
713 psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x",
714 0x15,temp);
715 goto ERROR2;
716 }
717 len ++;
718
719 if (off+len != formula_end) {
720 psiconv_warn(lev+2,off+len,"Formula corrupted!");
721 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
722 formula_end,len+off);
723 goto ERROR2;
724 }
725
726 if (length)
727 *length = len;
728
729 psiconv_progress(lev,off+len-1,
730 "End of formula (total length: %08x)", len);
731 return 0;
732
733ERROR2:
734 psiconv_free_formula(*result);
735ERROR1:
736 psiconv_warn(lev+1,off,"Reading of formula failed");
737 if (length)
738 *length = 0;
739 if (!res)
740 return -PSICONV_E_NOMEM;
741 else
742 return res;
743}
744
745

Legend:
Removed from v.103  
changed lines
  Added in v.142

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