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