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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 118 - (show annotations)
Sat Jun 30 13:36:39 2001 UTC (22 years, 9 months ago) by frodo
File MIME type: text/plain
File size: 10870 byte(s)
(Frodo) Now compiles again with newest ImageMagick.

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 #include <math.h>
25
26 #include "parse_routines.h"
27 #include "error.h"
28
29 /* Very inefficient, but good enough for now. By implementing it ourselves,
30 we do not have to link with -lm */
31 psiconv_float_t pow2(int n)
32 {
33 psiconv_float_t res=1.0;
34 int i;
35
36 for (i = 0; i < (n<0?-n:n); i++)
37 res *= 2.0;
38
39 return n<0?1/res:res;
40 }
41 psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off,
42 int *status)
43 {
44 psiconv_u8 *ptr;
45 ptr = psiconv_buffer_get(buf,off);
46 if (!ptr) {
47 psiconv_warn(lev,off,"Trying byte read past the end of the file");
48 if (status)
49 *status = -PSICONV_E_PARSE;
50 return 0;
51 }
52 if (status)
53 *status = 0;
54 return *ptr;
55 }
56
57 psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off,
58 int *status)
59 {
60 psiconv_u8 *ptr0,*ptr1;
61 ptr0 = psiconv_buffer_get(buf,off);
62 ptr1 = psiconv_buffer_get(buf,off+1);
63 if (!ptr0 || !ptr1) {
64 psiconv_warn(lev,off,"Trying word read past the end of the file");
65 if (status)
66 *status = -PSICONV_E_PARSE;
67 return 0;
68 }
69 if (status)
70 *status = 0;
71 return *ptr0 + (*ptr1 << 8);
72 }
73
74 psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off,
75 int *status)
76 {
77 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
78 ptr0 = psiconv_buffer_get(buf,off);
79 ptr1 = psiconv_buffer_get(buf,off+1);
80 ptr2 = psiconv_buffer_get(buf,off+2);
81 ptr3 = psiconv_buffer_get(buf,off+3);
82 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
83 psiconv_warn(lev,off,"Trying long read past the end of the file");
84 if (status)
85 *status = -PSICONV_E_PARSE;
86 return 0;
87 }
88 if (status)
89 *status = 0;
90 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
91 }
92
93 psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off,
94 int *length,int *status)
95 {
96 psiconv_u8 temp;
97 psiconv_S_t res;
98 int len,localstatus;
99
100 psiconv_progress(lev+1,off,"Going to read a S length indicator");
101 temp = psiconv_read_u8(buf,lev+2,off,&localstatus);
102 if (localstatus)
103 goto ERROR;
104 if ((temp & 0x03) == 0x02) {
105 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2;
106 if (localstatus)
107 goto ERROR;
108 len = 1;
109 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res);
110 } else if ((temp & 0x07) == 0x03) {
111 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3;
112 if (localstatus)
113 goto ERROR;
114 len = 2;
115 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res);
116 } else {
117 psiconv_warn(lev+2,off,"S indicator: unknown encoding!");
118 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp);
119 goto ERROR;
120 }
121
122 if (length)
123 *length = len;
124 if (status)
125 *status = 0;
126
127 psiconv_progress(lev+1,off+len-1,
128 "End of S length indicator (total length: %08x)", len);
129
130 return res;
131
132 ERROR:
133 psiconv_warn(lev+1,off,"Reading of S indicator failed");
134 if (status)
135 *status = localstatus;
136 if (length)
137 *length = 0;
138 return 0;
139 }
140
141 psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off,
142 int *length, int *status)
143 {
144 psiconv_u8 temp;
145 psiconv_X_t res;
146 int len,localstatus;
147
148 psiconv_progress(lev+1,off,"Going to read a X length indicator");
149 temp = psiconv_read_u8(buf,lev+2,off,&localstatus);
150 if (localstatus)
151 goto ERROR;
152 if ((temp & 0x01) == 0x00) {
153 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 1;
154 if (localstatus)
155 goto ERROR;
156 len = 1;
157 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res);
158 } else if ((temp & 0x03) == 0x01) {
159 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 2;
160 if (localstatus)
161 goto ERROR;
162 len = 2;
163 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res);
164 } else if ((temp & 0x07) == 0x03) {
165 res = psiconv_read_u32(buf,lev+2,off,&localstatus) >> 3;
166 if (localstatus)
167 goto ERROR;
168 len = 4;
169 psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res);
170 } else {
171 psiconv_warn(lev+2,off,"X indicator: unknown encoding!");
172 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp);
173 goto ERROR;
174 }
175
176 if (length)
177 *length = len;
178 if (status)
179 *status = 0;
180
181 psiconv_progress(lev+1,off+len-1,
182 "End of X length indicator (total length: %08x)", len);
183
184 return res;
185
186 ERROR:
187 psiconv_warn(lev+1,off,"Reading of X indicator failed");
188 if (status)
189 *status = localstatus;
190 if (length)
191 *length = 0;
192 return 0;
193 }
194
195 psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev,
196 psiconv_u32 off, int *length, int *status)
197 {
198 psiconv_length_t res;
199 int localstatus;
200
201 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off,
202 &localstatus));
203 if (localstatus) {
204 psiconv_warn(lev+1,off,"Reading of length failed");
205 if (length)
206 *length = 0;
207 if (status)
208 *status = localstatus;
209 return 0;
210 }
211 psiconv_debug(lev+1,off,"Length: %f",res);
212 if (length)
213 *length = 4;
214 if (status)
215 *status = 0;
216 return res;
217 }
218
219 psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev,
220 psiconv_u32 off, int *length, int *status)
221 {
222 psiconv_size_t res;
223 int localstatus;
224 res = ((psiconv_s32) psiconv_read_u32(buf,lev,off,&localstatus)) / 20.0;
225 if (localstatus) {
226 psiconv_warn(lev+1,off,"Reading of size failed");
227 if (length)
228 *length = 0;
229 if (status)
230 *status = localstatus;
231 return 0;
232 }
233 psiconv_debug(lev+1,off,"Size: %f",res);
234 if (status)
235 *status = 0;
236 if (length)
237 *length = 4;
238 return res;
239 }
240
241 int psiconv_parse_bool(const psiconv_buffer buf, int lev, psiconv_u32 off,
242 int *length, psiconv_bool_t *result)
243 {
244 psiconv_u8 temp;
245 int localstatus;
246 temp = psiconv_read_u8(buf,lev,off,&localstatus);
247 if (localstatus) {
248 psiconv_warn(lev+1,off,"Reading of bool failed");
249 if (length)
250 *length = 0;
251 return localstatus;
252 }
253 if (length)
254 *length = 1;
255 if (temp == 0) {
256 *result = psiconv_bool_false;
257 return 0;
258 } else if (temp == 1) {
259 *result = psiconv_bool_true;
260 return 0;
261 }
262 psiconv_warn(lev+1,off,"Unknown value for boolean");
263 psiconv_debug(lev+1,off,"Boolean value: %02x",temp);
264 *result = psiconv_bool_true;
265 return 0;
266 }
267
268 psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev,
269 psiconv_u32 off,int *length, int *status)
270 {
271 int stringlen,i,leng,len,localstatus;
272 psiconv_string_t result;
273 char *res_copy;
274
275 psiconv_progress(lev+1,off,"Going to read a string");
276
277 stringlen = psiconv_read_S(buf,lev+2,off,&leng,&localstatus);
278 if (localstatus)
279 goto ERROR1;
280 psiconv_debug(lev+2,off,"Length: %i",stringlen);
281 len = leng;
282
283 result = malloc(stringlen + 1);
284 if (!result)
285 goto ERROR1;
286 for (i = 0; (i < stringlen) && !localstatus; i++)
287 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus);
288 if (localstatus)
289 goto ERROR2;
290 result[stringlen] = 0;
291 len += stringlen;
292
293 res_copy = psiconv_make_printable(result);
294 if (!res_copy)
295 goto ERROR2;
296 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
297 free(res_copy);
298
299 if (length)
300 *length = len;
301
302 if (status)
303 *status = 0;
304
305 psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len);
306
307 return result;
308
309 ERROR2:
310 free(result);
311 ERROR1:
312 psiconv_warn(lev+1,off,"Reading of string failed");
313 if (status)
314 *status = localstatus;
315 if (length)
316 *length = 0;
317 return NULL;
318 }
319
320 psiconv_string_t psiconv_read_short_string(const psiconv_buffer buf,
321 int lev,
322 psiconv_u32 off,int *length, int *status)
323 {
324 int stringlen,i,len,localstatus;
325 psiconv_string_t result;
326 char *res_copy;
327
328 psiconv_progress(lev+1,off,"Going to read a short string");
329
330 stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus);
331 if (localstatus)
332 goto ERROR1;
333 psiconv_debug(lev+2,off,"Length: %i",stringlen);
334 len = 1;
335
336 result = malloc(stringlen + 1);
337 if (!result)
338 goto ERROR1;
339 for (i = 0; (i < stringlen) && !localstatus; i++)
340 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus);
341 if (localstatus)
342 goto ERROR2;
343 result[stringlen] = 0;
344 len += stringlen;
345
346 res_copy = psiconv_make_printable(result);
347 if (!res_copy)
348 goto ERROR2;
349 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
350 free(res_copy);
351
352 if (length)
353 *length = len;
354
355 if (status)
356 *status = 0;
357
358 psiconv_progress(lev+1,off+len-1,"End of short string (total length: %08x)",
359 len);
360
361 return result;
362
363
364 ERROR2:
365 free(result);
366 ERROR1:
367 psiconv_warn(lev+1,off,"Reading of short string failed");
368 if (status)
369 *status = localstatus;
370 if (length)
371 *length = 0;
372 return NULL;
373 }
374
375 psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev,
376 psiconv_u32 off, int *length, int *status)
377 {
378 psiconv_float_t result,bitvalue;
379 int res,bit;
380 psiconv_u32 temp=0;
381
382 psiconv_progress(lev+1,off,"Going to read a float");
383
384 bitvalue = 0.5;
385 result = 1.0;
386 for (bit = 0x33; bit > 0; bit--) {
387 if ((bit == 0x33) || ((bit & 0x07) == 0x07))
388 temp = psiconv_read_u8(buf,lev+2,off+ (bit >> 3),&res);
389 if (res)
390 goto ERROR;
391 if (temp & (0x01 << (bit & 0x07)))
392 result += bitvalue;
393 bitvalue /= 2.0;
394 }
395 temp = psiconv_read_u16(buf,lev+2,off+6,&res);
396 if (res)
397 goto ERROR;
398 if (temp & 0x8000)
399 result = -result;
400 temp = (temp & 0x7ff0) >> 4;
401 result *= pow2(((int) temp)-0x3ff);
402 psiconv_debug(lev+1,off,"Float value: %f",result);
403 if (length)
404 *length = 8;
405 if (*status)
406 *status = res;
407 return result;
408 ERROR:
409 psiconv_warn(lev+1,off,"Reading of float failed");
410 if (length)
411 *length = 0;
412 if (*status)
413 *status = res;
414 return 0.0;
415 }
416

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