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