1 | /* |
1 | /* |
2 | parse_page.c - Part of psiconv, a PSION 5 file formats converter |
2 | parse_page.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999-2005 Frodo Looijaard <frodo@frodo.looijaard.name> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
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 | |
27 | int psiconv_parse_page_header(const psiconv_buffer buf,int lev,psiconv_u32 off, |
29 | #ifdef DMALLOC |
|
|
30 | #include <dmalloc.h> |
|
|
31 | #endif |
|
|
32 | |
|
|
33 | |
|
|
34 | int 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 | if (!(*result = malloc(sizeof(**result)))) |
44 | if (!(*result = malloc(sizeof(**result)))) |
37 | goto ERROR1; |
45 | goto ERROR1; |
38 | |
46 | |
39 | psiconv_progress(lev+2,off+len, |
47 | psiconv_progress(config,lev+2,off+len, |
40 | "Going to read the first byte (0x01 expected)"); |
48 | "Going to read the has_content flag"); |
41 | temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
49 | temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
42 | if (res) |
50 | if (res) |
43 | goto ERROR2; |
51 | goto ERROR2; |
44 | if (temp != 0x01) { |
52 | if (temp == 0x00) |
45 | psiconv_warn(lev+2,off+len,"Page header first byte mismatch"); |
53 | has_content = 0; |
46 | psiconv_debug(lev+2,off+len,"First byte: read %02x, expected %02x", |
54 | else if (temp == 0x01) |
47 | temp,0x01); |
55 | has_content = 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; |
48 | } |
61 | } |
|
|
62 | psiconv_debug(config,lev+2,off+len,"Has_content flag: %02x",has_content); |
49 | len += 1; |
63 | len += 1; |
50 | |
64 | |
51 | 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"); |
52 | if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, |
66 | if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng, |
53 | &(*result)->on_first_page))) |
67 | &(*result)->on_first_page))) |
54 | goto ERROR2; |
68 | goto ERROR2; |
55 | len += leng; |
69 | len += leng; |
56 | |
70 | |
57 | 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"); |
58 | for (i = 0; i < 0x03; i++,len++) { |
72 | for (i = 0; i < 0x03; i++,len++) { |
59 | temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
73 | temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
60 | if (res) |
74 | if (res) |
61 | goto ERROR2; |
75 | goto ERROR2; |
62 | if (temp != 0x00) { |
76 | if (temp != 0x00) { |
63 | psiconv_warn(lev+2,off+len, |
77 | psiconv_warn(config,lev+2,off+len, |
64 | "Page Header unknown value in zero bytes section"); |
78 | "Page Header unknown value in zero bytes section"); |
65 | 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", |
66 | i,temp,0x00); |
80 | i,temp,0x00); |
67 | } |
81 | } |
68 | } |
82 | } |
69 | |
83 | |
70 | 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"); |
71 | if (!((*result)->base_paragraph_layout = psiconv_basic_paragraph_layout())) |
85 | if (!((*result)->base_paragraph_layout = psiconv_basic_paragraph_layout())) |
72 | goto ERROR2; |
86 | goto ERROR2; |
|
|
87 | |
|
|
88 | if (has_content) { |
73 | if ((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, |
74 | (*result)->base_paragraph_layout))) |
90 | (*result)->base_paragraph_layout))) |
75 | goto ERROR3; |
91 | goto ERROR3; |
76 | len += leng; |
92 | len += leng; |
|
|
93 | } |
77 | |
94 | |
78 | 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"); |
79 | if (!((*result)->base_character_layout = psiconv_basic_character_layout())) |
96 | if (!((*result)->base_character_layout = psiconv_basic_character_layout())) |
80 | goto ERROR3; |
97 | goto ERROR3; |
|
|
98 | if (has_content) { |
81 | if ((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, |
82 | (*result)->base_character_layout))) |
100 | (*result)->base_character_layout))) |
83 | goto ERROR4; |
101 | goto ERROR4; |
|
|
102 | } |
84 | len += leng; |
103 | len += leng; |
85 | |
104 | |
86 | |
105 | |
87 | 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) { |
88 | if ((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, |
89 | &(*result)->text, |
109 | &(*result)->text, |
90 | (*result)->base_character_layout, |
110 | (*result)->base_character_layout, |
91 | (*result)->base_paragraph_layout))) |
111 | (*result)->base_paragraph_layout))) |
92 | goto ERROR4; |
112 | goto ERROR4; |
93 | len += leng; |
113 | len += leng; |
|
|
114 | } else { |
|
|
115 | (*result)->text = NULL; |
|
|
116 | } |
94 | |
117 | |
95 | if (length) |
118 | if (length) |
96 | *length = len; |
119 | *length = len; |
97 | |
120 | |
98 | psiconv_progress(lev+1,off+len-1,"End of page header" |
121 | psiconv_progress(config,lev+1,off+len-1,"End of page header" |
99 | "(total length: %08x", len); |
122 | "(total length: %08x", len); |
100 | |
123 | |
101 | return res; |
124 | return res; |
102 | |
125 | |
103 | ERROR4: |
126 | ERROR4: |
… | |
… | |
105 | ERROR3: |
128 | ERROR3: |
106 | psiconv_free_paragraph_layout((*result)->base_paragraph_layout); |
129 | psiconv_free_paragraph_layout((*result)->base_paragraph_layout); |
107 | ERROR2: |
130 | ERROR2: |
108 | free (*result); |
131 | free (*result); |
109 | ERROR1: |
132 | ERROR1: |
110 | psiconv_warn(lev+1,off,"Reading of Page Header failed"); |
133 | psiconv_error(config,lev+1,off,"Reading of Page Header failed"); |
111 | if (length) |
134 | if (length) |
112 | *length = 0; |
135 | *length = 0; |
113 | if (!res) |
136 | if (!res) |
114 | return -PSICONV_E_NOMEM; |
137 | return -PSICONV_E_NOMEM; |
115 | else |
138 | else |
116 | return res; |
139 | return res; |
117 | } |
140 | } |
118 | |
141 | |
119 | int psiconv_parse_page_layout_section(const psiconv_buffer buf,int lev, |
142 | int psiconv_parse_page_layout_section(const psiconv_config config, |
|
|
143 | const psiconv_buffer buf,int lev, |
120 | psiconv_u32 off, int *length, |
144 | psiconv_u32 off, int *length, |
121 | psiconv_page_layout_section *result) |
145 | psiconv_page_layout_section *result) |
122 | { |
146 | { |
123 | int res = 0; |
147 | int res = 0; |
124 | int len = 0; |
148 | int len = 0; |
125 | int leng; |
149 | int leng; |
126 | psiconv_u32 temp; |
150 | psiconv_u32 temp; |
127 | |
151 | |
128 | 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"); |
129 | if (!(*result = malloc(sizeof(**result)))) |
153 | if (!(*result = malloc(sizeof(**result)))) |
130 | goto ERROR1; |
154 | goto ERROR1; |
131 | |
155 | |
132 | 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"); |
133 | (*result)->first_page_nr = psiconv_read_u32(buf,lev+2,off+len,&res); |
157 | (*result)->first_page_nr = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
134 | if (res) |
158 | if (res) |
135 | goto ERROR2; |
159 | goto ERROR2; |
136 | 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); |
137 | len += 4; |
161 | len += 4; |
138 | |
162 | |
139 | psiconv_progress(lev+2,off+len,"Going to read header distance"); |
163 | psiconv_progress(config,lev+2,off+len,"Going to read header distance"); |
140 | (*result)->header_dist = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
164 | (*result)->header_dist = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
141 | if (res) |
165 | if (res) |
142 | goto ERROR2; |
166 | goto ERROR2; |
143 | 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); |
144 | len += leng; |
168 | len += leng; |
145 | |
169 | |
146 | psiconv_progress(lev+2,off+len,"Going to read footer distance"); |
170 | psiconv_progress(config,lev+2,off+len,"Going to read footer distance"); |
147 | (*result)->footer_dist = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
171 | (*result)->footer_dist = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
148 | if (res) |
172 | if (res) |
149 | goto ERROR2; |
173 | goto ERROR2; |
150 | 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); |
151 | len += leng; |
175 | len += leng; |
152 | |
176 | |
153 | 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"); |
154 | (*result)->left_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
178 | (*result)->left_margin = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
155 | if (res) |
179 | if (res) |
156 | goto ERROR2; |
180 | goto ERROR2; |
157 | 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); |
158 | len += leng; |
182 | len += leng; |
159 | |
183 | |
160 | 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"); |
161 | (*result)->right_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
185 | (*result)->right_margin = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
162 | if (res) |
186 | if (res) |
163 | goto ERROR2; |
187 | goto ERROR2; |
164 | 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); |
165 | len += leng; |
189 | len += leng; |
166 | |
190 | |
167 | 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"); |
168 | (*result)->top_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
192 | (*result)->top_margin = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
169 | if (res) |
193 | if (res) |
170 | goto ERROR2; |
194 | goto ERROR2; |
171 | 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); |
172 | len += leng; |
196 | len += leng; |
173 | |
197 | |
174 | 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"); |
175 | (*result)->bottom_margin = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
199 | (*result)->bottom_margin = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
176 | if (res) |
200 | if (res) |
177 | goto ERROR2; |
201 | goto ERROR2; |
178 | 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); |
179 | len += leng; |
203 | len += leng; |
180 | |
204 | |
181 | psiconv_progress(lev+2,off+len,"Going to read the header"); |
205 | psiconv_progress(config,lev+2,off+len,"Going to read the header"); |
182 | if ((res = psiconv_parse_page_header(buf,lev+2,off+len,&leng, |
206 | if ((res = psiconv_parse_page_header(config,buf,lev+2,off+len,&leng, |
183 | &(*result)->header))) |
207 | &(*result)->header))) |
184 | goto ERROR2; |
208 | goto ERROR2; |
185 | len += leng; |
209 | len += leng; |
186 | |
210 | |
187 | psiconv_progress(lev+2,off+len,"Going to read the footer"); |
211 | psiconv_progress(config,lev+2,off+len,"Going to read the footer"); |
188 | if ((res = psiconv_parse_page_header(buf,lev+2,off+len,&leng, |
212 | if ((res = psiconv_parse_page_header(config,buf,lev+2,off+len,&leng, |
189 | &(*result)->footer))) |
213 | &(*result)->footer))) |
190 | goto ERROR3; |
214 | goto ERROR3; |
191 | len += leng; |
215 | len += leng; |
192 | |
216 | |
193 | 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"); |
194 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
218 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
195 | if (res) |
219 | if (res) |
196 | goto ERROR4; |
220 | goto ERROR4; |
197 | if (temp != PSICONV_ID_PAGE_DIMENSIONS) { |
221 | if ((temp != PSICONV_ID_PAGE_DIMENSIONS1) && |
|
|
222 | (temp != PSICONV_ID_PAGE_DIMENSIONS2)) { |
198 | psiconv_warn(lev+2,off+len, |
223 | psiconv_warn(config,lev+2,off+len, |
199 | "Page layout section page dimensions marker not found"); |
224 | "Page layout section page dimensions marker not found"); |
200 | psiconv_debug(lev+2,off+len, |
225 | psiconv_debug(config,lev+2,off+len, |
201 | "Page dimensions marker: read %08x, expected %08x",temp, |
226 | "Page dimensions marker: read %08x, expected %08x or %08x", |
|
|
227 | temp, PSICONV_ID_PAGE_DIMENSIONS1, |
202 | PSICONV_ID_PAGE_DIMENSIONS); |
228 | PSICONV_ID_PAGE_DIMENSIONS2); |
203 | } |
229 | } |
204 | len += 4; |
230 | len += 4; |
205 | |
231 | |
206 | 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"); |
207 | (*result)->page_width = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
233 | (*result)->page_width = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
208 | if (res) |
234 | if (res) |
209 | goto ERROR4; |
235 | goto ERROR4; |
210 | 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); |
211 | len += leng; |
237 | len += leng; |
212 | |
238 | |
213 | 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"); |
214 | (*result)->page_height = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
240 | (*result)->page_height = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
215 | if (res) |
241 | if (res) |
216 | goto ERROR4; |
242 | goto ERROR4; |
217 | 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); |
218 | 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 | |
219 | |
252 | |
220 | if (length) |
253 | if (length) |
221 | *length = len; |
254 | *length = len; |
222 | |
255 | |
223 | 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)", |
224 | len); |
257 | len); |
225 | |
258 | |
226 | return res; |
259 | return res; |
227 | |
260 | |
228 | ERROR4: |
261 | ERROR4: |
… | |
… | |
230 | ERROR3: |
263 | ERROR3: |
231 | psiconv_free_page_header((*result)->header); |
264 | psiconv_free_page_header((*result)->header); |
232 | ERROR2: |
265 | ERROR2: |
233 | free (*result); |
266 | free (*result); |
234 | ERROR1: |
267 | ERROR1: |
235 | psiconv_warn(lev+1,off,"Reading of Page Section failed"); |
268 | psiconv_error(config,lev+1,off,"Reading of Page Section failed"); |
236 | if (length) |
269 | if (length) |
237 | *length = 0; |
270 | *length = 0; |
238 | if (!res) |
271 | if (!res) |
239 | return -PSICONV_E_NOMEM; |
272 | return -PSICONV_E_NOMEM; |
240 | else |
273 | else |