/[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 101 - (hide annotations)
Tue Jan 30 23:57:28 2001 UTC (23 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 9325 byte(s)
(Frodo) Added floats

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

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