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

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

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