… | |
… | |
24 | #include <math.h> |
24 | #include <math.h> |
25 | |
25 | |
26 | #include "parse_routines.h" |
26 | #include "parse_routines.h" |
27 | #include "error.h" |
27 | #include "error.h" |
28 | |
28 | |
|
|
29 | /* Very inefficient, but good enough for now. By implementing it ourselves, |
|
|
30 | we do not have to link with -lm */ |
|
|
31 | psiconv_float_t pow2(int n) |
|
|
32 | { |
|
|
33 | psiconv_float_t res=1.0; |
|
|
34 | int i; |
|
|
35 | |
|
|
36 | for (i = 0; i < (n<0?-n:n); i++) |
|
|
37 | res *= 2.0; |
|
|
38 | |
|
|
39 | return n<0?1/res:res; |
|
|
40 | } |
29 | psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, |
41 | psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, |
30 | int *status) |
42 | int *status) |
31 | { |
43 | { |
32 | psiconv_u8 *ptr; |
44 | psiconv_u8 *ptr; |
33 | ptr = psiconv_buffer_get(buf,off); |
45 | ptr = psiconv_buffer_get(buf,off); |
… | |
… | |
76 | if (status) |
88 | if (status) |
77 | *status = 0; |
89 | *status = 0; |
78 | return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); |
90 | return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); |
79 | } |
91 | } |
80 | |
92 | |
|
|
93 | psiconv_s32 psiconv_read_sint(const psiconv_buffer buf,int lev,psiconv_u32 off, |
|
|
94 | int *length,int *status) |
|
|
95 | { |
|
|
96 | int localstatus; |
|
|
97 | psiconv_u32 temp; |
|
|
98 | |
|
|
99 | temp=psiconv_read_u32(buf,lev,off,&localstatus); |
|
|
100 | if (status) |
|
|
101 | *status = localstatus; |
|
|
102 | if (length) |
|
|
103 | *length = localstatus?0:4; |
|
|
104 | |
|
|
105 | return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1); |
|
|
106 | } |
|
|
107 | |
81 | psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, |
108 | psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, |
82 | int *length,int *status) |
109 | int *length,int *status) |
83 | { |
110 | { |
84 | psiconv_u8 temp; |
111 | psiconv_u8 temp; |
85 | psiconv_S_t res; |
112 | psiconv_S_t res; |
… | |
… | |
303 | if (length) |
330 | if (length) |
304 | *length = 0; |
331 | *length = 0; |
305 | return NULL; |
332 | return NULL; |
306 | } |
333 | } |
307 | |
334 | |
|
|
335 | psiconv_string_t psiconv_read_short_string(const psiconv_buffer buf, |
|
|
336 | int lev, |
|
|
337 | psiconv_u32 off,int *length, int *status) |
|
|
338 | { |
|
|
339 | int stringlen,i,len,localstatus; |
|
|
340 | psiconv_string_t result; |
|
|
341 | char *res_copy; |
|
|
342 | |
|
|
343 | psiconv_progress(lev+1,off,"Going to read a short string"); |
|
|
344 | |
|
|
345 | stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus); |
|
|
346 | if (localstatus) |
|
|
347 | goto ERROR1; |
|
|
348 | psiconv_debug(lev+2,off,"Length: %i",stringlen); |
|
|
349 | len = 1; |
|
|
350 | |
|
|
351 | result = malloc(stringlen + 1); |
|
|
352 | if (!result) |
|
|
353 | goto ERROR1; |
|
|
354 | for (i = 0; (i < stringlen) && !localstatus; i++) |
|
|
355 | result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); |
|
|
356 | if (localstatus) |
|
|
357 | goto ERROR2; |
|
|
358 | result[stringlen] = 0; |
|
|
359 | len += stringlen; |
|
|
360 | |
|
|
361 | res_copy = psiconv_make_printable(result); |
|
|
362 | if (!res_copy) |
|
|
363 | goto ERROR2; |
|
|
364 | psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); |
|
|
365 | free(res_copy); |
|
|
366 | |
|
|
367 | if (length) |
|
|
368 | *length = len; |
|
|
369 | |
|
|
370 | if (status) |
|
|
371 | *status = 0; |
|
|
372 | |
|
|
373 | psiconv_progress(lev+1,off+len-1,"End of short string (total length: %08x)", |
|
|
374 | len); |
|
|
375 | |
|
|
376 | return result; |
|
|
377 | |
|
|
378 | |
|
|
379 | ERROR2: |
|
|
380 | free(result); |
|
|
381 | ERROR1: |
|
|
382 | psiconv_warn(lev+1,off,"Reading of short string failed"); |
|
|
383 | if (status) |
|
|
384 | *status = localstatus; |
|
|
385 | if (length) |
|
|
386 | *length = 0; |
|
|
387 | return NULL; |
|
|
388 | } |
|
|
389 | |
308 | psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev, |
390 | psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev, |
309 | psiconv_u32 off, int *length, int *status) |
391 | psiconv_u32 off, int *length, int *status) |
310 | { |
392 | { |
311 | psiconv_float_t result,bitvalue; |
393 | psiconv_float_t result,bitvalue; |
312 | int res,bit; |
394 | int res,bit; |
… | |
… | |
329 | if (res) |
411 | if (res) |
330 | goto ERROR; |
412 | goto ERROR; |
331 | if (temp & 0x8000) |
413 | if (temp & 0x8000) |
332 | result = -result; |
414 | result = -result; |
333 | temp = (temp & 0x7ff0) >> 4; |
415 | temp = (temp & 0x7ff0) >> 4; |
334 | result *= pow(2.0,((int) temp)-0x3ff); |
416 | result *= pow2(((int) temp)-0x3ff); |
335 | psiconv_debug(lev+1,off,"Float value: %f",result); |
417 | psiconv_debug(lev+1,off,"Float value: %f",result); |
336 | if (length) |
418 | if (length) |
337 | *length = 8; |
419 | *length = 8; |
338 | if (*status) |
420 | if (*status) |
339 | *status = res; |
421 | *status = res; |
… | |
… | |
344 | *length = 0; |
426 | *length = 0; |
345 | if (*status) |
427 | if (*status) |
346 | *status = res; |
428 | *status = res; |
347 | return 0.0; |
429 | return 0.0; |
348 | } |
430 | } |
|
|
431 | |