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-2004 Frodo Looijaard <frodol@dds.nl> |
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. |
… | |
… | |
435 | psiconv_debug(config,lev+3,off+len,"Marker: %02x (%s)",marker, |
435 | psiconv_debug(config,lev+3,off+len,"Marker: %02x (%s)",marker, |
436 | formula_elements[marker].name); |
436 | formula_elements[marker].name); |
437 | len ++; |
437 | len ++; |
438 | |
438 | |
439 | if (formula_elements[marker].formula_type == psiconv_formula_unknown) { |
439 | if (formula_elements[marker].formula_type == psiconv_formula_unknown) { |
440 | psiconv_warn(config,lev+3,off+len,"Unknown formula marker found!"); |
440 | psiconv_error(config,lev+3,off+len,"Unknown formula marker found!"); |
441 | goto ERROR8; |
441 | goto ERROR8; |
442 | } else if ((formula_elements[marker].formula_type == |
442 | } else if ((formula_elements[marker].formula_type == |
443 | psiconv_formula_mark_eof) || |
443 | psiconv_formula_mark_eof) || |
444 | (formula_elements[marker].formula_type == |
444 | (formula_elements[marker].formula_type == |
445 | psiconv_formula_mark_opend) || |
445 | psiconv_formula_mark_opend) || |
… | |
… | |
559 | submarker=submarker2; |
559 | submarker=submarker2; |
560 | len++; |
560 | len++; |
561 | } |
561 | } |
562 | if (formula_elements[submarker].formula_type |
562 | if (formula_elements[submarker].formula_type |
563 | != psiconv_formula_mark_opend) { |
563 | != psiconv_formula_mark_opend) { |
564 | psiconv_warn(config,lev+3,off+len,"Formula corrupted!"); |
564 | psiconv_error(config,lev+3,off+len,"Formula corrupted!"); |
565 | psiconv_debug(config,lev+3,off+len,"Found unexpected marker %02x",submarker); |
565 | psiconv_debug(config,lev+3,off+len,"Found unexpected marker %02x",submarker); |
566 | goto ERROR8; |
566 | goto ERROR8; |
567 | } |
567 | } |
568 | psiconv_progress(config,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", |
569 | marker); |
569 | marker); |
570 | submarker = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
570 | submarker = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
571 | if (res) |
571 | if (res) |
572 | goto ERROR8; |
572 | goto ERROR8; |
573 | if (submarker != marker) { |
573 | if (submarker != marker) { |
574 | psiconv_warn(config,lev+3,off+len,"Formula corrupted!"); |
574 | psiconv_error(config,lev+3,off+len,"Formula corrupted!"); |
575 | psiconv_debug(config,lev+3,off+len,"Expected marker %02x, found %02x", |
575 | psiconv_debug(config,lev+3,off+len,"Expected marker %02x, found %02x", |
576 | marker,submarker); |
576 | marker,submarker); |
577 | goto ERROR8; |
577 | goto ERROR8; |
578 | } |
578 | } |
579 | len++; |
579 | len++; |
… | |
… | |
582 | nr_of_subs); |
582 | nr_of_subs); |
583 | temp = psiconv_read_u16(config,buf,lev+3,off+len,&res); |
583 | temp = psiconv_read_u16(config,buf,lev+3,off+len,&res); |
584 | if (res) |
584 | if (res) |
585 | goto ERROR8; |
585 | goto ERROR8; |
586 | if (temp != nr_of_subs) { |
586 | if (temp != nr_of_subs) { |
587 | psiconv_warn(config,lev+3,off+len,"Formula corrupted!"); |
587 | psiconv_error(config,lev+3,off+len,"Formula corrupted!"); |
588 | psiconv_debug(config,lev+3,off+len, |
588 | psiconv_debug(config,lev+3,off+len, |
589 | "Read %d arguments, but formula says there are %d", |
589 | "Read %d arguments, but formula says there are %d", |
590 | nr_of_subs,temp); |
590 | nr_of_subs,temp); |
591 | goto ERROR8; |
591 | goto ERROR8; |
592 | } |
592 | } |
… | |
… | |
628 | subformula4->type = subformula3->type = subformula2->type = |
628 | subformula4->type = subformula3->type = subformula2->type = |
629 | subformula1->type = formula->type = psiconv_formula_unknown; |
629 | subformula1->type = formula->type = psiconv_formula_unknown; |
630 | } |
630 | } |
631 | } |
631 | } |
632 | if ((len+off > maxlen) || !eof) { |
632 | if ((len+off > maxlen) || !eof) { |
633 | psiconv_warn(config,lev+2,off+len,"Formula corrupted!"); |
633 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
634 | psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x", |
634 | psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x", |
635 | maxlen,len+off); |
635 | maxlen,len+off); |
636 | goto ERROR8; |
636 | goto ERROR8; |
637 | } |
637 | } |
638 | if ((psiconv_list_length(formula_stack)) != 1) { |
638 | if ((psiconv_list_length(formula_stack)) != 1) { |
639 | psiconv_warn(config,lev+2,off+len,"Formula corrupted!"); |
639 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
640 | psiconv_debug(config,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)", |
641 | psiconv_list_length(formula_stack)); |
641 | psiconv_list_length(formula_stack)); |
642 | goto ERROR8; |
642 | goto ERROR8; |
643 | } |
643 | } |
644 | if ((res = psiconv_list_pop(formula_stack,*result))) |
644 | if ((res = psiconv_list_pop(formula_stack,*result))) |
… | |
… | |
666 | ERROR3: |
666 | ERROR3: |
667 | psiconv_free_formula_list(formula_stack); |
667 | psiconv_free_formula_list(formula_stack); |
668 | ERROR2: |
668 | ERROR2: |
669 | free (*result); |
669 | free (*result); |
670 | ERROR1: |
670 | ERROR1: |
671 | psiconv_warn(config,lev+1,off,"Reading of formula element list failed"); |
671 | psiconv_error(config,lev+1,off,"Reading of formula element list failed"); |
672 | if (length) |
672 | if (length) |
673 | *length = 0; |
673 | *length = 0; |
674 | if (!res) |
674 | if (!res) |
675 | return -PSICONV_E_NOMEM; |
675 | return -PSICONV_E_NOMEM; |
676 | else |
676 | else |
… | |
… | |
712 | psiconv_progress(config,lev+2,off+len,"Going to read the eof marker"); |
712 | psiconv_progress(config,lev+2,off+len,"Going to read the eof marker"); |
713 | temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
713 | temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
714 | if (res) |
714 | if (res) |
715 | goto ERROR2; |
715 | goto ERROR2; |
716 | if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) { |
716 | if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) { |
717 | psiconv_warn(config,lev+2,off+len,"Formula corrupted!"); |
717 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
718 | psiconv_debug(config,lev+2,off+len,"Expected marker: %02x, found byte: %02x", |
718 | psiconv_debug(config,lev+2,off+len,"Expected marker: %02x, found byte: %02x", |
719 | 0x15,temp); |
719 | 0x15,temp); |
720 | goto ERROR2; |
720 | goto ERROR2; |
721 | } |
721 | } |
722 | len ++; |
722 | len ++; |
723 | |
723 | |
724 | if (off+len != formula_end) { |
724 | if (off+len != formula_end) { |
725 | psiconv_warn(config,lev+2,off+len,"Formula corrupted!"); |
725 | psiconv_error(config,lev+2,off+len,"Formula corrupted!"); |
726 | psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x", |
726 | psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x", |
727 | formula_end,len+off); |
727 | formula_end,len+off); |
728 | goto ERROR2; |
728 | goto ERROR2; |
729 | } |
729 | } |
730 | |
730 | |
… | |
… | |
736 | return 0; |
736 | return 0; |
737 | |
737 | |
738 | ERROR2: |
738 | ERROR2: |
739 | psiconv_free_formula(*result); |
739 | psiconv_free_formula(*result); |
740 | ERROR1: |
740 | ERROR1: |
741 | psiconv_warn(config,lev+1,off,"Reading of formula failed"); |
741 | psiconv_error(config,lev+1,off,"Reading of formula failed"); |
742 | if (length) |
742 | if (length) |
743 | *length = 0; |
743 | *length = 0; |
744 | if (!res) |
744 | if (!res) |
745 | return -PSICONV_E_NOMEM; |
745 | return -PSICONV_E_NOMEM; |
746 | else |
746 | else |