… | |
… | |
275 | int res = 0; |
275 | int res = 0; |
276 | int len=0; |
276 | int len=0; |
277 | |
277 | |
278 | psiconv_u32 text_len; |
278 | psiconv_u32 text_len; |
279 | psiconv_paragraph para; |
279 | psiconv_paragraph para; |
280 | psiconv_u8 temp; |
280 | psiconv_ucs2 temp; |
|
|
281 | psiconv_list line; |
|
|
282 | psiconv_ucs2 *nextcharptr; |
281 | |
283 | |
282 | int nr; |
284 | int nr; |
283 | int i,j,start,leng; |
285 | int i,j,leng; |
284 | char *str_copy; |
286 | char *str_copy; |
285 | |
287 | |
286 | psiconv_progress(config,lev+1,off,"Going to parse the text section"); |
288 | psiconv_progress(config,lev+1,off,"Going to parse the text section"); |
287 | psiconv_progress(config,lev+2,off,"Reading the text length"); |
|
|
288 | |
289 | |
289 | if(!(*result = psiconv_list_new(sizeof(*para)))) |
290 | if(!(*result = psiconv_list_new(sizeof(*para)))) |
290 | goto ERROR1; |
291 | goto ERROR1; |
291 | if (!(para = malloc(sizeof(*para)))) |
292 | if (!(para = malloc(sizeof(*para)))) |
292 | goto ERROR2; |
293 | goto ERROR2; |
293 | |
294 | |
|
|
295 | psiconv_progress(config,lev+2,off,"Reading the text length"); |
294 | text_len = psiconv_read_X(config,buf,lev+2,off,&leng,&res); |
296 | text_len = psiconv_read_X(config,buf,lev+2,off,&leng,&res); |
295 | if (res) |
297 | if (res) |
296 | goto ERROR3; |
298 | goto ERROR3; |
297 | psiconv_debug(config,lev+2,off,"Length: %08x",text_len); |
299 | psiconv_debug(config,lev+2,off,"Length: %08x",text_len); |
298 | len += leng; |
300 | len += leng; |
299 | |
301 | |
300 | psiconv_progress(config,lev+2,off+len,"Going to read all paragraph text"); |
302 | if (!(line = psiconv_list_new(sizeof(psiconv_ucs2)))) |
|
|
303 | goto ERROR3; |
|
|
304 | |
|
|
305 | i = 0; |
301 | nr = 0; |
306 | nr = 0; |
302 | start = 0; |
307 | while (i < text_len) { |
303 | for (i = 0; i < text_len; i++) { |
|
|
304 | temp = psiconv_read_u8(config,buf,lev+2,off+len+i,&res); |
308 | temp = psiconv_unicode_read_char(config,buf,lev+2,off+len+i,&leng,&res); |
305 | if (res) |
309 | if (res) |
306 | goto ERROR3; |
|
|
307 | if (temp == 0x06) { |
|
|
308 | if (!(para->text = malloc((sizeof((*(para->text))) * (i - start + 1))))) |
|
|
309 | goto ERROR3; |
|
|
310 | for (j = 0; j < i - start; j++) { |
|
|
311 | temp = psiconv_read_u8(config,buf,lev+1,off + len + start + j,&res); |
|
|
312 | if (res) |
|
|
313 | goto ERROR4; |
|
|
314 | para->text[j] = psiconv_unicode_from_char(config,temp); |
|
|
315 | } |
|
|
316 | para->text[j] = 0; |
|
|
317 | |
|
|
318 | if ((res = psiconv_list_add(*result,para))) |
|
|
319 | goto ERROR4; |
310 | goto ERROR4; |
|
|
311 | if (i + leng > text_len) { |
|
|
312 | psiconv_error(config,lev+2,off+len+i,"Malformed text section"); |
|
|
313 | res = PSICONV_E_PARSE; |
|
|
314 | goto ERROR4; |
|
|
315 | } |
|
|
316 | if ((temp == 0x06) || (i + leng == text_len)) { |
|
|
317 | if (!(para->text = psiconv_unicode_from_list(line))) |
|
|
318 | goto ERROR4; |
320 | |
319 | |
321 | if (!(str_copy = psiconv_make_printable(config,para->text))) |
320 | if (!(str_copy = psiconv_make_printable(config,para->text))) |
322 | goto ERROR3; |
321 | goto ERROR5; |
323 | psiconv_debug(config,lev+2,off+i+len,"Line %d: %d characters",nr, |
322 | psiconv_debug(config,lev+2,off+i+len,"Line %d: %d characters",nr, |
324 | strlen(str_copy) +1); |
323 | strlen(str_copy) +1); |
325 | psiconv_debug(config,lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
324 | psiconv_debug(config,lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
326 | free(str_copy); |
325 | free(str_copy); |
|
|
326 | i += leng; |
327 | |
327 | |
328 | start = i + 1; |
328 | if (!(para->in_lines = psiconv_list_new(sizeof( |
|
|
329 | struct psiconv_in_line_layout_s)))) |
|
|
330 | goto ERROR5; |
|
|
331 | if (!(para->replacements = psiconv_list_new(sizeof( |
|
|
332 | struct psiconv_replacement_s)))) |
|
|
333 | goto ERROR6; |
|
|
334 | if (!(para->base_character = psiconv_basic_character_layout())) |
|
|
335 | goto ERROR7; |
|
|
336 | if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) |
|
|
337 | goto ERROR8; |
|
|
338 | para->base_style = 0; |
|
|
339 | |
|
|
340 | if ((res = psiconv_list_add(*result,para))) |
|
|
341 | goto ERROR9; |
|
|
342 | psiconv_progress(config,lev+2,off+len+i,"Starting a new line"); |
|
|
343 | psiconv_list_empty(line); |
329 | nr ++; |
344 | nr ++; |
|
|
345 | } else { |
|
|
346 | if ((res = psiconv_list_add(line,&temp))) |
|
|
347 | goto ERROR4; |
|
|
348 | i += leng; |
330 | } |
349 | } |
331 | } |
350 | } |
332 | |
351 | |
333 | if (start != text_len) { |
352 | psiconv_list_free(line); |
334 | psiconv_warn(config,lev+2,off+start+len, |
|
|
335 | "Last line does not end on EOL (%d characters left)", len - start); |
|
|
336 | if (!(para->text = malloc(text_len - start + 1))) |
|
|
337 | goto ERROR3; |
|
|
338 | for (j = 0; j < text_len - start; j++) { |
|
|
339 | temp = psiconv_read_u8(config,buf,lev+2,off + start + j + len, &res); |
|
|
340 | if (res) |
|
|
341 | goto ERROR4; |
|
|
342 | para->text[j] = psiconv_unicode_from_char(config,temp); |
|
|
343 | } |
|
|
344 | para->text[text_len - start] = 0; |
|
|
345 | if ((res = psiconv_list_add(*result,para))) |
|
|
346 | goto ERROR4; |
|
|
347 | if (!(str_copy = psiconv_make_printable(config,para->text))) |
|
|
348 | goto ERROR3; |
|
|
349 | psiconv_debug(config,lev+2,off+start+len,"Last line: %d characters",nr, |
|
|
350 | strlen(str_copy)+1); |
|
|
351 | psiconv_debug(config,lev+2,off+start+len,"Last line: `%s'",str_copy); |
|
|
352 | free(str_copy); |
|
|
353 | } |
|
|
354 | |
|
|
355 | free(para); |
353 | free(para); |
356 | |
354 | |
357 | /* Initialize the remaining parts of each paragraph */ |
|
|
358 | for (i = 0; i < psiconv_list_length(*result); i ++) { |
|
|
359 | if (!(para = psiconv_list_get(*result,i))) { |
|
|
360 | psiconv_error(config,lev+2,off+len,"Massive memory corruption"); |
|
|
361 | goto ERROR2_0; |
|
|
362 | } |
|
|
363 | if (!(para->in_lines = psiconv_list_new(sizeof( |
|
|
364 | struct psiconv_in_line_layout_s)))) |
|
|
365 | goto ERROR2_0; |
|
|
366 | if (!(para->replacements = psiconv_list_new(sizeof( |
|
|
367 | struct psiconv_replacement_s)))) |
|
|
368 | goto ERROR2_1; |
|
|
369 | if (!(para->base_character = psiconv_basic_character_layout())) |
|
|
370 | goto ERROR2_2; |
|
|
371 | if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) |
|
|
372 | goto ERROR2_3; |
|
|
373 | para->base_style = 0; |
|
|
374 | } |
|
|
375 | |
|
|
376 | |
|
|
377 | len += text_len; |
355 | len += text_len; |
378 | |
356 | |
379 | if (length) |
357 | if (length) |
380 | *length = len; |
358 | *length = len; |
381 | |
359 | |
382 | psiconv_progress(config,lev+1,off+len-1,"End of text section (total length: %08x", |
360 | psiconv_progress(config,lev+1,off+len-1, |
383 | len); |
361 | "End of text section (total length: %08x", len); |
384 | |
362 | |
385 | return res; |
363 | return res; |
386 | |
364 | |
387 | ERROR2_3: |
365 | ERROR9: |
|
|
366 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
367 | ERROR8: |
388 | psiconv_free_character_layout(para->base_character); |
368 | psiconv_free_character_layout(para->base_character); |
389 | ERROR2_2: |
369 | ERROR7: |
390 | psiconv_list_free(para->replacements); |
370 | psiconv_list_free(para->replacements); |
391 | ERROR2_1: |
371 | ERROR6: |
392 | psiconv_list_free(para->in_lines); |
372 | psiconv_list_free(para->in_lines); |
393 | ERROR2_0: |
373 | ERROR5: |
394 | for (j = 0; j < i; j++) { |
374 | free(para->text); |
395 | if (!(para = psiconv_list_get(*result,j))) { |
|
|
396 | psiconv_error(config,lev+1,off,"Massive memory corruption..."); |
|
|
397 | break; |
|
|
398 | } |
|
|
399 | psiconv_list_free(para->in_lines); |
|
|
400 | psiconv_list_free(para->replacements); |
|
|
401 | psiconv_free_character_layout(para->base_character); |
|
|
402 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
403 | } |
|
|
404 | goto ERROR2; |
|
|
405 | |
|
|
406 | ERROR4: |
375 | ERROR4: |
407 | free(para->text); |
376 | psiconv_list_free(line); |
408 | ERROR3: |
377 | ERROR3: |
409 | free(para); |
378 | free(para); |
410 | ERROR2: |
379 | ERROR2: |
411 | for (i = 0; i < psiconv_list_length(*result);i++) { |
380 | psiconv_free_text_and_layout(*result); |
412 | if (!(para = psiconv_list_get(*result,i))) { |
|
|
413 | psiconv_error(config,lev+1,off,"Massive memory corruption..."); |
|
|
414 | break; |
|
|
415 | } |
|
|
416 | free(para->text); |
|
|
417 | } |
|
|
418 | psiconv_list_free(*result); |
|
|
419 | ERROR1: |
381 | ERROR1: |
420 | psiconv_error(config,lev+1,off,"Reading of Text Section failed"); |
382 | psiconv_error(config,lev+1,off,"Reading of Text Section failed"); |
421 | if (length) |
383 | if (length) |
422 | *length = 0; |
384 | *length = 0; |
423 | if (!res) |
385 | if (!res) |
… | |
… | |
607 | goto ERROR4; |
569 | goto ERROR4; |
608 | if (temp != 0x00) { |
570 | if (temp != 0x00) { |
609 | psiconv_debug(config,lev+4,off+len,"Type: %02x",temp); |
571 | psiconv_debug(config,lev+4,off+len,"Type: %02x",temp); |
610 | for (j = 0; j < psiconv_list_length(anon_styles); j++) { |
572 | for (j = 0; j < psiconv_list_length(anon_styles); j++) { |
611 | if (!(anon_ptr = psiconv_list_get(anon_styles,j))) { |
573 | if (!(anon_ptr = psiconv_list_get(anon_styles,j))) { |
612 | psiconv_error(config,lev+4,off+len,"Massive memory curruption"); |
574 | psiconv_error(config,lev+4,off+len,"Data structure corruption"); |
613 | goto ERROR4; |
575 | goto ERROR4; |
614 | } |
576 | } |
615 | if (temp == anon_ptr->nr) |
577 | if (temp == anon_ptr->nr) |
616 | break; |
578 | break; |
617 | } |
579 | } |
… | |
… | |
713 | psiconv_progress(config,lev+3,off+len, |
675 | psiconv_progress(config,lev+3,off+len, |
714 | "Going to read the text layout inline elements"); |
676 | "Going to read the text layout inline elements"); |
715 | total = 0; |
677 | total = 0; |
716 | for (i = 0; i < psiconv_list_length(result); i++) { |
678 | for (i = 0; i < psiconv_list_length(result); i++) { |
717 | if (!(para = psiconv_list_get(result,i))) { |
679 | if (!(para = psiconv_list_get(result,i))) { |
718 | psiconv_error(config,lev+3,off+len,"Massive memory corruption"); |
680 | psiconv_error(config,lev+3,off+len,"Data structure corruption"); |
719 | goto ERROR4; |
681 | goto ERROR4; |
720 | } |
682 | } |
721 | line_length = -1; |
683 | line_length = -1; |
722 | for (j = 0; j < inline_count[i]; j++) { |
684 | for (j = 0; j < inline_count[i]; j++) { |
723 | psiconv_progress(config,lev+3,off+len,"Element %d: Paragraph %d, element %d", |
685 | psiconv_progress(config,lev+3,off+len,"Element %d: Paragraph %d, element %d", |
… | |
… | |
818 | |
780 | |
819 | free(inline_count); |
781 | free(inline_count); |
820 | |
782 | |
821 | for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { |
783 | for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { |
822 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
784 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
823 | psiconv_error(config,lev+4,off+len,"Massive memory corruption"); |
785 | psiconv_error(config,lev+4,off+len,"Data structure corruption"); |
824 | goto ERROR2; |
786 | goto ERROR2; |
825 | } |
787 | } |
826 | psiconv_free_character_layout(anon_ptr->character); |
788 | psiconv_free_character_layout(anon_ptr->character); |
827 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
789 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
828 | } |
790 | } |
… | |
… | |
860 | ERROR4: |
822 | ERROR4: |
861 | free(inline_count); |
823 | free(inline_count); |
862 | ERROR3: |
824 | ERROR3: |
863 | for (i = 0; i < psiconv_list_length(anon_styles); i++) { |
825 | for (i = 0; i < psiconv_list_length(anon_styles); i++) { |
864 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
826 | if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { |
865 | psiconv_error(config,lev+1,off,"Massive memory corruption"); |
827 | psiconv_error(config,lev+1,off,"Data structure corruption"); |
866 | break; |
828 | break; |
867 | } |
829 | } |
868 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
830 | psiconv_free_paragraph_layout(anon_ptr->paragraph); |
869 | psiconv_free_character_layout(anon_ptr->character); |
831 | psiconv_free_character_layout(anon_ptr->character); |
870 | } |
832 | } |
… | |
… | |
911 | goto ERROR3; |
873 | goto ERROR3; |
912 | if (!(styles_section->normal->paragraph = |
874 | if (!(styles_section->normal->paragraph = |
913 | psiconv_clone_paragraph_layout(base_para))) |
875 | psiconv_clone_paragraph_layout(base_para))) |
914 | goto ERROR4; |
876 | goto ERROR4; |
915 | styles_section->normal->hotkey = 0; |
877 | styles_section->normal->hotkey = 0; |
916 | if (( res = psiconv_unicode_from_chars(config,(psiconv_u8 *) "", |
878 | |
917 | &styles_section->normal->name))) |
879 | if (!(styles_section->normal->name = psiconv_unicode_empty_string())) |
918 | goto ERROR5; |
880 | goto ERROR5; |
919 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
881 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
920 | struct psiconv_word_style_s)))) |
882 | struct psiconv_word_style_s)))) |
921 | goto ERROR6; |
883 | goto ERROR6; |
922 | |
884 | |
… | |
… | |
1063 | psiconv_warn(config,lev+3,0,"Can't parse embedded object (still continuing)"); |
1025 | psiconv_warn(config,lev+3,0,"Can't parse embedded object (still continuing)"); |
1064 | (*result)->object->file = NULL; |
1026 | (*result)->object->file = NULL; |
1065 | } |
1027 | } |
1066 | } |
1028 | } |
1067 | |
1029 | |
|
|
1030 | psiconv_buffer_free(subbuf); |
|
|
1031 | psiconv_free_section_table_section(table); |
|
|
1032 | |
1068 | if (length) |
1033 | if (length) |
1069 | *length = len; |
1034 | *length = len; |
1070 | |
1035 | |
1071 | psiconv_progress(config,lev+1,off+len-1,"End of Embedded Object Section " |
1036 | psiconv_progress(config,lev+1,off+len-1,"End of Embedded Object Section " |
1072 | "(total length: %08x)",len); |
1037 | "(total length: %08x)",len); |