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

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