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

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

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