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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 168 - (hide annotations)
Tue Nov 25 17:57:05 2003 UTC (20 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 12022 byte(s)
(Frodo) config stuff and image generation stuff

* All parse and generate functions have a new config parameter
* New files configuration.[ch] in the psiconv lib
* Some image generation stuff (not ready, but won't do any harm)

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

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