/[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 101 Revision 106
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
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}
348
349
294int psiconv_parse_formula(const psiconv_buffer buf, int lev, 350static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev,
295 psiconv_u32 off, int *length, 351 psiconv_u32 off, int *length,
352 psiconv_sheet_ref_t *result)
353{
354 int res;
355 psiconv_u16 temp;
356
357 psiconv_progress(lev+1,off,"Going to read a sheet ref");
358 psiconv_progress(lev+2,off,"Going to read the offset encoding");
359 temp = psiconv_read_u16(buf,lev+2,off,&res);
360 if (res) {
361 if (length)
362 *length = 0;
363 return res;
364 }
365 psiconv_debug(lev+2,off,"Encoded word: %04x",temp);
366 result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false;
367 result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1);
368 psiconv_debug(lev+2,off,"Reference: %s offset %d",
369 result->absolute?"absolute":"relative",result->offset);
370 if (length)
371 *length = 2;
372 return 0;
373}
374
375static int psiconv_parse_sheet_cell_reference(const psiconv_buffer buf,int lev,
376 psiconv_u32 off, int *length,
377 psiconv_sheet_cell_reference_t *result)
378{
379 int len = 0;
380 int leng,res;
381 psiconv_u8 temp;
382
383 psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference");
384 psiconv_progress(lev+2,off+len,"Going to read the row reference");
385 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->row)))
386 goto ERROR;
387 len += leng;
388 psiconv_progress(lev+2,off+len,"Going to read the column reference");
389 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->column)))
390 goto ERROR;
391 len += leng;
392
393 psiconv_progress(lev+2,off+len,
394 "Going to read the trailing byte (%02x expected)",0);
395 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
396 if (res)
397 goto ERROR;
398 if (temp != 0) {
399 psiconv_warn(lev+2,off+len,"Unknown byte in cell reference (ignored");
400 psiconv_debug(lev+2,off+len,"Trailing byte: %02x",temp);
401 }
402 len ++;
403 psiconv_progress(lev,off+len-1,
404 "End of cell reference (total length: %08x)", len);
405 if (length)
406 *length = len;
407 return 0;
408ERROR:
409 if (length)
410 *length = 0;
411 return res;
412}
413
414static int psiconv_parse_sheet_cell_block(const psiconv_buffer buf,int lev,
415 psiconv_u32 off, int *length,
416 psiconv_sheet_cell_block_t *result)
417{
418 int len = 0;
419 int leng,res;
420
421 psiconv_progress(lev+1,off+len,"Going to read a sheet cell block");
422 psiconv_progress(lev+2,off+len,"Going to read the first cell");
423 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
424 &result->first)))
425 goto ERROR;
426 len += leng;
427 psiconv_progress(lev+2,off+len,"Going to read the last cell");
428 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
429 &result->last)))
430 goto ERROR;
431 len += leng;
432 psiconv_progress(lev,off+len-1,
433 "End of cell block (total length: %08x)", len);
434 if (length)
435 *length = len;
436 return 0;
437ERROR:
438 if (length)
439 *length = 0;
440 return res;
441}
442
443static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev,
444 psiconv_u32 off, int *length,
296 psiconv_formula *result) 445 psiconv_formula *result,
446 psiconv_u32 maxlen)
297{ 447{
298 int res=0; 448 int res=0;
299 int len=0; 449 int len=0;
300 int leng; 450 int leng;
301 int eof = 0; 451 int eof = 0;
302 psiconv_u8 marker; 452 psiconv_u8 marker,submarker;
303 psiconv_u32 bytelen;
304 psiconv_formula_list formula_stack; 453 psiconv_formula_list formula_stack;
305 psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; 454 psiconv_formula formula,subformula,subformula1,subformula2,
455 subformula3,subformula4;
456 psiconv_u16 temp,nr_of_subs;
306 457
307 psiconv_progress(lev+1,off,"Going to read a formula"); 458 psiconv_progress(lev+1,off,"Going to read a formula element list");
308 if (!(*result = malloc(sizeof(**result)))) 459 if (!(*result = malloc(sizeof(**result))))
309 goto ERROR1; 460 goto ERROR1;
310 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 461 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
311 goto ERROR2; 462 goto ERROR2;
312 if (!(formula = malloc(sizeof(*formula)))) 463 if (!(formula = malloc(sizeof(*formula))))
324 subformula3->type = psiconv_formula_unknown; 475 subformula3->type = psiconv_formula_unknown;
325 if (!(subformula4 = malloc(sizeof(*subformula4)))) 476 if (!(subformula4 = malloc(sizeof(*subformula4))))
326 goto ERROR7; 477 goto ERROR7;
327 subformula4->type = psiconv_formula_unknown; 478 subformula4->type = psiconv_formula_unknown;
328 479
329 psiconv_progress(lev+2,off+len,
330 "Going to read the formula byte length");
331 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
332 if (res)
333 goto ERROR8;
334 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
335 len += leng;
336 bytelen += len;
337
338 psiconv_progress(lev+2,off+len,"Going to read the formula items");
339 while (!eof && len < bytelen) { 480 while (!eof && len+off < maxlen) {
340 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");
341 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 482 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
342 if (res) 483 if (res)
343 goto ERROR8; 484 goto ERROR8;
344 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 485 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
346 len ++; 487 len ++;
347 488
348 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 489 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
349 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 490 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
350 goto ERROR8; 491 goto ERROR8;
351 } else if (formula_elements[marker].formula_type == 492 } else if ((formula_elements[marker].formula_type ==
352 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--;
353 psiconv_progress(lev+3,off+len,"End of formula"); 499 psiconv_progress(lev+3,off+len,"End of this formula list");
354 eof = 1; 500 eof = 1;
355 } else if (formula_elements[marker].formula_type == 501 } else if (formula_elements[marker].formula_type ==
356 psiconv_formula_dat_int) { 502 psiconv_formula_dat_int) {
357 psiconv_progress(lev+3,off+len,"Next item: an integer"); 503 psiconv_progress(lev+3,off+len,"Next item: an integer");
358 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);
375 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float); 521 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float);
376 len += leng; 522 len += leng;
377 if ((res = psiconv_list_add(formula_stack,formula))) 523 if ((res = psiconv_list_add(formula_stack,formula)))
378 goto ERROR8; 524 goto ERROR8;
379 formula->type = psiconv_formula_unknown; 525 formula->type = psiconv_formula_unknown;
380 526 } else if (formula_elements[marker].formula_type ==
527 psiconv_formula_dat_cellref) {
528 psiconv_progress(lev+3,off+len,"Next item: a cell reference");
529 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
530 &formula->data.dat_cellref)))
531 goto ERROR8;
532 formula->type = formula_elements[marker].formula_type;
533 len += leng;
534 if ((res = psiconv_list_add(formula_stack,formula)))
535 goto ERROR8;
536 formula->type = psiconv_formula_unknown;
537 } else if ((formula_elements[marker].formula_type ==
538 psiconv_formula_dat_cellblock) ||
539 (formula_elements[marker].formula_type ==
540 psiconv_formula_dat_vcellblock)) {
541 psiconv_progress(lev+3,off+len,"Next item: a cell block");
542 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
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)
556 goto ERROR8;
557 formula->type = formula_elements[marker].formula_type;
558 len += leng;
559 if ((res = psiconv_list_add(formula_stack,formula)))
560 goto ERROR8;
561 formula->type = psiconv_formula_unknown;
381 } else if ((formula_elements[marker].formula_type == 562 } else if ((formula_elements[marker].formula_type ==
382 psiconv_formula_dat_var) || 563 psiconv_formula_dat_var) ||
383 (formula_elements[marker].formula_type == 564 (formula_elements[marker].formula_type ==
384 psiconv_formula_dat_string) ||
385 (formula_elements[marker].formula_type ==
386 psiconv_formula_dat_cellref) ||
387 (formula_elements[marker].formula_type ==
388 psiconv_formula_dat_cellblock) ||
389 (formula_elements[marker].formula_type ==
390 psiconv_formula_dat_vcellblock) || 565 psiconv_formula_dat_vcellblock)) {
391 (formula_elements[marker].formula_type ==
392 psiconv_formula_mark_opsep) ||
393 (formula_elements[marker].formula_type ==
394 psiconv_formula_mark_opend)) {
395 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); 566 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!");
396 goto ERROR8; 567 goto ERROR8;
397 } else if (formula_elements[marker].number_of_args == -1) { 568 } else if (formula_elements[marker].number_of_args == -1) {
398 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);
399 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;
400 } else { 630 } else {
401 if (formula_elements[marker].number_of_args > 0) 631 if (formula_elements[marker].number_of_args > 0)
402 if ((res = psiconv_list_pop(formula_stack,subformula1))) 632 if ((res = psiconv_list_pop(formula_stack,subformula1)))
403 goto ERROR8; 633 goto ERROR8;
404 if (formula_elements[marker].number_of_args > 1) 634 if (formula_elements[marker].number_of_args > 1)
430 goto ERROR8; 660 goto ERROR8;
431 subformula4->type = subformula3->type = subformula2->type = 661 subformula4->type = subformula3->type = subformula2->type =
432 subformula1->type = formula->type = psiconv_formula_unknown; 662 subformula1->type = formula->type = psiconv_formula_unknown;
433 } 663 }
434 } 664 }
435 if ((len != bytelen) || !eof) { 665 if ((len+off > maxlen) || !eof) {
436 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 666 psiconv_warn(lev+2,off+len,"Formula corrupted!");
437 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",
438 bytelen,len); 668 maxlen,len+off);
439 goto ERROR8; 669 goto ERROR8;
440 } 670 }
441 if ((psiconv_list_length(formula_stack)) != 1) { 671 if ((psiconv_list_length(formula_stack)) != 1) {
442 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 672 psiconv_warn(lev+2,off+len,"Formula corrupted!");
443 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)",
451 681
452 if (length) 682 if (length)
453 *length = len; 683 *length = len;
454 684
455 psiconv_progress(lev,off+len-1, 685 psiconv_progress(lev,off+len-1,
456 "End of formula (total length: %08x)", len); 686 "End of formula element list (total length: %08x)", len);
457 return 0; 687 return 0;
458 688
459ERROR8: 689ERROR8:
460 psiconv_free_formula(subformula4); 690 psiconv_free_formula(subformula4);
461ERROR7: 691ERROR7:
469ERROR3: 699ERROR3:
470 psiconv_free_formula_list(formula_stack); 700 psiconv_free_formula_list(formula_stack);
471ERROR2: 701ERROR2:
472 free (*result); 702 free (*result);
473ERROR1: 703ERROR1:
474 psiconv_warn(lev+1,off,"Reading of formula failed"); 704 psiconv_warn(lev+1,off,"Reading of formula element list failed");
475 if (length) 705 if (length)
476 *length = 0; 706 *length = 0;
477 if (!res) 707 if (!res)
478 return -PSICONV_E_NOMEM; 708 return -PSICONV_E_NOMEM;
479 else 709 else
480 return res; 710 return res;
481} 711}
482 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.101  
changed lines
  Added in v.106

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