… | |
… | |
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; |
310 | goto ERROR4; |
307 | if (temp == 0x06) { |
311 | if (i + leng > text_len) { |
308 | if (!(para->text = malloc((sizeof((*(para->text))) * (i - start + 1))))) |
312 | psiconv_error(config,lev+2,off+len+i,"Malformed text section"); |
309 | goto ERROR3; |
313 | res = PSICONV_E_PARSE; |
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 | goto ERROR4; |
314 | para->text[j] = psiconv_unicode_from_char(config,temp); |
315 | } |
|
|
316 | if ((temp == 0x06) || (i + leng == text_len)) { |
|
|
317 | if (!(para->text = malloc(sizeof(*(para->text)) * |
|
|
318 | (psiconv_list_length(line) + 1)))) |
|
|
319 | goto ERROR4; |
|
|
320 | for (j = 0; j < psiconv_list_length(line); j++) { |
|
|
321 | if (!(nextcharptr = psiconv_list_get(line,j))) { |
|
|
322 | psiconv_error(config,lev+2,off+i+len,"Internal data corruption"); |
|
|
323 | goto ERROR5; |
|
|
324 | } |
|
|
325 | para->text[j] = *nextcharptr; |
315 | } |
326 | } |
316 | para->text[j] = 0; |
327 | para->text[j] = 0; |
317 | |
|
|
318 | if ((res = psiconv_list_add(*result,para))) |
|
|
319 | goto ERROR4; |
|
|
320 | |
328 | |
321 | if (!(str_copy = psiconv_make_printable(config,para->text))) |
329 | if (!(str_copy = psiconv_make_printable(config,para->text))) |
322 | goto ERROR3; |
330 | goto ERROR5; |
323 | psiconv_debug(config,lev+2,off+i+len,"Line %d: %d characters",nr, |
331 | psiconv_debug(config,lev+2,off+i+len,"Line %d: %d characters",nr, |
324 | strlen(str_copy) +1); |
332 | strlen(str_copy) +1); |
325 | psiconv_debug(config,lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
333 | psiconv_debug(config,lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); |
326 | free(str_copy); |
334 | free(str_copy); |
|
|
335 | i += leng; |
327 | |
336 | |
328 | start = i + 1; |
337 | if (!(para->in_lines = psiconv_list_new(sizeof( |
|
|
338 | struct psiconv_in_line_layout_s)))) |
|
|
339 | goto ERROR5; |
|
|
340 | if (!(para->replacements = psiconv_list_new(sizeof( |
|
|
341 | struct psiconv_replacement_s)))) |
|
|
342 | goto ERROR6; |
|
|
343 | if (!(para->base_character = psiconv_basic_character_layout())) |
|
|
344 | goto ERROR7; |
|
|
345 | if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) |
|
|
346 | goto ERROR8; |
|
|
347 | para->base_style = 0; |
|
|
348 | |
|
|
349 | if ((res = psiconv_list_add(*result,para))) |
|
|
350 | goto ERROR9; |
|
|
351 | psiconv_progress(config,lev+2,off+len+i,"Starting a new line"); |
|
|
352 | psiconv_list_empty(line); |
329 | nr ++; |
353 | nr ++; |
|
|
354 | } else { |
|
|
355 | if ((res = psiconv_list_add(line,&temp))) |
|
|
356 | goto ERROR4; |
|
|
357 | i += leng; |
330 | } |
358 | } |
331 | } |
359 | } |
332 | |
360 | |
333 | if (start != text_len) { |
361 | 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); |
362 | free(para); |
356 | |
363 | |
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; |
364 | len += text_len; |
378 | |
365 | |
379 | if (length) |
366 | if (length) |
380 | *length = len; |
367 | *length = len; |
381 | |
368 | |
382 | psiconv_progress(config,lev+1,off+len-1,"End of text section (total length: %08x", |
369 | psiconv_progress(config,lev+1,off+len-1, |
383 | len); |
370 | "End of text section (total length: %08x", len); |
384 | |
371 | |
385 | return res; |
372 | return res; |
386 | |
373 | |
387 | ERROR2_3: |
374 | ERROR9: |
|
|
375 | psiconv_free_paragraph_layout(para->base_paragraph); |
|
|
376 | ERROR8: |
388 | psiconv_free_character_layout(para->base_character); |
377 | psiconv_free_character_layout(para->base_character); |
389 | ERROR2_2: |
378 | ERROR7: |
390 | psiconv_list_free(para->replacements); |
379 | psiconv_list_free(para->replacements); |
391 | ERROR2_1: |
380 | ERROR6: |
392 | psiconv_list_free(para->in_lines); |
381 | psiconv_list_free(para->in_lines); |
393 | ERROR2_0: |
382 | ERROR5: |
394 | for (j = 0; j < i; j++) { |
383 | 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: |
384 | ERROR4: |
407 | free(para->text); |
385 | psiconv_list_free(line); |
408 | ERROR3: |
386 | ERROR3: |
409 | free(para); |
387 | free(para); |
410 | ERROR2: |
388 | ERROR2: |
411 | for (i = 0; i < psiconv_list_length(*result);i++) { |
389 | 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: |
390 | ERROR1: |
420 | psiconv_error(config,lev+1,off,"Reading of Text Section failed"); |
391 | psiconv_error(config,lev+1,off,"Reading of Text Section failed"); |
421 | if (length) |
392 | if (length) |
422 | *length = 0; |
393 | *length = 0; |
423 | if (!res) |
394 | if (!res) |
… | |
… | |
911 | goto ERROR3; |
882 | goto ERROR3; |
912 | if (!(styles_section->normal->paragraph = |
883 | if (!(styles_section->normal->paragraph = |
913 | psiconv_clone_paragraph_layout(base_para))) |
884 | psiconv_clone_paragraph_layout(base_para))) |
914 | goto ERROR4; |
885 | goto ERROR4; |
915 | styles_section->normal->hotkey = 0; |
886 | styles_section->normal->hotkey = 0; |
916 | if (( res = psiconv_unicode_from_chars(config,(psiconv_u8 *) "", |
887 | |
917 | &styles_section->normal->name))) |
888 | if (!(styles_section->normal->name = psiconv_unicode_empty_string())) |
918 | goto ERROR5; |
889 | goto ERROR5; |
919 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
890 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
920 | struct psiconv_word_style_s)))) |
891 | struct psiconv_word_style_s)))) |
921 | goto ERROR6; |
892 | goto ERROR6; |
922 | |
893 | |
… | |
… | |
1063 | psiconv_warn(config,lev+3,0,"Can't parse embedded object (still continuing)"); |
1034 | psiconv_warn(config,lev+3,0,"Can't parse embedded object (still continuing)"); |
1064 | (*result)->object->file = NULL; |
1035 | (*result)->object->file = NULL; |
1065 | } |
1036 | } |
1066 | } |
1037 | } |
1067 | |
1038 | |
|
|
1039 | psiconv_buffer_free(subbuf); |
|
|
1040 | psiconv_free_section_table_section(table); |
|
|
1041 | |
1068 | if (length) |
1042 | if (length) |
1069 | *length = len; |
1043 | *length = len; |
1070 | |
1044 | |
1071 | psiconv_progress(config,lev+1,off+len-1,"End of Embedded Object Section " |
1045 | psiconv_progress(config,lev+1,off+len-1,"End of Embedded Object Section " |
1072 | "(total length: %08x)",len); |
1046 | "(total length: %08x)",len); |