--- psiconv/trunk/lib/psiconv/parse_page.c 1999/10/03 21:10:47 2 +++ psiconv/trunk/lib/psiconv/parse_page.c 2002/01/29 18:38:38 142 @@ -1,6 +1,6 @@ /* parse_page.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,69 +18,101 @@ */ #include "config.h" +#include "compat.h" + #include #include -#include "data.h" #include "parse_routines.h" +#include "error.h" + +#ifdef DMALLOC +#include +#endif + int psiconv_parse_page_header(const psiconv_buffer buf,int lev,psiconv_u32 off, int *length,psiconv_page_header *result) { int res = 0; int len = 0; - int i,leng; + int i,leng,has_content; psiconv_u32 temp; psiconv_progress(lev+1,off,"Going to read a page header (or footer)"); - (*result) = malloc(sizeof(**result)); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; psiconv_progress(lev+2,off+len, - "Going to read the first byte (0x01 expected)"); - temp = psiconv_read_u8(buf,lev+2,off+len); - if (temp != 0x01) { - psiconv_warn(lev+2,off+len,"Page header first byte mismatch"); - psiconv_debug(lev+2,off+len,"First byte: read %02x, expected %02x", - temp,0x01); - res = -1; + "Going to read the has_content flag"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + if (temp == 0x00) + has_content = 0; + else if (temp == 0x01) + has_content = 1; + else { + psiconv_warn(lev+2,off+len, + "Page header has_content flag unknown value (assumed default)"); + psiconv_debug(lev+2,off+len,"Flag: %02x",temp); + has_content = 1; } + psiconv_debug(lev+2,off+len,"Has_content flag: %02x",has_content); len += 1; psiconv_progress(lev+2,off+len,"Going to read displayed-on-first-page flag"); - res |= psiconv_parse_bool(buf,lev+2,off+len,&leng,&(*result)->on_first_page); + if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, + &(*result)->on_first_page))) + goto ERROR2; len += leng; psiconv_progress(lev+2,off+len,"Going to read three zero bytes"); for (i = 0; i < 0x03; i++,len++) { - 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 != 0x00) { psiconv_warn(lev+2,off+len, "Page Header unknown value in zero bytes section"); psiconv_debug(lev+2,off+len,"Byte %d: read %02x, expected %02x", i,temp,0x00); - res = -1; } } psiconv_progress(lev+2,off+len,"Going to read base paragraph layout"); - (*result)->base_paragraph_layout = psiconv_basic_paragraph_layout(); - res |= psiconv_parse_paragraph_layout_list(buf,lev+2,off+len,&leng, - (*result)->base_paragraph_layout); - len += leng; + if (!((*result)->base_paragraph_layout = psiconv_basic_paragraph_layout())) + goto ERROR2; + + if (has_content) { + if ((res = psiconv_parse_paragraph_layout_list(buf,lev+2,off+len,&leng, + (*result)->base_paragraph_layout))) + goto ERROR3; + len += leng; + } psiconv_progress(lev+2,off+len,"Going to read base character layout"); - (*result)->base_character_layout = psiconv_basic_character_layout(); - res |= psiconv_parse_character_layout_list(buf,lev+2,off+len,&leng, - (*result)->base_character_layout); + if (!((*result)->base_character_layout = psiconv_basic_character_layout())) + goto ERROR3; + if (has_content) { + if ((res = psiconv_parse_character_layout_list(buf,lev+2,off+len,&leng, + (*result)->base_character_layout))) + goto ERROR4; + } len += leng; psiconv_progress(lev+2,off+len,"Going to read the TextEd section"); - res |= psiconv_parse_texted_section(buf,lev+2,off+len,&leng, - &(*result)->text, - (*result)->base_character_layout, - (*result)->base_paragraph_layout); - len += leng; + if (has_content) { + if ((res = psiconv_parse_texted_section(buf,lev+2,off+len,&leng, + &(*result)->text, + (*result)->base_character_layout, + (*result)->base_paragraph_layout))) + goto ERROR4; + len += leng; + } else { + (*result)->text = NULL; + } if (length) *length = len; @@ -89,6 +121,21 @@ "(total length: %08x", len); return res; + +ERROR4: + psiconv_free_character_layout((*result)->base_character_layout); +ERROR3: + psiconv_free_paragraph_layout((*result)->base_paragraph_layout); +ERROR2: + free (*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Page Header failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; } int psiconv_parse_page_layout_section(const psiconv_buffer buf,int lev, @@ -101,72 +148,106 @@ psiconv_u32 temp; psiconv_progress(lev+1,off,"Going to read the page layout section"); - (*result) = malloc(sizeof(**result)); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; psiconv_progress(lev+2,off+len,"Going to read first page number"); - (*result)->first_page_nr = psiconv_read_u32(buf,lev+2,off+len); + (*result)->first_page_nr = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"First page: %d",(*result)->first_page_nr); len += 4; psiconv_progress(lev+2,off+len,"Going to read header distance"); - (*result)->header_dist = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->header_dist = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Header distance: %6.3f",(*result)->header_dist); len += leng; psiconv_progress(lev+2,off+len,"Going to read footer distance"); - (*result)->footer_dist = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->footer_dist = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Footer distance: %6.3f",(*result)->footer_dist); len += leng; psiconv_progress(lev+2,off+len,"Going to read the left margin"); - (*result)->left_margin = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->left_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Left margin: %6.3f",(*result)->left_margin); len += leng; psiconv_progress(lev+2,off+len,"Going read the to right margin"); - (*result)->right_margin = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->right_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Right margin: %6.3f",(*result)->right_margin); len += leng; psiconv_progress(lev+2,off+len,"Going to read the top margin"); - (*result)->top_margin = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->top_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Top margin: %6.3f",(*result)->top_margin); len += leng; psiconv_progress(lev+2,off+len,"Going to read the bottom margin"); - (*result)->bottom_margin = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->bottom_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; psiconv_debug(lev+2,off+len,"Bottom margin: %6.3f",(*result)->bottom_margin); len += leng; psiconv_progress(lev+2,off+len,"Going to read the header"); - res |= psiconv_parse_page_header(buf,lev+2,off+len,&leng,&(*result)->header); + if ((res = psiconv_parse_page_header(buf,lev+2,off+len,&leng, + &(*result)->header))) + goto ERROR2; len += leng; psiconv_progress(lev+2,off+len,"Going to read the footer"); - res |= psiconv_parse_page_header(buf,lev+2,off+len,&leng,&(*result)->footer); + if ((res = psiconv_parse_page_header(buf,lev+2,off+len,&leng, + &(*result)->footer))) + goto ERROR3; len += leng; psiconv_progress(lev+2,off+len,"Going to read page dimensions id"); - temp = psiconv_read_u32(buf,lev+2,off+len); - if (temp != PSICONV_ID_PAGE_DIMENSIONS) { + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR4; + if ((temp != PSICONV_ID_PAGE_DIMENSIONS1) && + (temp != PSICONV_ID_PAGE_DIMENSIONS2)) { psiconv_warn(lev+2,off+len, "Page layout section page dimensions marker not found"); psiconv_debug(lev+2,off+len, - "Page dimensions marker: read %08x, expected %08x",temp, - PSICONV_ID_PAGE_DIMENSIONS); - res = -1; + "Page dimensions marker: read %08x, expected %08x or %08x", + temp, PSICONV_ID_PAGE_DIMENSIONS1, + PSICONV_ID_PAGE_DIMENSIONS2); } + len += 4; psiconv_progress(lev+2,off+len,"Going to read the page width"); - (*result)->page_width = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->page_width = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR4; psiconv_debug(lev+2,off+len,"Page width: %6.3f",(*result)->page_width); len += leng; psiconv_progress(lev+2,off+len,"Going to read the page height"); - (*result)->page_height = psiconv_read_length(buf,lev+2,off+len,&leng); + (*result)->page_height = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR4; psiconv_debug(lev+2,off+len,"Page height: %6.3f",(*result)->page_height); len += leng; + psiconv_progress(lev+2,off+len,"Going to read page portrait/landscape"); + if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,&(*result)->landscape))) + goto ERROR4; + psiconv_debug(lev+2,off+len,"Landscape: %d",(*result)->landscape); + len += leng; + + if (length) *length = len; @@ -174,4 +255,19 @@ len); return res; + +ERROR4: + psiconv_free_page_header((*result)->footer); +ERROR3: + psiconv_free_page_header((*result)->header); +ERROR2: + free (*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Page Section failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; }