… | |
… | |
289 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
289 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
290 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
290 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
291 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
291 | {psiconv_formula_unknown,0,"*UNKNOWN*"}, |
292 | {psiconv_formula_unknown,0,"*UNKNOWN*"}}; |
292 | {psiconv_formula_unknown,0,"*UNKNOWN*"}}; |
293 | |
293 | |
|
|
294 | static 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 | |
|
|
338 | ERROR2: |
|
|
339 | free(result); |
|
|
340 | ERROR1: |
|
|
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 | |
294 | |
349 | |
295 | static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev, |
350 | static 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) |
298 | { |
353 | { |
… | |
… | |
383 | if (length) |
438 | if (length) |
384 | *length = 0; |
439 | *length = 0; |
385 | return res; |
440 | return res; |
386 | } |
441 | } |
387 | |
442 | |
388 | int psiconv_parse_formula(const psiconv_buffer buf, int lev, |
443 | static 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; |
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,subformula1,subformula2,subformula3,subformula4; |
400 | |
455 | |
401 | psiconv_progress(lev+1,off,"Going to read a formula"); |
456 | psiconv_progress(lev+1,off,"Going to read a formula element list"); |
402 | if (!(*result = malloc(sizeof(**result)))) |
457 | if (!(*result = malloc(sizeof(**result)))) |
403 | goto ERROR1; |
458 | goto ERROR1; |
404 | if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
459 | if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
405 | goto ERROR2; |
460 | goto ERROR2; |
406 | if (!(formula = malloc(sizeof(*formula)))) |
461 | if (!(formula = malloc(sizeof(*formula)))) |
… | |
… | |
418 | subformula3->type = psiconv_formula_unknown; |
473 | subformula3->type = psiconv_formula_unknown; |
419 | if (!(subformula4 = malloc(sizeof(*subformula4)))) |
474 | if (!(subformula4 = malloc(sizeof(*subformula4)))) |
420 | goto ERROR7; |
475 | goto ERROR7; |
421 | subformula4->type = psiconv_formula_unknown; |
476 | subformula4->type = psiconv_formula_unknown; |
422 | |
477 | |
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) { |
478 | while (!eof && len+off < maxlen) { |
434 | psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); |
479 | psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); |
435 | marker = psiconv_read_u8(buf,lev+2,off+len,&res); |
480 | marker = psiconv_read_u8(buf,lev+2,off+len,&res); |
436 | if (res) |
481 | if (res) |
437 | goto ERROR8; |
482 | goto ERROR8; |
438 | psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, |
483 | psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, |
… | |
… | |
440 | len ++; |
485 | len ++; |
441 | |
486 | |
442 | if (formula_elements[marker].formula_type == psiconv_formula_unknown) { |
487 | if (formula_elements[marker].formula_type == psiconv_formula_unknown) { |
443 | psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); |
488 | psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); |
444 | goto ERROR8; |
489 | goto ERROR8; |
445 | } else if (formula_elements[marker].formula_type == |
490 | } else if ((formula_elements[marker].formula_type == |
446 | psiconv_formula_mark_eof) { |
491 | psiconv_formula_mark_eof) || |
|
|
492 | (formula_elements[marker].formula_type == |
|
|
493 | psiconv_formula_mark_opend) || |
|
|
494 | (formula_elements[marker].formula_type == |
|
|
495 | psiconv_formula_mark_opsep)) { |
|
|
496 | len--; |
447 | psiconv_progress(lev+3,off+len,"End of formula"); |
497 | psiconv_progress(lev+3,off+len,"End of this formula list"); |
448 | eof = 1; |
498 | eof = 1; |
449 | } else if (formula_elements[marker].formula_type == |
499 | } else if (formula_elements[marker].formula_type == |
450 | psiconv_formula_dat_int) { |
500 | psiconv_formula_dat_int) { |
451 | psiconv_progress(lev+3,off+len,"Next item: an integer"); |
501 | psiconv_progress(lev+3,off+len,"Next item: an integer"); |
452 | formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); |
502 | formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); |
… | |
… | |
491 | formula->type = formula_elements[marker].formula_type; |
541 | formula->type = formula_elements[marker].formula_type; |
492 | len += leng; |
542 | len += leng; |
493 | if ((res = psiconv_list_add(formula_stack,formula))) |
543 | if ((res = psiconv_list_add(formula_stack,formula))) |
494 | goto ERROR8; |
544 | goto ERROR8; |
495 | formula->type = psiconv_formula_unknown; |
545 | formula->type = psiconv_formula_unknown; |
|
|
546 | } else if (formula_elements[marker].formula_type == |
|
|
547 | psiconv_formula_dat_string) { |
|
|
548 | psiconv_progress(lev+3,off+len,"Next item: a string"); |
|
|
549 | formula->data.dat_string = |
|
|
550 | psiconv_read_sheet_string(buf,lev+2,off+len,&leng,&res); |
|
|
551 | if (res) |
|
|
552 | goto ERROR8; |
|
|
553 | formula->type = formula_elements[marker].formula_type; |
|
|
554 | len += leng; |
|
|
555 | if ((res = psiconv_list_add(formula_stack,formula))) |
|
|
556 | goto ERROR8; |
|
|
557 | formula->type = psiconv_formula_unknown; |
496 | } else if ((formula_elements[marker].formula_type == |
558 | } else if ((formula_elements[marker].formula_type == |
497 | psiconv_formula_dat_var) || |
559 | psiconv_formula_dat_var) || |
498 | (formula_elements[marker].formula_type == |
560 | (formula_elements[marker].formula_type == |
499 | psiconv_formula_dat_string) || |
|
|
500 | (formula_elements[marker].formula_type == |
|
|
501 | psiconv_formula_dat_vcellblock) || |
561 | 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!"); |
562 | psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); |
507 | goto ERROR8; |
563 | goto ERROR8; |
508 | } else if (formula_elements[marker].number_of_args == -1) { |
564 | } else if (formula_elements[marker].number_of_args == -1) { |
509 | psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); |
565 | psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); |
510 | goto ERROR8; |
566 | goto ERROR8; |
… | |
… | |
541 | goto ERROR8; |
597 | goto ERROR8; |
542 | subformula4->type = subformula3->type = subformula2->type = |
598 | subformula4->type = subformula3->type = subformula2->type = |
543 | subformula1->type = formula->type = psiconv_formula_unknown; |
599 | subformula1->type = formula->type = psiconv_formula_unknown; |
544 | } |
600 | } |
545 | } |
601 | } |
546 | if ((len != bytelen) || !eof) { |
602 | if ((len+off > maxlen) || !eof) { |
547 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
603 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
548 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
604 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
549 | bytelen,len); |
605 | maxlen,len+off); |
550 | goto ERROR8; |
606 | goto ERROR8; |
551 | } |
607 | } |
552 | if ((psiconv_list_length(formula_stack)) != 1) { |
608 | if ((psiconv_list_length(formula_stack)) != 1) { |
553 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
609 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
554 | psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", |
610 | psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", |
… | |
… | |
562 | |
618 | |
563 | if (length) |
619 | if (length) |
564 | *length = len; |
620 | *length = len; |
565 | |
621 | |
566 | psiconv_progress(lev,off+len-1, |
622 | psiconv_progress(lev,off+len-1, |
567 | "End of formula (total length: %08x)", len); |
623 | "End of formula element list (total length: %08x)", len); |
568 | return 0; |
624 | return 0; |
569 | |
625 | |
570 | ERROR8: |
626 | ERROR8: |
571 | psiconv_free_formula(subformula4); |
627 | psiconv_free_formula(subformula4); |
572 | ERROR7: |
628 | ERROR7: |
… | |
… | |
580 | ERROR3: |
636 | ERROR3: |
581 | psiconv_free_formula_list(formula_stack); |
637 | psiconv_free_formula_list(formula_stack); |
582 | ERROR2: |
638 | ERROR2: |
583 | free (*result); |
639 | free (*result); |
584 | ERROR1: |
640 | ERROR1: |
585 | psiconv_warn(lev+1,off,"Reading of formula failed"); |
641 | psiconv_warn(lev+1,off,"Reading of formula element list failed"); |
586 | if (length) |
642 | if (length) |
587 | *length = 0; |
643 | *length = 0; |
588 | if (!res) |
644 | if (!res) |
589 | return -PSICONV_E_NOMEM; |
645 | return -PSICONV_E_NOMEM; |
590 | else |
646 | else |
591 | return res; |
647 | return res; |
592 | } |
648 | } |
593 | |
649 | |
|
|
650 | |
|
|
651 | |
|
|
652 | |
|
|
653 | int psiconv_parse_formula(const psiconv_buffer buf, int lev, |
|
|
654 | psiconv_u32 off, int *length, |
|
|
655 | psiconv_formula *result) |
|
|
656 | { |
|
|
657 | int res=0; |
|
|
658 | int len=0; |
|
|
659 | int leng; |
|
|
660 | psiconv_u32 bytelen,formula_end; |
|
|
661 | psiconv_u8 temp; |
|
|
662 | |
|
|
663 | psiconv_progress(lev+1,off,"Going to read a formula"); |
|
|
664 | |
|
|
665 | psiconv_progress(lev+2,off+len, |
|
|
666 | "Going to read the formula byte length"); |
|
|
667 | bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res); |
|
|
668 | if (res) |
|
|
669 | goto ERROR1; |
|
|
670 | psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen); |
|
|
671 | len += leng; |
|
|
672 | bytelen += len; |
|
|
673 | formula_end = off + bytelen; |
|
|
674 | |
|
|
675 | psiconv_progress(lev+2,off+len,"Going to read the formula elements list"); |
|
|
676 | if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng, |
|
|
677 | result,formula_end))) |
|
|
678 | goto ERROR1; |
|
|
679 | len += leng; |
|
|
680 | |
|
|
681 | psiconv_progress(lev+2,off+len,"Going to read the eof marker"); |
|
|
682 | temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
|
|
683 | if (res) |
|
|
684 | goto ERROR2; |
|
|
685 | if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) { |
|
|
686 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
|
|
687 | psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x", |
|
|
688 | 0x15,temp); |
|
|
689 | goto ERROR2; |
|
|
690 | } |
|
|
691 | len ++; |
|
|
692 | |
|
|
693 | if (off+len != formula_end) { |
|
|
694 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
|
|
695 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
|
|
696 | formula_end,len+off); |
|
|
697 | goto ERROR2; |
|
|
698 | } |
|
|
699 | |
|
|
700 | if (length) |
|
|
701 | *length = len; |
|
|
702 | |
|
|
703 | psiconv_progress(lev,off+len-1, |
|
|
704 | "End of formula (total length: %08x)", len); |
|
|
705 | return 0; |
|
|
706 | |
|
|
707 | ERROR2: |
|
|
708 | psiconv_free_formula(*result); |
|
|
709 | ERROR1: |
|
|
710 | psiconv_warn(lev+1,off,"Reading of formula failed"); |
|
|
711 | if (length) |
|
|
712 | *length = 0; |
|
|
713 | if (!res) |
|
|
714 | return -PSICONV_E_NOMEM; |
|
|
715 | else |
|
|
716 | return res; |
|
|
717 | } |
|
|
718 | |
|
|
719 | |