/[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 105
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;
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,subformula1,subformula2,subformula3,subformula4;
306 455
307 psiconv_progress(lev+1,off,"Going to read a formula"); 456 psiconv_progress(lev+1,off,"Going to read a formula element list");
308 if (!(*result = malloc(sizeof(**result)))) 457 if (!(*result = malloc(sizeof(**result))))
309 goto ERROR1; 458 goto ERROR1;
310 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 459 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
311 goto ERROR2; 460 goto ERROR2;
312 if (!(formula = malloc(sizeof(*formula)))) 461 if (!(formula = malloc(sizeof(*formula))))
324 subformula3->type = psiconv_formula_unknown; 473 subformula3->type = psiconv_formula_unknown;
325 if (!(subformula4 = malloc(sizeof(*subformula4)))) 474 if (!(subformula4 = malloc(sizeof(*subformula4))))
326 goto ERROR7; 475 goto ERROR7;
327 subformula4->type = psiconv_formula_unknown; 476 subformula4->type = psiconv_formula_unknown;
328 477
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) { 478 while (!eof && len+off < maxlen) {
340 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");
341 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 480 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
342 if (res) 481 if (res)
343 goto ERROR8; 482 goto ERROR8;
344 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 483 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
346 len ++; 485 len ++;
347 486
348 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 487 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
349 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 488 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
350 goto ERROR8; 489 goto ERROR8;
351 } else if (formula_elements[marker].formula_type == 490 } else if ((formula_elements[marker].formula_type ==
352 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--;
353 psiconv_progress(lev+3,off+len,"End of formula"); 497 psiconv_progress(lev+3,off+len,"End of this formula list");
354 eof = 1; 498 eof = 1;
355 } else if (formula_elements[marker].formula_type == 499 } else if (formula_elements[marker].formula_type ==
356 psiconv_formula_dat_int) { 500 psiconv_formula_dat_int) {
357 psiconv_progress(lev+3,off+len,"Next item: an integer"); 501 psiconv_progress(lev+3,off+len,"Next item: an integer");
358 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);
375 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float); 519 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float);
376 len += leng; 520 len += leng;
377 if ((res = psiconv_list_add(formula_stack,formula))) 521 if ((res = psiconv_list_add(formula_stack,formula)))
378 goto ERROR8; 522 goto ERROR8;
379 formula->type = psiconv_formula_unknown; 523 formula->type = psiconv_formula_unknown;
380 524 } else if (formula_elements[marker].formula_type ==
525 psiconv_formula_dat_cellref) {
526 psiconv_progress(lev+3,off+len,"Next item: a cell reference");
527 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
528 &formula->data.dat_cellref)))
529 goto ERROR8;
530 formula->type = formula_elements[marker].formula_type;
531 len += leng;
532 if ((res = psiconv_list_add(formula_stack,formula)))
533 goto ERROR8;
534 formula->type = psiconv_formula_unknown;
535 } else if (formula_elements[marker].formula_type ==
536 psiconv_formula_dat_cellblock) {
537 psiconv_progress(lev+3,off+len,"Next item: a cell block");
538 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
539 &formula->data.dat_cellblock)))
540 goto ERROR8;
541 formula->type = formula_elements[marker].formula_type;
542 len += leng;
543 if ((res = psiconv_list_add(formula_stack,formula)))
544 goto ERROR8;
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;
381 } else if ((formula_elements[marker].formula_type == 558 } else if ((formula_elements[marker].formula_type ==
382 psiconv_formula_dat_var) || 559 psiconv_formula_dat_var) ||
383 (formula_elements[marker].formula_type == 560 (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) || 561 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!"); 562 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!");
396 goto ERROR8; 563 goto ERROR8;
397 } else if (formula_elements[marker].number_of_args == -1) { 564 } else if (formula_elements[marker].number_of_args == -1) {
398 psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); 565 psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!");
399 goto ERROR8; 566 goto ERROR8;
430 goto ERROR8; 597 goto ERROR8;
431 subformula4->type = subformula3->type = subformula2->type = 598 subformula4->type = subformula3->type = subformula2->type =
432 subformula1->type = formula->type = psiconv_formula_unknown; 599 subformula1->type = formula->type = psiconv_formula_unknown;
433 } 600 }
434 } 601 }
435 if ((len != bytelen) || !eof) { 602 if ((len+off > maxlen) || !eof) {
436 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 603 psiconv_warn(lev+2,off+len,"Formula corrupted!");
437 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",
438 bytelen,len); 605 maxlen,len+off);
439 goto ERROR8; 606 goto ERROR8;
440 } 607 }
441 if ((psiconv_list_length(formula_stack)) != 1) { 608 if ((psiconv_list_length(formula_stack)) != 1) {
442 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 609 psiconv_warn(lev+2,off+len,"Formula corrupted!");
443 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)",
451 618
452 if (length) 619 if (length)
453 *length = len; 620 *length = len;
454 621
455 psiconv_progress(lev,off+len-1, 622 psiconv_progress(lev,off+len-1,
456 "End of formula (total length: %08x)", len); 623 "End of formula element list (total length: %08x)", len);
457 return 0; 624 return 0;
458 625
459ERROR8: 626ERROR8:
460 psiconv_free_formula(subformula4); 627 psiconv_free_formula(subformula4);
461ERROR7: 628ERROR7:
469ERROR3: 636ERROR3:
470 psiconv_free_formula_list(formula_stack); 637 psiconv_free_formula_list(formula_stack);
471ERROR2: 638ERROR2:
472 free (*result); 639 free (*result);
473ERROR1: 640ERROR1:
474 psiconv_warn(lev+1,off,"Reading of formula failed"); 641 psiconv_warn(lev+1,off,"Reading of formula element list failed");
475 if (length) 642 if (length)
476 *length = 0; 643 *length = 0;
477 if (!res) 644 if (!res)
478 return -PSICONV_E_NOMEM; 645 return -PSICONV_E_NOMEM;
479 else 646 else
480 return res; 647 return res;
481} 648}
482 649
650
651
652
653int 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
707ERROR2:
708 psiconv_free_formula(*result);
709ERROR1:
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

Legend:
Removed from v.101  
changed lines
  Added in v.105

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