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