1 |
/* |
2 |
parse_word.c - Part of psiconv, a PSION 5 file formats converter |
3 |
Copyright (c) 1999-2005 Frodo Looijaard <frodo@frodo.looijaard.name> |
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 |
|
25 |
#include "parse_routines.h" |
26 |
#include "error.h" |
27 |
|
28 |
#ifdef DMALLOC |
29 |
#include <dmalloc.h> |
30 |
#endif |
31 |
|
32 |
int psiconv_parse_word_status_section(const psiconv_config config, |
33 |
const psiconv_buffer buf, int lev, |
34 |
psiconv_u32 off, int *length, |
35 |
psiconv_word_status_section *result) |
36 |
{ |
37 |
int res=0; |
38 |
int len=0; |
39 |
psiconv_u32 temp; |
40 |
int leng; |
41 |
|
42 |
psiconv_progress(config,lev+1,off,"Going to read the word status section"); |
43 |
if (!(*result = malloc(sizeof(**result)))) |
44 |
goto ERROR1; |
45 |
|
46 |
psiconv_progress(config,lev+2,off+len, |
47 |
"Going to read the initial byte (%02x expected)",0x02); |
48 |
temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
49 |
if (res) |
50 |
goto ERROR2; |
51 |
if (temp != 0x02) { |
52 |
psiconv_warn(config,lev+2,off+len, |
53 |
"Word status section initial byte unknown value (ignored)"); |
54 |
psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp); |
55 |
} |
56 |
len ++; |
57 |
|
58 |
psiconv_progress(config,lev+2,off+len, |
59 |
"Going to read the first byte of display flags"); |
60 |
temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
61 |
if (res) |
62 |
goto ERROR2; |
63 |
|
64 |
(*result)->show_tabs = temp&0x01 ? psiconv_bool_true : psiconv_bool_false; |
65 |
psiconv_debug(config,lev+2,off+len,"Show tabs: %02x",(*result)->show_tabs); |
66 |
(*result)->show_spaces = temp&0x02 ? psiconv_bool_true : psiconv_bool_false; |
67 |
psiconv_debug(config,lev+2,off+len,"Show spaces: %02x",(*result)->show_spaces); |
68 |
(*result)->show_paragraph_ends = temp &0x04 ? psiconv_bool_true : |
69 |
psiconv_bool_false; |
70 |
psiconv_debug(config,lev+2,off+len,"Show paragraph ends: %02x", |
71 |
(*result)->show_paragraph_ends); |
72 |
(*result)->show_line_breaks = temp & 0x08 ? psiconv_bool_true : |
73 |
psiconv_bool_false; |
74 |
psiconv_debug(config,lev+2,off+len,"Show line breaks: %02x", |
75 |
(*result)->show_line_breaks); |
76 |
(*result)->show_hard_minus = temp & 0x20 ? psiconv_bool_true : |
77 |
psiconv_bool_false; |
78 |
psiconv_debug(config,lev+2,off+len,"Show hard minus: %02x", |
79 |
(*result)->show_hard_minus); |
80 |
(*result)->show_hard_space = temp & 0x40 ? psiconv_bool_true : |
81 |
psiconv_bool_false; |
82 |
psiconv_debug(config,lev+2,off+len,"Show hard space: %02x", |
83 |
(*result)->show_hard_space); |
84 |
if (temp & 0x90) { |
85 |
psiconv_warn(config,lev+2,off+len,"Word status section first byte of display " |
86 |
"flags contains unknown flags (ignored)"); |
87 |
psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0x90); |
88 |
} |
89 |
len ++; |
90 |
|
91 |
psiconv_progress(config,lev+2,off+len,"Going to read second byte of display flags"); |
92 |
temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
93 |
if (res) |
94 |
goto ERROR2; |
95 |
|
96 |
(*result)->show_full_pictures = temp & 0x01 ? psiconv_bool_true : |
97 |
psiconv_bool_false; |
98 |
psiconv_debug(config,lev+2,off+len,"Show full pictures: %02x", |
99 |
(*result)->show_full_pictures); |
100 |
(*result)->show_full_graphs = temp & 0x02 ? psiconv_bool_true : |
101 |
psiconv_bool_false; |
102 |
psiconv_debug(config,lev+2,off+len,"Show full graphs: %02x", |
103 |
(*result)->show_full_graphs); |
104 |
if (temp & 0xfc) { |
105 |
psiconv_warn(config,lev+2,off+len,"Word status section second byte of display " |
106 |
"flags contains unknown flags (ignored)"); |
107 |
psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); |
108 |
} |
109 |
len ++; |
110 |
|
111 |
psiconv_progress(config,lev+2,off+len,"Going to read top toolbar setting"); |
112 |
if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng, |
113 |
&(*result)->show_top_toolbar))) |
114 |
goto ERROR2; |
115 |
len += leng; |
116 |
|
117 |
psiconv_progress(config,lev+2,off+len,"Going to read side toolbar setting"); |
118 |
if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng, |
119 |
&(*result)->show_side_toolbar))) |
120 |
goto ERROR2; |
121 |
len += leng; |
122 |
|
123 |
psiconv_progress(config,lev+2,off+len,"Going to read operational flags"); |
124 |
temp = psiconv_read_u8(config,buf,lev+2,off+len,&res); |
125 |
if (res) |
126 |
goto ERROR2; |
127 |
(*result)->fit_lines_to_screen = temp & 0x08 ? psiconv_bool_true : |
128 |
psiconv_bool_false; |
129 |
psiconv_debug(config,lev+2,off+len,"Fit lines to screen: %02x", |
130 |
(*result)->fit_lines_to_screen); |
131 |
if (temp & 0xf7) { |
132 |
psiconv_warn(config,lev+2,off+len,"Word status section operational flags " |
133 |
"contains unknown flags (ignored)"); |
134 |
psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xfc); |
135 |
} |
136 |
len ++; |
137 |
|
138 |
psiconv_progress(config,lev+2,off+len,"Going to read cursor position"); |
139 |
(*result)->cursor_position = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
140 |
if (res) |
141 |
goto ERROR2; |
142 |
psiconv_debug(config,lev+2,off+len,"Cursor position: %08x", |
143 |
(*result)->cursor_position); |
144 |
len += 0x04; |
145 |
|
146 |
psiconv_progress(config,lev+2,off+len,"Going to read display size"); |
147 |
(*result)->display_size = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
148 |
if (res) |
149 |
goto ERROR2; |
150 |
psiconv_debug(config,lev+2,off+len,"Display size: %08x", |
151 |
(*result)->display_size); |
152 |
len += 0x04; |
153 |
|
154 |
if (length) |
155 |
*length = len; |
156 |
|
157 |
psiconv_progress(config,lev,off+len-1, |
158 |
"End of word status section (total length: %08x)", len); |
159 |
return 0; |
160 |
|
161 |
ERROR2: |
162 |
free (*result); |
163 |
ERROR1: |
164 |
psiconv_error(config,lev+1,off,"Reading of Word Status Section failed"); |
165 |
if (length) |
166 |
*length = 0; |
167 |
if (!res) |
168 |
return -PSICONV_E_NOMEM; |
169 |
else |
170 |
return res; |
171 |
} |
172 |
|
173 |
int psiconv_parse_word_styles_section(const psiconv_config config, |
174 |
const psiconv_buffer buf, int lev, |
175 |
psiconv_u32 off, int *length, |
176 |
psiconv_word_styles_section *result) |
177 |
{ |
178 |
int res=0; |
179 |
int len=0; |
180 |
int leng,i,nr,j; |
181 |
psiconv_word_style style; |
182 |
psiconv_u32 temp; |
183 |
|
184 |
psiconv_progress(config,lev+1,off,"Going to read the word styles section"); |
185 |
if (!(*result = malloc(sizeof(**result)))) |
186 |
goto ERROR1; |
187 |
|
188 |
psiconv_progress(config,lev+2,off+len,"Going to read style normal"); |
189 |
if (!(style = malloc(sizeof(*style)))) |
190 |
goto ERROR2; |
191 |
style->name = NULL; |
192 |
if (!(style->paragraph = psiconv_basic_paragraph_layout())) |
193 |
goto ERROR2_1; |
194 |
psiconv_progress(config,lev+3,off+len,"Going to read the paragraph codes"); |
195 |
if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+3,off+len,&leng, |
196 |
style->paragraph))) |
197 |
goto ERROR2_2; |
198 |
len += leng; |
199 |
psiconv_progress(config,lev+3,off+len,"Going to read the character codes"); |
200 |
if (!(style->character = psiconv_basic_character_layout())) |
201 |
goto ERROR2_2; |
202 |
if ((res = psiconv_parse_character_layout_list(config,buf,lev+3,off+len,&leng, |
203 |
style->character))) |
204 |
goto ERROR2_3; |
205 |
len += leng; |
206 |
/* Ugly: I really don't know whether this is right for UTF8 */ |
207 |
psiconv_progress(config,lev+3,off+len,"Going to read the hotkey"); |
208 |
style->hotkey = psiconv_unicode_read_char(config,buf,lev+3,off+len,NULL,&res); |
209 |
psiconv_debug(config,lev+3,off+len,"Normal Hotkey value %08x",style->hotkey); |
210 |
if (res) |
211 |
goto ERROR2_3; |
212 |
len += 0x04; |
213 |
(*result)->normal = style; |
214 |
|
215 |
psiconv_progress(config,lev+2,off+len,"Going to read hotkeys list"); |
216 |
if (!((*result)->styles = psiconv_list_new(sizeof(*style)))) |
217 |
goto ERROR3; |
218 |
if (!(style = malloc(sizeof(*style)))) { |
219 |
goto ERROR3_1; |
220 |
} |
221 |
|
222 |
psiconv_progress(config,lev+3,off+len,"Going to read the number of entries"); |
223 |
nr = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
224 |
if (res) |
225 |
goto ERROR3_2; |
226 |
len ++; |
227 |
psiconv_debug(config,lev+3,off+len,"Nummer of hotkeys: %02x",nr); |
228 |
for (i = 0; i < nr; i ++) { |
229 |
/* Ugly: I really don't know whether this is right for UTF8 */ |
230 |
style->hotkey = psiconv_unicode_read_char(config,buf,lev+3,off+len, |
231 |
NULL,&res); |
232 |
psiconv_debug(config,lev+3,off+len,"Hotkey %d value %08x",i,style->hotkey); |
233 |
len += 0x04; |
234 |
if ((res = psiconv_list_add((*result)->styles,style))) |
235 |
goto ERROR3_2; |
236 |
} |
237 |
free(style); |
238 |
|
239 |
psiconv_progress(config,lev+2,off+len,"Going to read all other styles"); |
240 |
psiconv_progress(config,lev+2,off+len,"Going to read the number of styles"); |
241 |
nr = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
242 |
if (res) |
243 |
goto ERROR4; |
244 |
if (nr != psiconv_list_length((*result)->styles)) { |
245 |
psiconv_warn(config,lev+3,off+len,"Number of styles and hotkeys do not match"); |
246 |
psiconv_debug(config,lev+3,off+len,"%d hotkeys, %d styles", |
247 |
psiconv_list_length((*result)->styles), nr); |
248 |
} |
249 |
len ++; |
250 |
|
251 |
for (i = 0; i < nr; i++) { |
252 |
psiconv_progress(config,lev+2,off+len,"Next style: %d",i); |
253 |
if (i >= psiconv_list_length((*result)->styles)) { |
254 |
if (!(style = malloc(sizeof(*style)))) |
255 |
goto ERROR5; |
256 |
style->hotkey = 0; |
257 |
if (psiconv_list_add((*result)->styles,style)) { |
258 |
free(style); |
259 |
goto ERROR5; |
260 |
} |
261 |
psiconv_debug(config,lev+3,off+len,"New entry added in list"); |
262 |
free(style); |
263 |
} |
264 |
if (!(style = psiconv_list_get((*result)->styles,i))) |
265 |
goto ERROR5; |
266 |
psiconv_progress(config,lev+3,off+len,"Going to read the style name"); |
267 |
style->name = psiconv_read_string(config,buf,lev+3,off+len,&leng,&res); |
268 |
if (res) |
269 |
goto ERROR5; |
270 |
len += leng; |
271 |
psiconv_progress(config,lev+3,off+len, |
272 |
"Going to read whether this style is built-in"); |
273 |
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
274 |
if (res) |
275 |
goto ERROR6; |
276 |
|
277 |
if (temp == PSICONV_ID_STYLE_BUILT_IN) { |
278 |
style->built_in = psiconv_bool_true; |
279 |
psiconv_debug(config,lev+3,off+len,"Built-in style"); |
280 |
} else if (temp == PSICONV_ID_STYLE_REMOVABLE) { |
281 |
style->built_in = psiconv_bool_false; |
282 |
psiconv_debug(config,lev+3,off+len,"Removable style"); |
283 |
} else { |
284 |
psiconv_warn(config,lev+3,off+len, |
285 |
"Word styles section unknown style id (treated as built-in)"); |
286 |
psiconv_debug(config,lev+3,off+len,"Unknown id: %08x",temp); |
287 |
style->built_in = psiconv_bool_false; |
288 |
} |
289 |
len += 4; |
290 |
psiconv_progress(config,lev+3,off+len,"Going to read outline level"); |
291 |
style->outline_level = psiconv_read_u32(config,buf,lev+3,off+len,&res); |
292 |
if (res) |
293 |
goto ERROR6; |
294 |
psiconv_debug(config,lev+3,off+len,"Outline Level: %08x", style->outline_level); |
295 |
len += 4; |
296 |
psiconv_progress(config,lev+3,off+len,"Going to read the character codes"); |
297 |
if (!(style->character = psiconv_clone_character_layout((*result)->normal->character))) |
298 |
goto ERROR6; |
299 |
if ((res = psiconv_parse_character_layout_list(config,buf,lev+3,off+len,&leng, |
300 |
style->character))) |
301 |
goto ERROR7; |
302 |
len += leng; |
303 |
psiconv_progress(config,lev+3,off+len,"Going to read the paragraph codes"); |
304 |
if (!(style->paragraph = psiconv_clone_paragraph_layout((*result)->normal->paragraph))) |
305 |
goto ERROR7; |
306 |
if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+3,off+len,&leng, |
307 |
style->paragraph))) |
308 |
goto ERROR8; |
309 |
len += leng; |
310 |
} |
311 |
|
312 |
psiconv_progress(config,lev+2,off+len,"Reading trailing bytes"); |
313 |
for (i = 0; i < psiconv_list_length((*result)->styles); i++) { |
314 |
temp = psiconv_read_u8(config,buf,lev+3,off+len,&res); |
315 |
if (res) |
316 |
goto ERROR4; |
317 |
if (temp != 0xff) { |
318 |
psiconv_warn(config,lev+3,off+len,"Unknown trailing style byte"); |
319 |
psiconv_debug(config,lev+3,off+len,"Trailing byte: %02x expected, read %02x", |
320 |
0xff,temp); |
321 |
} else |
322 |
psiconv_debug(config,lev+3,off+len,"Read trailing byte 0xff"); |
323 |
len++; |
324 |
} |
325 |
|
326 |
if (length) |
327 |
*length = len; |
328 |
|
329 |
psiconv_progress(config,lev,off+len-1, |
330 |
"End of word styles section (total length: %08x)", len); |
331 |
|
332 |
return res; |
333 |
|
334 |
|
335 |
ERROR3_2: |
336 |
free(style); |
337 |
ERROR3_1: |
338 |
psiconv_list_free((*result)->styles); |
339 |
goto ERROR3; |
340 |
|
341 |
ERROR2_3: |
342 |
psiconv_free_character_layout(style->character); |
343 |
ERROR2_2: |
344 |
psiconv_free_paragraph_layout(style->paragraph); |
345 |
ERROR2_1: |
346 |
free (style); |
347 |
goto ERROR2; |
348 |
|
349 |
ERROR8: |
350 |
psiconv_free_paragraph_layout(style->paragraph); |
351 |
ERROR7: |
352 |
psiconv_free_character_layout(style->character); |
353 |
ERROR6: |
354 |
free(style->name); |
355 |
ERROR5: |
356 |
for (j = 0; j < i ;j++) { |
357 |
if (!(style = psiconv_list_get((*result)->styles,j))) { |
358 |
psiconv_error(config,lev+1,off,"Data structure corruption"); |
359 |
goto ERROR4; |
360 |
} |
361 |
psiconv_free_character_layout(style->character); |
362 |
psiconv_free_paragraph_layout(style->paragraph); |
363 |
free(style->name); |
364 |
} |
365 |
ERROR4: |
366 |
psiconv_list_free((*result)->styles); |
367 |
ERROR3: |
368 |
psiconv_free_word_style((*result)->normal); |
369 |
ERROR2: |
370 |
free (*result); |
371 |
ERROR1: |
372 |
psiconv_error(config,lev+1,off,"Reading of Word Status Section failed"); |
373 |
if (length) |
374 |
*length = 0; |
375 |
if (!res) |
376 |
return -PSICONV_E_NOMEM; |
377 |
else |
378 |
return res; |
379 |
} |
380 |
|
381 |
|
382 |
|