--- psiconv/trunk/lib/psiconv/parse_word.c 1999/11/01 12:35:16 27 +++ psiconv/trunk/lib/psiconv/parse_word.c 2004/01/26 12:59:54 188 @@ -1,6 +1,6 @@ /* parse_word.c - Part of psiconv, a PSION 5 file formats converter - Copyright (c) 1999 Frodo Looijaard + Copyright (c) 1999, 2000 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 @@ -18,10 +18,19 @@ */ #include "config.h" +#include "compat.h" + #include + #include "parse_routines.h" +#include "error.h" + +#ifdef DMALLOC +#include +#endif -int psiconv_parse_word_status_section(const psiconv_buffer buf, int lev, +int psiconv_parse_word_status_section(const psiconv_config config, + const psiconv_buffer buf, int lev, psiconv_u32 off, int *length, psiconv_word_status_section *result) { @@ -30,248 +39,360 @@ psiconv_u32 temp; int leng; - psiconv_progress(lev+1,off,"Going to read the word status section"); - *result = malloc(sizeof(**result)); + psiconv_progress(config,lev+1,off,"Going to read the word status section"); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; - psiconv_progress(lev+2,off+len, + psiconv_progress(config,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(config,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"); - psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); - res = -1; + psiconv_warn(config,lev+2,off+len, + "Word status section initial byte unknown value (ignored)"); + psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp); } len ++; - psiconv_progress(lev+2,off+len, + psiconv_progress(config,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(config,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); + psiconv_debug(config,lev+2,off+len,"Show tabs: %02x",(*result)->show_tabs); (*result)->show_spaces = temp&0x02 ? psiconv_bool_true : psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Show spaces: %02x",(*result)->show_spaces); + psiconv_debug(config,lev+2,off+len,"Show spaces: %02x",(*result)->show_spaces); (*result)->show_paragraph_ends = temp &0x04 ? psiconv_bool_true : psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Show paragraph ends: %02x", + psiconv_debug(config,lev+2,off+len,"Show paragraph ends: %02x", (*result)->show_paragraph_ends); (*result)->show_line_breaks = temp & 0x08 ? psiconv_bool_true : psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Show line breaks: %02x", + psiconv_debug(config,lev+2,off+len,"Show line breaks: %02x", (*result)->show_line_breaks); (*result)->show_hard_minus = temp & 0x20 ? psiconv_bool_true : psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Show hard minus: %02x", + psiconv_debug(config,lev+2,off+len,"Show hard minus: %02x", (*result)->show_hard_minus); (*result)->show_hard_space = temp & 0x40 ? psiconv_bool_true : psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Show hard space: %02x", + psiconv_debug(config,lev+2,off+len,"Show hard space: %02x", (*result)->show_hard_space); if (temp & 0x90) { - psiconv_warn(lev+2,off+len,"Word status section first byte of display " - "flags contains unknown flags"); - psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0x90); - res = -1; + psiconv_warn(config,lev+2,off+len,"Word status section first byte of display " + "flags contains unknown flags (ignored)"); + psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0x90); } len ++; - psiconv_progress(lev+2,off+len,"Going to read second byte of display flags"); - temp = psiconv_read_u8(buf,lev+2,off+len); + psiconv_progress(config,lev+2,off+len,"Going to read second byte of display flags"); + temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); + if (res) + goto ERROR2; (*result)->show_full_pictures = temp & 0x01 ? psiconv_bool_true : psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Show full pictures: %02x", + psiconv_debug(config,lev+2,off+len,"Show full pictures: %02x", (*result)->show_full_pictures); (*result)->show_full_graphs = temp & 0x02 ? psiconv_bool_true : psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Show full graphs: %02x", + psiconv_debug(config,lev+2,off+len,"Show full graphs: %02x", (*result)->show_full_graphs); if (temp & 0xfc) { - psiconv_warn(lev+2,off+len,"Word status section second byte of display " - "flags contains unknown flags"); - psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); - res = -1; + psiconv_warn(config,lev+2,off+len,"Word status section second byte of display " + "flags contains unknown flags (ignored)"); + psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); } 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); + psiconv_progress(config,lev+2,off+len,"Going to read top toolbar setting"); + if ((res = psiconv_parse_bool(config,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); + psiconv_progress(config,lev+2,off+len,"Going to read side toolbar setting"); + if ((res = psiconv_parse_bool(config,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); + psiconv_progress(config,lev+2,off+len,"Going to read operational flags"); + temp = psiconv_read_u8(config,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", + psiconv_debug(config,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"); - psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); - res = -1; + psiconv_warn(config,lev+2,off+len,"Word status section operational flags " + "contains unknown flags (ignored)"); + psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); } len ++; - psiconv_progress(lev+2,off+len,"Going to read cursor position"); - (*result)->cursor_position = psiconv_read_u32(buf,lev+2,off + len); - psiconv_debug(lev+2,off+len,"Cursor position: %08x", + psiconv_progress(config,lev+2,off+len,"Going to read cursor position"); + (*result)->cursor_position = psiconv_read_u32(config,buf,lev+2,off + len,&res); + if (res) + goto ERROR2; + psiconv_debug(config,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); - psiconv_debug(lev+2,off+len,"Display size: %08x", - (*result)->cursor_position); + psiconv_progress(config,lev+2,off+len,"Going to read display size"); + (*result)->display_size = psiconv_read_u32(config,buf,lev+2,off + len,&res); + if (res) + goto ERROR2; + psiconv_debug(config,lev+2,off+len,"Display size: %08x", + (*result)->display_size); len += 0x04; if (length) *length = len; - psiconv_progress(lev,off+len-1, + psiconv_progress(config,lev,off+len-1, "End of word status section (total length: %08x)", len); + return 0; - return res; +ERROR2: + free (*result); +ERROR1: + psiconv_error(config,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, +int psiconv_parse_word_styles_section(const psiconv_config config, + const psiconv_buffer buf, int lev, psiconv_u32 off, int *length, psiconv_word_styles_section *result) { 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)); - - psiconv_progress(lev+2,off+len,"Going to read style normal"); - style = malloc(sizeof(*style)); + psiconv_progress(config,lev+1,off,"Going to read the word styles section"); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; + + psiconv_progress(config,lev+2,off+len,"Going to read style normal"); + if (!(style = malloc(sizeof(*style)))) + goto ERROR2; style->name = NULL; - style->paragraph = psiconv_basic_paragraph_layout(); - 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 (!(style->paragraph = psiconv_basic_paragraph_layout())) + goto ERROR2_1; + psiconv_progress(config,lev+3,off+len,"Going to read the paragraph codes"); + if ((res = psiconv_parse_paragraph_layout_list(config,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); + psiconv_progress(config,lev+3,off+len,"Going to read the character codes"); + if (!(style->character = psiconv_basic_character_layout())) + goto ERROR2_2; + if ((res = psiconv_parse_character_layout_list(config,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); + psiconv_progress(config,lev+3,off+len,"Going to read the hotkey"); + temp = psiconv_read_u32(config,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_debug(lev+3,off+len,"Hotkey value %08x",temp); + psiconv_warn(config,lev+3,off+len, + "Normal style hotkey has unknown value (ignored)"); + psiconv_debug(config,lev+3,off+len,"Hotkey value %08x",temp); temp = 0; - res = -1; } - style->hotkey = temp; - if ((style->hotkey >= 32) && (style->hotkey < 127)) - psiconv_debug(lev+3,off+len,"Hotkey: %c",style->hotkey); - else if (style->hotkey == 0x00) - psiconv_debug(lev+3,off+len,"Hotkey: "); - else - psiconv_debug(lev+3,off+len,"Hotkey: %02x",style->hotkey); + style->hotkey = psiconv_unicode_from_char(config,temp); (*result)->normal = style; len += 0x04; - psiconv_progress(lev+2,off+len,"Going to read hotkeys list"); - (*result)->styles = psiconv_list_new(sizeof(*style)); - style = malloc(sizeof(*style)); - psiconv_progress(lev+3,off+len,"Going to read the number of entries"); - nr = psiconv_read_u8(buf,lev+3,off+len); + psiconv_progress(config,lev+2,off+len,"Going to read hotkeys list"); + if (!((*result)->styles = psiconv_list_new(sizeof(*style)))) + goto ERROR3; + if (!(style = malloc(sizeof(*style)))) { + goto ERROR3_1; + } + + psiconv_progress(config,lev+3,off+len,"Going to read the number of entries"); + nr = psiconv_read_u8(config,buf,lev+3,off+len,&res); + if (res) + goto ERROR3_2; len ++; - psiconv_debug(lev+3,off+len,"Nummer of hotkeys: %02x",nr); + psiconv_debug(config,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(config,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_debug(lev+3,off+len,"Hotkey %d value %08x",i,temp); + psiconv_warn(config,lev+3,off+len,"Style hotkey has unknown value (ignored)"); + psiconv_debug(config,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); + psiconv_debug(config,lev+3,off+len,"Hotkey %d: %c",i,style->hotkey); else if (style->hotkey == 0x00) - psiconv_debug(lev+3,off+len,"Hotkey %d: ",i); + psiconv_debug(config,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); + psiconv_debug(config,lev+3,off+len,"Hotkey %d: %02x",i,style->hotkey); + 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); + psiconv_progress(config,lev+2,off+len,"Going to read all other styles"); + psiconv_progress(config,lev+2,off+len,"Going to read the number of styles"); + nr = psiconv_read_u8(config,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_warn(config,lev+3,off+len,"Number of styles and hotkeys do not match"); + psiconv_debug(config,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); + psiconv_progress(config,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); - psiconv_debug(lev+3,off+len,"New entry added in list"); + if (psiconv_list_add((*result)->styles,style)) { + free(style); + goto ERROR5; + } + psiconv_debug(config,lev+3,off+len,"New entry added in list"); free(style); } - style = psiconv_list_get((*result)->styles,i); - psiconv_progress(lev+3,off+len,"Going to read the style name"); - style->name = psiconv_read_string(buf,lev+3,off+len,&leng); + if (!(style = psiconv_list_get((*result)->styles,i))) + goto ERROR5; + psiconv_progress(config,lev+3,off+len,"Going to read the style name"); + style->name = psiconv_read_string(config,buf,lev+3,off+len,&leng,&res); + if (res) + goto ERROR5; len += leng; - psiconv_progress(lev+3,off+len, + psiconv_progress(config,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(config,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"); + psiconv_debug(config,lev+3,off+len,"Built-in style"); } else if (temp == PSICONV_ID_STYLE_REMOVABLE) { style->built_in = psiconv_bool_false; - psiconv_debug(lev+3,off+len,"Removable style"); + psiconv_debug(config,lev+3,off+len,"Removable style"); } else { - psiconv_warn(lev+3,off+len,"Word styles section unknown style id"); - psiconv_debug(lev+3,off+len,"Unknown id: %08x",temp); + psiconv_warn(config,lev+3,off+len, + "Word styles section unknown style id (treated as built-in)"); + psiconv_debug(config,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); - psiconv_debug(lev+3,off+len,"Outline Level: %08x", style->outline_level); + psiconv_progress(config,lev+3,off+len,"Going to read outline level"); + style->outline_level = psiconv_read_u32(config,buf,lev+3,off+len,&res); + if (res) + goto ERROR6; + psiconv_debug(config,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); + psiconv_progress(config,lev+3,off+len,"Going to read the character codes"); + if (!(style->character = psiconv_clone_character_layout((*result)->normal->character))) + goto ERROR6; + if ((res = psiconv_parse_character_layout_list(config,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); + psiconv_progress(config,lev+3,off+len,"Going to read the paragraph codes"); + if (!(style->paragraph = psiconv_clone_paragraph_layout((*result)->normal->paragraph))) + goto ERROR7; + if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+3,off+len,&leng, + style->paragraph))) + goto ERROR8; len += leng; } + psiconv_progress(config,lev+2,off+len,"Reading trailing bytes"); + for (i = 0; i < psiconv_list_length((*result)->styles); i++) { + temp = psiconv_read_u8(config,buf,lev+3,off+len,&res); + if (res) + goto ERROR4; + if (temp != 0xff) { + psiconv_warn(config,lev+3,off+len,"Unknown trailing style byte"); + psiconv_debug(config,lev+3,off+len,"Trailing byte: %02x expected, read %02x", + 0xff,temp); + } else + psiconv_debug(config,lev+3,off+len,"Read trailing byte 0xff"); + len++; + } + if (length) *length = len; - psiconv_progress(lev,off+len-1, + psiconv_progress(config,lev,off+len-1, "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_error(config,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_error(config,lev+1,off,"Reading of Word Status Section failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; } +