/[public]/psiconv/trunk/lib/psiconv/parse_page.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/parse_page.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.63  
changed lines
  Added in v.140

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26