/[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 104 Revision 108
46 {psiconv_formula_op_mul,2,"*"}, 46 {psiconv_formula_op_mul,2,"*"},
47 {psiconv_formula_op_div,2,"/"}, 47 {psiconv_formula_op_div,2,"/"},
48 {psiconv_formula_op_pow,2,"^"}, 48 {psiconv_formula_op_pow,2,"^"},
49 {psiconv_formula_op_pos,1,"+"}, 49 {psiconv_formula_op_pos,1,"+"},
50 {psiconv_formula_op_neg,1,"-"}, 50 {psiconv_formula_op_neg,1,"-"},
51 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 51 {psiconv_formula_op_not,1,"NOT"},
52 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 52 {psiconv_formula_op_and,2,"AND"},
53 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 10 */ 53 {psiconv_formula_op_or,2,"OR"}, /* 10 */
54 {psiconv_formula_op_con,2,"&"}, 54 {psiconv_formula_op_con,2,"&"},
55 {psiconv_formula_op_bra,1,"{}"}, 55 {psiconv_formula_op_bra,1,"()"},
56 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 56 {psiconv_formula_unknown,0,"*UNKNOWN*"},
57 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 57 {psiconv_formula_unknown,0,"*UNKNOWN*"},
58 {psiconv_formula_mark_eof,0,"End of formula"}, 58 {psiconv_formula_mark_eof,0,"End of formula"},
59 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 59 {psiconv_formula_unknown,0,"*UNKNOWN*"},
60 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 60 {psiconv_formula_unknown,0,"*UNKNOWN*"},
284 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 284 {psiconv_formula_unknown,0,"*UNKNOWN*"},
285 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 285 {psiconv_formula_unknown,0,"*UNKNOWN*"},
286 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 286 {psiconv_formula_unknown,0,"*UNKNOWN*"},
287 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 287 {psiconv_formula_unknown,0,"*UNKNOWN*"},
288 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 288 {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*"}}; 289 {psiconv_formula_unknown,0,"*UNKNOWN*"}};
293 290
294static psiconv_string_t psiconv_read_sheet_string(const psiconv_buffer buf, 291static psiconv_string_t psiconv_read_sheet_string(const psiconv_buffer buf,
295 int lev, 292 int lev,
296 psiconv_u32 off,int *length, int *status) 293 psiconv_u32 off,int *length, int *status)
438 if (length) 435 if (length)
439 *length = 0; 436 *length = 0;
440 return res; 437 return res;
441} 438}
442 439
443int psiconv_parse_formula(const psiconv_buffer buf, int lev, 440static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev,
444 psiconv_u32 off, int *length, 441 psiconv_u32 off, int *length,
445 psiconv_formula *result) 442 psiconv_formula *result,
443 psiconv_u32 maxlen)
446{ 444{
447 int res=0; 445 int res=0;
448 int len=0; 446 int len=0;
449 int leng; 447 int leng;
450 int eof = 0; 448 int eof = 0;
451 psiconv_u8 marker; 449 psiconv_u8 marker,submarker;
452 psiconv_u32 bytelen;
453 psiconv_formula_list formula_stack; 450 psiconv_formula_list formula_stack;
454 psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; 451 psiconv_formula formula,subformula,subformula1,subformula2,
452 subformula3,subformula4;
453 psiconv_u16 temp,nr_of_subs;
455 454
456 psiconv_progress(lev+1,off,"Going to read a formula"); 455 psiconv_progress(lev+1,off,"Going to read a formula element list");
457 if (!(*result = malloc(sizeof(**result)))) 456 if (!(*result = malloc(sizeof(**result))))
458 goto ERROR1; 457 goto ERROR1;
459 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 458 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
460 goto ERROR2; 459 goto ERROR2;
461 if (!(formula = malloc(sizeof(*formula)))) 460 if (!(formula = malloc(sizeof(*formula))))
473 subformula3->type = psiconv_formula_unknown; 472 subformula3->type = psiconv_formula_unknown;
474 if (!(subformula4 = malloc(sizeof(*subformula4)))) 473 if (!(subformula4 = malloc(sizeof(*subformula4))))
475 goto ERROR7; 474 goto ERROR7;
476 subformula4->type = psiconv_formula_unknown; 475 subformula4->type = psiconv_formula_unknown;
477 476
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) { 477 while (!eof && len+off < maxlen) {
489 psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); 478 psiconv_progress(lev+3,off+len,"Going to read a formula item marker");
490 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 479 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
491 if (res) 480 if (res)
492 goto ERROR8; 481 goto ERROR8;
493 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 482 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
495 len ++; 484 len ++;
496 485
497 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 486 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
498 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 487 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
499 goto ERROR8; 488 goto ERROR8;
500 } else if (formula_elements[marker].formula_type == 489 } else if ((formula_elements[marker].formula_type ==
501 psiconv_formula_mark_eof) { 490 psiconv_formula_mark_eof) ||
491 (formula_elements[marker].formula_type ==
492 psiconv_formula_mark_opend) ||
493 (formula_elements[marker].formula_type ==
494 psiconv_formula_mark_opsep)) {
495 len--;
502 psiconv_progress(lev+3,off+len,"End of formula"); 496 psiconv_progress(lev+3,off+len,"End of this formula list");
503 eof = 1; 497 eof = 1;
504 } else if (formula_elements[marker].formula_type == 498 } else if (formula_elements[marker].formula_type ==
505 psiconv_formula_dat_int) { 499 psiconv_formula_dat_int) {
506 psiconv_progress(lev+3,off+len,"Next item: an integer"); 500 psiconv_progress(lev+3,off+len,"Next item: an integer");
507 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); 501 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
535 formula->type = formula_elements[marker].formula_type; 529 formula->type = formula_elements[marker].formula_type;
536 len += leng; 530 len += leng;
537 if ((res = psiconv_list_add(formula_stack,formula))) 531 if ((res = psiconv_list_add(formula_stack,formula)))
538 goto ERROR8; 532 goto ERROR8;
539 formula->type = psiconv_formula_unknown; 533 formula->type = psiconv_formula_unknown;
540 } else if (formula_elements[marker].formula_type == 534 } else if ((formula_elements[marker].formula_type ==
535 psiconv_formula_dat_cellblock) ||
536 (formula_elements[marker].formula_type ==
541 psiconv_formula_dat_cellblock) { 537 psiconv_formula_dat_vcellblock)) {
542 psiconv_progress(lev+3,off+len,"Next item: a cell block"); 538 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, 539 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
544 &formula->data.dat_cellblock))) 540 &formula->data.dat_cellblock)))
545 goto ERROR8; 541 goto ERROR8;
546 formula->type = formula_elements[marker].formula_type; 542 formula->type = formula_elements[marker].formula_type;
561 goto ERROR8; 557 goto ERROR8;
562 formula->type = psiconv_formula_unknown; 558 formula->type = psiconv_formula_unknown;
563 } else if ((formula_elements[marker].formula_type == 559 } else if ((formula_elements[marker].formula_type ==
564 psiconv_formula_dat_var) || 560 psiconv_formula_dat_var) ||
565 (formula_elements[marker].formula_type == 561 (formula_elements[marker].formula_type ==
566 psiconv_formula_dat_vcellblock) || 562 psiconv_formula_dat_vcellblock)) {
567 (formula_elements[marker].formula_type ==
568 psiconv_formula_mark_opsep) ||
569 (formula_elements[marker].formula_type ==
570 psiconv_formula_mark_opend)) {
571 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); 563 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!");
572 goto ERROR8; 564 goto ERROR8;
573 } else if (formula_elements[marker].number_of_args == -1) { 565 } else if (formula_elements[marker].number_of_args == -1) {
574 psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); 566 psiconv_progress(lev+3,off+len,"Going to parse a vararg function");
567 if (!(formula->data.fun_operands =
568 psiconv_list_new(sizeof(*formula))))
569 goto ERROR8;
570 formula->type = formula_elements[marker].formula_type;
571 nr_of_subs = 0;
572 do {
573 nr_of_subs ++;
574 psiconv_progress(lev+4,off+len,"Going to read vararg argument %d",
575 nr_of_subs);
576 if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng,
577 &subformula,maxlen)))
578 goto ERROR8;
579 len += leng;
580 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) {
581 psiconv_free_formula(subformula);
582 goto ERROR8;
583 }
584 free(subformula);
585 psiconv_progress(lev+4,off+len,"Going to read the next marker");
586 submarker = psiconv_read_u8(buf,lev+4,off+len,&res);
587 len ++;
588 if (res)
589 goto ERROR8;
590 } while (formula_elements[submarker].formula_type
591 == psiconv_formula_mark_opsep);
592 if (formula_elements[submarker].formula_type
593 != psiconv_formula_mark_opend) {
594 psiconv_warn(lev+3,off+len,"Formula corrupted!");
595 psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker);
575 goto ERROR8; 596 goto ERROR8;
597 }
598 psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x",
599 marker);
600 submarker = psiconv_read_u8(buf,lev+3,off+len,&res);
601 if (res)
602 goto ERROR8;
603 if (submarker != marker) {
604 psiconv_warn(lev+3,off+len,"Formula corrupted!");
605 psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x",
606 marker,submarker);
607 goto ERROR8;
608 }
609 len++;
610 psiconv_progress(lev+3,off+len,
611 "Going to read the number of arguments (%d expected)",
612 nr_of_subs);
613 temp = psiconv_read_u16(buf,lev+3,off+len,&res);
614 if (res)
615 goto ERROR8;
616 if (temp != nr_of_subs) {
617 psiconv_warn(lev+3,off+len,"Formula corrupted!");
618 psiconv_debug(lev+3,off+len,
619 "Read %d arguments, but formula says there are %d",
620 nr_of_subs,temp);
621 goto ERROR8;
622 }
623 len += 2;
624 if ((res = psiconv_list_add(formula_stack,formula)))
625 goto ERROR8;
626 formula->type = psiconv_formula_unknown;
576 } else { 627 } else {
577 if (formula_elements[marker].number_of_args > 0) 628 if (formula_elements[marker].number_of_args > 0)
578 if ((res = psiconv_list_pop(formula_stack,subformula1))) 629 if ((res = psiconv_list_pop(formula_stack,subformula1)))
579 goto ERROR8; 630 goto ERROR8;
580 if (formula_elements[marker].number_of_args > 1) 631 if (formula_elements[marker].number_of_args > 1)
606 goto ERROR8; 657 goto ERROR8;
607 subformula4->type = subformula3->type = subformula2->type = 658 subformula4->type = subformula3->type = subformula2->type =
608 subformula1->type = formula->type = psiconv_formula_unknown; 659 subformula1->type = formula->type = psiconv_formula_unknown;
609 } 660 }
610 } 661 }
611 if ((len != bytelen) || !eof) { 662 if ((len+off > maxlen) || !eof) {
612 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 663 psiconv_warn(lev+2,off+len,"Formula corrupted!");
613 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", 664 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
614 bytelen,len); 665 maxlen,len+off);
615 goto ERROR8; 666 goto ERROR8;
616 } 667 }
617 if ((psiconv_list_length(formula_stack)) != 1) { 668 if ((psiconv_list_length(formula_stack)) != 1) {
618 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 669 psiconv_warn(lev+2,off+len,"Formula corrupted!");
619 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", 670 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)",
627 678
628 if (length) 679 if (length)
629 *length = len; 680 *length = len;
630 681
631 psiconv_progress(lev,off+len-1, 682 psiconv_progress(lev,off+len-1,
632 "End of formula (total length: %08x)", len); 683 "End of formula element list (total length: %08x)", len);
633 return 0; 684 return 0;
634 685
635ERROR8: 686ERROR8:
636 psiconv_free_formula(subformula4); 687 psiconv_free_formula(subformula4);
637ERROR7: 688ERROR7:
645ERROR3: 696ERROR3:
646 psiconv_free_formula_list(formula_stack); 697 psiconv_free_formula_list(formula_stack);
647ERROR2: 698ERROR2:
648 free (*result); 699 free (*result);
649ERROR1: 700ERROR1:
650 psiconv_warn(lev+1,off,"Reading of formula failed"); 701 psiconv_warn(lev+1,off,"Reading of formula element list failed");
651 if (length) 702 if (length)
652 *length = 0; 703 *length = 0;
653 if (!res) 704 if (!res)
654 return -PSICONV_E_NOMEM; 705 return -PSICONV_E_NOMEM;
655 else 706 else
656 return res; 707 return res;
657} 708}
658 709
710
711
712
713int psiconv_parse_formula(const psiconv_buffer buf, int lev,
714 psiconv_u32 off, int *length,
715 psiconv_formula *result)
716{
717 int res=0;
718 int len=0;
719 int leng;
720 psiconv_u32 bytelen,formula_end;
721 psiconv_u8 temp;
722
723 psiconv_progress(lev+1,off,"Going to read a formula");
724
725 psiconv_progress(lev+2,off+len,
726 "Going to read the formula byte length");
727 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
728 if (res)
729 goto ERROR1;
730 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
731 len += leng;
732 bytelen += len;
733 formula_end = off + bytelen;
734
735 psiconv_progress(lev+2,off+len,"Going to read the formula elements list");
736 if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng,
737 result,formula_end)))
738 goto ERROR1;
739 len += leng;
740
741 psiconv_progress(lev+2,off+len,"Going to read the eof marker");
742 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
743 if (res)
744 goto ERROR2;
745 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) {
746 psiconv_warn(lev+2,off+len,"Formula corrupted!");
747 psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x",
748 0x15,temp);
749 goto ERROR2;
750 }
751 len ++;
752
753 if (off+len != formula_end) {
754 psiconv_warn(lev+2,off+len,"Formula corrupted!");
755 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
756 formula_end,len+off);
757 goto ERROR2;
758 }
759
760 if (length)
761 *length = len;
762
763 psiconv_progress(lev,off+len-1,
764 "End of formula (total length: %08x)", len);
765 return 0;
766
767ERROR2:
768 psiconv_free_formula(*result);
769ERROR1:
770 psiconv_warn(lev+1,off,"Reading of formula failed");
771 if (length)
772 *length = 0;
773 if (!res)
774 return -PSICONV_E_NOMEM;
775 else
776 return res;
777}
778
779

Legend:
Removed from v.104  
changed lines
  Added in v.108

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