/[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 102 Revision 129
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
294 290
295static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev, 291static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev,
296 psiconv_u32 off, int *length, 292 psiconv_u32 off, int *length,
297 psiconv_sheet_ref_t *result) 293 psiconv_sheet_ref_t *result)
298{ 294{
383 if (length) 379 if (length)
384 *length = 0; 380 *length = 0;
385 return res; 381 return res;
386} 382}
387 383
388int psiconv_parse_formula(const psiconv_buffer buf, int lev, 384static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev,
389 psiconv_u32 off, int *length, 385 psiconv_u32 off, int *length,
390 psiconv_formula *result) 386 psiconv_formula *result,
387 psiconv_u32 maxlen)
391{ 388{
392 int res=0; 389 int res=0;
393 int len=0; 390 int len=0;
394 int leng; 391 int leng;
395 int eof = 0; 392 int eof = 0;
396 psiconv_u8 marker; 393 psiconv_u8 marker,submarker,submarker2;
397 psiconv_u32 bytelen;
398 psiconv_formula_list formula_stack; 394 psiconv_formula_list formula_stack;
399 psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; 395 psiconv_formula formula,subformula,subformula1,subformula2,
396 subformula3,subformula4;
397 psiconv_u16 temp,nr_of_subs;
400 398
401 psiconv_progress(lev+1,off,"Going to read a formula"); 399 psiconv_progress(lev+1,off,"Going to read a formula element list");
402 if (!(*result = malloc(sizeof(**result)))) 400 if (!(*result = malloc(sizeof(**result))))
403 goto ERROR1; 401 goto ERROR1;
404 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 402 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
405 goto ERROR2; 403 goto ERROR2;
406 if (!(formula = malloc(sizeof(*formula)))) 404 if (!(formula = malloc(sizeof(*formula))))
418 subformula3->type = psiconv_formula_unknown; 416 subformula3->type = psiconv_formula_unknown;
419 if (!(subformula4 = malloc(sizeof(*subformula4)))) 417 if (!(subformula4 = malloc(sizeof(*subformula4))))
420 goto ERROR7; 418 goto ERROR7;
421 subformula4->type = psiconv_formula_unknown; 419 subformula4->type = psiconv_formula_unknown;
422 420
423 psiconv_progress(lev+2,off+len,
424 "Going to read the formula byte length");
425 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
426 if (res)
427 goto ERROR8;
428 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
429 len += leng;
430 bytelen += len;
431
432 psiconv_progress(lev+2,off+len,"Going to read the formula items");
433 while (!eof && len < bytelen) { 421 while (!eof && len+off < maxlen) {
434 psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); 422 psiconv_progress(lev+3,off+len,"Going to read a formula item marker");
435 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 423 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
436 if (res) 424 if (res)
437 goto ERROR8; 425 goto ERROR8;
438 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 426 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
440 len ++; 428 len ++;
441 429
442 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 430 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
443 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 431 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
444 goto ERROR8; 432 goto ERROR8;
445 } else if (formula_elements[marker].formula_type == 433 } else if ((formula_elements[marker].formula_type ==
446 psiconv_formula_mark_eof) { 434 psiconv_formula_mark_eof) ||
435 (formula_elements[marker].formula_type ==
436 psiconv_formula_mark_opend) ||
437 (formula_elements[marker].formula_type ==
438 psiconv_formula_mark_opsep)) {
439 len--;
447 psiconv_progress(lev+3,off+len,"End of formula"); 440 psiconv_progress(lev+3,off+len,"End of this formula list");
448 eof = 1; 441 eof = 1;
449 } else if (formula_elements[marker].formula_type == 442 } else if (formula_elements[marker].formula_type ==
450 psiconv_formula_dat_int) { 443 psiconv_formula_dat_int) {
451 psiconv_progress(lev+3,off+len,"Next item: an integer"); 444 psiconv_progress(lev+3,off+len,"Next item: an integer");
452 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); 445 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
480 formula->type = formula_elements[marker].formula_type; 473 formula->type = formula_elements[marker].formula_type;
481 len += leng; 474 len += leng;
482 if ((res = psiconv_list_add(formula_stack,formula))) 475 if ((res = psiconv_list_add(formula_stack,formula)))
483 goto ERROR8; 476 goto ERROR8;
484 formula->type = psiconv_formula_unknown; 477 formula->type = psiconv_formula_unknown;
485 } else if (formula_elements[marker].formula_type == 478 } else if ((formula_elements[marker].formula_type ==
479 psiconv_formula_dat_cellblock) ||
480 (formula_elements[marker].formula_type ==
486 psiconv_formula_dat_cellblock) { 481 psiconv_formula_dat_vcellblock)) {
487 psiconv_progress(lev+3,off+len,"Next item: a cell block"); 482 psiconv_progress(lev+3,off+len,"Next item: a cell block");
488 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, 483 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
489 &formula->data.dat_cellblock))) 484 &formula->data.dat_cellblock)))
490 goto ERROR8; 485 goto ERROR8;
491 formula->type = formula_elements[marker].formula_type; 486 formula->type = formula_elements[marker].formula_type;
492 len += leng; 487 len += leng;
493 if ((res = psiconv_list_add(formula_stack,formula))) 488 if ((res = psiconv_list_add(formula_stack,formula)))
494 goto ERROR8; 489 goto ERROR8;
495 formula->type = psiconv_formula_unknown; 490 formula->type = psiconv_formula_unknown;
491 } else if (formula_elements[marker].formula_type ==
492 psiconv_formula_dat_string) {
493 psiconv_progress(lev+3,off+len,"Next item: a string");
494 formula->data.dat_string =
495 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res);
496 if (res)
497 goto ERROR8;
498 formula->type = formula_elements[marker].formula_type;
499 len += leng;
500 if ((res = psiconv_list_add(formula_stack,formula)))
501 goto ERROR8;
502 formula->type = psiconv_formula_unknown;
496 } else if ((formula_elements[marker].formula_type == 503 } else if ((formula_elements[marker].formula_type ==
497 psiconv_formula_dat_var) || 504 psiconv_formula_dat_var)) {
498 (formula_elements[marker].formula_type == 505 psiconv_progress(lev+3,off+len,"Next item: a variable reference");
499 psiconv_formula_dat_string) || 506 formula->data.dat_variable = psiconv_read_u32(buf,lev+2,off+len,&res);
500 (formula_elements[marker].formula_type == 507 if (res)
501 psiconv_formula_dat_vcellblock) ||
502 (formula_elements[marker].formula_type ==
503 psiconv_formula_mark_opsep) ||
504 (formula_elements[marker].formula_type ==
505 psiconv_formula_mark_opend)) {
506 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!");
507 goto ERROR8; 508 goto ERROR8;
509 formula->type = formula_elements[marker].formula_type;
510 len += 4;
511 if ((res = psiconv_list_add(formula_stack,formula)))
512 goto ERROR8;
513 formula->type = psiconv_formula_unknown;
508 } else if (formula_elements[marker].number_of_args == -1) { 514 } else if (formula_elements[marker].number_of_args == -1) {
509 psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); 515 psiconv_progress(lev+3,off+len,"Going to parse a vararg function");
516 if (!(formula->data.fun_operands =
517 psiconv_list_new(sizeof(*formula))))
518 goto ERROR8;
519 formula->type = formula_elements[marker].formula_type;
520 nr_of_subs = 0;
521 do {
522 nr_of_subs ++;
523 psiconv_progress(lev+4,off+len,"Going to read vararg argument %d",
524 nr_of_subs);
525 if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng,
526 &subformula,maxlen)))
527 goto ERROR8;
528 len += leng;
529 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) {
530 psiconv_free_formula(subformula);
531 goto ERROR8;
532 }
533 free(subformula);
534 psiconv_progress(lev+4,off+len,"Going to read the next marker");
535 submarker = psiconv_read_u8(buf,lev+4,off+len,&res);
536 len ++;
537 if (res)
538 goto ERROR8;
539 submarker2 = psiconv_read_u8(buf,lev+4,off+len,&res);
540 if (res)
541 goto ERROR8;
542 } while ((formula_elements[submarker].formula_type
543 == psiconv_formula_mark_opsep) &&
544 (formula_elements[submarker2].formula_type
545 != psiconv_formula_mark_opend));
546 if ((formula_elements[submarker].formula_type ==
547 psiconv_formula_mark_opsep) &&
548 (formula_elements[submarker2].formula_type ==
549 psiconv_formula_mark_opend)) {
550 submarker=submarker2;
551 len++;
552 }
553 if (formula_elements[submarker].formula_type
554 != psiconv_formula_mark_opend) {
555 psiconv_warn(lev+3,off+len,"Formula corrupted!");
556 psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker);
510 goto ERROR8; 557 goto ERROR8;
558 }
559 psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x",
560 marker);
561 submarker = psiconv_read_u8(buf,lev+3,off+len,&res);
562 if (res)
563 goto ERROR8;
564 if (submarker != marker) {
565 psiconv_warn(lev+3,off+len,"Formula corrupted!");
566 psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x",
567 marker,submarker);
568 goto ERROR8;
569 }
570 len++;
571 psiconv_progress(lev+3,off+len,
572 "Going to read the number of arguments (%d expected)",
573 nr_of_subs);
574 temp = psiconv_read_u16(buf,lev+3,off+len,&res);
575 if (res)
576 goto ERROR8;
577 if (temp != nr_of_subs) {
578 psiconv_warn(lev+3,off+len,"Formula corrupted!");
579 psiconv_debug(lev+3,off+len,
580 "Read %d arguments, but formula says there are %d",
581 nr_of_subs,temp);
582 goto ERROR8;
583 }
584 len += 2;
585 if ((res = psiconv_list_add(formula_stack,formula)))
586 goto ERROR8;
587 formula->type = psiconv_formula_unknown;
511 } else { 588 } else {
512 if (formula_elements[marker].number_of_args > 0) 589 if (formula_elements[marker].number_of_args > 0)
513 if ((res = psiconv_list_pop(formula_stack,subformula1))) 590 if ((res = psiconv_list_pop(formula_stack,subformula1)))
514 goto ERROR8; 591 goto ERROR8;
515 if (formula_elements[marker].number_of_args > 1) 592 if (formula_elements[marker].number_of_args > 1)
541 goto ERROR8; 618 goto ERROR8;
542 subformula4->type = subformula3->type = subformula2->type = 619 subformula4->type = subformula3->type = subformula2->type =
543 subformula1->type = formula->type = psiconv_formula_unknown; 620 subformula1->type = formula->type = psiconv_formula_unknown;
544 } 621 }
545 } 622 }
546 if ((len != bytelen) || !eof) { 623 if ((len+off > maxlen) || !eof) {
547 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 624 psiconv_warn(lev+2,off+len,"Formula corrupted!");
548 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", 625 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
549 bytelen,len); 626 maxlen,len+off);
550 goto ERROR8; 627 goto ERROR8;
551 } 628 }
552 if ((psiconv_list_length(formula_stack)) != 1) { 629 if ((psiconv_list_length(formula_stack)) != 1) {
553 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 630 psiconv_warn(lev+2,off+len,"Formula corrupted!");
554 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", 631 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)",
562 639
563 if (length) 640 if (length)
564 *length = len; 641 *length = len;
565 642
566 psiconv_progress(lev,off+len-1, 643 psiconv_progress(lev,off+len-1,
567 "End of formula (total length: %08x)", len); 644 "End of formula element list (total length: %08x)", len);
568 return 0; 645 return 0;
569 646
570ERROR8: 647ERROR8:
571 psiconv_free_formula(subformula4); 648 psiconv_free_formula(subformula4);
572ERROR7: 649ERROR7:
580ERROR3: 657ERROR3:
581 psiconv_free_formula_list(formula_stack); 658 psiconv_free_formula_list(formula_stack);
582ERROR2: 659ERROR2:
583 free (*result); 660 free (*result);
584ERROR1: 661ERROR1:
585 psiconv_warn(lev+1,off,"Reading of formula failed"); 662 psiconv_warn(lev+1,off,"Reading of formula element list failed");
586 if (length) 663 if (length)
587 *length = 0; 664 *length = 0;
588 if (!res) 665 if (!res)
589 return -PSICONV_E_NOMEM; 666 return -PSICONV_E_NOMEM;
590 else 667 else
591 return res; 668 return res;
592} 669}
593 670
671
672
673
674int psiconv_parse_formula(const psiconv_buffer buf, int lev,
675 psiconv_u32 off, int *length,
676 psiconv_formula *result)
677{
678 int res=0;
679 int len=0;
680 int leng;
681 psiconv_u32 bytelen,formula_end;
682 psiconv_u8 temp;
683
684 psiconv_progress(lev+1,off,"Going to read a formula");
685
686 psiconv_progress(lev+2,off+len,
687 "Going to read the formula byte length");
688 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
689 if (res)
690 goto ERROR1;
691 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
692 len += leng;
693 bytelen += len;
694 formula_end = off + bytelen;
695
696 psiconv_progress(lev+2,off+len,"Going to read the formula elements list");
697 if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng,
698 result,formula_end)))
699 goto ERROR1;
700 len += leng;
701
702 psiconv_progress(lev+2,off+len,"Going to read the eof marker");
703 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
704 if (res)
705 goto ERROR2;
706 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) {
707 psiconv_warn(lev+2,off+len,"Formula corrupted!");
708 psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x",
709 0x15,temp);
710 goto ERROR2;
711 }
712 len ++;
713
714 if (off+len != formula_end) {
715 psiconv_warn(lev+2,off+len,"Formula corrupted!");
716 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
717 formula_end,len+off);
718 goto ERROR2;
719 }
720
721 if (length)
722 *length = len;
723
724 psiconv_progress(lev,off+len-1,
725 "End of formula (total length: %08x)", len);
726 return 0;
727
728ERROR2:
729 psiconv_free_formula(*result);
730ERROR1:
731 psiconv_warn(lev+1,off,"Reading of formula failed");
732 if (length)
733 *length = 0;
734 if (!res)
735 return -PSICONV_E_NOMEM;
736 else
737 return res;
738}
739
740

Legend:
Removed from v.102  
changed lines
  Added in v.129

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