/[public]/psiconv/trunk/lib/psiconv/parse_common.c
ViewVC logotype

Contents of /psiconv/trunk/lib/psiconv/parse_common.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations)
Fri Dec 3 00:59:12 1999 UTC (24 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 22313 byte(s)
(Frodo) Some base work for ClipArt files

1 /*
2 parse_common.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
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 #include <stdlib.h>
23 #include <string.h>
24
25 #include "data.h"
26 #include "parse_routines.h"
27
28 static int psiconv_parse_layout_section(const psiconv_buffer buf,
29 int lev,psiconv_u32 off,
30 int *length,
31 psiconv_text_and_layout result,
32 psiconv_word_styles_section styles,
33 int with_styles);
34
35 int psiconv_parse_header_section(const psiconv_buffer buf,int lev,
36 psiconv_u32 off, int *length,
37 psiconv_header_section *result)
38 {
39 int res=0;
40 int len=0;
41 psiconv_u32 temp;
42
43 psiconv_progress(lev+1,off+len,"Going to read the header section");
44 (*result) = malloc(sizeof(**result));
45
46 psiconv_progress(lev+2,off+len,"Going to read UID1 to UID3");
47 (*result)->uid1 = psiconv_read_u32(buf,lev+2,off+len);
48 psiconv_debug(lev+2,off+len,"UID1: %08x",(*result)->uid1);
49 if ((*result)->uid1 == PSICONV_ID_CLIPART) {
50 /* That's all folks... */
51 (*result)->file = psiconv_clipart_file;
52 (*result)->uid2 = 0;
53 (*result)->uid3 = 0;
54 (*result)->checksum = 0;
55 len += 4;
56 goto DONE;
57 }
58 if ((*result)->uid1 != PSICONV_ID_PSION5) {
59 psiconv_warn(lev+2,off+len,"UID1 has unknown value. This is probably "
60 "not a (parsable) Psion 5 file");
61 res = -1;
62 }
63 len += 4;
64 (*result)->uid2 = psiconv_read_u32(buf,lev+2,off+len);
65 psiconv_debug(lev+2,off+len,"UID2: %08x",(*result)->uid2);
66 len += 4;
67 (*result)->uid3 = psiconv_read_u32(buf,lev+2,off+len);
68 psiconv_debug(lev+2,off+len,"UID3: %08x",(*result)->uid3);
69 len += 4;
70
71 (*result)->file = psiconv_unknown_file;
72 if ((*result)->uid1 == PSICONV_ID_PSION5) {
73 if ((*result)->uid2 == PSICONV_ID_DATA_FILE) {
74 if ((*result)->uid3 == PSICONV_ID_WORD) {
75 (*result)->file = psiconv_word_file;
76 psiconv_debug(lev+2,off+len,"File is a Word file");
77 } else if ((*result)->uid3 == PSICONV_ID_TEXTED) {
78 (*result)->file = psiconv_texted_file;
79 psiconv_debug(lev+2,off+len,"File is a TextEd file");
80 } else if ((*result)->uid3 == PSICONV_ID_SKETCH) {
81 (*result)->file = psiconv_sketch_file;
82 psiconv_debug(lev+2,off+len,"File is a Sketch file");
83 }
84 } else if ((*result)->uid2 == PSICONV_ID_MBM_FILE) {
85 (*result)->file = psiconv_mbm_file;
86 if ((*result)->uid3 != 0x00)
87 psiconv_warn(lev+2,off+len,"UID3 set in MBM file?!?");
88 psiconv_debug(lev+2,off+len,"File is a MBM file");
89 }
90 }
91 if ((*result)->file == psiconv_unknown_file) {
92 psiconv_warn(lev+2,off+len,"Unknown file type");
93 (*result)->file = psiconv_unknown_file;
94 }
95
96 psiconv_progress(lev+2,off+len,"Checking UID4");
97 temp = psiconv_read_u32(buf,lev+2,off+len);
98 if (temp == psiconv_checkuid((*result)->uid1,(*result)->uid2,
99 (*result)->uid3))
100 psiconv_debug(lev+2,off+len,"Checksum %08x is correct",temp);
101 else {
102 psiconv_warn(lev+2,off+len,"Checksum failed, file corrupted!");
103 psiconv_debug(lev+2,off+len,"Expected checksum %08x, found %08x",
104 psiconv_checkuid((*result)->uid1,(*result)->uid2,
105 (*result)->uid3),temp);
106 res = -1;
107 }
108 len += 4;
109
110 DONE:
111 if (length)
112 *length = len;
113
114 psiconv_progress(lev+1,off+len-1,
115 "End of Header Section (total length: %08x)",len);
116
117 return res;
118 }
119
120 int psiconv_parse_section_table_section(const psiconv_buffer buf, int lev,
121 psiconv_u32 off, int *length,
122 psiconv_section_table_section *result)
123 {
124 int res=0;
125 int len=0;
126 psiconv_section_table_entry entry;
127
128 int i;
129 psiconv_u8 nr;
130
131 psiconv_progress(lev+1,off+len,"Going to read the section table section");
132 *result = psiconv_list_new(sizeof(*entry));
133
134 psiconv_progress(lev+2,off+len,"Going to read the section table length");
135 nr = psiconv_read_u8(buf,lev+2,off+len);
136 psiconv_debug(lev+2,off+len,"Length: %08x",nr);
137 if (nr & 0x01) {
138 psiconv_warn(lev+2,off+len,
139 "Section table length odd - ignoring last entry");
140 res = -1;
141 }
142 len ++;
143
144 psiconv_progress(lev+2,off+len,"Going to read the section table entries");
145 entry = malloc(sizeof(*entry));
146 for (i = 0; i < nr / 2; i++) {
147 entry->id = psiconv_read_u32(buf,lev+2,off + len);
148 psiconv_debug(lev+2,off + len,"Entry %d: ID = %08x",i,entry->id);
149 len += 0x04;
150 entry->offset = psiconv_read_u32(buf,lev+2,off + len);
151 psiconv_debug(lev+2,off +len,"Entry %d: Offset = %08x",i,entry->offset);
152 len += 0x04;
153 psiconv_list_add(*result,entry);
154 }
155
156 free(entry);
157
158 if (length)
159 *length = len;
160
161 psiconv_progress(lev+1,off+len-1,"End of section table section "
162 "(total length: %08x)", len);
163
164 return res;
165 }
166
167 int psiconv_parse_application_id_section(const psiconv_buffer buf, int lev,
168 psiconv_u32 off, int *length,
169 psiconv_application_id_section *result)
170 {
171 int res=0;
172 int len=0;
173 int leng;
174
175 psiconv_progress(lev+1,off,"Going to read the application id section");
176 (*result) = malloc(sizeof(**result));
177
178 psiconv_progress(lev+2,off+len,"Going to read the type identifier");
179 (*result)->id = psiconv_read_u32(buf,lev+2,off+len);
180 psiconv_debug(lev+2,off+len,"Identifier: %08x",(*result)->id);
181 len += 4;
182
183 psiconv_progress(lev+2,off+len,"Going to read the application id string");
184 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng);
185 len += leng;
186
187 if (length)
188 *length = len;
189
190 psiconv_progress(lev+1,off+len-1,"End of application id section "
191 "(total length: %08x", len);
192
193 return res;
194 }
195
196 int psiconv_parse_text_section(const psiconv_buffer buf,int lev,psiconv_u32 off,
197 int *length,psiconv_text_and_layout *result)
198 {
199
200 int res = 0;
201 int len=0;
202
203 psiconv_u32 text_len;
204 psiconv_paragraph para;
205
206 int nr;
207 int i,j,start,leng;
208 char *str_copy;
209
210 psiconv_progress(lev+1,off,"Going to parse the text section");
211 psiconv_progress(lev+2,off,"Reading the text length");
212 text_len = psiconv_read_X(buf,lev+2,off,&leng);
213 psiconv_debug(lev+2,off,"Length: %08x",text_len);
214 len += leng;
215
216 *result = psiconv_list_new(sizeof(*para));
217 para = malloc(sizeof(*para));
218
219 psiconv_progress(lev+2,off+len,"Going to read all paragraph text");
220 nr = 0;
221 start = 0;
222 for (i = 0; i < text_len; i++)
223 if (psiconv_read_u8(buf,lev+2,off+len+i) == 0x06) {
224 para->text = malloc(i - start + 1);
225 for (j = 0; j < i - start; j++)
226 para->text[j] = psiconv_read_u8(buf,lev+1,off + len + start + j);
227 para->text[j] = 0;
228
229 psiconv_list_add(*result,para);
230
231 str_copy = psiconv_make_printable(para->text);
232 psiconv_debug(lev+2,off+i+len,"Line %d: %d characters",nr,
233 strlen(str_copy) +1);
234 psiconv_debug(lev+2,off+i+len,"Line %d: `%s'",nr,str_copy);
235 free(str_copy);
236
237 start = i + 1;
238 nr ++;
239 }
240
241 if (start != text_len) {
242 res = -1;
243 psiconv_warn(lev+2,off+start+len,
244 "Last line does not end on EOL (%d characters left)", len - start);
245 para->text = malloc(text_len - start + 1);
246 for (j = 0; j < text_len - start; j++)
247 para->text[j] = psiconv_read_u8(buf,lev+2,off + start + j + len);
248 para->text[text_len - start] = 0;
249 psiconv_list_add(*result,para);
250 str_copy = psiconv_make_printable(para->text);
251 psiconv_debug(lev+2,off+start+len,"Last line: %d characters",nr,
252 strlen(str_copy)+1);
253 psiconv_debug(lev+2,off+start+len,"Last line: `%s'",str_copy);
254 free(str_copy);
255 }
256
257 free(para);
258
259 /* Initialize the remaining parts of each paragraph */
260 for (i = 0; i < psiconv_list_length(*result); i ++) {
261 para = psiconv_list_get(*result,i);
262 para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout));
263 para->replacements = psiconv_list_new(sizeof(struct psiconv_replacement));
264 para->base_style = 0;
265 para->base_character = psiconv_basic_character_layout();
266 para->base_paragraph = psiconv_basic_paragraph_layout();
267 }
268
269
270 len += text_len;
271
272 if (length)
273 *length = len;
274
275 psiconv_progress(lev+1,off+len-1,"End of text section (total length: %08x",
276 len);
277
278 return res;
279 }
280
281 /* First do a parse_text_section, or you will get into trouble here */
282 int psiconv_parse_layout_section(const psiconv_buffer buf,
283 int lev,psiconv_u32 off,
284 int *length,
285 psiconv_text_and_layout result,
286 psiconv_word_styles_section styles,
287 int with_styles)
288 {
289 int res = 0;
290 int len = 0;
291 psiconv_u32 temp;
292 int parse_styles,nr,i,j,total,leng,line_length;
293
294 typedef struct anon_style
295 {
296 int nr;
297 psiconv_s16 base_style;
298 psiconv_character_layout character;
299 psiconv_paragraph_layout paragraph;
300 } *anon_style;
301
302 typedef psiconv_list anon_style_list; /* of struct anon_style */
303
304 anon_style_list anon_styles;
305 struct anon_style anon;
306 anon_style anon_ptr=NULL;
307
308 psiconv_paragraph para;
309 struct psiconv_in_line_layout in_line;
310
311 int *inline_count;
312
313
314 psiconv_progress(lev+1,off,"Going to read the layout section");
315
316 psiconv_progress(lev+2,off,"Going to read the section type");
317 temp = psiconv_read_u16(buf,lev+2,off+len);
318 psiconv_debug(lev+2,off+len,"Type: %02x",temp);
319 parse_styles = with_styles;
320 if ((temp == 0x0001) && !with_styles) {
321 psiconv_warn(lev+2,off+len,"Styleless layout section expected, "
322 "but styled section found!");
323 parse_styles = 1;
324 res = -1;
325 } else if ((temp == 0x0000) && (with_styles)) {
326 psiconv_warn(lev+2,off+len,"Styled layout section expected, "
327 "but styleless section found!");
328 parse_styles = 0;
329 res = -1;
330 } else if ((temp != 0x0000) && (temp != 0x0001)) {
331 psiconv_warn(lev+2,off+len,
332 "Layout section type indicator has unknown value!");
333 res = -1;
334 }
335 len += 0x02;
336
337 psiconv_progress(lev+2,off+len,"Going to read paragraph type list");
338 anon_styles = psiconv_list_new(sizeof(anon));
339 psiconv_progress(lev+3,off+len,"Going to read paragraph type list length");
340 nr = psiconv_read_u8(buf,lev+3,off+len);
341 psiconv_debug(lev+3,off+len,"Length: %02x",nr);
342 len ++;
343
344 psiconv_progress(lev+3,off+len,
345 "Going to read the paragraph type list elements");
346 for (i = 0; i < nr; i ++) {
347 psiconv_progress(lev+3,off+len,"Element %d",i);
348 anon.nr = psiconv_read_u32(buf,lev+4,off+len);
349 psiconv_debug(lev+4,off+len,"Number: %08x",anon.nr);
350 len += 0x04;
351
352 psiconv_progress(lev+4,off,"Going to determine the base style");
353 if (parse_styles) {
354 anon.base_style = psiconv_read_u8(buf,lev+3,
355 off+len+4+psiconv_read_u32(buf,lev+4,
356 off+len));
357 psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len),
358 "Style indicator: %02x",anon.base_style);
359 } else
360 anon.base_style = 0;
361 anon.paragraph = psiconv_clone_paragraph_layout(psiconv_get_style
362 (styles,anon.base_style)->paragraph);
363 anon.character = psiconv_clone_character_layout(psiconv_get_style
364 (styles,anon.base_style)->character);
365
366 psiconv_progress(lev+4,off+len,"Going to read the paragraph layout");
367 res |= psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng,
368 anon.paragraph);
369 len += leng;
370 if (parse_styles)
371 len ++;
372
373 psiconv_progress(lev+4,off+len,"Going to read the character layout");
374 res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng,
375 anon.character);
376 len += leng;
377 psiconv_list_add(anon_styles,&anon);
378 }
379
380 psiconv_progress(lev+2,off+len,"Going to parse the paragraph element list");
381 psiconv_progress(lev+3,off+len,"Going to read the number of paragraphs");
382 nr = psiconv_read_u32(buf,lev+3,off+len);
383 if (nr != psiconv_list_length(result)) {
384 psiconv_warn(lev+3,off+len,
385 "Number of text paragraphs and paragraph elements does not match");
386 psiconv_debug(lev+3,off+len,
387 "%d text paragraphs, %d paragraph elements",
388 psiconv_list_length(result),nr);
389 }
390 psiconv_debug(lev+3,off+len,"Number of paragraphs: %d",nr);
391 len += 4;
392 inline_count = malloc(nr * sizeof(*inline_count));
393
394 psiconv_progress(lev+3,off+len,"Going to read the paragraph elements");
395 for (i = 0; i < nr; i ++) {
396 psiconv_progress(lev+3,off+len,"Element %d",i);
397 if (i >= psiconv_list_length(result)) {
398 psiconv_debug(lev+4,off+len,"Going to allocate a new element");
399 para = malloc(sizeof(*para));
400 para->in_lines = psiconv_list_new(sizeof(struct psiconv_in_line_layout));
401 para->base_style = 0;
402 para->base_character = psiconv_basic_character_layout();
403 para->base_paragraph = psiconv_basic_paragraph_layout();
404 free(para);
405 }
406 para = psiconv_list_get(result,i);
407
408 psiconv_progress(lev+4,off+len,"Going to read the paragraph length");
409 temp = psiconv_read_u32(buf,lev+4,off+len);
410 if (temp != strlen(para->text)+1) {
411 res = -1;
412 psiconv_warn(lev+4,off+len,
413 "Disagreement of the length of paragraph in layout section");
414 psiconv_debug(lev+4,off+len,
415 "Paragraph length: layout section says %d, counted %d",
416 temp,strlen(para->text)+1);
417 } else
418 psiconv_debug(lev+4,off+len,"Paragraph length: %d",temp);
419 len += 4;
420
421 psiconv_progress(lev+4,off+len,"Going to read the paragraph type");
422 temp = psiconv_read_u8(buf,lev+4,off+len);
423 if (temp != 0x00) {
424 psiconv_debug(lev+4,off+len,"Type: %02x",temp);
425 for (j = 0; j < psiconv_list_length(anon_styles); j++) {
426 anon_ptr = psiconv_list_get(anon_styles,j);
427 if (temp == anon_ptr->nr)
428 break;
429 }
430 if (j == psiconv_list_length(anon_styles)) {
431 psiconv_warn(lev+4,off+len,"Layout section paragraph type unknown");
432 psiconv_debug(lev+4,off+len,"Unknown type - using base styles instead");
433 para->base_style = 0;
434 psiconv_free_paragraph_layout(para->base_paragraph);
435 psiconv_free_character_layout(para->base_character);
436 para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style
437 (styles,0)->paragraph);
438 para->base_character = psiconv_clone_character_layout(psiconv_get_style
439 (styles,0)->character);
440 } else {
441 para->base_style = anon_ptr->base_style;
442 psiconv_free_paragraph_layout(para->base_paragraph);
443 psiconv_free_character_layout(para->base_character);
444 para->base_paragraph = psiconv_clone_paragraph_layout
445 (anon_ptr->paragraph);
446 para->base_character = psiconv_clone_character_layout
447 (anon_ptr->character);
448 }
449 inline_count[i] = 0;
450 len += 0x01;
451 } else {
452 psiconv_debug(lev+4,off+len,"Type: %02x (not based on a paragraph type)"
453 ,temp);
454 len += 0x01;
455 if (parse_styles) {
456 psiconv_progress(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len),
457 "Going to read the paragraph element base style");
458 temp = psiconv_read_u8(buf,lev+4,
459 off+len+psiconv_read_u32(buf,lev+4,off+len));
460 psiconv_debug(lev+4,off+len+psiconv_read_u32(buf,lev+4,off+len),
461 "Style: %02x",temp);
462 } else
463 temp = 0x00;
464 psiconv_free_paragraph_layout(para->base_paragraph);
465 psiconv_free_character_layout(para->base_character);
466 para->base_paragraph = psiconv_clone_paragraph_layout(psiconv_get_style
467 (styles,temp)->paragraph);
468 para->base_character = psiconv_clone_character_layout(psiconv_get_style
469 (styles,temp)->character);
470 para->base_style = temp;
471 psiconv_progress(lev+4,off+len,"Going to read paragraph layout");
472 psiconv_parse_paragraph_layout_list(buf,lev+4,off+len,&leng,
473 para->base_paragraph);
474 len += leng;
475 if (parse_styles)
476 len += 1;
477 psiconv_progress(lev+4,off+len,"Going to read number of in-line "
478 "layout elements");
479 inline_count[i] = psiconv_read_u32(buf,lev+4,off+len);
480 psiconv_debug(lev+4,off+len,"Nr: %08x",inline_count[i]);
481 len += 4;
482 }
483 }
484
485 psiconv_progress(lev+2,off+len,"Going to read the text layout inline list");
486
487 psiconv_progress(lev+3,off+len,"Going to read the number of elements");
488 nr = psiconv_read_u32(buf,lev+3,off+len);
489 psiconv_debug(lev+3,off,"Elements: %08x",nr);
490 len += 0x04;
491
492 psiconv_progress(lev+3,off+len,
493 "Going to read the text layout inline elements");
494 total = 0;
495 for (i = 0; i < psiconv_list_length(result); i++) {
496 para = psiconv_list_get(result,i);
497 line_length = -1;
498 for (j = 0; j < inline_count[i]; j++) {
499 psiconv_progress(lev+3,off+len,"Element %d: Paragraph %d, element %d",
500 total,i,j);
501 if (total >= nr) {
502 psiconv_warn(lev+3,off+len,
503 "Layout section inlines: not enough element");
504 res = -1;
505 psiconv_debug(lev+3,off+len,"Can't read element!");
506 } else {
507 total ++;
508 in_line.layout = psiconv_clone_character_layout(para->base_character);
509 psiconv_progress(lev+4,off+len,"Going to read the element type");
510 temp = psiconv_read_u8(buf,lev+4,len+off);
511 len += 1;
512 psiconv_debug(lev+4,off,"Type: %02x",temp);
513 psiconv_progress(lev+4,off,
514 "Going to read the number of characters it applies to");
515 in_line.length = psiconv_read_u32(buf,lev+4,len+off);
516 psiconv_debug(lev+4,off+len,"Length: %02x",in_line.length);
517 len += 4;
518 psiconv_progress(lev+4,off+len,"Going to read the character layout");
519 res |= psiconv_parse_character_layout_list(buf,lev+4,off+len,&leng,
520 in_line.layout);
521 len += leng;
522
523 if (temp == 0x01) {
524 psiconv_debug(lev+4,off+len,"Skipping object data");
525 len += 0x10;
526 } else if (temp != 0x00) {
527 psiconv_warn(lev+4,off+len,"Layout section unknown inline type");
528 res = -1;
529 }
530 if (line_length + in_line.length > strlen(para->text)) {
531 psiconv_warn(lev+4,off+len,
532 "Layout section inlines: line length mismatch");
533 res = -1;
534 in_line.length = strlen(para->text) - line_length;
535 }
536 line_length += in_line.length;
537 psiconv_list_add(para->in_lines,&in_line);
538 }
539 }
540 }
541
542 if (total != nr) {
543 psiconv_warn(lev+4,off+len,
544 "Layout section too many inlines, skipping remaining");
545 }
546
547 free(inline_count);
548
549 for (i = 0 ; i < psiconv_list_length(anon_styles); i ++) {
550 anon_ptr = psiconv_list_get(anon_styles,i);
551 psiconv_free_character_layout(anon_ptr->character);
552 psiconv_free_paragraph_layout(anon_ptr->paragraph);
553 }
554 psiconv_list_free(anon_styles);
555
556 if (length)
557 *length = len;
558
559 psiconv_progress(lev+1,off+len-1,"End of layout section (total length: %08x",
560 len);
561
562 return res;
563 }
564
565 int psiconv_parse_styled_layout_section(const psiconv_buffer buf,
566 int lev,psiconv_u32 off,
567 int *length,
568 psiconv_text_and_layout result,
569 psiconv_word_styles_section styles)
570 {
571 return psiconv_parse_layout_section(buf,lev,off,length,result,styles,1);
572 }
573
574 int psiconv_parse_styleless_layout_section(const psiconv_buffer buf,
575 int lev,psiconv_u32 off,
576 int *length,
577 psiconv_text_and_layout result,
578 psiconv_character_layout base_char,
579 psiconv_paragraph_layout base_para)
580 {
581 int res;
582 psiconv_word_styles_section styles_section;
583
584 styles_section = malloc(sizeof(*styles_section));
585 styles_section->normal = malloc(sizeof(*styles_section->normal));
586 styles_section->normal->character = psiconv_clone_character_layout(base_char);
587 styles_section->normal->paragraph = psiconv_clone_paragraph_layout(base_para);
588 styles_section->normal->hotkey = 0;
589 styles_section->normal->name = strdup("");
590 styles_section->styles = psiconv_list_new(sizeof(struct psiconv_word_style));
591
592 res = psiconv_parse_layout_section(buf,lev,off,length,result,
593 styles_section,0);
594
595 psiconv_free_word_styles_section(styles_section);
596 return res;
597 }
598

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26