| 1 |
/* |
| 2 |
parse_page.c - Part of psiconv, a PSION 5 file formats converter |
| 3 |
Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program; if not, write to the Free Software |
| 17 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "compat.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "parse_routines.h" |
| 27 |
#include "error.h" |
| 28 |
|
| 29 |
#ifdef DMALLOC |
| 30 |
#include <dmalloc.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
|
| 34 |
int psiconv_parse_page_header(const psiconv_buffer buf,int lev,psiconv_u32 off, |
| 35 |
int *length,psiconv_page_header *result) |
| 36 |
{ |
| 37 |
int res = 0; |
| 38 |
int len = 0; |
| 39 |
int i,leng,has_content; |
| 40 |
psiconv_u32 temp; |
| 41 |
|
| 42 |
psiconv_progress(lev+1,off,"Going to read a page header (or footer)"); |
| 43 |
if (!(*result = malloc(sizeof(**result)))) |
| 44 |
goto ERROR1; |
| 45 |
|
| 46 |
psiconv_progress(lev+2,off+len, |
| 47 |
"Going to read the has_content flag"); |
| 48 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
| 49 |
if (res) |
| 50 |
goto ERROR2; |
| 51 |
if (temp == 0x00) |
| 52 |
has_content = 0; |
| 53 |
else if (temp == 0x01) |
| 54 |
has_content = 1; |
| 55 |
else { |
| 56 |
psiconv_warn(lev+2,off+len, |
| 57 |
"Page header has_content flag unknown value (assumed default)"); |
| 58 |
psiconv_debug(lev+2,off+len,"Flag: %02x",temp); |
| 59 |
has_content = 1; |
| 60 |
} |
| 61 |
psiconv_debug(lev+2,off+len,"Has_content flag: %02x",has_content); |
| 62 |
len += 1; |
| 63 |
|
| 64 |
psiconv_progress(lev+2,off+len,"Going to read displayed-on-first-page flag"); |
| 65 |
if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, |
| 66 |
&(*result)->on_first_page))) |
| 67 |
goto ERROR2; |
| 68 |
len += leng; |
| 69 |
|
| 70 |
psiconv_progress(lev+2,off+len,"Going to read three zero bytes"); |
| 71 |
for (i = 0; i < 0x03; i++,len++) { |
| 72 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
| 73 |
if (res) |
| 74 |
goto ERROR2; |
| 75 |
if (temp != 0x00) { |
| 76 |
psiconv_warn(lev+2,off+len, |
| 77 |
"Page Header unknown value in zero bytes section"); |
| 78 |
psiconv_debug(lev+2,off+len,"Byte %d: read %02x, expected %02x", |
| 79 |
i,temp,0x00); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
psiconv_progress(lev+2,off+len,"Going to read base paragraph layout"); |
| 84 |
if (!((*result)->base_paragraph_layout = psiconv_basic_paragraph_layout())) |
| 85 |
goto ERROR2; |
| 86 |
|
| 87 |
if (has_content) { |
| 88 |
if ((res = psiconv_parse_paragraph_layout_list(buf,lev+2,off+len,&leng, |
| 89 |
(*result)->base_paragraph_layout))) |
| 90 |
goto ERROR3; |
| 91 |
len += leng; |
| 92 |
} |
| 93 |
|
| 94 |
psiconv_progress(lev+2,off+len,"Going to read base character layout"); |
| 95 |
if (!((*result)->base_character_layout = psiconv_basic_character_layout())) |
| 96 |
goto ERROR3; |
| 97 |
if (has_content) { |
| 98 |
if ((res = psiconv_parse_character_layout_list(buf,lev+2,off+len,&leng, |
| 99 |
(*result)->base_character_layout))) |
| 100 |
goto ERROR4; |
| 101 |
} |
| 102 |
len += leng; |
| 103 |
|
| 104 |
|
| 105 |
psiconv_progress(lev+2,off+len,"Going to read the TextEd section"); |
| 106 |
if (has_content) { |
| 107 |
if ((res = psiconv_parse_texted_section(buf,lev+2,off+len,&leng, |
| 108 |
&(*result)->text, |
| 109 |
(*result)->base_character_layout, |
| 110 |
(*result)->base_paragraph_layout))) |
| 111 |
goto ERROR4; |
| 112 |
len += leng; |
| 113 |
} else { |
| 114 |
(*result)->text = NULL; |
| 115 |
} |
| 116 |
|
| 117 |
if (length) |
| 118 |
*length = len; |
| 119 |
|
| 120 |
psiconv_progress(lev+1,off+len-1,"End of page header" |
| 121 |
"(total length: %08x", len); |
| 122 |
|
| 123 |
return res; |
| 124 |
|
| 125 |
ERROR4: |
| 126 |
psiconv_free_character_layout((*result)->base_character_layout); |
| 127 |
ERROR3: |
| 128 |
psiconv_free_paragraph_layout((*result)->base_paragraph_layout); |
| 129 |
ERROR2: |
| 130 |
free (*result); |
| 131 |
ERROR1: |
| 132 |
psiconv_warn(lev+1,off,"Reading of Page Header failed"); |
| 133 |
if (length) |
| 134 |
*length = 0; |
| 135 |
if (!res) |
| 136 |
return -PSICONV_E_NOMEM; |
| 137 |
else |
| 138 |
return res; |
| 139 |
} |
| 140 |
|
| 141 |
int psiconv_parse_page_layout_section(const psiconv_buffer buf,int lev, |
| 142 |
psiconv_u32 off, int *length, |
| 143 |
psiconv_page_layout_section *result) |
| 144 |
{ |
| 145 |
int res = 0; |
| 146 |
int len = 0; |
| 147 |
int leng; |
| 148 |
psiconv_u32 temp; |
| 149 |
|
| 150 |
psiconv_progress(lev+1,off,"Going to read the page layout section"); |
| 151 |
if (!(*result = malloc(sizeof(**result)))) |
| 152 |
goto ERROR1; |
| 153 |
|
| 154 |
psiconv_progress(lev+2,off+len,"Going to read first page number"); |
| 155 |
(*result)->first_page_nr = psiconv_read_u32(buf,lev+2,off+len,&res); |
| 156 |
if (res) |
| 157 |
goto ERROR2; |
| 158 |
psiconv_debug(lev+2,off+len,"First page: %d",(*result)->first_page_nr); |
| 159 |
len += 4; |
| 160 |
|
| 161 |
psiconv_progress(lev+2,off+len,"Going to read header distance"); |
| 162 |
(*result)->header_dist = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 163 |
if (res) |
| 164 |
goto ERROR2; |
| 165 |
psiconv_debug(lev+2,off+len,"Header distance: %6.3f",(*result)->header_dist); |
| 166 |
len += leng; |
| 167 |
|
| 168 |
psiconv_progress(lev+2,off+len,"Going to read footer distance"); |
| 169 |
(*result)->footer_dist = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 170 |
if (res) |
| 171 |
goto ERROR2; |
| 172 |
psiconv_debug(lev+2,off+len,"Footer distance: %6.3f",(*result)->footer_dist); |
| 173 |
len += leng; |
| 174 |
|
| 175 |
psiconv_progress(lev+2,off+len,"Going to read the left margin"); |
| 176 |
(*result)->left_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 177 |
if (res) |
| 178 |
goto ERROR2; |
| 179 |
psiconv_debug(lev+2,off+len,"Left margin: %6.3f",(*result)->left_margin); |
| 180 |
len += leng; |
| 181 |
|
| 182 |
psiconv_progress(lev+2,off+len,"Going read the to right margin"); |
| 183 |
(*result)->right_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 184 |
if (res) |
| 185 |
goto ERROR2; |
| 186 |
psiconv_debug(lev+2,off+len,"Right margin: %6.3f",(*result)->right_margin); |
| 187 |
len += leng; |
| 188 |
|
| 189 |
psiconv_progress(lev+2,off+len,"Going to read the top margin"); |
| 190 |
(*result)->top_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 191 |
if (res) |
| 192 |
goto ERROR2; |
| 193 |
psiconv_debug(lev+2,off+len,"Top margin: %6.3f",(*result)->top_margin); |
| 194 |
len += leng; |
| 195 |
|
| 196 |
psiconv_progress(lev+2,off+len,"Going to read the bottom margin"); |
| 197 |
(*result)->bottom_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 198 |
if (res) |
| 199 |
goto ERROR2; |
| 200 |
psiconv_debug(lev+2,off+len,"Bottom margin: %6.3f",(*result)->bottom_margin); |
| 201 |
len += leng; |
| 202 |
|
| 203 |
psiconv_progress(lev+2,off+len,"Going to read the header"); |
| 204 |
if ((res = psiconv_parse_page_header(buf,lev+2,off+len,&leng, |
| 205 |
&(*result)->header))) |
| 206 |
goto ERROR2; |
| 207 |
len += leng; |
| 208 |
|
| 209 |
psiconv_progress(lev+2,off+len,"Going to read the footer"); |
| 210 |
if ((res = psiconv_parse_page_header(buf,lev+2,off+len,&leng, |
| 211 |
&(*result)->footer))) |
| 212 |
goto ERROR3; |
| 213 |
len += leng; |
| 214 |
|
| 215 |
psiconv_progress(lev+2,off+len,"Going to read page dimensions id"); |
| 216 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
| 217 |
if (res) |
| 218 |
goto ERROR4; |
| 219 |
if ((temp != PSICONV_ID_PAGE_DIMENSIONS1) && |
| 220 |
(temp != PSICONV_ID_PAGE_DIMENSIONS2)) { |
| 221 |
psiconv_warn(lev+2,off+len, |
| 222 |
"Page layout section page dimensions marker not found"); |
| 223 |
psiconv_debug(lev+2,off+len, |
| 224 |
"Page dimensions marker: read %08x, expected %08x or %08x", |
| 225 |
temp, PSICONV_ID_PAGE_DIMENSIONS1, |
| 226 |
PSICONV_ID_PAGE_DIMENSIONS2); |
| 227 |
} |
| 228 |
len += 4; |
| 229 |
|
| 230 |
psiconv_progress(lev+2,off+len,"Going to read the page width"); |
| 231 |
(*result)->page_width = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 232 |
if (res) |
| 233 |
goto ERROR4; |
| 234 |
psiconv_debug(lev+2,off+len,"Page width: %6.3f",(*result)->page_width); |
| 235 |
len += leng; |
| 236 |
|
| 237 |
psiconv_progress(lev+2,off+len,"Going to read the page height"); |
| 238 |
(*result)->page_height = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
| 239 |
if (res) |
| 240 |
goto ERROR4; |
| 241 |
psiconv_debug(lev+2,off+len,"Page height: %6.3f",(*result)->page_height); |
| 242 |
len += leng; |
| 243 |
|
| 244 |
psiconv_progress(lev+2,off+len,"Going to read page portrait/landscape"); |
| 245 |
if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,&(*result)->landscape))) |
| 246 |
goto ERROR4; |
| 247 |
psiconv_debug(lev+2,off+len,"Landscape: %d",(*result)->landscape); |
| 248 |
len += leng; |
| 249 |
|
| 250 |
|
| 251 |
if (length) |
| 252 |
*length = len; |
| 253 |
|
| 254 |
psiconv_progress(lev,off+len-1,"End of page section (total length: %08x)", |
| 255 |
len); |
| 256 |
|
| 257 |
return res; |
| 258 |
|
| 259 |
ERROR4: |
| 260 |
psiconv_free_page_header((*result)->footer); |
| 261 |
ERROR3: |
| 262 |
psiconv_free_page_header((*result)->header); |
| 263 |
ERROR2: |
| 264 |
free (*result); |
| 265 |
ERROR1: |
| 266 |
psiconv_warn(lev+1,off,"Reading of Page Section failed"); |
| 267 |
if (length) |
| 268 |
*length = 0; |
| 269 |
if (!res) |
| 270 |
return -PSICONV_E_NOMEM; |
| 271 |
else |
| 272 |
return res; |
| 273 |
} |