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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 54 Revision 171
1/* 1/*
2 parse_simple.c - Part of psiconv, a PSION 5 file formats converter 2 parse_simple.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl>
4 4
5 This program is free software; you can redistribute it and/or modify 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 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 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
16 along with this program; if not, write to the Free Software 16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/ 18*/
19 19
20#include "config.h" 20#include "config.h"
21#include "compat.h"
22
21#include <stdlib.h> 23#include <stdlib.h>
24#include <math.h>
22 25
23#include "parse_routines.h" 26#include "parse_routines.h"
27#include "error.h"
24 28
29#ifdef DMALLOC
30#include <dmalloc.h>
31#endif
32
33static 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 */
37psiconv_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}
25psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off) 47psiconv_u8 psiconv_read_u8(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
48 int *status)
26{ 49{
27 psiconv_u8 *ptr; 50 psiconv_u8 *ptr;
28 ptr = psiconv_list_get(buf,off); 51 ptr = psiconv_buffer_get(buf,off);
29 if (!ptr) 52 if (!ptr) {
30 psiconv_fatal(lev,off,"Trying byte read past the end of the file"); 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;
31 return *ptr; 60 return *ptr;
32} 61}
33 62
34psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off) 63psiconv_u16 psiconv_read_u16(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
64 int *status)
35{ 65{
36 psiconv_u8 *ptr0,*ptr1; 66 psiconv_u8 *ptr0,*ptr1;
37 ptr0 = psiconv_list_get(buf,off); 67 ptr0 = psiconv_buffer_get(buf,off);
38 ptr1 = psiconv_list_get(buf,off+1); 68 ptr1 = psiconv_buffer_get(buf,off+1);
39 if (!ptr0 || !ptr1) 69 if (!ptr0 || !ptr1) {
40 psiconv_fatal(lev,off,"Trying word read past the end of the file"); 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;
41 return *ptr0 + (*ptr1 << 8); 77 return *ptr0 + (*ptr1 << 8);
42} 78}
43 79
44psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off) 80psiconv_u32 psiconv_read_u32(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
81 int *status)
45{ 82{
46 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 83 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
47 ptr0 = psiconv_list_get(buf,off); 84 ptr0 = psiconv_buffer_get(buf,off);
48 ptr1 = psiconv_list_get(buf,off+1); 85 ptr1 = psiconv_buffer_get(buf,off+1);
49 ptr2 = psiconv_list_get(buf,off+2); 86 ptr2 = psiconv_buffer_get(buf,off+2);
50 ptr3 = psiconv_list_get(buf,off+3); 87 ptr3 = psiconv_buffer_get(buf,off+3);
51 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) 88 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
52 psiconv_fatal(lev,off,"Trying long read past the end of the file"); 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;
53 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); 96 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
54} 97}
55 98
99psiconv_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
56psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, 114psiconv_S_t psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
57 int *length) 115 int *length,int *status)
58{ 116{
59 psiconv_u8 temp; 117 psiconv_u8 temp;
60 psiconv_S_t res; 118 psiconv_S_t res;
61 int len; 119 int len,localstatus;
62 120
63 psiconv_progress(lev+1,off,"Going to read a S length indicator"); 121 psiconv_progress(config,lev+1,off,"Going to read a S length indicator");
64 temp = psiconv_read_u8(buf,lev+2,off); 122 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
123 if (localstatus)
124 goto ERROR;
65 if ((temp & 0x03) == 0x02) { 125 if ((temp & 0x03) == 0x02) {
66 res = psiconv_read_u8(buf,lev+2,off) >> 2; 126 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 2;
127 if (localstatus)
128 goto ERROR;
67 len = 1; 129 len = 1;
68 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 130 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
69 } else if ((temp & 0x07) == 0x03) { 131 } else if ((temp & 0x07) == 0x05) {
70 res = psiconv_read_u16(buf,lev+2,off) >> 3; 132 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 3;
133 if (localstatus)
134 goto ERROR;
71 len = 2; 135 len = 2;
72 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 136 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
73 } else { 137 } else {
74 psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); 138 psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!");
75 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 139 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
76 res = 0; 140 goto ERROR;
77 len = 1;
78 } 141 }
79 142
80 if (length) 143 if (length)
81 *length = len; 144 *length = len;
145 if (status)
146 *status = 0;
82 147
83 psiconv_progress(lev+1,off+len-1, 148 psiconv_progress(config,lev+1,off+len-1,
84 "End of S length indicator (total length: %08x)", len); 149 "End of S length indicator (total length: %08x)", len);
85 150
86 return res; 151 return res;
87}
88 152
153ERROR:
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
89psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off, 162psiconv_X_t psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
90 int *length) 163 int *length, int *status)
91{ 164{
92 psiconv_u8 temp; 165 psiconv_u8 temp;
93 psiconv_X_t res; 166 psiconv_X_t res;
94 int len; 167 int len,localstatus;
95 168
96 psiconv_progress(lev+1,off,"Going to read a X length indicator"); 169 psiconv_progress(config,lev+1,off,"Going to read a X length indicator");
97 temp = psiconv_read_u8(buf,lev+2,off); 170 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
171 if (localstatus)
172 goto ERROR;
98 if ((temp & 0x01) == 0x00) { 173 if ((temp & 0x01) == 0x00) {
99 res = psiconv_read_u8(buf,lev+2,off) >> 1; 174 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 1;
175 if (localstatus)
176 goto ERROR;
100 len = 1; 177 len = 1;
101 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 178 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
102 } else if ((temp & 0x03) == 0x01) { 179 } else if ((temp & 0x03) == 0x01) {
103 res = psiconv_read_u16(buf,lev+2,off) >> 2; 180 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 2;
181 if (localstatus)
182 goto ERROR;
104 len = 2; 183 len = 2;
105 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 184 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
106 } else if ((temp & 0x07) == 0x03) { 185 } else if ((temp & 0x07) == 0x03) {
107 res = psiconv_read_u32(buf,lev+2,off) >> 3; 186 res = psiconv_read_u32(config,buf,lev+2,off,&localstatus) >> 3;
187 if (localstatus)
188 goto ERROR;
108 len = 4; 189 len = 4;
109 psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); 190 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res);
110 } else { 191 } else {
111 psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); 192 psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!");
112 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 193 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
113 res = 0; 194 goto ERROR;
114 len = 1;
115 } 195 }
116 196
117 if (length) 197 if (length)
118 *length = len; 198 *length = len;
199 if (status)
200 *status = 0;
119 201
120 psiconv_progress(lev+1,off+len-1, 202 psiconv_progress(config,lev+1,off+len-1,
121 "End of X length indicator (total length: %08x)", len); 203 "End of X length indicator (total length: %08x)", len);
122 204
123 return res; 205 return res;
124}
125 206
207ERROR:
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
126psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev, 216psiconv_length_t psiconv_read_length(const psiconv_config config,const psiconv_buffer buf, int lev,
127 psiconv_u32 off, int *length) 217 psiconv_u32 off, int *length, int *status)
128{ 218{
129 psiconv_length_t res; 219 psiconv_length_t res;
220 int localstatus;
221
130 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off)); 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 }
131 psiconv_debug(lev+1,off,"Length: %f",res); 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
240psiconv_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;
132 if (length) 257 if (length)
133 *length = 4; 258 *length = 4;
134 return res; 259 return res;
135} 260}
136 261
137psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev,
138 psiconv_u32 off, int *length)
139{
140 psiconv_size_t res;
141 res = ((psiconv_s32) psiconv_read_u32(buf,lev,off)) / 20.0;
142 psiconv_debug(lev+1,off,"Size: %f",res);
143 if (length)
144 *length = 4;
145 return res;
146}
147
148int psiconv_parse_bool(const psiconv_buffer buf, int lev, psiconv_u32 off, 262int psiconv_parse_bool(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
149 int *length, psiconv_bool_t *result) 263 int *length, psiconv_bool_t *result)
150{ 264{
151 psiconv_u8 temp; 265 psiconv_u8 temp;
266 int localstatus;
152 temp = psiconv_read_u8(buf,lev,off); 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 }
153 if (length) 274 if (length)
154 *length = 1; 275 *length = 1;
155 if (temp == 0) { 276 if (temp == 0) {
156 *result = psiconv_bool_false; 277 *result = psiconv_bool_false;
157 return 0; 278 return 0;
158 } else if (temp == 1) { 279 } else if (temp == 1) {
159 *result = psiconv_bool_true; 280 *result = psiconv_bool_true;
160 return 0; 281 return 0;
161 } 282 }
162 psiconv_warn(lev+1,off,"Unknown value for boolean"); 283 psiconv_warn(config,lev+1,off,"Unknown value for boolean");
163 psiconv_debug(lev+1,off,"Boolean value: %02x",temp); 284 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp);
164 *result = psiconv_bool_true; 285 *result = psiconv_bool_true;
165 return -1; 286 return 0;
166} 287}
167 288
168psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev, 289psiconv_string_t psiconv_read_string(const psiconv_config config,const psiconv_buffer buf,int lev,
169 psiconv_u32 off,int *length) 290 psiconv_u32 off,int *length, int *status)
170{ 291{
171 int stringlen,i,leng,len; 292 int stringlen,i,leng,len,localstatus;
172 psiconv_string_t result; 293 psiconv_string_t result;
173 char *res_copy; 294 char *res_copy;
174 295
175 psiconv_progress(lev+1,off,"Going to read a string"); 296 psiconv_progress(config,lev+1,off,"Going to read a string");
176 297
177 stringlen = psiconv_read_S(buf,lev+2,off,&leng); 298 stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus);
299 if (localstatus)
300 goto ERROR1;
178 psiconv_debug(lev+2,off,"Length: %i",stringlen); 301 psiconv_debug(config,lev+2,off,"Length: %i",stringlen);
179 len = leng; 302 len = leng;
180 303
181 result = malloc(stringlen + 1); 304 result = malloc(stringlen + 1);
305 if (!result)
306 goto ERROR1;
182 for (i = 0; i < stringlen; i++) 307 for (i = 0; (i < stringlen) && !localstatus; i++)
183 result[i] = psiconv_read_u8(buf,lev,off+i+len); 308 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus);
309 if (localstatus)
310 goto ERROR2;
184 result[stringlen] = 0; 311 result[stringlen] = 0;
185 len += stringlen; 312 len += stringlen;
186 313
187 res_copy = psiconv_make_printable(result); 314 res_copy = psiconv_make_printable(result);
315 if (!res_copy)
316 goto ERROR2;
188 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); 317 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
189 free(res_copy); 318 free(res_copy);
190 319
191 if (length) 320 if (length)
192 *length = len; 321 *length = len;
193 322
323 if (status)
324 *status = 0;
325
194 psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len); 326 psiconv_progress(config,lev+1,off+len-1,"End of string (total length: %08x)",len);
195 327
196 return result; 328 return result;
329
330ERROR2:
331 free(result);
332ERROR1:
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;
197} 339}
340
341psiconv_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
385ERROR2:
386 free(result);
387ERROR1:
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
396psiconv_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;
429ERROR:
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

Legend:
Removed from v.54  
changed lines
  Added in v.171

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