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

Legend:
Removed from v.2  
changed lines
  Added in v.168

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