/[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 171 - (hide annotations)
Wed Nov 26 20:56:17 2003 UTC (20 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 12059 byte(s)
(Frodo) Support MBM output format

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

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