--- psiconv/trunk/lib/psiconv/parse_page.c 2000/12/13 16:30:21 63 +++ psiconv/trunk/lib/psiconv/parse_page.c 2000/12/15 00:21:51 64 @@ -33,53 +33,63 @@ 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); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (!res) + goto ERROR2; 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; } 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); + if (!((*result)->base_paragraph_layout = psiconv_basic_paragraph_layout())) + goto ERROR2; + 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 ((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, + if ((res = psiconv_parse_texted_section(buf,lev+2,off+len,&leng, &(*result)->text, (*result)->base_character_layout, - (*result)->base_paragraph_layout); + (*result)->base_paragraph_layout))) + goto ERROR4; len += leng; if (length) @@ -89,6 +99,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,69 +126,93 @@ 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); + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR4; if (temp != PSICONV_ID_PAGE_DIMENSIONS) { 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; } 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; @@ -174,4 +223,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; }