--- psiconv/trunk/lib/psiconv/generate_common.c 2004/02/09 23:12:58 208 +++ psiconv/trunk/lib/psiconv/generate_common.c 2004/02/26 16:27:43 233 @@ -43,12 +43,20 @@ int res; psiconv_progress(config,lev,0,"Writing header section"); if ((res = psiconv_write_u32(config,buf,lev+1,uid1))) - return res; + goto ERROR; if ((res = psiconv_write_u32(config,buf,lev+1,uid2))) - return res; + goto ERROR; if ((res = psiconv_write_u32(config,buf,lev+1,uid3))) - return res; - return psiconv_write_u32(config,buf,lev+1,psiconv_checkuid(uid1,uid2,uid3)); + goto ERROR; + if ((res = psiconv_write_u32(config,buf,lev+1, + psiconv_checkuid(uid1,uid2,uid3)))) + goto ERROR; + psiconv_progress(config,lev,0,"End of header section"); + return 0; + +ERROR: + psiconv_error(config,lev,0,"Writing of header section failed"); + return res; } int psiconv_write_section_table_section(const psiconv_config config, @@ -60,23 +68,30 @@ psiconv_progress(config,lev,0,"Writing section table section"); if (!value) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Null section table section"); - return -PSICONV_E_GENERATE; + psiconv_error(config,lev,0,"Null section table section"); + res = -PSICONV_E_GENERATE; + goto ERROR; } if ((res = psiconv_write_u8(config,buf,lev+1,2 * psiconv_list_length(value)))) - return res; + goto ERROR; for (i = 0; i < psiconv_list_length(value); i++) { if (!(entry = psiconv_list_get(value,i))) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Massive memory corruption"); - return -PSICONV_E_NOMEM; + psiconv_error(config,lev+1,0,"Data structure corruption"); + res = -PSICONV_E_NOMEM; + goto ERROR; } if ((res = psiconv_write_u32(config,buf,lev+1,entry->id))) - return res; + goto ERROR; if ((res = psiconv_write_offset(config,buf,lev+1,entry->offset))) - return res; + goto ERROR; } + psiconv_progress(config,lev,0,"End of section table section"); return -PSICONV_E_OK; + +ERROR: + psiconv_error(config,lev,0,"Writing of section table section failed"); + return res; } int psiconv_write_application_id_section(const psiconv_config config, @@ -86,8 +101,14 @@ int res; psiconv_progress(config,lev,0,"Writing application id section"); if ((res = psiconv_write_u32(config,buf,lev+1,id))) - return res; - return psiconv_write_string(config,buf,lev+1,text); + goto ERROR; + if ((res = psiconv_write_string(config,buf,lev+1,text))) + goto ERROR; + psiconv_progress(config,lev,0,"End of application id section"); + return 0; +ERROR: + psiconv_error(config,lev,0,"Writing of application id section failed"); + return res; } int psiconv_write_text_section(const psiconv_config config, @@ -95,40 +116,50 @@ const psiconv_text_and_layout value) { int res; - psiconv_buffer extra_buf; + psiconv_buffer extra_buf = NULL; int i,j; psiconv_paragraph paragraph; psiconv_progress(config,lev,0,"Writing text section"); if (!value) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Null text section"); - return -PSICONV_E_GENERATE; + psiconv_error(config,lev+1,0,"Null text section"); + res = -PSICONV_E_GENERATE; + goto ERROR; } if (psiconv_list_length(value)) { - if (!(extra_buf = psiconv_buffer_new())) - return -PSICONV_E_NOMEM; + if (!(extra_buf = psiconv_buffer_new())) { + psiconv_error(config,lev+1,0,"Out of memory error"); + res = -PSICONV_E_NOMEM; + goto ERROR; + } for (i = 0; i < psiconv_list_length(value); i++) { if (!(paragraph = psiconv_list_get(value,i))) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Massive memory corruption"); + psiconv_error(config,lev+1,0,"Data structure corruption"); res = -PSICONV_E_NOMEM; goto ERROR; } for (j = 0; j < psiconv_unicode_strlen(paragraph->text); j++) - if ((res = psiconv_write_u8(config,extra_buf,lev+1, - psiconv_unicode_to_char(config,paragraph->text[j])))) + if ((res = psiconv_unicode_write_char(config,extra_buf,lev+1, + paragraph->text[j]))) goto ERROR; - psiconv_write_u8(config,extra_buf,lev+1,0x06); + psiconv_unicode_write_char(config,extra_buf,lev+1,0x06); } if ((res = psiconv_write_X(config,buf,lev+1,psiconv_buffer_length(extra_buf)))) goto ERROR; res = psiconv_buffer_concat(buf,extra_buf); } else /* Hack: empty text sections are just not allowed */ - return psiconv_write_u16(config,buf,lev+1,0x0602); + if ((res = psiconv_write_u16(config,buf,lev+1,0x0602))) + goto ERROR; + + psiconv_progress(config,lev,0,"End of text section"); + return 0; ERROR: - psiconv_buffer_free(extra_buf); + if (extra_buf) + psiconv_buffer_free(extra_buf); + psiconv_error(config,lev,0,"Writing of text section failed"); return res; } @@ -158,38 +189,44 @@ psiconv_progress(config,lev,0,"Writing layout section"); if (!value) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Null text section"); - return -PSICONV_E_GENERATE; + psiconv_error(config,lev,0,"Null text section"); + res = -PSICONV_E_GENERATE; + goto ERROR1; } if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) { + psiconv_error(config,lev+1,0,"Out of memory error"); res = -PSICONV_E_NOMEM; goto ERROR1; } if (!(buf_types = psiconv_buffer_new())) { + psiconv_error(config,lev+1,0,"Out of memory error"); res = -PSICONV_E_NOMEM; goto ERROR2; } if (!(buf_elements = psiconv_buffer_new())) { + psiconv_error(config,lev+1,0,"Out of memory error"); res = -PSICONV_E_NOMEM; goto ERROR3; } if (!(buf_inlines = psiconv_buffer_new())) { + psiconv_error(config,lev+1,0,"Out of memory error"); res = -PSICONV_E_NOMEM; goto ERROR4; } if (!(buf_objects = psiconv_buffer_new())) { + psiconv_error(config,lev+1,0,"Out of memory error"); res = -PSICONV_E_NOMEM; goto ERROR5; } for (i = 0; i < psiconv_list_length(value); i++) { if (!(paragraph = psiconv_list_get(value,i))) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Massive memory corruption"); + psiconv_error(config,lev+1,0,"Data structure corruption"); res = -PSICONV_E_NOMEM; goto ERROR6; } @@ -199,8 +236,11 @@ /* We need it for the next if-statement */ if (psiconv_list_length(paragraph->in_lines) == 1) - if (!(in_line = psiconv_list_get(paragraph->in_lines,1))) + if (!(in_line = psiconv_list_get(paragraph->in_lines,1))) { + psiconv_error(config,lev+1,0,"Data structure corruption"); + res = -PSICONV_E_NOMEM; goto ERROR6; + } if ((psiconv_list_length(paragraph->in_lines) > 1) || ((psiconv_list_length(paragraph->in_lines) == 1) && @@ -210,7 +250,7 @@ if ((res = psiconv_write_u8(config,buf_elements,lev+1,0x00))) goto ERROR6; if (!(style = psiconv_get_style(styles,paragraph->base_style))) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Unknown style"); + psiconv_error(config,lev+1,0,"Unknown style"); res = -PSICONV_E_GENERATE; goto ERROR6; } @@ -230,8 +270,7 @@ for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { nr_of_inlines ++; if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { - psiconv_error(config,0,psiconv_buffer_length(buf), - "Massive memory corruption"); + psiconv_error(config,lev,0,"Data structure corruption"); res = -PSICONV_E_NOMEM; goto ERROR6; } @@ -243,6 +282,7 @@ complete length of all inlines equals the text length */ if (j == psiconv_list_length(paragraph->in_lines)-1) { if (paralen > psiconv_unicode_strlen(paragraph->text)+1) { + psiconv_error(config,lev+1,0,"Inline formatting data length and line length are inconsistent"); res = -PSICONV_E_GENERATE; goto ERROR6; } @@ -258,10 +298,14 @@ if ((res = psiconv_write_u32(config,buf_inlines,lev+1,PSICONV_ID_OBJECT))) goto ERROR6; obj_id = psiconv_buffer_unique_id(); - if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id))) + if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; - if ((res = psiconv_buffer_add_target(buf_objects,obj_id))) + } + if ((res = psiconv_buffer_add_target(buf_objects,obj_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; + } if ((res = psiconv_write_embedded_object_section(config,buf_objects,lev+1, in_line->object))) goto ERROR6; @@ -279,8 +323,7 @@ para_charlayout = paragraph->base_character; else { if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { - psiconv_error(config,0,psiconv_buffer_length(buf), - "Massive memory corruption"); + psiconv_error(config,lev,0,"Data structure corruption"); res = -PSICONV_E_NOMEM; goto ERROR6; } @@ -288,8 +331,7 @@ } for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { - psiconv_error(config,0,psiconv_buffer_length(buf), - "Massive memory corruption"); + psiconv_error(config,lev,0,"Data structure corruption"); res = -PSICONV_E_NOMEM; goto ERROR6; } @@ -310,12 +352,14 @@ new_type.character = para_charlayout; new_type.style = paragraph->base_style; paragraph_type = &new_type; - if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) + if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; + } if ((res = psiconv_write_u32(config,buf_types,lev+1,paragraph_type->nr))) goto ERROR6; if (!(style = psiconv_get_style(styles,paragraph_type->style))) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Unknown style"); + psiconv_error(config,lev,0,"Unknown style"); res = -PSICONV_E_GENERATE; goto ERROR6; } @@ -362,18 +406,26 @@ goto ERROR6; if ((res = psiconv_write_u8(config,buf,lev+1, ptl_length))) goto ERROR6; - if ((res = psiconv_buffer_concat(buf,buf_types))) + if ((res = psiconv_buffer_concat(buf,buf_types))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; + } if ((res = psiconv_write_u32(config,buf,lev+1,pel_length))) goto ERROR6; - if ((res = psiconv_buffer_concat(buf,buf_elements))) + if ((res = psiconv_buffer_concat(buf,buf_elements))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; + } if ((res = psiconv_write_u32(config,buf,lev+1,nr_of_inlines))) goto ERROR6; - if ((res = psiconv_buffer_concat(buf,buf_inlines))) + if ((res = psiconv_buffer_concat(buf,buf_inlines))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; - if ((res = psiconv_buffer_concat(buf,buf_objects))) + } + if ((res = psiconv_buffer_concat(buf,buf_objects))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; + } ERROR6: psiconv_buffer_free(buf_objects); @@ -386,6 +438,10 @@ ERROR2: psiconv_list_free(paragraph_type_list); ERROR1: + if (res) + psiconv_error(config,lev,0,"Writing of layout section failed"); + else + psiconv_progress(config,lev,0,"End of layout section"); return res; } @@ -394,8 +450,15 @@ psiconv_text_and_layout result, psiconv_word_styles_section styles) { + int res; + psiconv_progress(config,lev,0,"Writing styled layout section"); - return psiconv_write_layout_section(config,buf,lev+1,result,styles,1); + res = psiconv_write_layout_section(config,buf,lev+1,result,styles,1); + if (res) + psiconv_error(config,lev,0,"Writing of styles layout section failed"); + else + psiconv_progress(config,lev,0,"End of styled layout section"); + return res; } int psiconv_write_styleless_layout_section(const psiconv_config config, @@ -408,26 +471,38 @@ psiconv_word_styles_section styles_section; psiconv_progress(config,lev,0,"Writing styleless layout section"); - if (!(styles_section = malloc(sizeof(*styles_section)))) + if (!(styles_section = malloc(sizeof(*styles_section)))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR1; - if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) + } + if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } if (!(styles_section->normal->character = - psiconv_clone_character_layout(base_char))) + psiconv_clone_character_layout(base_char))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR3; + } if (!(styles_section->normal->paragraph = - psiconv_clone_paragraph_layout(base_para))) + psiconv_clone_paragraph_layout(base_para))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR4; + } styles_section->normal->hotkey = 0; - if ((res = psiconv_unicode_from_chars(config,"", - &styles_section->normal->name))) + if (!(styles_section->normal->name = psiconv_unicode_empty_string())) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR5; + } if (!(styles_section->styles = psiconv_list_new(sizeof( - struct psiconv_word_style_s)))) + struct psiconv_word_style_s)))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR6; + } res = psiconv_write_layout_section(config,buf,lev+1,value,styles_section,0); psiconv_free_word_styles_section(styles_section); + psiconv_progress(config,lev,0,"End of styleless layout section"); return res; ERROR6: @@ -441,6 +516,7 @@ ERROR2: free(styles_section); ERROR1: + psiconv_error(config,lev,0,"Writing of styleless layout section failed"); if (!res) return -PSICONV_E_NOMEM; else @@ -458,12 +534,13 @@ psiconv_progress(config,lev,0,"Writing embedded object section"); if (!value) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Null Object"); + psiconv_error(config,lev,0,"Null Object"); res = -PSICONV_E_GENERATE; goto ERROR1; } if (!(extra_buf = psiconv_buffer_new())) { + psiconv_error(config,lev+1,0,"Out of memory error"); res = -PSICONV_E_NOMEM; goto ERROR1; } @@ -475,27 +552,39 @@ goto ERROR2; if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_DISPLAY_SECTION))) goto ERROR2; - if ((res = psiconv_buffer_add_reference(buf,display_id))) + if ((res = psiconv_buffer_add_reference(buf,display_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_ICON_SECTION))) goto ERROR2; - if ((res = psiconv_buffer_add_reference(buf,icon_id))) + if ((res = psiconv_buffer_add_reference(buf,icon_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION))) goto ERROR2; - if ((res = psiconv_buffer_add_reference(buf,table_id))) + if ((res = psiconv_buffer_add_reference(buf,table_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } - if ((res = psiconv_buffer_add_target(buf,display_id))) + if ((res = psiconv_buffer_add_target(buf,display_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } if ((res = psiconv_write_object_display_section(config,buf,lev+1,value->display))) goto ERROR2; - if ((res = psiconv_buffer_add_target(buf,icon_id))) + if ((res = psiconv_buffer_add_target(buf,icon_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } if ((res = psiconv_write_object_icon_section(config,buf,lev+1,value->icon))) goto ERROR2; - if ((res = psiconv_buffer_add_target(buf,table_id))) + if ((res = psiconv_buffer_add_target(buf,table_id))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } switch(value->object->type) { case psiconv_word_file: if ((res = psiconv_write_word_file(config,extra_buf,lev+1, @@ -515,23 +604,28 @@ break; */ default: - psiconv_error(config,0,psiconv_buffer_length(buf), - "Unknown or unsupported object type"); + psiconv_error(config,lev,0,"Unknown or unsupported object type"); res = -PSICONV_E_GENERATE; goto ERROR2; } - if ((res = psiconv_buffer_resolve(extra_buf))) + if ((res = psiconv_buffer_resolve(extra_buf))) { + psiconv_error(config,lev+1,0,"Internal error resolving buffer references"); goto ERROR2; - if ((res = psiconv_buffer_concat(buf,extra_buf))) + } + if ((res = psiconv_buffer_concat(buf,extra_buf))) { + psiconv_error(config,lev+1,0,"Out of memory error"); goto ERROR2; + } psiconv_buffer_free(extra_buf); + psiconv_progress(config,lev,0,"End of embedded object section"); return 0; ERROR2: psiconv_buffer_free(extra_buf); ERROR1: + psiconv_error(config,lev,0,"Writing of embedded object section failed"); return res; } @@ -545,7 +639,7 @@ psiconv_progress(config,lev,0,"Writing object display section"); if (!value) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Null Object Display Section"); + psiconv_error(config,lev,0,"Null Object Display Section"); res = -PSICONV_E_GENERATE; goto ERROR1; } @@ -559,9 +653,12 @@ if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000))) goto ERROR1; + psiconv_progress(config,lev,0,"End of object display section"); + return 0; ERROR1: + psiconv_error(config,lev,0,"Writing of object display section failed"); return res; } @@ -574,7 +671,7 @@ psiconv_progress(config,lev,0,"Writing object icon section"); if (!value) { - psiconv_error(config,0,psiconv_buffer_length(buf),"Null Object Icon Section"); + psiconv_error(config,lev,0,"Null Object Icon Section"); res = -PSICONV_E_GENERATE; goto ERROR1; } @@ -586,8 +683,10 @@ if ((res = psiconv_write_length(config,buf,lev+1,value->icon_height))) goto ERROR1; + psiconv_progress(config,lev,0,"End of object icon section"); return 0; ERROR1: + psiconv_error(config,lev,0,"Writing of object icon section failed"); return res; }