1 |
/* |
2 |
parse_texted.c - Part of psiconv, a PSION 5 file formats converter |
3 |
Copyright (c) 1999, 2000 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 |
|
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_texted_section(const psiconv_buffer buf,int lev, |
33 |
psiconv_u32 off, int *length, |
34 |
psiconv_texted_section *result, |
35 |
psiconv_character_layout base_char, |
36 |
psiconv_paragraph_layout base_para) |
37 |
{ |
38 |
int res = 0; |
39 |
int len = 0; |
40 |
psiconv_u32 layout_sec = 0; |
41 |
psiconv_u32 unknown_sec = 0; |
42 |
psiconv_u32 replacement_sec = 0; |
43 |
psiconv_u32 temp; |
44 |
int leng; |
45 |
|
46 |
psiconv_progress(lev+1,off,"Going to read a texted section"); |
47 |
if (!((*result) = malloc(sizeof(**result)))) |
48 |
goto ERROR1; |
49 |
|
50 |
psiconv_progress(lev+2,off+len,"Going to read section id"); |
51 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
52 |
if (res) |
53 |
goto ERROR2; |
54 |
if (temp != PSICONV_ID_TEXTED_BODY) { |
55 |
psiconv_warn(lev+2,off+len, |
56 |
"Page header section body id not found"); |
57 |
psiconv_debug(lev+2,off+len, |
58 |
"Page body id: read %08x, expected %08x",temp, |
59 |
PSICONV_ID_TEXTED); |
60 |
res = -PSICONV_E_PARSE; |
61 |
goto ERROR2; |
62 |
} |
63 |
len += 4; |
64 |
|
65 |
psiconv_progress(lev+2,off+len,"Going to read the section jumptable"); |
66 |
while (temp = psiconv_read_u32(buf,lev+3,off+len,&res), |
67 |
!res && temp != PSICONV_ID_TEXTED_TEXT) { |
68 |
len += 4; |
69 |
if (temp == PSICONV_ID_TEXTED_LAYOUT) { |
70 |
layout_sec = psiconv_read_u32(buf,lev+3,off+len,&res); |
71 |
if (res) |
72 |
goto ERROR2; |
73 |
psiconv_debug(lev+3,off+len,"Found Layout section at %08x",layout_sec); |
74 |
} else if (temp == PSICONV_ID_TEXTED_REPLACEMENT) { |
75 |
replacement_sec = psiconv_read_u32(buf,lev+3,off+len,&res); |
76 |
if (res) |
77 |
goto ERROR2; |
78 |
psiconv_debug(lev+3,off+len,"Found Replacement section at %08x", |
79 |
replacement_sec); |
80 |
} else if (temp == PSICONV_ID_TEXTED_UNKNOWN) { |
81 |
unknown_sec= psiconv_read_u32(buf,lev+3,off+len,&res); |
82 |
if (res) |
83 |
goto ERROR2; |
84 |
if (unknown_sec) { |
85 |
psiconv_warn(lev+3,off+len, |
86 |
"Unknown section in TextEd jumptable has real offset (ignoring)"); |
87 |
} |
88 |
psiconv_debug(lev+3,off+len,"Found Unknown section at %08x", |
89 |
unknown_sec); |
90 |
} else { |
91 |
psiconv_warn(lev+3,off+len, |
92 |
"Unknown section in TextEd jumptable (ignoring)"); |
93 |
psiconv_debug(lev+3,off+len,"Section ID %08x at offset %08x",temp, |
94 |
psiconv_read_u32(buf,lev+3,off+len,NULL)); |
95 |
} |
96 |
len += 4; |
97 |
} |
98 |
if (res) |
99 |
goto ERROR2; |
100 |
|
101 |
len += 4; |
102 |
psiconv_progress(lev+2,off+len,"Going to read the text"); |
103 |
if ((res = psiconv_parse_text_section(buf,lev+2,off+len,&leng, |
104 |
&(*result)->paragraphs))) |
105 |
goto ERROR2; |
106 |
len += leng; |
107 |
|
108 |
if (layout_sec) { |
109 |
psiconv_progress(lev+2,off+len,"Going to read the layout"); |
110 |
if ((res = psiconv_parse_styleless_layout_section(buf,lev+2,layout_sec,NULL, |
111 |
(*result)->paragraphs, |
112 |
base_char,base_para))) |
113 |
goto ERROR3; |
114 |
} |
115 |
|
116 |
#if 0 |
117 |
if (replacement_sec) { |
118 |
psiconv_progress(lev+2,off+len,"Going to read the replacements"); |
119 |
/* WHATEVER */ |
120 |
} |
121 |
#endif |
122 |
|
123 |
if (length) |
124 |
*length = len; |
125 |
|
126 |
psiconv_progress(lev+1,off+len-1,"End of TextEd section " |
127 |
"(total length: %08x", len); |
128 |
|
129 |
return 0; |
130 |
|
131 |
ERROR3: |
132 |
psiconv_free_text_and_layout((*result)->paragraphs); |
133 |
ERROR2: |
134 |
free (*result); |
135 |
ERROR1: |
136 |
psiconv_warn(lev+1,off,"Reading of TextEd Section failed"); |
137 |
if (length) |
138 |
*length = 0; |
139 |
if (!res) |
140 |
return -PSICONV_E_NOMEM; |
141 |
else |
142 |
return res; |
143 |
} |
144 |
|
145 |
|