--- psiconv/trunk/lib/psiconv/parse_common.c 2003/11/25 17:57:05 168 +++ psiconv/trunk/lib/psiconv/parse_common.c 2005/11/15 14:52:48 268 @@ -1,6 +1,6 @@ /* parse_common.c - Part of psiconv, a PSION 5 file formats converter - Copyright (c) 1999, 2000 Frodo Looijaard + Copyright (c) 1999-2004 Frodo Looijaard This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -71,7 +71,7 @@ goto DONE; } if ((*result)->uid1 != PSICONV_ID_PSION5) { - psiconv_warn(config,lev+2,off+len, + psiconv_error(config,lev+2,off+len, "UID1 has unknown value. This is probably " "not a (parsable) Psion 5 file"); res = -PSICONV_E_PARSE; @@ -125,7 +125,7 @@ (*result)->uid3)) psiconv_debug(config,lev+2,off+len,"Checksum %08x is correct",temp); else { - psiconv_warn(config,lev+2,off+len,"Checksum failed, file corrupted!"); + psiconv_error(config,lev+2,off+len,"Checksum failed, file corrupted!"); psiconv_debug(config,lev+2,off+len,"Expected checksum %08x, found %08x", psiconv_checkuid((*result)->uid1,(*result)->uid2, (*result)->uid3),temp); @@ -146,7 +146,7 @@ ERROR2: free(*result); ERROR1: - psiconv_warn(config,lev+1,off,"Reading of Header Section failed"); + psiconv_error(config,lev+1,off,"Reading of Header Section failed"); if (length) *length = 0; if (res == 0) @@ -213,7 +213,7 @@ ERROR2: psiconv_list_free(*result); ERROR1: - psiconv_warn(config,lev+1,off,"Reading of Section Table Section failed"); + psiconv_error(config,lev+1,off,"Reading of Section Table Section failed"); if (length) *length = 0; if (res == 0) @@ -258,7 +258,7 @@ ERROR2: free(*result); ERROR1: - psiconv_warn(config,lev+1,off,"Reading of Application ID Section failed"); + psiconv_error(config,lev+1,off,"Reading of Application ID Section failed"); if (length) *length = 0; if (res == 0) @@ -277,146 +277,108 @@ psiconv_u32 text_len; psiconv_paragraph para; + psiconv_ucs2 temp; + psiconv_list line; int nr; - int i,j,start,leng,temp; + int i,leng; char *str_copy; psiconv_progress(config,lev+1,off,"Going to parse the text section"); - psiconv_progress(config,lev+2,off,"Reading the text length"); if(!(*result = psiconv_list_new(sizeof(*para)))) goto ERROR1; if (!(para = malloc(sizeof(*para)))) goto ERROR2; + psiconv_progress(config,lev+2,off,"Reading the text length"); text_len = psiconv_read_X(config,buf,lev+2,off,&leng,&res); if (res) goto ERROR3; psiconv_debug(config,lev+2,off,"Length: %08x",text_len); len += leng; - psiconv_progress(config,lev+2,off+len,"Going to read all paragraph text"); + if (!(line = psiconv_list_new(sizeof(psiconv_ucs2)))) + goto ERROR3; + + i = 0; nr = 0; - start = 0; - for (i = 0; i < text_len; i++) { - temp = psiconv_read_u8(config,buf,lev+2,off+len+i,&res); + while (i < text_len) { + temp = psiconv_unicode_read_char(config,buf,lev+2,off+len+i,&leng,&res); if (res) - goto ERROR3; - if (temp == 0x06) { - if (!(para->text = malloc(i - start + 1))) - goto ERROR3; - for (j = 0; j < i - start; j++) { - temp = psiconv_read_u8(config,buf,lev+1,off + len + start + j,&res); - if (res) - goto ERROR4; - para->text[j] = temp; - } - para->text[j] = 0; - - if ((res = psiconv_list_add(*result,para))) - goto ERROR4; + goto ERROR4; + if (i + leng > text_len) { + psiconv_error(config,lev+2,off+len+i,"Malformed text section"); + res = PSICONV_E_PARSE; + goto ERROR4; + } + if ((temp == 0x06) || (i + leng == text_len)) { + if (!(para->text = psiconv_unicode_from_list(line))) + goto ERROR4; - if (!(str_copy = psiconv_make_printable(para->text))) - goto ERROR3; + if (!(str_copy = psiconv_make_printable(config,para->text))) + goto ERROR5; psiconv_debug(config,lev+2,off+i+len,"Line %d: %d characters",nr, strlen(str_copy) +1); psiconv_debug(config,lev+2,off+i+len,"Line %d: `%s'",nr,str_copy); free(str_copy); + i += leng; - start = i + 1; + if (!(para->in_lines = psiconv_list_new(sizeof( + struct psiconv_in_line_layout_s)))) + goto ERROR5; + if (!(para->replacements = psiconv_list_new(sizeof( + struct psiconv_replacement_s)))) + goto ERROR6; + if (!(para->base_character = psiconv_basic_character_layout())) + goto ERROR7; + if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) + goto ERROR8; + para->base_style = 0; + + if ((res = psiconv_list_add(*result,para))) + goto ERROR9; + psiconv_progress(config,lev+2,off+len+i,"Starting a new line"); + psiconv_list_empty(line); nr ++; + } else { + if ((res = psiconv_list_add(line,&temp))) + goto ERROR4; + i += leng; } } - - if (start != text_len) { - psiconv_warn(config,lev+2,off+start+len, - "Last line does not end on EOL (%d characters left)", len - start); - if (!(para->text = malloc(text_len - start + 1))) - goto ERROR3; - for (j = 0; j < text_len - start; j++) { - temp = psiconv_read_u8(config,buf,lev+2,off + start + j + len, &res); - if (res) - goto ERROR4; - para->text[j] = temp; - } - para->text[text_len - start] = 0; - if ((res = psiconv_list_add(*result,para))) - goto ERROR4; - if (!(str_copy = psiconv_make_printable(para->text))) - goto ERROR3; - psiconv_debug(config,lev+2,off+start+len,"Last line: %d characters",nr, - strlen(str_copy)+1); - psiconv_debug(config,lev+2,off+start+len,"Last line: `%s'",str_copy); - free(str_copy); - } - - free(para); - /* Initialize the remaining parts of each paragraph */ - for (i = 0; i < psiconv_list_length(*result); i ++) { - if (!(para = psiconv_list_get(*result,i))) { - psiconv_warn(config,lev+2,off+len,"Massive memory corruption"); - goto ERROR2_0; - } - if (!(para->in_lines = psiconv_list_new(sizeof( - struct psiconv_in_line_layout_s)))) - goto ERROR2_0; - if (!(para->replacements = psiconv_list_new(sizeof( - struct psiconv_replacement_s)))) - goto ERROR2_1; - if (!(para->base_character = psiconv_basic_character_layout())) - goto ERROR2_2; - if (!(para->base_paragraph = psiconv_basic_paragraph_layout())) - goto ERROR2_3; - para->base_style = 0; - } - + psiconv_list_free(line); + free(para); len += text_len; if (length) *length = len; - psiconv_progress(config,lev+1,off+len-1,"End of text section (total length: %08x", - len); + psiconv_progress(config,lev+1,off+len-1, + "End of text section (total length: %08x", len); return res; -ERROR2_3: +ERROR9: + psiconv_free_paragraph_layout(para->base_paragraph); +ERROR8: psiconv_free_character_layout(para->base_character); -ERROR2_2: +ERROR7: psiconv_list_free(para->replacements); -ERROR2_1: +ERROR6: psiconv_list_free(para->in_lines); -ERROR2_0: - for (j = 0; j < i; j++) { - if (!(para = psiconv_list_get(*result,j))) { - psiconv_warn(config,lev+1,off,"Massive memory corruption..."); - break; - } - psiconv_list_free(para->in_lines); - psiconv_list_free(para->replacements); - psiconv_free_character_layout(para->base_character); - psiconv_free_paragraph_layout(para->base_paragraph); - } - goto ERROR2; - -ERROR4: +ERROR5: free(para->text); +ERROR4: + psiconv_list_free(line); ERROR3: free(para); ERROR2: - for (i = 0; i < psiconv_list_length(*result);i++) { - if (!(para = psiconv_list_get(*result,i))) { - psiconv_warn(config,lev+1,off,"Massive memory corruption..."); - break; - } - free(para->text); - } - psiconv_list_free(*result); + psiconv_free_text_and_layout(*result); ERROR1: - psiconv_warn(config,lev+1,off,"Reading of Text Section failed"); + psiconv_error(config,lev+1,off,"Reading of Text Section failed"); if (length) *length = 0; if (!res) @@ -590,12 +552,12 @@ temp = psiconv_read_u32(config,buf,lev+4,off+len,&res); if (res) goto ERROR4; - if (temp != strlen(para->text)+1) { + if (temp != psiconv_unicode_strlen(para->text)+1) { psiconv_warn(config,lev+4,off+len, "Disagreement of the length of paragraph in layout section"); psiconv_debug(config,lev+4,off+len, "Paragraph length: layout section says %d, counted %d", - temp,strlen(para->text)+1); + temp,psiconv_unicode_strlen(para->text)+1); } else psiconv_debug(config,lev+4,off+len,"Paragraph length: %d",temp); len += 4; @@ -608,7 +570,7 @@ psiconv_debug(config,lev+4,off+len,"Type: %02x",temp); for (j = 0; j < psiconv_list_length(anon_styles); j++) { if (!(anon_ptr = psiconv_list_get(anon_styles,j))) { - psiconv_warn(config,lev+4,off+len,"Massive memory curruption"); + psiconv_error(config,lev+4,off+len,"Data structure corruption"); goto ERROR4; } if (temp == anon_ptr->nr) @@ -619,7 +581,7 @@ psiconv_debug(config,lev+4,off+len,"Unknown type - using base styles instead"); para->base_style = 0; if (!(temp_style = psiconv_get_style(styles,0))) { - psiconv_warn(config,lev+4,off,"Base style unknown"); + psiconv_error(config,lev+4,off,"Base style unknown"); goto ERROR4; } if (!(temp_para = psiconv_clone_paragraph_layout @@ -667,7 +629,7 @@ if (!(temp_style = psiconv_get_style (styles,temp))) { psiconv_warn(config,lev+4,off,"Unknown Style referenced"); if (!(temp_style = psiconv_get_style(styles,0))) { - psiconv_warn(config,lev+4,off,"Base style unknown"); + psiconv_error(config,lev+4,off,"Base style unknown"); goto ERROR4; } } @@ -714,7 +676,7 @@ total = 0; for (i = 0; i < psiconv_list_length(result); i++) { if (!(para = psiconv_list_get(result,i))) { - psiconv_warn(config,lev+3,off+len,"Massive memory corruption"); + psiconv_error(config,lev+3,off+len,"Data structure corruption"); goto ERROR4; } line_length = -1; @@ -797,11 +759,11 @@ } else if (temp != 0x00) { psiconv_warn(config,lev+4,off+len,"Layout section unknown inline type"); } - if (line_length + in_line.length > strlen(para->text)) { + if (line_length + in_line.length > psiconv_unicode_strlen(para->text)) { psiconv_warn(config,lev+4,off+len, "Layout section inlines: line length mismatch"); res = -1; - in_line.length = strlen(para->text) - line_length; + in_line.length = psiconv_unicode_strlen(para->text) - line_length; } line_length += in_line.length; if ((res = psiconv_list_add(para->in_lines,&in_line))) @@ -819,7 +781,7 @@ for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) { if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { - psiconv_warn(config,lev+4,off+len,"Massive memory corruption"); + psiconv_error(config,lev+4,off+len,"Data structure corruption"); goto ERROR2; } psiconv_free_character_layout(anon_ptr->character); @@ -861,7 +823,7 @@ ERROR3: for (i = 0; i < psiconv_list_length(anon_styles); i++) { if (!(anon_ptr = psiconv_list_get(anon_styles,i))) { - psiconv_warn(config,lev+1,off,"Massive memory corruption"); + psiconv_error(config,lev+1,off,"Data structure corruption"); break; } psiconv_free_paragraph_layout(anon_ptr->paragraph); @@ -871,7 +833,7 @@ ERROR2: psiconv_list_free(anon_styles); ERROR1: - psiconv_warn(config,lev+1,off,"Reading of Layout Section failed"); + psiconv_error(config,lev+1,off,"Reading of Layout Section failed"); if (length) *length = 0; if (!res) @@ -912,7 +874,8 @@ psiconv_clone_paragraph_layout(base_para))) goto ERROR4; styles_section->normal->hotkey = 0; - if (!(styles_section->normal->name = strdup(""))) + + if (!(styles_section->normal->name = psiconv_unicode_empty_string())) goto ERROR5; if (!(styles_section->styles = psiconv_list_new(sizeof( struct psiconv_word_style_s)))) @@ -935,7 +898,7 @@ ERROR2: free(styles_section); ERROR1: - psiconv_warn(config,lev+1,off,"Reading of Styleless Layout Section failed"); + psiconv_error(config,lev+1,off,"Reading of Styleless Layout Section failed"); if (length) *length = 0; if (!res) @@ -1063,6 +1026,9 @@ } } + psiconv_buffer_free(subbuf); + psiconv_free_section_table_section(table); + if (length) *length = len; @@ -1083,7 +1049,7 @@ ERROR2: psiconv_free_section_table_section(table); ERROR1: - psiconv_warn(config,lev+1,off,"Reading Embedded Object failed"); + psiconv_error(config,lev+1,off,"Reading Embedded Object failed"); if (length) *length = 0; @@ -1132,7 +1098,7 @@ } } if (i == psiconv_list_length(table)) { - psiconv_warn(config,lev+2,off,"No Application ID Section found"); + psiconv_error(config,lev+2,off,"No Application ID Section found"); res = PSICONV_E_PARSE; goto ERROR2; } @@ -1234,7 +1200,7 @@ ERROR2: free(*result); ERROR1: - psiconv_warn(config,lev+1,off+len,"Reading of Object Display Section failed"); + psiconv_error(config,lev+1,off+len,"Reading of Object Display Section failed"); if (length) *length=0; if (!res) @@ -1260,7 +1226,6 @@ (*result)->icon_name = psiconv_read_string(config,buf,lev+2,off+len,&leng,&res); if (res) goto ERROR2; - psiconv_debug(config,lev+2,off+len,"Icon name: %s",(*result)->icon_name); len += leng; psiconv_progress(config,lev+2,off+len,"Going to read the icon width"); @@ -1289,7 +1254,7 @@ ERROR2: free(*result); ERROR1: - psiconv_warn(config,lev+1,off+len,"Reading of Object Icon Section failed"); + psiconv_error(config,lev+1,off+len,"Reading of Object Icon Section failed"); if (length) *length=0; if (!res)