… | |
… | |
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 | #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 */ |
|
|
35 | psiconv_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 | } |
29 | psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, |
45 | psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, |
30 | int *status) |
46 | int *status) |
31 | { |
47 | { |
32 | psiconv_u8 *ptr; |
48 | psiconv_u8 *ptr; |
33 | ptr = psiconv_buffer_get(buf,off); |
49 | ptr = psiconv_buffer_get(buf,off); |
… | |
… | |
76 | if (status) |
92 | if (status) |
77 | *status = 0; |
93 | *status = 0; |
78 | return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); |
94 | return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); |
79 | } |
95 | } |
80 | |
96 | |
|
|
97 | psiconv_s32 psiconv_read_sint(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(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 | |
81 | psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, |
112 | psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, |
82 | int *length,int *status) |
113 | int *length,int *status) |
83 | { |
114 | { |
84 | psiconv_u8 temp; |
115 | psiconv_u8 temp; |
85 | psiconv_S_t res; |
116 | psiconv_S_t res; |
… | |
… | |
93 | res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; |
124 | res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; |
94 | if (localstatus) |
125 | if (localstatus) |
95 | goto ERROR; |
126 | goto ERROR; |
96 | len = 1; |
127 | len = 1; |
97 | psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
128 | psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
98 | } else if ((temp & 0x07) == 0x03) { |
129 | } else if ((temp & 0x07) == 0x05) { |
99 | res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; |
130 | res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; |
100 | if (localstatus) |
131 | if (localstatus) |
101 | goto ERROR; |
132 | goto ERROR; |
102 | len = 2; |
133 | len = 2; |
103 | psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
134 | psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
… | |
… | |
384 | if (res) |
415 | if (res) |
385 | goto ERROR; |
416 | goto ERROR; |
386 | if (temp & 0x8000) |
417 | if (temp & 0x8000) |
387 | result = -result; |
418 | result = -result; |
388 | temp = (temp & 0x7ff0) >> 4; |
419 | temp = (temp & 0x7ff0) >> 4; |
389 | result *= pow(2.0,((int) temp)-0x3ff); |
420 | result *= pow2(((int) temp)-0x3ff); |
390 | psiconv_debug(lev+1,off,"Float value: %f",result); |
421 | psiconv_debug(lev+1,off,"Float value: %f",result); |
391 | if (length) |
422 | if (length) |
392 | *length = 8; |
423 | *length = 8; |
393 | if (*status) |
424 | if (*status) |
394 | *status = res; |
425 | *status = res; |
… | |
… | |
399 | *length = 0; |
430 | *length = 0; |
400 | if (*status) |
431 | if (*status) |
401 | *status = res; |
432 | *status = res; |
402 | return 0.0; |
433 | return 0.0; |
403 | } |
434 | } |
|
|
435 | |