… | |
… | |
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 | |
294 | int psiconv_parse_formula(const psiconv_buffer buf, int lev, |
291 | static 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 | |
|
|
316 | static 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; |
|
|
349 | ERROR: |
|
|
350 | if (length) |
|
|
351 | *length = 0; |
|
|
352 | return res; |
|
|
353 | } |
|
|
354 | |
|
|
355 | static 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; |
|
|
378 | ERROR: |
|
|
379 | if (length) |
|
|
380 | *length = 0; |
|
|
381 | return res; |
|
|
382 | } |
|
|
383 | |
|
|
384 | static 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; |
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; |
|
|
478 | } else if ((formula_elements[marker].formula_type == |
|
|
479 | psiconv_formula_dat_cellblock) || |
|
|
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 == |
|
|
492 | psiconv_formula_dat_string) { |
|
|
493 | psiconv_progress(lev+3,off+len,"Next item: a string"); |
|
|
494 | formula->data.dat_string = |
|
|
495 | psiconv_read_short_string(buf,lev+2,off+len,&leng,&res); |
|
|
496 | if (res) |
|
|
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; |
381 | } else if ((formula_elements[marker].formula_type == |
503 | } else if ((formula_elements[marker].formula_type == |
382 | psiconv_formula_dat_var) || |
504 | psiconv_formula_dat_var) || |
383 | (formula_elements[marker].formula_type == |
505 | (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) || |
506 | 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!"); |
507 | psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); |
396 | goto ERROR8; |
508 | goto ERROR8; |
397 | } else if (formula_elements[marker].number_of_args == -1) { |
509 | } else if (formula_elements[marker].number_of_args == -1) { |
398 | psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); |
510 | psiconv_progress(lev+3,off+len,"Going to parse a vararg function"); |
|
|
511 | if (!(formula->data.fun_operands = |
|
|
512 | psiconv_list_new(sizeof(*formula)))) |
|
|
513 | goto ERROR8; |
|
|
514 | formula->type = formula_elements[marker].formula_type; |
|
|
515 | nr_of_subs = 0; |
|
|
516 | do { |
|
|
517 | nr_of_subs ++; |
|
|
518 | psiconv_progress(lev+4,off+len,"Going to read vararg argument %d", |
|
|
519 | nr_of_subs); |
|
|
520 | if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng, |
|
|
521 | &subformula,maxlen))) |
|
|
522 | goto ERROR8; |
|
|
523 | len += leng; |
|
|
524 | if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) { |
|
|
525 | psiconv_free_formula(subformula); |
|
|
526 | goto ERROR8; |
|
|
527 | } |
|
|
528 | free(subformula); |
|
|
529 | psiconv_progress(lev+4,off+len,"Going to read the next marker"); |
|
|
530 | submarker = psiconv_read_u8(buf,lev+4,off+len,&res); |
|
|
531 | len ++; |
|
|
532 | if (res) |
|
|
533 | goto ERROR8; |
|
|
534 | } while (formula_elements[submarker].formula_type |
|
|
535 | == psiconv_formula_mark_opsep); |
|
|
536 | if (formula_elements[submarker].formula_type |
|
|
537 | != psiconv_formula_mark_opend) { |
|
|
538 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
|
|
539 | psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker); |
399 | goto ERROR8; |
540 | goto ERROR8; |
|
|
541 | } |
|
|
542 | psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x", |
|
|
543 | marker); |
|
|
544 | submarker = psiconv_read_u8(buf,lev+3,off+len,&res); |
|
|
545 | if (res) |
|
|
546 | goto ERROR8; |
|
|
547 | if (submarker != marker) { |
|
|
548 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
|
|
549 | psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x", |
|
|
550 | marker,submarker); |
|
|
551 | goto ERROR8; |
|
|
552 | } |
|
|
553 | len++; |
|
|
554 | psiconv_progress(lev+3,off+len, |
|
|
555 | "Going to read the number of arguments (%d expected)", |
|
|
556 | nr_of_subs); |
|
|
557 | temp = psiconv_read_u16(buf,lev+3,off+len,&res); |
|
|
558 | if (res) |
|
|
559 | goto ERROR8; |
|
|
560 | if (temp != nr_of_subs) { |
|
|
561 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
|
|
562 | psiconv_debug(lev+3,off+len, |
|
|
563 | "Read %d arguments, but formula says there are %d", |
|
|
564 | nr_of_subs,temp); |
|
|
565 | goto ERROR8; |
|
|
566 | } |
|
|
567 | len += 2; |
|
|
568 | if ((res = psiconv_list_add(formula_stack,formula))) |
|
|
569 | goto ERROR8; |
|
|
570 | formula->type = psiconv_formula_unknown; |
400 | } else { |
571 | } else { |
401 | if (formula_elements[marker].number_of_args > 0) |
572 | if (formula_elements[marker].number_of_args > 0) |
402 | if ((res = psiconv_list_pop(formula_stack,subformula1))) |
573 | if ((res = psiconv_list_pop(formula_stack,subformula1))) |
403 | goto ERROR8; |
574 | goto ERROR8; |
404 | if (formula_elements[marker].number_of_args > 1) |
575 | if (formula_elements[marker].number_of_args > 1) |
… | |
… | |
430 | goto ERROR8; |
601 | goto ERROR8; |
431 | subformula4->type = subformula3->type = subformula2->type = |
602 | subformula4->type = subformula3->type = subformula2->type = |
432 | subformula1->type = formula->type = psiconv_formula_unknown; |
603 | subformula1->type = formula->type = psiconv_formula_unknown; |
433 | } |
604 | } |
434 | } |
605 | } |
435 | if ((len != bytelen) || !eof) { |
606 | if ((len+off > maxlen) || !eof) { |
436 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
607 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
437 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
608 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
438 | bytelen,len); |
609 | maxlen,len+off); |
439 | goto ERROR8; |
610 | goto ERROR8; |
440 | } |
611 | } |
441 | if ((psiconv_list_length(formula_stack)) != 1) { |
612 | if ((psiconv_list_length(formula_stack)) != 1) { |
442 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
613 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
443 | psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", |
614 | psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", |
… | |
… | |
451 | |
622 | |
452 | if (length) |
623 | if (length) |
453 | *length = len; |
624 | *length = len; |
454 | |
625 | |
455 | psiconv_progress(lev,off+len-1, |
626 | psiconv_progress(lev,off+len-1, |
456 | "End of formula (total length: %08x)", len); |
627 | "End of formula element list (total length: %08x)", len); |
457 | return 0; |
628 | return 0; |
458 | |
629 | |
459 | ERROR8: |
630 | ERROR8: |
460 | psiconv_free_formula(subformula4); |
631 | psiconv_free_formula(subformula4); |
461 | ERROR7: |
632 | ERROR7: |
… | |
… | |
469 | ERROR3: |
640 | ERROR3: |
470 | psiconv_free_formula_list(formula_stack); |
641 | psiconv_free_formula_list(formula_stack); |
471 | ERROR2: |
642 | ERROR2: |
472 | free (*result); |
643 | free (*result); |
473 | ERROR1: |
644 | ERROR1: |
474 | psiconv_warn(lev+1,off,"Reading of formula failed"); |
645 | psiconv_warn(lev+1,off,"Reading of formula element list failed"); |
475 | if (length) |
646 | if (length) |
476 | *length = 0; |
647 | *length = 0; |
477 | if (!res) |
648 | if (!res) |
478 | return -PSICONV_E_NOMEM; |
649 | return -PSICONV_E_NOMEM; |
479 | else |
650 | else |
480 | return res; |
651 | return res; |
481 | } |
652 | } |
482 | |
653 | |
|
|
654 | |
|
|
655 | |
|
|
656 | |
|
|
657 | int psiconv_parse_formula(const psiconv_buffer buf, int lev, |
|
|
658 | psiconv_u32 off, int *length, |
|
|
659 | psiconv_formula *result) |
|
|
660 | { |
|
|
661 | int res=0; |
|
|
662 | int len=0; |
|
|
663 | int leng; |
|
|
664 | psiconv_u32 bytelen,formula_end; |
|
|
665 | psiconv_u8 temp; |
|
|
666 | |
|
|
667 | psiconv_progress(lev+1,off,"Going to read a formula"); |
|
|
668 | |
|
|
669 | psiconv_progress(lev+2,off+len, |
|
|
670 | "Going to read the formula byte length"); |
|
|
671 | bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res); |
|
|
672 | if (res) |
|
|
673 | goto ERROR1; |
|
|
674 | psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen); |
|
|
675 | len += leng; |
|
|
676 | bytelen += len; |
|
|
677 | formula_end = off + bytelen; |
|
|
678 | |
|
|
679 | psiconv_progress(lev+2,off+len,"Going to read the formula elements list"); |
|
|
680 | if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng, |
|
|
681 | result,formula_end))) |
|
|
682 | goto ERROR1; |
|
|
683 | len += leng; |
|
|
684 | |
|
|
685 | psiconv_progress(lev+2,off+len,"Going to read the eof marker"); |
|
|
686 | temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
|
|
687 | if (res) |
|
|
688 | goto ERROR2; |
|
|
689 | if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) { |
|
|
690 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
|
|
691 | psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x", |
|
|
692 | 0x15,temp); |
|
|
693 | goto ERROR2; |
|
|
694 | } |
|
|
695 | len ++; |
|
|
696 | |
|
|
697 | if (off+len != formula_end) { |
|
|
698 | psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
|
|
699 | psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
|
|
700 | formula_end,len+off); |
|
|
701 | goto ERROR2; |
|
|
702 | } |
|
|
703 | |
|
|
704 | if (length) |
|
|
705 | *length = len; |
|
|
706 | |
|
|
707 | psiconv_progress(lev,off+len-1, |
|
|
708 | "End of formula (total length: %08x)", len); |
|
|
709 | return 0; |
|
|
710 | |
|
|
711 | ERROR2: |
|
|
712 | psiconv_free_formula(*result); |
|
|
713 | ERROR1: |
|
|
714 | psiconv_warn(lev+1,off,"Reading of formula failed"); |
|
|
715 | if (length) |
|
|
716 | *length = 0; |
|
|
717 | if (!res) |
|
|
718 | return -PSICONV_E_NOMEM; |
|
|
719 | else |
|
|
720 | return res; |
|
|
721 | } |
|
|
722 | |
|
|
723 | |