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

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

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