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