--- psiconv/trunk/lib/psiconv/parse_simple.c 2000/12/25 22:25:33 79 +++ psiconv/trunk/lib/psiconv/parse_simple.c 2001/07/24 22:44:59 132 @@ -21,10 +21,23 @@ #include "compat.h" #include +#include #include "parse_routines.h" #include "error.h" +/* Very inefficient, but good enough for now. By implementing it ourselves, + we do not have to link with -lm */ +psiconv_float_t pow2(int n) +{ + psiconv_float_t res=1.0; + int i; + + for (i = 0; i < (n<0?-n:n); i++) + res *= 2.0; + + return n<0?1/res:res; +} psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, int *status) { @@ -77,6 +90,21 @@ return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); } +psiconv_s32 psiconv_read_sint(const psiconv_buffer buf,int lev,psiconv_u32 off, + int *length,int *status) +{ + int localstatus; + psiconv_u32 temp; + + temp=psiconv_read_u32(buf,lev,off,&localstatus); + if (status) + *status = localstatus; + if (length) + *length = localstatus?0:4; + + return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1); +} + psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, int *length,int *status) { @@ -94,7 +122,7 @@ goto ERROR; len = 1; psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); - } else if ((temp & 0x07) == 0x03) { + } else if ((temp & 0x07) == 0x05) { res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; if (localstatus) goto ERROR; @@ -303,3 +331,101 @@ *length = 0; return NULL; } + +psiconv_string_t psiconv_read_short_string(const psiconv_buffer buf, + int lev, + psiconv_u32 off,int *length, int *status) +{ + int stringlen,i,len,localstatus; + psiconv_string_t result; + char *res_copy; + + psiconv_progress(lev+1,off,"Going to read a short string"); + + stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus); + if (localstatus) + goto ERROR1; + psiconv_debug(lev+2,off,"Length: %i",stringlen); + len = 1; + + result = malloc(stringlen + 1); + if (!result) + goto ERROR1; + for (i = 0; (i < stringlen) && !localstatus; i++) + result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); + if (localstatus) + goto ERROR2; + result[stringlen] = 0; + len += stringlen; + + res_copy = psiconv_make_printable(result); + if (!res_copy) + goto ERROR2; + psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); + free(res_copy); + + if (length) + *length = len; + + if (status) + *status = 0; + + psiconv_progress(lev+1,off+len-1,"End of short string (total length: %08x)", + len); + + return result; + + +ERROR2: + free(result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of short string failed"); + if (status) + *status = localstatus; + if (length) + *length = 0; + return NULL; +} + +psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, int *status) +{ + psiconv_float_t result,bitvalue; + int res,bit; + psiconv_u32 temp=0; + + psiconv_progress(lev+1,off,"Going to read a float"); + + bitvalue = 0.5; + result = 1.0; + for (bit = 0x33; bit > 0; bit--) { + if ((bit == 0x33) || ((bit & 0x07) == 0x07)) + temp = psiconv_read_u8(buf,lev+2,off+ (bit >> 3),&res); + if (res) + goto ERROR; + if (temp & (0x01 << (bit & 0x07))) + result += bitvalue; + bitvalue /= 2.0; + } + temp = psiconv_read_u16(buf,lev+2,off+6,&res); + if (res) + goto ERROR; + if (temp & 0x8000) + result = -result; + temp = (temp & 0x7ff0) >> 4; + result *= pow2(((int) temp)-0x3ff); + psiconv_debug(lev+1,off,"Float value: %f",result); + if (length) + *length = 8; + if (*status) + *status = res; + return result; +ERROR: + psiconv_warn(lev+1,off,"Reading of float failed"); + if (length) + *length = 0; + if (*status) + *status = res; + return 0.0; +} +