--- psiconv/trunk/lib/psiconv/parse_word.c 2000/12/13 16:30:21 63 +++ psiconv/trunk/lib/psiconv/parse_word.c 2000/12/15 00:21:51 64 @@ -31,22 +31,26 @@ int leng; psiconv_progress(lev+1,off,"Going to read the word status section"); - *result = malloc(sizeof(**result)); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; psiconv_progress(lev+2,off+len, "Going to read the initial byte (%02x expected)",0x02); - temp = psiconv_read_u8(buf,lev+2,off+len); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; if (temp != 0x02) { psiconv_warn(lev+2,off+len, - "Word status section initial byte unknown value"); + "Word status section initial byte unknown value (ignored)"); psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); - res = -1; } len ++; psiconv_progress(lev+2,off+len, "Going to read the first byte of display flags"); - temp = psiconv_read_u8(buf,lev+2,off+len); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; (*result)->show_tabs = temp&0x01 ? psiconv_bool_true : psiconv_bool_false; psiconv_debug(lev+2,off+len,"Show tabs: %02x",(*result)->show_tabs); @@ -70,14 +74,15 @@ (*result)->show_hard_space); if (temp & 0x90) { psiconv_warn(lev+2,off+len,"Word status section first byte of display " - "flags contains unknown flags"); + "flags contains unknown flags (ignored)"); psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0x90); - res = -1; } len ++; psiconv_progress(lev+2,off+len,"Going to read second byte of display flags"); - temp = psiconv_read_u8(buf,lev+2,off+len); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; (*result)->show_full_pictures = temp & 0x01 ? psiconv_bool_true : psiconv_bool_false; @@ -89,42 +94,50 @@ (*result)->show_full_graphs); if (temp & 0xfc) { psiconv_warn(lev+2,off+len,"Word status section second byte of display " - "flags contains unknown flags"); + "flags contains unknown flags (ignored)"); psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); - res = -1; } len ++; psiconv_progress(lev+2,off+len,"Going to read top toolbar setting"); - psiconv_parse_bool(buf,lev+2,off+len,&leng,&(*result)->show_top_toolbar); + if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, + &(*result)->show_top_toolbar))) + goto ERROR2; len += leng; psiconv_progress(lev+2,off+len,"Going to read side toolbar setting"); - psiconv_parse_bool(buf,lev+2,off+len,&leng,&(*result)->show_side_toolbar); + if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, + &(*result)->show_side_toolbar))) + goto ERROR2; len += leng; psiconv_progress(lev+2,off+len,"Going to read operational flags"); - temp = psiconv_read_u8(buf,lev+2,off+len); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; (*result)->fit_lines_to_screen = temp & 0x08 ? psiconv_bool_true : psiconv_bool_false; psiconv_debug(lev+2,off+len,"Fit lines to screen: %02x", (*result)->fit_lines_to_screen); if (temp & 0xf7) { psiconv_warn(lev+2,off+len,"Word status section operational flags " - "contains unknown flags"); + "contains unknown flags (ignored)"); psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); - res = -1; } len ++; psiconv_progress(lev+2,off+len,"Going to read cursor position"); - (*result)->cursor_position = psiconv_read_u32(buf,lev+2,off + len); + (*result)->cursor_position = psiconv_read_u32(buf,lev+2,off + len,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Cursor position: %08x", (*result)->cursor_position); len += 0x04; psiconv_progress(lev+2,off+len,"Going to read display size"); - (*result)->cursor_position = psiconv_read_u32(buf,lev+2,off + len); + (*result)->cursor_position = psiconv_read_u32(buf,lev+2,off + len,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Display size: %08x", (*result)->cursor_position); len += 0x04; @@ -134,8 +147,18 @@ psiconv_progress(lev,off+len-1, "End of word status section (total length: %08x)", len); + return 0; - return res; +ERROR2: + free (*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Word Status Section failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; } int psiconv_parse_word_styles_section(const psiconv_buffer buf, int lev, @@ -144,35 +167,43 @@ { int res=0; int len=0; - int leng,i,nr; + int leng,i,nr,j; psiconv_word_style style; psiconv_u32 temp; psiconv_progress(lev+1,off,"Going to read the word styles section"); - *result = malloc(sizeof(**result)); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; psiconv_progress(lev+2,off+len,"Going to read style normal"); - style = malloc(sizeof(*style)); + if (!(style = malloc(sizeof(*style)))) + goto ERROR2; style->name = NULL; - style->paragraph = psiconv_basic_paragraph_layout(); + if (!(style->paragraph = psiconv_basic_paragraph_layout())) + goto ERROR2_1; psiconv_progress(lev+3,off+len,"Going to read the paragraph codes"); - res |= psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, - style->paragraph); + if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, + style->paragraph))) + goto ERROR2_2; len += leng; psiconv_progress(lev+3,off+len,"Going to read the character codes"); - style->character = psiconv_basic_character_layout(); - res |= psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, - style->character); + if (!(style->character = psiconv_basic_character_layout())) + goto ERROR2_2; + if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, + style->character))) + goto ERROR2_3; len += leng; psiconv_progress(lev+3,off+len,"Going to read the hotkey"); - temp = psiconv_read_u32(buf,lev+3,off+len); + temp = psiconv_read_u32(buf,lev+3,off+len,&res); + if (res) + goto ERROR2_3; if (temp & 0xffffff00) { - psiconv_warn(lev+3,off+len,"Normal style hotkey has unknown value"); + psiconv_warn(lev+3,off+len, + "Normal style hotkey has unknown value (ignored)"); psiconv_debug(lev+3,off+len,"Hotkey value %08x",temp); temp = 0; - res = -1; } - style->hotkey = temp; + style->hotkey = temp & 0x000000ff; if ((style->hotkey >= 32) && (style->hotkey < 127)) psiconv_debug(lev+3,off+len,"Hotkey: %c",style->hotkey); else if (style->hotkey == 0x00) @@ -183,59 +214,78 @@ len += 0x04; psiconv_progress(lev+2,off+len,"Going to read hotkeys list"); - (*result)->styles = psiconv_list_new(sizeof(*style)); - style = malloc(sizeof(*style)); + if (!((*result)->styles = psiconv_list_new(sizeof(*style)))) + goto ERROR3; + if (!(style = malloc(sizeof(*style)))) { + goto ERROR3_1; + } + psiconv_progress(lev+3,off+len,"Going to read the number of entries"); - nr = psiconv_read_u8(buf,lev+3,off+len); + nr = psiconv_read_u8(buf,lev+3,off+len,&res); + if (res) + goto ERROR3_2; len ++; psiconv_debug(lev+3,off+len,"Nummer of hotkeys: %02x",nr); for (i = 0; i < nr; i ++) { - temp = psiconv_read_u32(buf,lev+2,off+len); + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR3_2; if (temp & 0xffffff00) { - psiconv_warn(lev+3,off+len,"Style hotkey has unknown value"); + psiconv_warn(lev+3,off+len,"Style hotkey has unknown value (ignored)"); psiconv_debug(lev+3,off+len,"Hotkey %d value %08x",i,temp); temp = 0; - res = -1; } - style->hotkey = temp; + style->hotkey = temp & 0x000000ff; if ((style->hotkey >= 32) && (style->hotkey < 127)) psiconv_debug(lev+3,off+len,"Hotkey %d: %c",i,style->hotkey); else if (style->hotkey == 0x00) psiconv_debug(lev+3,off+len,"Hotkey %d: ",i); else psiconv_debug(lev+3,off+len,"Hotkey %d: %02x",i,style->hotkey); - psiconv_list_add((*result)->styles,style); + if ((res = psiconv_list_add((*result)->styles,style))) + goto ERROR3_2; len += 0x04; } free(style); psiconv_progress(lev+2,off+len,"Going to read all other styles"); psiconv_progress(lev+2,off+len,"Going to read the number of styles"); - nr = psiconv_read_u8(buf,lev+3,off+len); + nr = psiconv_read_u8(buf,lev+3,off+len,&res); + if (res) + goto ERROR4; if (nr != psiconv_list_length((*result)->styles)) { psiconv_warn(lev+3,off+len,"Number of styles and hotkeys do not match"); psiconv_debug(lev+3,off+len,"%d hotkeys, %d styles", psiconv_list_length((*result)->styles), nr); - res = -1; } len ++; for (i = 0; i < nr; i++) { psiconv_progress(lev+2,off+len,"Next style: %d",i); if (i >= psiconv_list_length((*result)->styles)) { - style = malloc(sizeof(*style)); + if (!(style = malloc(sizeof(*style)))) + goto ERROR5; style->hotkey = 0; - psiconv_list_add((*result)->styles,style); + if (!(psiconv_list_add((*result)->styles,style))) { + free(style); + goto ERROR5; + } psiconv_debug(lev+3,off+len,"New entry added in list"); free(style); } - style = psiconv_list_get((*result)->styles,i); + if (!(style = psiconv_list_get((*result)->styles,i))) + goto ERROR5; psiconv_progress(lev+3,off+len,"Going to read the style name"); - style->name = psiconv_read_string(buf,lev+3,off+len,&leng); + style->name = psiconv_read_string(buf,lev+3,off+len,&leng,&res); + if (res) + goto ERROR5; len += leng; psiconv_progress(lev+3,off+len, "Going to read whether this style is built-in"); - temp = psiconv_read_u32(buf,lev+2,off+len); + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR6; + if (temp == PSICONV_ID_STYLE_BUILT_IN) { style->built_in = psiconv_bool_true; psiconv_debug(lev+3,off+len,"Built-in style"); @@ -243,25 +293,31 @@ style->built_in = psiconv_bool_false; psiconv_debug(lev+3,off+len,"Removable style"); } else { - psiconv_warn(lev+3,off+len,"Word styles section unknown style id"); + psiconv_warn(lev+3,off+len, + "Word styles section unknown style id (treated as built-in)"); psiconv_debug(lev+3,off+len,"Unknown id: %08x",temp); style->built_in = psiconv_bool_false; - res = -1; } len += 4; psiconv_progress(lev+3,off+len,"Going to read outline level"); - style->outline_level = psiconv_read_u32(buf,lev+3,off+len); + style->outline_level = psiconv_read_u32(buf,lev+3,off+len,&res); + if (res) + goto ERROR6; psiconv_debug(lev+3,off+len,"Outline Level: %08x", style->outline_level); len += 4; psiconv_progress(lev+3,off+len,"Going to read the character codes"); - style->character = psiconv_basic_character_layout(); - res |= psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, - style->character); + if (!(style->character = psiconv_basic_character_layout())) + goto ERROR6; + if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, + style->character))) + goto ERROR7; len += leng; psiconv_progress(lev+3,off+len,"Going to read the paragraph codes"); - style->paragraph = psiconv_basic_paragraph_layout(); - res |= psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, - style->paragraph); + if (!(style->paragraph = psiconv_basic_paragraph_layout())) + goto ERROR7; + if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, + style->paragraph))) + goto ERROR8; len += leng; } @@ -272,6 +328,53 @@ "End of word styles section (total length: %08x)", len); return res; + + +ERROR3_2: + free(style); +ERROR3_1: + psiconv_list_free((*result)->styles); +goto ERROR3; + +ERROR2_3: + psiconv_free_character_layout(style->character); +ERROR2_2: + psiconv_free_paragraph_layout(style->paragraph); +ERROR2_1: + free (style); +goto ERROR2; + +ERROR8: + psiconv_free_paragraph_layout(style->paragraph); +ERROR7: + psiconv_free_character_layout(style->character); +ERROR6: + free(style->name); +ERROR5: + for (j = 0; j < i ;j++) { + if (!(style = psiconv_list_get((*result)->styles,j))) { + psiconv_warn(lev+1,off,"Massive memory corruption"); + goto ERROR4; + } + psiconv_free_character_layout(style->character); + psiconv_free_paragraph_layout(style->paragraph); + free(style->name); + } +ERROR4: + psiconv_list_free((*result)->styles); +ERROR3: + psiconv_free_word_style((*result)->normal); +ERROR2: + free (*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Word Status Section failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; } +