/[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 108 Revision 168
22 22
23#include <stdlib.h> 23#include <stdlib.h>
24 24
25#include "parse_routines.h" 25#include "parse_routines.h"
26#include "error.h" 26#include "error.h"
27
28#ifdef DMALLOC
29#include <dmalloc.h>
30#endif
31
27 32
28struct formula_element 33struct formula_element
29{ 34{
30 psiconv_formula_type_t formula_type; 35 psiconv_formula_type_t formula_type;
31 int number_of_args; 36 int number_of_args;
286 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 291 {psiconv_formula_unknown,0,"*UNKNOWN*"},
287 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 292 {psiconv_formula_unknown,0,"*UNKNOWN*"},
288 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 293 {psiconv_formula_unknown,0,"*UNKNOWN*"},
289 {psiconv_formula_unknown,0,"*UNKNOWN*"}}; 294 {psiconv_formula_unknown,0,"*UNKNOWN*"}};
290 295
291static psiconv_string_t psiconv_read_sheet_string(const psiconv_buffer buf,
292 int lev,
293 psiconv_u32 off,int *length, int *status)
294{
295 int stringlen,i,len,localstatus;
296 psiconv_string_t result;
297 char *res_copy;
298
299 psiconv_progress(lev+1,off,"Going to read a sheet string");
300
301 stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus);
302 if (localstatus)
303 goto ERROR1;
304 psiconv_debug(lev+2,off,"Length: %i",stringlen);
305 len = 1;
306
307 result = malloc(stringlen + 1);
308 if (!result)
309 goto ERROR1;
310 for (i = 0; (i < stringlen) && !localstatus; i++)
311 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus);
312 if (localstatus)
313 goto ERROR2;
314 result[stringlen] = 0;
315 len += stringlen;
316
317 res_copy = psiconv_make_printable(result);
318 if (!res_copy)
319 goto ERROR2;
320 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
321 free(res_copy);
322
323 if (length)
324 *length = len;
325
326 if (status)
327 *status = 0;
328
329 psiconv_progress(lev+1,off+len-1,"End of sheet string (total length: %08x)",
330 len);
331
332 return result;
333
334
335ERROR2:
336 free(result);
337ERROR1:
338 psiconv_warn(lev+1,off,"Reading of sheet string failed");
339 if (status)
340 *status = localstatus;
341 if (length)
342 *length = 0;
343 return NULL;
344}
345
346
347static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev, 296static int psiconv_parse_sheet_ref(const psiconv_config config,
297 const psiconv_buffer buf,int lev,
348 psiconv_u32 off, int *length, 298 psiconv_u32 off, int *length,
349 psiconv_sheet_ref_t *result) 299 psiconv_sheet_ref_t *result)
350{ 300{
351 int res; 301 int res;
352 psiconv_u16 temp; 302 psiconv_u16 temp;
353 303
354 psiconv_progress(lev+1,off,"Going to read a sheet ref"); 304 psiconv_progress(config,lev+1,off,"Going to read a sheet ref");
355 psiconv_progress(lev+2,off,"Going to read the offset encoding"); 305 psiconv_progress(config,lev+2,off,"Going to read the offset encoding");
356 temp = psiconv_read_u16(buf,lev+2,off,&res); 306 temp = psiconv_read_u16(config,buf,lev+2,off,&res);
357 if (res) { 307 if (res) {
358 if (length) 308 if (length)
359 *length = 0; 309 *length = 0;
360 return res; 310 return res;
361 } 311 }
362 psiconv_debug(lev+2,off,"Encoded word: %04x",temp); 312 psiconv_debug(config,lev+2,off,"Encoded word: %04x",temp);
363 result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false; 313 result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false;
364 result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1); 314 result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1);
365 psiconv_debug(lev+2,off,"Reference: %s offset %d", 315 psiconv_debug(config,lev+2,off,"Reference: %s offset %d",
366 result->absolute?"absolute":"relative",result->offset); 316 result->absolute?"absolute":"relative",result->offset);
367 if (length) 317 if (length)
368 *length = 2; 318 *length = 2;
369 return 0; 319 return 0;
370} 320}
371 321
372static int psiconv_parse_sheet_cell_reference(const psiconv_buffer buf,int lev, 322static int psiconv_parse_sheet_cell_reference(const psiconv_config config,
323 const psiconv_buffer buf,int lev,
373 psiconv_u32 off, int *length, 324 psiconv_u32 off, int *length,
374 psiconv_sheet_cell_reference_t *result) 325 psiconv_sheet_cell_reference_t *result)
375{ 326{
376 int len = 0; 327 int len = 0;
377 int leng,res; 328 int leng,res;
378 psiconv_u8 temp; 329 psiconv_u8 temp;
379 330
380 psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference"); 331 psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell reference");
381 psiconv_progress(lev+2,off+len,"Going to read the row reference"); 332 psiconv_progress(config,lev+2,off+len,"Going to read the row reference");
382 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->row))) 333 if ((res = psiconv_parse_sheet_ref(config,buf,lev+2,off+len,&leng,&result->row)))
383 goto ERROR; 334 goto ERROR;
384 len += leng; 335 len += leng;
385 psiconv_progress(lev+2,off+len,"Going to read the column reference"); 336 psiconv_progress(config,lev+2,off+len,"Going to read the column reference");
386 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->column))) 337 if ((res = psiconv_parse_sheet_ref(config,buf,lev+2,off+len,&leng,&result->column)))
387 goto ERROR; 338 goto ERROR;
388 len += leng; 339 len += leng;
389 340
390 psiconv_progress(lev+2,off+len, 341 psiconv_progress(config,lev+2,off+len,
391 "Going to read the trailing byte (%02x expected)",0); 342 "Going to read the trailing byte (%02x expected)",0);
392 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 343 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
393 if (res) 344 if (res)
394 goto ERROR; 345 goto ERROR;
395 if (temp != 0) { 346 if (temp != 0) {
396 psiconv_warn(lev+2,off+len,"Unknown byte in cell reference (ignored"); 347 psiconv_warn(config,lev+2,off+len,"Unknown byte in cell reference (ignored");
397 psiconv_debug(lev+2,off+len,"Trailing byte: %02x",temp); 348 psiconv_debug(config,lev+2,off+len,"Trailing byte: %02x",temp);
398 } 349 }
399 len ++; 350 len ++;
400 psiconv_progress(lev,off+len-1, 351 psiconv_progress(config,lev,off+len-1,
401 "End of cell reference (total length: %08x)", len); 352 "End of cell reference (total length: %08x)", len);
402 if (length) 353 if (length)
403 *length = len; 354 *length = len;
404 return 0; 355 return 0;
405ERROR: 356ERROR:
406 if (length) 357 if (length)
407 *length = 0; 358 *length = 0;
408 return res; 359 return res;
409} 360}
410 361
411static int psiconv_parse_sheet_cell_block(const psiconv_buffer buf,int lev, 362static int psiconv_parse_sheet_cell_block(const psiconv_config config,
363 const psiconv_buffer buf,int lev,
412 psiconv_u32 off, int *length, 364 psiconv_u32 off, int *length,
413 psiconv_sheet_cell_block_t *result) 365 psiconv_sheet_cell_block_t *result)
414{ 366{
415 int len = 0; 367 int len = 0;
416 int leng,res; 368 int leng,res;
417 369
418 psiconv_progress(lev+1,off+len,"Going to read a sheet cell block"); 370 psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell block");
419 psiconv_progress(lev+2,off+len,"Going to read the first cell"); 371 psiconv_progress(config,lev+2,off+len,"Going to read the first cell");
420 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, 372 if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng,
421 &result->first))) 373 &result->first)))
422 goto ERROR; 374 goto ERROR;
423 len += leng; 375 len += leng;
424 psiconv_progress(lev+2,off+len,"Going to read the last cell"); 376 psiconv_progress(config,lev+2,off+len,"Going to read the last cell");
425 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, 377 if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng,
426 &result->last))) 378 &result->last)))
427 goto ERROR; 379 goto ERROR;
428 len += leng; 380 len += leng;
429 psiconv_progress(lev,off+len-1, 381 psiconv_progress(config,lev,off+len-1,
430 "End of cell block (total length: %08x)", len); 382 "End of cell block (total length: %08x)", len);
431 if (length) 383 if (length)
432 *length = len; 384 *length = len;
433 return 0; 385 return 0;
434ERROR: 386ERROR:
435 if (length) 387 if (length)
436 *length = 0; 388 *length = 0;
437 return res; 389 return res;
438} 390}
439 391
440static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev, 392static int psiconv_parse_formula_element_list(const psiconv_config config,
393 const psiconv_buffer buf, int lev,
441 psiconv_u32 off, int *length, 394 psiconv_u32 off, int *length,
442 psiconv_formula *result, 395 psiconv_formula *result,
443 psiconv_u32 maxlen) 396 psiconv_u32 maxlen)
444{ 397{
445 int res=0; 398 int res=0;
446 int len=0; 399 int len=0;
447 int leng; 400 int leng;
448 int eof = 0; 401 int eof = 0;
449 psiconv_u8 marker,submarker; 402 psiconv_u8 marker,submarker,submarker2;
450 psiconv_formula_list formula_stack; 403 psiconv_formula_list formula_stack;
451 psiconv_formula formula,subformula,subformula1,subformula2, 404 psiconv_formula formula,subformula,subformula1,subformula2,
452 subformula3,subformula4; 405 subformula3,subformula4;
453 psiconv_u16 temp,nr_of_subs; 406 psiconv_u16 temp,nr_of_subs;
454 407
455 psiconv_progress(lev+1,off,"Going to read a formula element list"); 408 psiconv_progress(config,lev+1,off,"Going to read a formula element list");
456 if (!(*result = malloc(sizeof(**result)))) 409 if (!(*result = malloc(sizeof(**result))))
457 goto ERROR1; 410 goto ERROR1;
458 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 411 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
459 goto ERROR2; 412 goto ERROR2;
460 if (!(formula = malloc(sizeof(*formula)))) 413 if (!(formula = malloc(sizeof(*formula))))
473 if (!(subformula4 = malloc(sizeof(*subformula4)))) 426 if (!(subformula4 = malloc(sizeof(*subformula4))))
474 goto ERROR7; 427 goto ERROR7;
475 subformula4->type = psiconv_formula_unknown; 428 subformula4->type = psiconv_formula_unknown;
476 429
477 while (!eof && len+off < maxlen) { 430 while (!eof && len+off < maxlen) {
478 psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); 431 psiconv_progress(config,lev+3,off+len,"Going to read a formula item marker");
479 marker = psiconv_read_u8(buf,lev+2,off+len,&res); 432 marker = psiconv_read_u8(config,buf,lev+2,off+len,&res);
480 if (res) 433 if (res)
481 goto ERROR8; 434 goto ERROR8;
482 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, 435 psiconv_debug(config,lev+3,off+len,"Marker: %02x (%s)",marker,
483 formula_elements[marker].name); 436 formula_elements[marker].name);
484 len ++; 437 len ++;
485 438
486 if (formula_elements[marker].formula_type == psiconv_formula_unknown) { 439 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
487 psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); 440 psiconv_warn(config,lev+3,off+len,"Unknown formula marker found!");
488 goto ERROR8; 441 goto ERROR8;
489 } else if ((formula_elements[marker].formula_type == 442 } else if ((formula_elements[marker].formula_type ==
490 psiconv_formula_mark_eof) || 443 psiconv_formula_mark_eof) ||
491 (formula_elements[marker].formula_type == 444 (formula_elements[marker].formula_type ==
492 psiconv_formula_mark_opend) || 445 psiconv_formula_mark_opend) ||
493 (formula_elements[marker].formula_type == 446 (formula_elements[marker].formula_type ==
494 psiconv_formula_mark_opsep)) { 447 psiconv_formula_mark_opsep)) {
495 len--; 448 len--;
496 psiconv_progress(lev+3,off+len,"End of this formula list"); 449 psiconv_progress(config,lev+3,off+len,"End of this formula list");
497 eof = 1; 450 eof = 1;
498 } else if (formula_elements[marker].formula_type == 451 } else if (formula_elements[marker].formula_type ==
499 psiconv_formula_dat_int) { 452 psiconv_formula_dat_int) {
500 psiconv_progress(lev+3,off+len,"Next item: an integer"); 453 psiconv_progress(config,lev+3,off+len,"Next item: an integer");
501 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); 454 formula->data.dat_int = psiconv_read_u32(config,buf,lev+2,off+len,&res);
502 if (res) 455 if (res)
503 goto ERROR8; 456 goto ERROR8;
504 formula->type = formula_elements[marker].formula_type; 457 formula->type = formula_elements[marker].formula_type;
505 psiconv_debug(lev+3,off+len,"Value: %08x",formula->data.dat_int); 458 psiconv_debug(config,lev+3,off+len,"Value: %08x",formula->data.dat_int);
506 len += 4; 459 len += 4;
507 if ((res = psiconv_list_add(formula_stack,formula))) 460 if ((res = psiconv_list_add(formula_stack,formula)))
508 goto ERROR8; 461 goto ERROR8;
509 formula->type = psiconv_formula_unknown; 462 formula->type = psiconv_formula_unknown;
510 } else if (formula_elements[marker].formula_type == 463 } else if (formula_elements[marker].formula_type ==
511 psiconv_formula_dat_float) { 464 psiconv_formula_dat_float) {
512 psiconv_progress(lev+3,off+len,"Next item: a float"); 465 psiconv_progress(config,lev+3,off+len,"Next item: a float");
513 formula->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng, 466 formula->data.dat_float = psiconv_read_float(config,buf,lev+2,off+len,&leng,
514 &res); 467 &res);
515 if (res) 468 if (res)
516 goto ERROR8; 469 goto ERROR8;
517 formula->type = formula_elements[marker].formula_type; 470 formula->type = formula_elements[marker].formula_type;
518 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float); 471 psiconv_debug(config,lev+3,off+len,"Value: %f",formula->data.dat_float);
519 len += leng; 472 len += leng;
520 if ((res = psiconv_list_add(formula_stack,formula))) 473 if ((res = psiconv_list_add(formula_stack,formula)))
521 goto ERROR8; 474 goto ERROR8;
522 formula->type = psiconv_formula_unknown; 475 formula->type = psiconv_formula_unknown;
523 } else if (formula_elements[marker].formula_type == 476 } else if (formula_elements[marker].formula_type ==
524 psiconv_formula_dat_cellref) { 477 psiconv_formula_dat_cellref) {
525 psiconv_progress(lev+3,off+len,"Next item: a cell reference"); 478 psiconv_progress(config,lev+3,off+len,"Next item: a cell reference");
526 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, 479 if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng,
527 &formula->data.dat_cellref))) 480 &formula->data.dat_cellref)))
528 goto ERROR8; 481 goto ERROR8;
529 formula->type = formula_elements[marker].formula_type; 482 formula->type = formula_elements[marker].formula_type;
530 len += leng; 483 len += leng;
531 if ((res = psiconv_list_add(formula_stack,formula))) 484 if ((res = psiconv_list_add(formula_stack,formula)))
533 formula->type = psiconv_formula_unknown; 486 formula->type = psiconv_formula_unknown;
534 } else if ((formula_elements[marker].formula_type == 487 } else if ((formula_elements[marker].formula_type ==
535 psiconv_formula_dat_cellblock) || 488 psiconv_formula_dat_cellblock) ||
536 (formula_elements[marker].formula_type == 489 (formula_elements[marker].formula_type ==
537 psiconv_formula_dat_vcellblock)) { 490 psiconv_formula_dat_vcellblock)) {
538 psiconv_progress(lev+3,off+len,"Next item: a cell block"); 491 psiconv_progress(config,lev+3,off+len,"Next item: a cell block");
539 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, 492 if ((res = psiconv_parse_sheet_cell_block(config,buf,lev+2,off+len,&leng,
540 &formula->data.dat_cellblock))) 493 &formula->data.dat_cellblock)))
541 goto ERROR8; 494 goto ERROR8;
542 formula->type = formula_elements[marker].formula_type; 495 formula->type = formula_elements[marker].formula_type;
543 len += leng; 496 len += leng;
544 if ((res = psiconv_list_add(formula_stack,formula))) 497 if ((res = psiconv_list_add(formula_stack,formula)))
545 goto ERROR8; 498 goto ERROR8;
546 formula->type = psiconv_formula_unknown; 499 formula->type = psiconv_formula_unknown;
547 } else if (formula_elements[marker].formula_type == 500 } else if (formula_elements[marker].formula_type ==
548 psiconv_formula_dat_string) { 501 psiconv_formula_dat_string) {
549 psiconv_progress(lev+3,off+len,"Next item: a string"); 502 psiconv_progress(config,lev+3,off+len,"Next item: a string");
550 formula->data.dat_string = 503 formula->data.dat_string =
551 psiconv_read_sheet_string(buf,lev+2,off+len,&leng,&res); 504 psiconv_read_short_string(config,buf,lev+2,off+len,&leng,&res);
552 if (res) 505 if (res)
553 goto ERROR8; 506 goto ERROR8;
554 formula->type = formula_elements[marker].formula_type; 507 formula->type = formula_elements[marker].formula_type;
555 len += leng; 508 len += leng;
556 if ((res = psiconv_list_add(formula_stack,formula))) 509 if ((res = psiconv_list_add(formula_stack,formula)))
557 goto ERROR8; 510 goto ERROR8;
558 formula->type = psiconv_formula_unknown; 511 formula->type = psiconv_formula_unknown;
559 } else if ((formula_elements[marker].formula_type == 512 } else if ((formula_elements[marker].formula_type ==
560 psiconv_formula_dat_var) || 513 psiconv_formula_dat_var)) {
561 (formula_elements[marker].formula_type == 514 psiconv_progress(config,lev+3,off+len,"Next item: a variable reference");
562 psiconv_formula_dat_vcellblock)) { 515 formula->data.dat_variable = psiconv_read_u32(config,buf,lev+2,off+len,&res);
563 psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); 516 if (res)
564 goto ERROR8; 517 goto ERROR8;
518 formula->type = formula_elements[marker].formula_type;
519 len += 4;
520 if ((res = psiconv_list_add(formula_stack,formula)))
521 goto ERROR8;
522 formula->type = psiconv_formula_unknown;
565 } else if (formula_elements[marker].number_of_args == -1) { 523 } else if (formula_elements[marker].number_of_args == -1) {
566 psiconv_progress(lev+3,off+len,"Going to parse a vararg function"); 524 psiconv_progress(config,lev+3,off+len,"Going to parse a vararg function");
567 if (!(formula->data.fun_operands = 525 if (!(formula->data.fun_operands =
568 psiconv_list_new(sizeof(*formula)))) 526 psiconv_list_new(sizeof(*formula))))
569 goto ERROR8; 527 goto ERROR8;
570 formula->type = formula_elements[marker].formula_type; 528 formula->type = formula_elements[marker].formula_type;
571 nr_of_subs = 0; 529 nr_of_subs = 0;
572 do { 530 do {
573 nr_of_subs ++; 531 nr_of_subs ++;
574 psiconv_progress(lev+4,off+len,"Going to read vararg argument %d", 532 psiconv_progress(config,lev+4,off+len,"Going to read vararg argument %d",
575 nr_of_subs); 533 nr_of_subs);
576 if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng, 534 if ((res = psiconv_parse_formula_element_list(config,buf,lev+4,off+len,&leng,
577 &subformula,maxlen))) 535 &subformula,maxlen)))
578 goto ERROR8; 536 goto ERROR8;
579 len += leng; 537 len += leng;
580 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) { 538 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) {
581 psiconv_free_formula(subformula); 539 psiconv_free_formula(subformula);
582 goto ERROR8; 540 goto ERROR8;
583 } 541 }
584 free(subformula); 542 free(subformula);
585 psiconv_progress(lev+4,off+len,"Going to read the next marker"); 543 psiconv_progress(config,lev+4,off+len,"Going to read the next marker");
586 submarker = psiconv_read_u8(buf,lev+4,off+len,&res); 544 submarker = psiconv_read_u8(config,buf,lev+4,off+len,&res);
587 len ++; 545 len ++;
588 if (res) 546 if (res)
589 goto ERROR8; 547 goto ERROR8;
548 submarker2 = psiconv_read_u8(config,buf,lev+4,off+len,&res);
549 if (res)
550 goto ERROR8;
590 } while (formula_elements[submarker].formula_type 551 } while ((formula_elements[submarker].formula_type
591 == psiconv_formula_mark_opsep); 552 == psiconv_formula_mark_opsep) &&
553 (formula_elements[submarker2].formula_type
554 != psiconv_formula_mark_opend));
555 if ((formula_elements[submarker].formula_type ==
556 psiconv_formula_mark_opsep) &&
557 (formula_elements[submarker2].formula_type ==
558 psiconv_formula_mark_opend)) {
559 submarker=submarker2;
560 len++;
561 }
592 if (formula_elements[submarker].formula_type 562 if (formula_elements[submarker].formula_type
593 != psiconv_formula_mark_opend) { 563 != psiconv_formula_mark_opend) {
594 psiconv_warn(lev+3,off+len,"Formula corrupted!"); 564 psiconv_warn(config,lev+3,off+len,"Formula corrupted!");
595 psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker); 565 psiconv_debug(config,lev+3,off+len,"Found unexpected marker %02x",submarker);
596 goto ERROR8; 566 goto ERROR8;
597 } 567 }
598 psiconv_progress(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",
599 marker); 569 marker);
600 submarker = psiconv_read_u8(buf,lev+3,off+len,&res); 570 submarker = psiconv_read_u8(config,buf,lev+3,off+len,&res);
601 if (res) 571 if (res)
602 goto ERROR8; 572 goto ERROR8;
603 if (submarker != marker) { 573 if (submarker != marker) {
604 psiconv_warn(lev+3,off+len,"Formula corrupted!"); 574 psiconv_warn(config,lev+3,off+len,"Formula corrupted!");
605 psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x", 575 psiconv_debug(config,lev+3,off+len,"Expected marker %02x, found %02x",
606 marker,submarker); 576 marker,submarker);
607 goto ERROR8; 577 goto ERROR8;
608 } 578 }
609 len++; 579 len++;
610 psiconv_progress(lev+3,off+len, 580 psiconv_progress(config,lev+3,off+len,
611 "Going to read the number of arguments (%d expected)", 581 "Going to read the number of arguments (%d expected)",
612 nr_of_subs); 582 nr_of_subs);
613 temp = psiconv_read_u16(buf,lev+3,off+len,&res); 583 temp = psiconv_read_u16(config,buf,lev+3,off+len,&res);
614 if (res) 584 if (res)
615 goto ERROR8; 585 goto ERROR8;
616 if (temp != nr_of_subs) { 586 if (temp != nr_of_subs) {
617 psiconv_warn(lev+3,off+len,"Formula corrupted!"); 587 psiconv_warn(config,lev+3,off+len,"Formula corrupted!");
618 psiconv_debug(lev+3,off+len, 588 psiconv_debug(config,lev+3,off+len,
619 "Read %d arguments, but formula says there are %d", 589 "Read %d arguments, but formula says there are %d",
620 nr_of_subs,temp); 590 nr_of_subs,temp);
621 goto ERROR8; 591 goto ERROR8;
622 } 592 }
623 len += 2; 593 len += 2;
658 subformula4->type = subformula3->type = subformula2->type = 628 subformula4->type = subformula3->type = subformula2->type =
659 subformula1->type = formula->type = psiconv_formula_unknown; 629 subformula1->type = formula->type = psiconv_formula_unknown;
660 } 630 }
661 } 631 }
662 if ((len+off > maxlen) || !eof) { 632 if ((len+off > maxlen) || !eof) {
663 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 633 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
664 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", 634 psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x",
665 maxlen,len+off); 635 maxlen,len+off);
666 goto ERROR8; 636 goto ERROR8;
667 } 637 }
668 if ((psiconv_list_length(formula_stack)) != 1) { 638 if ((psiconv_list_length(formula_stack)) != 1) {
669 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 639 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
670 psiconv_debug(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)",
671 psiconv_list_length(formula_stack)); 641 psiconv_list_length(formula_stack));
672 goto ERROR8; 642 goto ERROR8;
673 } 643 }
674 if ((res = psiconv_list_pop(formula_stack,*result))) 644 if ((res = psiconv_list_pop(formula_stack,*result)))
675 goto ERROR8; 645 goto ERROR8;
677 free(formula); 647 free(formula);
678 648
679 if (length) 649 if (length)
680 *length = len; 650 *length = len;
681 651
682 psiconv_progress(lev,off+len-1, 652 psiconv_progress(config,lev,off+len-1,
683 "End of formula element list (total length: %08x)", len); 653 "End of formula element list (total length: %08x)", len);
684 return 0; 654 return 0;
685 655
686ERROR8: 656ERROR8:
687 psiconv_free_formula(subformula4); 657 psiconv_free_formula(subformula4);
696ERROR3: 666ERROR3:
697 psiconv_free_formula_list(formula_stack); 667 psiconv_free_formula_list(formula_stack);
698ERROR2: 668ERROR2:
699 free (*result); 669 free (*result);
700ERROR1: 670ERROR1:
701 psiconv_warn(lev+1,off,"Reading of formula element list failed"); 671 psiconv_warn(config,lev+1,off,"Reading of formula element list failed");
702 if (length) 672 if (length)
703 *length = 0; 673 *length = 0;
704 if (!res) 674 if (!res)
705 return -PSICONV_E_NOMEM; 675 return -PSICONV_E_NOMEM;
706 else 676 else
708} 678}
709 679
710 680
711 681
712 682
713int psiconv_parse_formula(const psiconv_buffer buf, int lev, 683int psiconv_parse_formula(const psiconv_config config,
684 const psiconv_buffer buf, int lev,
714 psiconv_u32 off, int *length, 685 psiconv_u32 off, int *length,
715 psiconv_formula *result) 686 psiconv_formula *result)
716{ 687{
717 int res=0; 688 int res=0;
718 int len=0; 689 int len=0;
719 int leng; 690 int leng;
720 psiconv_u32 bytelen,formula_end; 691 psiconv_u32 bytelen,formula_end;
721 psiconv_u8 temp; 692 psiconv_u8 temp;
722 693
723 psiconv_progress(lev+1,off,"Going to read a formula"); 694 psiconv_progress(config,lev+1,off,"Going to read a formula");
724 695
725 psiconv_progress(lev+2,off+len, 696 psiconv_progress(config,lev+2,off+len,
726 "Going to read the formula byte length"); 697 "Going to read the formula byte length");
727 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res); 698 bytelen = psiconv_read_S(config,buf,lev+2,off+len,&leng,&res);
728 if (res) 699 if (res)
729 goto ERROR1; 700 goto ERROR1;
730 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen); 701 psiconv_debug(config,lev+2,off+len,"Formula byte length: %d",bytelen);
731 len += leng; 702 len += leng;
732 bytelen += len; 703 bytelen += len;
733 formula_end = off + bytelen; 704 formula_end = off + bytelen;
734 705
735 psiconv_progress(lev+2,off+len,"Going to read the formula elements list"); 706 psiconv_progress(config,lev+2,off+len,"Going to read the formula elements list");
736 if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng, 707 if ((res = psiconv_parse_formula_element_list(config,buf,lev+2,off+len,&leng,
737 result,formula_end))) 708 result,formula_end)))
738 goto ERROR1; 709 goto ERROR1;
739 len += leng; 710 len += leng;
740 711
741 psiconv_progress(lev+2,off+len,"Going to read the eof marker"); 712 psiconv_progress(config,lev+2,off+len,"Going to read the eof marker");
742 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 713 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
743 if (res) 714 if (res)
744 goto ERROR2; 715 goto ERROR2;
745 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) { 716 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) {
746 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 717 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
747 psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x", 718 psiconv_debug(config,lev+2,off+len,"Expected marker: %02x, found byte: %02x",
748 0x15,temp); 719 0x15,temp);
749 goto ERROR2; 720 goto ERROR2;
750 } 721 }
751 len ++; 722 len ++;
752 723
753 if (off+len != formula_end) { 724 if (off+len != formula_end) {
754 psiconv_warn(lev+2,off+len,"Formula corrupted!"); 725 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
755 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", 726 psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x",
756 formula_end,len+off); 727 formula_end,len+off);
757 goto ERROR2; 728 goto ERROR2;
758 } 729 }
759 730
760 if (length) 731 if (length)
761 *length = len; 732 *length = len;
762 733
763 psiconv_progress(lev,off+len-1, 734 psiconv_progress(config,lev,off+len-1,
764 "End of formula (total length: %08x)", len); 735 "End of formula (total length: %08x)", len);
765 return 0; 736 return 0;
766 737
767ERROR2: 738ERROR2:
768 psiconv_free_formula(*result); 739 psiconv_free_formula(*result);
769ERROR1: 740ERROR1:
770 psiconv_warn(lev+1,off,"Reading of formula failed"); 741 psiconv_warn(config,lev+1,off,"Reading of formula failed");
771 if (length) 742 if (length)
772 *length = 0; 743 *length = 0;
773 if (!res) 744 if (!res)
774 return -PSICONV_E_NOMEM; 745 return -PSICONV_E_NOMEM;
775 else 746 else

Legend:
Removed from v.108  
changed lines
  Added in v.168

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