--- psiconv/trunk/lib/psiconv/parse_texted.c 2000/12/13 16:30:21 63 +++ psiconv/trunk/lib/psiconv/parse_texted.c 2004/02/04 12:19:09 196 @@ -1,6 +1,6 @@ /* parse_texted.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 @@ -18,12 +18,19 @@ */ #include "config.h" +#include "compat.h" + #include -#include "data.h" #include "parse_routines.h" +#include "error.h" + +#ifdef DMALLOC +#include +#endif -int psiconv_parse_texted_section(const psiconv_buffer buf,int lev, +int psiconv_parse_texted_section(const psiconv_config config, + const psiconv_buffer buf,int lev, psiconv_u32 off, int *length, psiconv_texted_section *result, psiconv_character_layout base_char, @@ -37,65 +44,79 @@ psiconv_u32 temp; int leng; - psiconv_progress(lev+1,off,"Going to read a texted section"); - (*result) = malloc(sizeof(**result)); - - psiconv_progress(lev+2,off+len,"Going to read section id"); - temp = psiconv_read_u32(buf,lev+2,off+len); + psiconv_progress(config,lev+1,off,"Going to read a texted section"); + if (!((*result) = malloc(sizeof(**result)))) + goto ERROR1; + + psiconv_progress(config,lev+2,off+len,"Going to read section id"); + temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); + if (res) + goto ERROR2; if (temp != PSICONV_ID_TEXTED_BODY) { - psiconv_warn(lev+2,off+len, + psiconv_error(config,lev+2,off+len, "Page header section body id not found"); - psiconv_debug(lev+2,off+len, + psiconv_debug(config,lev+2,off+len, "Page body id: read %08x, expected %08x",temp, PSICONV_ID_TEXTED); - res = -1; + res = -PSICONV_E_PARSE; + goto ERROR2; } len += 4; - psiconv_progress(lev+2,off+len,"Going to read the section jumptable"); - while (temp = psiconv_read_u32(buf,lev+3,off+len), - temp != PSICONV_ID_TEXTED_TEXT) { + psiconv_progress(config,lev+2,off+len,"Going to read the section jumptable"); + while (temp = psiconv_read_u32(config,buf,lev+3,off+len,&res), + !res && temp != PSICONV_ID_TEXTED_TEXT) { len += 4; if (temp == PSICONV_ID_TEXTED_LAYOUT) { - layout_sec = psiconv_read_u32(buf,lev+3,off+len); - psiconv_debug(lev+3,off+len,"Found Layout section at %08x",layout_sec); + layout_sec = psiconv_read_u32(config,buf,lev+3,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(config,lev+3,off+len,"Found Layout section at %08x",layout_sec); } else if (temp == PSICONV_ID_TEXTED_REPLACEMENT) { - replacement_sec = psiconv_read_u32(buf,lev+3,off+len); - psiconv_debug(lev+3,off+len,"Found Replacement section at %08x", + replacement_sec = psiconv_read_u32(config,buf,lev+3,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(config,lev+3,off+len,"Found Replacement section at %08x", replacement_sec); } else if (temp == PSICONV_ID_TEXTED_UNKNOWN) { - unknown_sec= psiconv_read_u32(buf,lev+3,off+len); + unknown_sec= psiconv_read_u32(config,buf,lev+3,off+len,&res); + if (res) + goto ERROR2; if (unknown_sec) { - psiconv_warn(lev+3,off+len, - "Unknown section in TextEd jumptable has real offset"); - res = -1; + psiconv_warn(config,lev+3,off+len, + "Unknown section in TextEd jumptable has real offset (ignoring)"); } - psiconv_debug(lev+3,off+len,"Found Unknown section at %08x", + psiconv_debug(config,lev+3,off+len,"Found Unknown section at %08x", unknown_sec); } else { - psiconv_warn(lev+3,off+len,"Unknown section in TextEd jumptable"); - psiconv_debug(lev+3,off+len,"Section ID %08x at offset %08x",temp, - psiconv_read_u32(buf,lev+3,off+len)); - res = -1; + psiconv_warn(config,lev+3,off+len, + "Unknown section in TextEd jumptable (ignoring)"); + psiconv_debug(config,lev+3,off+len,"Section ID %08x at offset %08x",temp, + psiconv_read_u32(config,buf,lev+3,off+len,NULL)); } len += 4; } + if (res) + goto ERROR2; len += 4; - psiconv_progress(lev+2,off+len,"Going to read the text"); - psiconv_parse_text_section(buf,lev+2,off+len,&leng,&(*result)->paragraphs); + psiconv_progress(config,lev+2,off+len,"Going to read the text"); + if ((res = psiconv_parse_text_section(config,buf,lev+2,off+len,&leng, + &(*result)->paragraphs))) + goto ERROR2; len += leng; if (layout_sec) { - psiconv_progress(lev+2,off+len,"Going to read the layout"); - psiconv_parse_styleless_layout_section(buf,lev+2,layout_sec,NULL, + psiconv_progress(config,lev+2,off+len,"Going to read the layout"); + if ((res = psiconv_parse_styleless_layout_section(config,buf,lev+2,layout_sec,NULL, (*result)->paragraphs, - base_char,base_para); + base_char,base_para))) + goto ERROR3; } #if 0 if (replacement_sec) { - psiconv_progress(lev+2,off+len,"Going to read the replacements"); + psiconv_progress(config,lev+2,off+len,"Going to read the replacements"); /* WHATEVER */ } #endif @@ -103,10 +124,23 @@ if (length) *length = len; - psiconv_progress(lev+1,off+len-1,"End of TextEd section " + psiconv_progress(config,lev+1,off+len-1,"End of TextEd section " "(total length: %08x", len); - return res; + return 0; + +ERROR3: + psiconv_free_text_and_layout((*result)->paragraphs); +ERROR2: + free (*result); +ERROR1: + psiconv_error(config,lev+1,off,"Reading of TextEd Section failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; }