1 |
/* |
2 |
parse_simple.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 |
psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, |
29 |
int *status) |
30 |
{ |
31 |
psiconv_u8 *ptr; |
32 |
ptr = psiconv_buffer_get(buf,off); |
33 |
if (!ptr) { |
34 |
psiconv_warn(lev,off,"Trying byte read past the end of the file"); |
35 |
if (status) |
36 |
*status = -PSICONV_E_PARSE; |
37 |
return 0; |
38 |
} |
39 |
if (status) |
40 |
*status = 0; |
41 |
return *ptr; |
42 |
} |
43 |
|
44 |
psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off, |
45 |
int *status) |
46 |
{ |
47 |
psiconv_u8 *ptr0,*ptr1; |
48 |
ptr0 = psiconv_buffer_get(buf,off); |
49 |
ptr1 = psiconv_buffer_get(buf,off+1); |
50 |
if (!ptr0 || !ptr1) { |
51 |
psiconv_warn(lev,off,"Trying word read past the end of the file"); |
52 |
if (status) |
53 |
*status = -PSICONV_E_PARSE; |
54 |
return 0; |
55 |
} |
56 |
if (status) |
57 |
*status = 0; |
58 |
return *ptr0 + (*ptr1 << 8); |
59 |
} |
60 |
|
61 |
psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off, |
62 |
int *status) |
63 |
{ |
64 |
psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; |
65 |
ptr0 = psiconv_buffer_get(buf,off); |
66 |
ptr1 = psiconv_buffer_get(buf,off+1); |
67 |
ptr2 = psiconv_buffer_get(buf,off+2); |
68 |
ptr3 = psiconv_buffer_get(buf,off+3); |
69 |
if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { |
70 |
psiconv_warn(lev,off,"Trying long read past the end of the file"); |
71 |
if (status) |
72 |
*status = -PSICONV_E_PARSE; |
73 |
return 0; |
74 |
} |
75 |
if (status) |
76 |
*status = 0; |
77 |
return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); |
78 |
} |
79 |
|
80 |
psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, |
81 |
int *length,int *status) |
82 |
{ |
83 |
psiconv_u8 temp; |
84 |
psiconv_S_t res; |
85 |
int len,localstatus; |
86 |
|
87 |
psiconv_progress(lev+1,off,"Going to read a S length indicator"); |
88 |
temp = psiconv_read_u8(buf,lev+2,off,&localstatus); |
89 |
if (localstatus) |
90 |
goto ERROR; |
91 |
if ((temp & 0x03) == 0x02) { |
92 |
res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; |
93 |
if (localstatus) |
94 |
goto ERROR; |
95 |
len = 1; |
96 |
psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
97 |
} else if ((temp & 0x07) == 0x03) { |
98 |
res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; |
99 |
if (localstatus) |
100 |
goto ERROR; |
101 |
len = 2; |
102 |
psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
103 |
} else { |
104 |
psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); |
105 |
psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); |
106 |
goto ERROR; |
107 |
} |
108 |
|
109 |
if (length) |
110 |
*length = len; |
111 |
if (status) |
112 |
*status = 0; |
113 |
|
114 |
psiconv_progress(lev+1,off+len-1, |
115 |
"End of S length indicator (total length: %08x)", len); |
116 |
|
117 |
return res; |
118 |
|
119 |
ERROR: |
120 |
psiconv_warn(lev+1,off,"Reading of S indicator failed"); |
121 |
if (status) |
122 |
*status = localstatus; |
123 |
if (length) |
124 |
*length = 0; |
125 |
return 0; |
126 |
} |
127 |
|
128 |
psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off, |
129 |
int *length, int *status) |
130 |
{ |
131 |
psiconv_u8 temp; |
132 |
psiconv_X_t res; |
133 |
int len,localstatus; |
134 |
|
135 |
psiconv_progress(lev+1,off,"Going to read a X length indicator"); |
136 |
temp = psiconv_read_u8(buf,lev+2,off,&localstatus); |
137 |
if (localstatus) |
138 |
goto ERROR; |
139 |
if ((temp & 0x01) == 0x00) { |
140 |
res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 1; |
141 |
if (localstatus) |
142 |
goto ERROR; |
143 |
len = 1; |
144 |
psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
145 |
} else if ((temp & 0x03) == 0x01) { |
146 |
res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 2; |
147 |
if (localstatus) |
148 |
goto ERROR; |
149 |
len = 2; |
150 |
psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
151 |
} else if ((temp & 0x07) == 0x03) { |
152 |
res = psiconv_read_u32(buf,lev+2,off,&localstatus) >> 3; |
153 |
if (localstatus) |
154 |
goto ERROR; |
155 |
len = 4; |
156 |
psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); |
157 |
} else { |
158 |
psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); |
159 |
psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); |
160 |
goto ERROR; |
161 |
} |
162 |
|
163 |
if (length) |
164 |
*length = len; |
165 |
if (status) |
166 |
*status = 0; |
167 |
|
168 |
psiconv_progress(lev+1,off+len-1, |
169 |
"End of X length indicator (total length: %08x)", len); |
170 |
|
171 |
return res; |
172 |
|
173 |
ERROR: |
174 |
psiconv_warn(lev+1,off,"Reading of X indicator failed"); |
175 |
if (status) |
176 |
*status = localstatus; |
177 |
if (length) |
178 |
*length = 0; |
179 |
return 0; |
180 |
} |
181 |
|
182 |
psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev, |
183 |
psiconv_u32 off, int *length, int *status) |
184 |
{ |
185 |
psiconv_length_t res; |
186 |
int localstatus; |
187 |
|
188 |
res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off, |
189 |
&localstatus)); |
190 |
if (localstatus) { |
191 |
psiconv_warn(lev+1,off,"Reading of length failed"); |
192 |
if (length) |
193 |
*length = 0; |
194 |
if (status) |
195 |
*status = localstatus; |
196 |
return 0; |
197 |
} |
198 |
psiconv_debug(lev+1,off,"Length: %f",res); |
199 |
if (length) |
200 |
*length = 4; |
201 |
if (status) |
202 |
*status = 0; |
203 |
return res; |
204 |
} |
205 |
|
206 |
psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev, |
207 |
psiconv_u32 off, int *length, int *status) |
208 |
{ |
209 |
psiconv_size_t res; |
210 |
int localstatus; |
211 |
res = ((psiconv_s32) psiconv_read_u32(buf,lev,off,&localstatus)) / 20.0; |
212 |
if (localstatus) { |
213 |
psiconv_warn(lev+1,off,"Reading of size failed"); |
214 |
if (length) |
215 |
*length = 0; |
216 |
if (status) |
217 |
*status = localstatus; |
218 |
return 0; |
219 |
} |
220 |
psiconv_debug(lev+1,off,"Size: %f",res); |
221 |
if (status) |
222 |
*status = 0; |
223 |
if (length) |
224 |
*length = 4; |
225 |
return res; |
226 |
} |
227 |
|
228 |
int psiconv_parse_bool(const psiconv_buffer buf, int lev, psiconv_u32 off, |
229 |
int *length, psiconv_bool_t *result) |
230 |
{ |
231 |
psiconv_u8 temp; |
232 |
int localstatus; |
233 |
temp = psiconv_read_u8(buf,lev,off,&localstatus); |
234 |
if (localstatus) { |
235 |
psiconv_warn(lev+1,off,"Reading of bool failed"); |
236 |
if (length) |
237 |
*length = 0; |
238 |
return localstatus; |
239 |
} |
240 |
if (length) |
241 |
*length = 1; |
242 |
if (temp == 0) { |
243 |
*result = psiconv_bool_false; |
244 |
return 0; |
245 |
} else if (temp == 1) { |
246 |
*result = psiconv_bool_true; |
247 |
return 0; |
248 |
} |
249 |
psiconv_warn(lev+1,off,"Unknown value for boolean"); |
250 |
psiconv_debug(lev+1,off,"Boolean value: %02x",temp); |
251 |
*result = psiconv_bool_true; |
252 |
return 0; |
253 |
} |
254 |
|
255 |
psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev, |
256 |
psiconv_u32 off,int *length, int *status) |
257 |
{ |
258 |
int stringlen,i,leng,len,localstatus; |
259 |
psiconv_string_t result; |
260 |
char *res_copy; |
261 |
|
262 |
psiconv_progress(lev+1,off,"Going to read a string"); |
263 |
|
264 |
stringlen = psiconv_read_S(buf,lev+2,off,&leng,&localstatus); |
265 |
if (localstatus) |
266 |
goto ERROR1; |
267 |
psiconv_debug(lev+2,off,"Length: %i",stringlen); |
268 |
len = leng; |
269 |
|
270 |
result = malloc(stringlen + 1); |
271 |
if (!result) |
272 |
goto ERROR1; |
273 |
for (i = 0; (i < stringlen) && !localstatus; i++) |
274 |
result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); |
275 |
if (localstatus) |
276 |
goto ERROR2; |
277 |
result[stringlen] = 0; |
278 |
len += stringlen; |
279 |
|
280 |
res_copy = psiconv_make_printable(result); |
281 |
if (!res_copy) |
282 |
goto ERROR2; |
283 |
psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); |
284 |
free(res_copy); |
285 |
|
286 |
if (length) |
287 |
*length = len; |
288 |
|
289 |
if (status) |
290 |
*status = 0; |
291 |
|
292 |
psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len); |
293 |
|
294 |
return result; |
295 |
|
296 |
ERROR2: |
297 |
free(result); |
298 |
ERROR1: |
299 |
psiconv_warn(lev+1,off,"Reading of string failed"); |
300 |
if (status) |
301 |
*status = localstatus; |
302 |
if (length) |
303 |
*length = 0; |
304 |
return NULL; |
305 |
} |