/[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 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 290
294int psiconv_parse_formula(const psiconv_buffer buf, int lev, 291static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev,
295 psiconv_u32 off, int *length, 292 psiconv_u32 off, int *length,
293 psiconv_sheet_ref_t *result)
294{
295 int res;
296 psiconv_u16 temp;
297
298 psiconv_progress(lev+1,off,"Going to read a sheet ref");
299 psiconv_progress(lev+2,off,"Going to read the offset encoding");
300 temp = psiconv_read_u16(buf,lev+2,off,&res);
301 if (res) {
302 if (length)
303 *length = 0;
304 return res;
305 }
306 psiconv_debug(lev+2,off,"Encoded word: %04x",temp);
307 result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false;
308 result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1);
309 psiconv_debug(lev+2,off,"Reference: %s offset %d",
310 result->absolute?"absolute":"relative",result->offset);
311 if (length)
312 *length = 2;
313 return 0;
314}
315
316static int psiconv_parse_sheet_cell_reference(const psiconv_buffer buf,int lev,
317 psiconv_u32 off, int *length,
318 psiconv_sheet_cell_reference_t *result)
319{
320 int len = 0;
321 int leng,res;
322 psiconv_u8 temp;
323
324 psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference");
325 psiconv_progress(lev+2,off+len,"Going to read the row reference");
326 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->row)))
327 goto ERROR;
328 len += leng;
329 psiconv_progress(lev+2,off+len,"Going to read the column reference");
330 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->column)))
331 goto ERROR;
332 len += leng;
333
334 psiconv_progress(lev+2,off+len,
335 "Going to read the trailing byte (%02x expected)",0);
336 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
337 if (res)
338 goto ERROR;
339 if (temp != 0) {
340 psiconv_warn(lev+2,off+len,"Unknown byte in cell reference (ignored");
341 psiconv_debug(lev+2,off+len,"Trailing byte: %02x",temp);
342 }
343 len ++;
344 psiconv_progress(lev,off+len-1,
345 "End of cell reference (total length: %08x)", len);
346 if (length)
347 *length = len;
348 return 0;
349ERROR:
350 if (length)
351 *length = 0;
352 return res;
353}
354
355static int psiconv_parse_sheet_cell_block(const psiconv_buffer buf,int lev,
356 psiconv_u32 off, int *length,
357 psiconv_sheet_cell_block_t *result)
358{
359 int len = 0;
360 int leng,res;
361
362 psiconv_progress(lev+1,off+len,"Going to read a sheet cell block");
363 psiconv_progress(lev+2,off+len,"Going to read the first cell");
364 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
365 &result->first)))
366 goto ERROR;
367 len += leng;
368 psiconv_progress(lev+2,off+len,"Going to read the last cell");
369 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
370 &result->last)))
371 goto ERROR;
372 len += leng;
373 psiconv_progress(lev,off+len-1,
374 "End of cell block (total length: %08x)", len);
375 if (length)
376 *length = len;
377 return 0;
378ERROR:
379 if (length)
380 *length = 0;
381 return res;
382}
383
384static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev,
385 psiconv_u32 off, int *length,
296 psiconv_formula *result) 386 psiconv_formula *result,
387 psiconv_u32 maxlen)
297{ 388{
298 int res=0; 389 int res=0;
299 int len=0; 390 int len=0;
300 int leng; 391 int leng;
301 int eof = 0; 392 int eof = 0;
302 psiconv_u8 marker; 393 psiconv_u8 marker,submarker,submarker2;
303 psiconv_u32 bytelen;
304 psiconv_formula_list formula_stack; 394 psiconv_formula_list formula_stack;
305 psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; 395 psiconv_formula formula,subformula,subformula1,subformula2,
396 subformula3,subformula4;
397 psiconv_u16 temp,nr_of_subs;
306 398
307 psiconv_progress(lev+1,off,"Going to read a formula"); 399 psiconv_progress(lev+1,off,"Going to read a formula element list");
308 if (!(*result = malloc(sizeof(**result)))) 400 if (!(*result = malloc(sizeof(**result))))
309 goto ERROR1; 401 goto ERROR1;
310 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 402 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
311 goto ERROR2; 403 goto ERROR2;
312 if (!(formula = malloc(sizeof(*formula)))) 404 if (!(formula = malloc(sizeof(*formula))))
324 subformula3->type = psiconv_formula_unknown; 416 subformula3->type = psiconv_formula_unknown;
325 if (!(subformula4 = malloc(sizeof(*subformula4)))) 417 if (!(subformula4 = malloc(sizeof(*subformula4))))
326 goto ERROR7; 418 goto ERROR7;
327 subformula4->type = psiconv_formula_unknown; 419 subformula4->type = psiconv_formula_unknown;
328 420
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) { 421 while (!eof && len+off < maxlen) {
340 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");
341 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 423 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
342 if (res) 424 if (res)
343 goto ERROR8; 425 goto ERROR8;
344 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 426 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
346 len ++; 428 len ++;
347 429
348 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 430 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
349 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 431 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
350 goto ERROR8; 432 goto ERROR8;
351 } else if (formula_elements[marker].formula_type == 433 } else if ((formula_elements[marker].formula_type ==
352 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--;
353 psiconv_progress(lev+3,off+len,"End of formula"); 440 psiconv_progress(lev+3,off+len,"End of this formula list");
354 eof = 1; 441 eof = 1;
355 } else if (formula_elements[marker].formula_type == 442 } else if (formula_elements[marker].formula_type ==
356 psiconv_formula_dat_int) { 443 psiconv_formula_dat_int) {
357 psiconv_progress(lev+3,off+len,"Next item: an integer"); 444 psiconv_progress(lev+3,off+len,"Next item: an integer");
358 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);
375 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float); 462 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float);
376 len += leng; 463 len += leng;
377 if ((res = psiconv_list_add(formula_stack,formula))) 464 if ((res = psiconv_list_add(formula_stack,formula)))
378 goto ERROR8; 465 goto ERROR8;
379 formula->type = psiconv_formula_unknown; 466 formula->type = psiconv_formula_unknown;
380 467 } else if (formula_elements[marker].formula_type ==
468 psiconv_formula_dat_cellref) {
469 psiconv_progress(lev+3,off+len,"Next item: a cell reference");
470 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
471 &formula->data.dat_cellref)))
472 goto ERROR8;
473 formula->type = formula_elements[marker].formula_type;
474 len += leng;
475 if ((res = psiconv_list_add(formula_stack,formula)))
476 goto ERROR8;
477 formula->type = psiconv_formula_unknown;
381 } else if ((formula_elements[marker].formula_type == 478 } else if ((formula_elements[marker].formula_type ==
382 psiconv_formula_dat_var) || 479 psiconv_formula_dat_cellblock) ||
383 (formula_elements[marker].formula_type == 480 (formula_elements[marker].formula_type ==
481 psiconv_formula_dat_vcellblock)) {
482 psiconv_progress(lev+3,off+len,"Next item: a cell block");
483 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
484 &formula->data.dat_cellblock)))
485 goto ERROR8;
486 formula->type = formula_elements[marker].formula_type;
487 len += leng;
488 if ((res = psiconv_list_add(formula_stack,formula)))
489 goto ERROR8;
490 formula->type = psiconv_formula_unknown;
491 } else if (formula_elements[marker].formula_type ==
384 psiconv_formula_dat_string) || 492 psiconv_formula_dat_string) {
385 (formula_elements[marker].formula_type == 493 psiconv_progress(lev+3,off+len,"Next item: a string");
386 psiconv_formula_dat_cellref) || 494 formula->data.dat_string =
387 (formula_elements[marker].formula_type == 495 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res);
388 psiconv_formula_dat_cellblock) || 496 if (res)
389 (formula_elements[marker].formula_type ==
390 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!");
396 goto ERROR8; 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;
503 } else if ((formula_elements[marker].formula_type ==
504 psiconv_formula_dat_var)) {
505 psiconv_progress(lev+3,off+len,"Next item: a variable reference");
506 formula->data.dat_variable = psiconv_read_u32(buf,lev+2,off+len,&res);
507 if (res)
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;
397 } else if (formula_elements[marker].number_of_args == -1) { 514 } else if (formula_elements[marker].number_of_args == -1) {
398 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);
399 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;
400 } else { 588 } else {
401 if (formula_elements[marker].number_of_args > 0) 589 if (formula_elements[marker].number_of_args > 0)
402 if ((res = psiconv_list_pop(formula_stack,subformula1))) 590 if ((res = psiconv_list_pop(formula_stack,subformula1)))
403 goto ERROR8; 591 goto ERROR8;
404 if (formula_elements[marker].number_of_args > 1) 592 if (formula_elements[marker].number_of_args > 1)
430 goto ERROR8; 618 goto ERROR8;
431 subformula4->type = subformula3->type = subformula2->type = 619 subformula4->type = subformula3->type = subformula2->type =
432 subformula1->type = formula->type = psiconv_formula_unknown; 620 subformula1->type = formula->type = psiconv_formula_unknown;
433 } 621 }
434 } 622 }
435 if ((len != bytelen) || !eof) { 623 if ((len+off > maxlen) || !eof) {
436 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 624 psiconv_warn(lev+2,off+len,"Formula corrupted!");
437 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",
438 bytelen,len); 626 maxlen,len+off);
439 goto ERROR8; 627 goto ERROR8;
440 } 628 }
441 if ((psiconv_list_length(formula_stack)) != 1) { 629 if ((psiconv_list_length(formula_stack)) != 1) {
442 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 630 psiconv_warn(lev+2,off+len,"Formula corrupted!");
443 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)",
451 639
452 if (length) 640 if (length)
453 *length = len; 641 *length = len;
454 642
455 psiconv_progress(lev,off+len-1, 643 psiconv_progress(lev,off+len-1,
456 "End of formula (total length: %08x)", len); 644 "End of formula element list (total length: %08x)", len);
457 return 0; 645 return 0;
458 646
459ERROR8: 647ERROR8:
460 psiconv_free_formula(subformula4); 648 psiconv_free_formula(subformula4);
461ERROR7: 649ERROR7:
469ERROR3: 657ERROR3:
470 psiconv_free_formula_list(formula_stack); 658 psiconv_free_formula_list(formula_stack);
471ERROR2: 659ERROR2:
472 free (*result); 660 free (*result);
473ERROR1: 661ERROR1:
474 psiconv_warn(lev+1,off,"Reading of formula failed"); 662 psiconv_warn(lev+1,off,"Reading of formula element list failed");
475 if (length) 663 if (length)
476 *length = 0; 664 *length = 0;
477 if (!res) 665 if (!res)
478 return -PSICONV_E_NOMEM; 666 return -PSICONV_E_NOMEM;
479 else 667 else
480 return res; 668 return res;
481} 669}
482 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.101  
changed lines
  Added in v.129

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