/[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 107
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_op_not,1,"NOT"},
52 {psiconv_formula_op_and,2,"AND"},
53 {psiconv_formula_op_or,2,"OR"},
51 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 54 {psiconv_formula_unknown,0,"*UNKNOWN*"},
52 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 55 {psiconv_formula_unknown,0,"*UNKNOWN*"},
53 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 10 */ 56 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 10 */
54 {psiconv_formula_op_con,2,"&"}, 57 {psiconv_formula_op_con,2,"&"},
55 {psiconv_formula_op_bra,1,"{}"}, 58 {psiconv_formula_op_bra,1,"()"},
56 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 59 {psiconv_formula_unknown,0,"*UNKNOWN*"},
57 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 60 {psiconv_formula_unknown,0,"*UNKNOWN*"},
58 {psiconv_formula_mark_eof,0,"End of formula"}, 61 {psiconv_formula_mark_eof,0,"End of formula"},
59 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 62 {psiconv_formula_unknown,0,"*UNKNOWN*"},
60 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 63 {psiconv_formula_unknown,0,"*UNKNOWN*"},
284 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 287 {psiconv_formula_unknown,0,"*UNKNOWN*"},
285 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 288 {psiconv_formula_unknown,0,"*UNKNOWN*"},
286 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 289 {psiconv_formula_unknown,0,"*UNKNOWN*"},
287 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 290 {psiconv_formula_unknown,0,"*UNKNOWN*"},
288 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 291 {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*"}}; 292 {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}
293 348
294 349
295static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev, 350static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev,
296 psiconv_u32 off, int *length, 351 psiconv_u32 off, int *length,
297 psiconv_sheet_ref_t *result) 352 psiconv_sheet_ref_t *result)
383 if (length) 438 if (length)
384 *length = 0; 439 *length = 0;
385 return res; 440 return res;
386} 441}
387 442
388int psiconv_parse_formula(const psiconv_buffer buf, int lev, 443static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev,
389 psiconv_u32 off, int *length, 444 psiconv_u32 off, int *length,
390 psiconv_formula *result) 445 psiconv_formula *result,
446 psiconv_u32 maxlen)
391{ 447{
392 int res=0; 448 int res=0;
393 int len=0; 449 int len=0;
394 int leng; 450 int leng;
395 int eof = 0; 451 int eof = 0;
396 psiconv_u8 marker; 452 psiconv_u8 marker,submarker;
397 psiconv_u32 bytelen;
398 psiconv_formula_list formula_stack; 453 psiconv_formula_list formula_stack;
399 psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; 454 psiconv_formula formula,subformula,subformula1,subformula2,
455 subformula3,subformula4;
456 psiconv_u16 temp,nr_of_subs;
400 457
401 psiconv_progress(lev+1,off,"Going to read a formula"); 458 psiconv_progress(lev+1,off,"Going to read a formula element list");
402 if (!(*result = malloc(sizeof(**result)))) 459 if (!(*result = malloc(sizeof(**result))))
403 goto ERROR1; 460 goto ERROR1;
404 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 461 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
405 goto ERROR2; 462 goto ERROR2;
406 if (!(formula = malloc(sizeof(*formula)))) 463 if (!(formula = malloc(sizeof(*formula))))
418 subformula3->type = psiconv_formula_unknown; 475 subformula3->type = psiconv_formula_unknown;
419 if (!(subformula4 = malloc(sizeof(*subformula4)))) 476 if (!(subformula4 = malloc(sizeof(*subformula4))))
420 goto ERROR7; 477 goto ERROR7;
421 subformula4->type = psiconv_formula_unknown; 478 subformula4->type = psiconv_formula_unknown;
422 479
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) { 480 while (!eof && len+off < maxlen) {
434 psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); 481 psiconv_progress(lev+3,off+len,"Going to read a formula item marker");
435 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 482 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
436 if (res) 483 if (res)
437 goto ERROR8; 484 goto ERROR8;
438 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 485 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
440 len ++; 487 len ++;
441 488
442 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 489 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
443 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 490 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
444 goto ERROR8; 491 goto ERROR8;
445 } else if (formula_elements[marker].formula_type == 492 } else if ((formula_elements[marker].formula_type ==
446 psiconv_formula_mark_eof) { 493 psiconv_formula_mark_eof) ||
494 (formula_elements[marker].formula_type ==
495 psiconv_formula_mark_opend) ||
496 (formula_elements[marker].formula_type ==
497 psiconv_formula_mark_opsep)) {
498 len--;
447 psiconv_progress(lev+3,off+len,"End of formula"); 499 psiconv_progress(lev+3,off+len,"End of this formula list");
448 eof = 1; 500 eof = 1;
449 } else if (formula_elements[marker].formula_type == 501 } else if (formula_elements[marker].formula_type ==
450 psiconv_formula_dat_int) { 502 psiconv_formula_dat_int) {
451 psiconv_progress(lev+3,off+len,"Next item: an integer"); 503 psiconv_progress(lev+3,off+len,"Next item: an integer");
452 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); 504 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
480 formula->type = formula_elements[marker].formula_type; 532 formula->type = formula_elements[marker].formula_type;
481 len += leng; 533 len += leng;
482 if ((res = psiconv_list_add(formula_stack,formula))) 534 if ((res = psiconv_list_add(formula_stack,formula)))
483 goto ERROR8; 535 goto ERROR8;
484 formula->type = psiconv_formula_unknown; 536 formula->type = psiconv_formula_unknown;
485 } else if (formula_elements[marker].formula_type == 537 } else if ((formula_elements[marker].formula_type ==
538 psiconv_formula_dat_cellblock) ||
539 (formula_elements[marker].formula_type ==
486 psiconv_formula_dat_cellblock) { 540 psiconv_formula_dat_vcellblock)) {
487 psiconv_progress(lev+3,off+len,"Next item: a cell block"); 541 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, 542 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
489 &formula->data.dat_cellblock))) 543 &formula->data.dat_cellblock)))
544 goto ERROR8;
545 formula->type = formula_elements[marker].formula_type;
546 len += leng;
547 if ((res = psiconv_list_add(formula_stack,formula)))
548 goto ERROR8;
549 formula->type = psiconv_formula_unknown;
550 } else if (formula_elements[marker].formula_type ==
551 psiconv_formula_dat_string) {
552 psiconv_progress(lev+3,off+len,"Next item: a string");
553 formula->data.dat_string =
554 psiconv_read_sheet_string(buf,lev+2,off+len,&leng,&res);
555 if (res)
490 goto ERROR8; 556 goto ERROR8;
491 formula->type = formula_elements[marker].formula_type; 557 formula->type = formula_elements[marker].formula_type;
492 len += leng; 558 len += leng;
493 if ((res = psiconv_list_add(formula_stack,formula))) 559 if ((res = psiconv_list_add(formula_stack,formula)))
494 goto ERROR8; 560 goto ERROR8;
495 formula->type = psiconv_formula_unknown; 561 formula->type = psiconv_formula_unknown;
496 } else if ((formula_elements[marker].formula_type == 562 } else if ((formula_elements[marker].formula_type ==
497 psiconv_formula_dat_var) || 563 psiconv_formula_dat_var) ||
498 (formula_elements[marker].formula_type == 564 (formula_elements[marker].formula_type ==
499 psiconv_formula_dat_string) ||
500 (formula_elements[marker].formula_type ==
501 psiconv_formula_dat_vcellblock) || 565 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!"); 566 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!");
507 goto ERROR8; 567 goto ERROR8;
508 } else if (formula_elements[marker].number_of_args == -1) { 568 } else if (formula_elements[marker].number_of_args == -1) {
509 psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); 569 psiconv_progress(lev+3,off+len,"Going to parse a vararg function");
570 if (!(formula->data.fun_operands =
571 psiconv_list_new(sizeof(*formula))))
572 goto ERROR8;
573 formula->type = formula_elements[marker].formula_type;
574 nr_of_subs = 0;
575 do {
576 nr_of_subs ++;
577 psiconv_progress(lev+4,off+len,"Going to read vararg argument %d",
578 nr_of_subs);
579 if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng,
580 &subformula,maxlen)))
581 goto ERROR8;
582 len += leng;
583 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) {
584 psiconv_free_formula(subformula);
585 goto ERROR8;
586 }
587 free(subformula);
588 psiconv_progress(lev+4,off+len,"Going to read the next marker");
589 submarker = psiconv_read_u8(buf,lev+4,off+len,&res);
590 len ++;
591 if (res)
592 goto ERROR8;
593 } while (formula_elements[submarker].formula_type
594 == psiconv_formula_mark_opsep);
595 if (formula_elements[submarker].formula_type
596 != psiconv_formula_mark_opend) {
597 psiconv_warn(lev+3,off+len,"Formula corrupted!");
598 psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker);
510 goto ERROR8; 599 goto ERROR8;
600 }
601 psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x",
602 marker);
603 submarker = psiconv_read_u8(buf,lev+3,off+len,&res);
604 if (res)
605 goto ERROR8;
606 if (submarker != marker) {
607 psiconv_warn(lev+3,off+len,"Formula corrupted!");
608 psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x",
609 marker,submarker);
610 goto ERROR8;
611 }
612 len++;
613 psiconv_progress(lev+3,off+len,
614 "Going to read the number of arguments (%d expected)",
615 nr_of_subs);
616 temp = psiconv_read_u16(buf,lev+3,off+len,&res);
617 if (res)
618 goto ERROR8;
619 if (temp != nr_of_subs) {
620 psiconv_warn(lev+3,off+len,"Formula corrupted!");
621 psiconv_debug(lev+3,off+len,
622 "Read %d arguments, but formula says there are %d",
623 nr_of_subs,temp);
624 goto ERROR8;
625 }
626 len += 2;
627 if ((res = psiconv_list_add(formula_stack,formula)))
628 goto ERROR8;
629 formula->type = psiconv_formula_unknown;
511 } else { 630 } else {
512 if (formula_elements[marker].number_of_args > 0) 631 if (formula_elements[marker].number_of_args > 0)
513 if ((res = psiconv_list_pop(formula_stack,subformula1))) 632 if ((res = psiconv_list_pop(formula_stack,subformula1)))
514 goto ERROR8; 633 goto ERROR8;
515 if (formula_elements[marker].number_of_args > 1) 634 if (formula_elements[marker].number_of_args > 1)
541 goto ERROR8; 660 goto ERROR8;
542 subformula4->type = subformula3->type = subformula2->type = 661 subformula4->type = subformula3->type = subformula2->type =
543 subformula1->type = formula->type = psiconv_formula_unknown; 662 subformula1->type = formula->type = psiconv_formula_unknown;
544 } 663 }
545 } 664 }
546 if ((len != bytelen) || !eof) { 665 if ((len+off > maxlen) || !eof) {
547 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 666 psiconv_warn(lev+2,off+len,"Formula corrupted!");
548 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", 667 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
549 bytelen,len); 668 maxlen,len+off);
550 goto ERROR8; 669 goto ERROR8;
551 } 670 }
552 if ((psiconv_list_length(formula_stack)) != 1) { 671 if ((psiconv_list_length(formula_stack)) != 1) {
553 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 672 psiconv_warn(lev+2,off+len,"Formula corrupted!");
554 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", 673 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)",
562 681
563 if (length) 682 if (length)
564 *length = len; 683 *length = len;
565 684
566 psiconv_progress(lev,off+len-1, 685 psiconv_progress(lev,off+len-1,
567 "End of formula (total length: %08x)", len); 686 "End of formula element list (total length: %08x)", len);
568 return 0; 687 return 0;
569 688
570ERROR8: 689ERROR8:
571 psiconv_free_formula(subformula4); 690 psiconv_free_formula(subformula4);
572ERROR7: 691ERROR7:
580ERROR3: 699ERROR3:
581 psiconv_free_formula_list(formula_stack); 700 psiconv_free_formula_list(formula_stack);
582ERROR2: 701ERROR2:
583 free (*result); 702 free (*result);
584ERROR1: 703ERROR1:
585 psiconv_warn(lev+1,off,"Reading of formula failed"); 704 psiconv_warn(lev+1,off,"Reading of formula element list failed");
586 if (length) 705 if (length)
587 *length = 0; 706 *length = 0;
588 if (!res) 707 if (!res)
589 return -PSICONV_E_NOMEM; 708 return -PSICONV_E_NOMEM;
590 else 709 else
591 return res; 710 return res;
592} 711}
593 712
713
714
715
716int psiconv_parse_formula(const psiconv_buffer buf, int lev,
717 psiconv_u32 off, int *length,
718 psiconv_formula *result)
719{
720 int res=0;
721 int len=0;
722 int leng;
723 psiconv_u32 bytelen,formula_end;
724 psiconv_u8 temp;
725
726 psiconv_progress(lev+1,off,"Going to read a formula");
727
728 psiconv_progress(lev+2,off+len,
729 "Going to read the formula byte length");
730 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
731 if (res)
732 goto ERROR1;
733 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
734 len += leng;
735 bytelen += len;
736 formula_end = off + bytelen;
737
738 psiconv_progress(lev+2,off+len,"Going to read the formula elements list");
739 if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng,
740 result,formula_end)))
741 goto ERROR1;
742 len += leng;
743
744 psiconv_progress(lev+2,off+len,"Going to read the eof marker");
745 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
746 if (res)
747 goto ERROR2;
748 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) {
749 psiconv_warn(lev+2,off+len,"Formula corrupted!");
750 psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x",
751 0x15,temp);
752 goto ERROR2;
753 }
754 len ++;
755
756 if (off+len != formula_end) {
757 psiconv_warn(lev+2,off+len,"Formula corrupted!");
758 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
759 formula_end,len+off);
760 goto ERROR2;
761 }
762
763 if (length)
764 *length = len;
765
766 psiconv_progress(lev,off+len-1,
767 "End of formula (total length: %08x)", len);
768 return 0;
769
770ERROR2:
771 psiconv_free_formula(*result);
772ERROR1:
773 psiconv_warn(lev+1,off,"Reading of formula failed");
774 if (length)
775 *length = 0;
776 if (!res)
777 return -PSICONV_E_NOMEM;
778 else
779 return res;
780}
781
782

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

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