/[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 79 Revision 168
19 19
20#include "config.h" 20#include "config.h"
21#include "compat.h" 21#include "compat.h"
22 22
23#include <stdlib.h> 23#include <stdlib.h>
24#include <math.h>
24 25
25#include "parse_routines.h" 26#include "parse_routines.h"
26#include "error.h" 27#include "error.h"
27 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}
28psiconv_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,
29 int *status) 46 int *status)
30{ 47{
31 psiconv_u8 *ptr; 48 psiconv_u8 *ptr;
32 ptr = psiconv_buffer_get(buf,off); 49 ptr = psiconv_buffer_get(buf,off);
33 if (!ptr) { 50 if (!ptr) {
34 psiconv_warn(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");
35 if (status) 52 if (status)
36 *status = -PSICONV_E_PARSE; 53 *status = -PSICONV_E_PARSE;
37 return 0; 54 return 0;
38 } 55 }
39 if (status) 56 if (status)
40 *status = 0; 57 *status = 0;
41 return *ptr; 58 return *ptr;
42} 59}
43 60
44psiconv_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,
45 int *status) 62 int *status)
46{ 63{
47 psiconv_u8 *ptr0,*ptr1; 64 psiconv_u8 *ptr0,*ptr1;
48 ptr0 = psiconv_buffer_get(buf,off); 65 ptr0 = psiconv_buffer_get(buf,off);
49 ptr1 = psiconv_buffer_get(buf,off+1); 66 ptr1 = psiconv_buffer_get(buf,off+1);
50 if (!ptr0 || !ptr1) { 67 if (!ptr0 || !ptr1) {
51 psiconv_warn(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");
52 if (status) 69 if (status)
53 *status = -PSICONV_E_PARSE; 70 *status = -PSICONV_E_PARSE;
54 return 0; 71 return 0;
55 } 72 }
56 if (status) 73 if (status)
57 *status = 0; 74 *status = 0;
58 return *ptr0 + (*ptr1 << 8); 75 return *ptr0 + (*ptr1 << 8);
59} 76}
60 77
61psiconv_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,
62 int *status) 79 int *status)
63{ 80{
64 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 81 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
65 ptr0 = psiconv_buffer_get(buf,off); 82 ptr0 = psiconv_buffer_get(buf,off);
66 ptr1 = psiconv_buffer_get(buf,off+1); 83 ptr1 = psiconv_buffer_get(buf,off+1);
67 ptr2 = psiconv_buffer_get(buf,off+2); 84 ptr2 = psiconv_buffer_get(buf,off+2);
68 ptr3 = psiconv_buffer_get(buf,off+3); 85 ptr3 = psiconv_buffer_get(buf,off+3);
69 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 86 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
70 psiconv_warn(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");
71 if (status) 88 if (status)
72 *status = -PSICONV_E_PARSE; 89 *status = -PSICONV_E_PARSE;
73 return 0; 90 return 0;
74 } 91 }
75 if (status) 92 if (status)
76 *status = 0; 93 *status = 0;
77 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); 94 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
78} 95}
79 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
80psiconv_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,
81 int *length,int *status) 113 int *length,int *status)
82{ 114{
83 psiconv_u8 temp; 115 psiconv_u8 temp;
84 psiconv_S_t res; 116 psiconv_S_t res;
85 int len,localstatus; 117 int len,localstatus;
86 118
87 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");
88 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 120 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
89 if (localstatus) 121 if (localstatus)
90 goto ERROR; 122 goto ERROR;
91 if ((temp & 0x03) == 0x02) { 123 if ((temp & 0x03) == 0x02) {
92 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; 124 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 2;
93 if (localstatus) 125 if (localstatus)
94 goto ERROR; 126 goto ERROR;
95 len = 1; 127 len = 1;
96 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 128 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
97 } else if ((temp & 0x07) == 0x03) { 129 } else if ((temp & 0x07) == 0x05) {
98 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; 130 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 3;
99 if (localstatus) 131 if (localstatus)
100 goto ERROR; 132 goto ERROR;
101 len = 2; 133 len = 2;
102 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 134 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
103 } else { 135 } else {
104 psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); 136 psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!");
105 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 137 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
106 goto ERROR; 138 goto ERROR;
107 } 139 }
108 140
109 if (length) 141 if (length)
110 *length = len; 142 *length = len;
111 if (status) 143 if (status)
112 *status = 0; 144 *status = 0;
113 145
114 psiconv_progress(lev+1,off+len-1, 146 psiconv_progress(config,lev+1,off+len-1,
115 "End of S length indicator (total length: %08x)", len); 147 "End of S length indicator (total length: %08x)", len);
116 148
117 return res; 149 return res;
118 150
119ERROR: 151ERROR:
120 psiconv_warn(lev+1,off,"Reading of S indicator failed"); 152 psiconv_warn(config,lev+1,off,"Reading of S indicator failed");
121 if (status) 153 if (status)
122 *status = localstatus; 154 *status = localstatus;
123 if (length) 155 if (length)
124 *length = 0; 156 *length = 0;
125 return 0; 157 return 0;
126} 158}
127 159
128psiconv_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,
129 int *length, int *status) 161 int *length, int *status)
130{ 162{
131 psiconv_u8 temp; 163 psiconv_u8 temp;
132 psiconv_X_t res; 164 psiconv_X_t res;
133 int len,localstatus; 165 int len,localstatus;
134 166
135 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");
136 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 168 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
137 if (localstatus) 169 if (localstatus)
138 goto ERROR; 170 goto ERROR;
139 if ((temp & 0x01) == 0x00) { 171 if ((temp & 0x01) == 0x00) {
140 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 1; 172 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 1;
141 if (localstatus) 173 if (localstatus)
142 goto ERROR; 174 goto ERROR;
143 len = 1; 175 len = 1;
144 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 176 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
145 } else if ((temp & 0x03) == 0x01) { 177 } else if ((temp & 0x03) == 0x01) {
146 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 2; 178 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 2;
147 if (localstatus) 179 if (localstatus)
148 goto ERROR; 180 goto ERROR;
149 len = 2; 181 len = 2;
150 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 182 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
151 } else if ((temp & 0x07) == 0x03) { 183 } else if ((temp & 0x07) == 0x03) {
152 res = psiconv_read_u32(buf,lev+2,off,&localstatus) >> 3; 184 res = psiconv_read_u32(config,buf,lev+2,off,&localstatus) >> 3;
153 if (localstatus) 185 if (localstatus)
154 goto ERROR; 186 goto ERROR;
155 len = 4; 187 len = 4;
156 psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); 188 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res);
157 } else { 189 } else {
158 psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); 190 psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!");
159 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 191 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
160 goto ERROR; 192 goto ERROR;
161 } 193 }
162 194
163 if (length) 195 if (length)
164 *length = len; 196 *length = len;
165 if (status) 197 if (status)
166 *status = 0; 198 *status = 0;
167 199
168 psiconv_progress(lev+1,off+len-1, 200 psiconv_progress(config,lev+1,off+len-1,
169 "End of X length indicator (total length: %08x)", len); 201 "End of X length indicator (total length: %08x)", len);
170 202
171 return res; 203 return res;
172 204
173ERROR: 205ERROR:
174 psiconv_warn(lev+1,off,"Reading of X indicator failed"); 206 psiconv_warn(config,lev+1,off,"Reading of X indicator failed");
175 if (status) 207 if (status)
176 *status = localstatus; 208 *status = localstatus;
177 if (length) 209 if (length)
178 *length = 0; 210 *length = 0;
179 return 0; 211 return 0;
180} 212}
181 213
182psiconv_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,
183 psiconv_u32 off, int *length, int *status) 215 psiconv_u32 off, int *length, int *status)
184{ 216{
185 psiconv_length_t res; 217 psiconv_length_t res;
186 int localstatus; 218 int localstatus;
187 219
188 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,
189 &localstatus)); 221 &localstatus));
190 if (localstatus) { 222 if (localstatus) {
191 psiconv_warn(lev+1,off,"Reading of length failed"); 223 psiconv_warn(config,lev+1,off,"Reading of length failed");
192 if (length) 224 if (length)
193 *length = 0; 225 *length = 0;
194 if (status) 226 if (status)
195 *status = localstatus; 227 *status = localstatus;
196 return 0; 228 return 0;
197 } 229 }
198 psiconv_debug(lev+1,off,"Length: %f",res); 230 psiconv_debug(config,lev+1,off,"Length: %f",res);
199 if (length) 231 if (length)
200 *length = 4; 232 *length = 4;
201 if (status) 233 if (status)
202 *status = 0; 234 *status = 0;
203 return res; 235 return res;
204} 236}
205 237
206psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev, 238psiconv_size_t psiconv_read_size(const psiconv_config config,const psiconv_buffer buf, int lev,
207 psiconv_u32 off, int *length, int *status) 239 psiconv_u32 off, int *length, int *status)
208{ 240{
209 psiconv_size_t res; 241 psiconv_size_t res;
210 int localstatus; 242 int localstatus;
211 res = ((psiconv_s32) psiconv_read_u32(buf,lev,off,&localstatus)) / 20.0; 243 res = ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,&localstatus)) / 20.0;
212 if (localstatus) { 244 if (localstatus) {
213 psiconv_warn(lev+1,off,"Reading of size failed"); 245 psiconv_warn(config,lev+1,off,"Reading of size failed");
214 if (length) 246 if (length)
215 *length = 0; 247 *length = 0;
216 if (status) 248 if (status)
217 *status = localstatus; 249 *status = localstatus;
218 return 0; 250 return 0;
219 } 251 }
220 psiconv_debug(lev+1,off,"Size: %f",res); 252 psiconv_debug(config,lev+1,off,"Size: %f",res);
221 if (status) 253 if (status)
222 *status = 0; 254 *status = 0;
223 if (length) 255 if (length)
224 *length = 4; 256 *length = 4;
225 return res; 257 return res;
226} 258}
227 259
228int 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,
229 int *length, psiconv_bool_t *result) 261 int *length, psiconv_bool_t *result)
230{ 262{
231 psiconv_u8 temp; 263 psiconv_u8 temp;
232 int localstatus; 264 int localstatus;
233 temp = psiconv_read_u8(buf,lev,off,&localstatus); 265 temp = psiconv_read_u8(config,buf,lev,off,&localstatus);
234 if (localstatus) { 266 if (localstatus) {
235 psiconv_warn(lev+1,off,"Reading of bool failed"); 267 psiconv_warn(config,lev+1,off,"Reading of bool failed");
236 if (length) 268 if (length)
237 *length = 0; 269 *length = 0;
238 return localstatus; 270 return localstatus;
239 } 271 }
240 if (length) 272 if (length)
244 return 0; 276 return 0;
245 } else if (temp == 1) { 277 } else if (temp == 1) {
246 *result = psiconv_bool_true; 278 *result = psiconv_bool_true;
247 return 0; 279 return 0;
248 } 280 }
249 psiconv_warn(lev+1,off,"Unknown value for boolean"); 281 psiconv_warn(config,lev+1,off,"Unknown value for boolean");
250 psiconv_debug(lev+1,off,"Boolean value: %02x",temp); 282 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp);
251 *result = psiconv_bool_true; 283 *result = psiconv_bool_true;
252 return 0; 284 return 0;
253} 285}
254 286
255psiconv_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,
256 psiconv_u32 off,int *length, int *status) 288 psiconv_u32 off,int *length, int *status)
257{ 289{
258 int stringlen,i,leng,len,localstatus; 290 int stringlen,i,leng,len,localstatus;
259 psiconv_string_t result; 291 psiconv_string_t result;
260 char *res_copy; 292 char *res_copy;
261 293
262 psiconv_progress(lev+1,off,"Going to read a string"); 294 psiconv_progress(config,lev+1,off,"Going to read a string");
263 295
264 stringlen = psiconv_read_S(buf,lev+2,off,&leng,&localstatus); 296 stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus);
265 if (localstatus) 297 if (localstatus)
266 goto ERROR1; 298 goto ERROR1;
267 psiconv_debug(lev+2,off,"Length: %i",stringlen); 299 psiconv_debug(config,lev+2,off,"Length: %i",stringlen);
268 len = leng; 300 len = leng;
269 301
270 result = malloc(stringlen + 1); 302 result = malloc(stringlen + 1);
271 if (!result) 303 if (!result)
272 goto ERROR1; 304 goto ERROR1;
273 for (i = 0; (i < stringlen) && !localstatus; i++) 305 for (i = 0; (i < stringlen) && !localstatus; i++)
274 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); 306 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus);
275 if (localstatus) 307 if (localstatus)
276 goto ERROR2; 308 goto ERROR2;
277 result[stringlen] = 0; 309 result[stringlen] = 0;
278 len += stringlen; 310 len += stringlen;
279 311
280 res_copy = psiconv_make_printable(result); 312 res_copy = psiconv_make_printable(result);
281 if (!res_copy) 313 if (!res_copy)
282 goto ERROR2; 314 goto ERROR2;
283 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); 315 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
284 free(res_copy); 316 free(res_copy);
285 317
286 if (length) 318 if (length)
287 *length = len; 319 *length = len;
288 320
289 if (status) 321 if (status)
290 *status = 0; 322 *status = 0;
291 323
292 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);
293 325
294 return result; 326 return result;
295 327
296ERROR2: 328ERROR2:
297 free(result); 329 free(result);
298ERROR1: 330ERROR1:
299 psiconv_warn(lev+1,off,"Reading of string failed"); 331 psiconv_warn(config,lev+1,off,"Reading of string failed");
300 if (status) 332 if (status)
301 *status = localstatus; 333 *status = localstatus;
302 if (length) 334 if (length)
303 *length = 0; 335 *length = 0;
304 return NULL; 336 return NULL;
305} 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.79  
changed lines
  Added in v.168

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