--- psiconv/trunk/lib/psiconv/parse_simple.c 2003/11/25 17:57:05 168 +++ psiconv/trunk/lib/psiconv/parse_simple.c 2004/01/26 12:59:54 188 @@ -25,11 +25,18 @@ #include "parse_routines.h" #include "error.h" +#include "unicode.h" #ifdef DMALLOC #include #endif +static psiconv_float_t pow2(int n); +static psiconv_string_t psiconv_read_string_aux(const psiconv_config config, + const psiconv_buffer buf,int lev, + psiconv_u32 off,int *length, int *status, + int kind); + /* 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) @@ -48,7 +55,7 @@ psiconv_u8 *ptr; ptr = psiconv_buffer_get(buf,off); if (!ptr) { - psiconv_warn(config,lev,off,"Trying byte read past the end of the file"); + psiconv_error(config,lev,off,"Trying byte read past the end of the file"); if (status) *status = -PSICONV_E_PARSE; return 0; @@ -65,7 +72,7 @@ ptr0 = psiconv_buffer_get(buf,off); ptr1 = psiconv_buffer_get(buf,off+1); if (!ptr0 || !ptr1) { - psiconv_warn(config,lev,off,"Trying word read past the end of the file"); + psiconv_error(config,lev,off,"Trying word read past the end of the file"); if (status) *status = -PSICONV_E_PARSE; return 0; @@ -84,7 +91,7 @@ ptr2 = psiconv_buffer_get(buf,off+2); ptr3 = psiconv_buffer_get(buf,off+3); if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { - psiconv_warn(config,lev,off,"Trying long read past the end of the file"); + psiconv_error(config,lev,off,"Trying long read past the end of the file"); if (status) *status = -PSICONV_E_PARSE; return 0; @@ -109,11 +116,11 @@ return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1); } -psiconv_S_t psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, +psiconv_u32 psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, int *length,int *status) { psiconv_u8 temp; - psiconv_S_t res; + psiconv_u32 res; int len,localstatus; psiconv_progress(config,lev+1,off,"Going to read a S length indicator"); @@ -133,7 +140,7 @@ len = 2; psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res); } else { - psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!"); + psiconv_error(config,lev+2,off,"S indicator: unknown encoding!"); psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp); goto ERROR; } @@ -149,7 +156,7 @@ return res; ERROR: - psiconv_warn(config,lev+1,off,"Reading of S indicator failed"); + psiconv_error(config,lev+1,off,"Reading of S indicator failed"); if (status) *status = localstatus; if (length) @@ -157,11 +164,11 @@ return 0; } -psiconv_X_t psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, +psiconv_u32 psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, int *length, int *status) { psiconv_u8 temp; - psiconv_X_t res; + psiconv_u32 res; int len,localstatus; psiconv_progress(config,lev+1,off,"Going to read a X length indicator"); @@ -187,7 +194,7 @@ len = 4; psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res); } else { - psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!"); + psiconv_error(config,lev+2,off,"X indicator: unknown encoding!"); psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp); goto ERROR; } @@ -203,7 +210,7 @@ return res; ERROR: - psiconv_warn(config,lev+1,off,"Reading of X indicator failed"); + psiconv_error(config,lev+1,off,"Reading of X indicator failed"); if (status) *status = localstatus; if (length) @@ -220,7 +227,7 @@ res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(config,buf,lev,off, &localstatus)); if (localstatus) { - psiconv_warn(config,lev+1,off,"Reading of length failed"); + psiconv_error(config,lev+1,off,"Reading of length failed"); if (length) *length = 0; if (status) @@ -242,7 +249,7 @@ int localstatus; res = ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,&localstatus)) / 20.0; if (localstatus) { - psiconv_warn(config,lev+1,off,"Reading of size failed"); + psiconv_error(config,lev+1,off,"Reading of size failed"); if (length) *length = 0; if (status) @@ -264,7 +271,7 @@ int localstatus; temp = psiconv_read_u8(config,buf,lev,off,&localstatus); if (localstatus) { - psiconv_warn(config,lev+1,off,"Reading of bool failed"); + psiconv_error(config,lev+1,off,"Reading of bool failed"); if (length) *length = 0; return localstatus; @@ -284,85 +291,77 @@ return 0; } -psiconv_string_t psiconv_read_string(const psiconv_config config,const psiconv_buffer buf,int lev, +psiconv_string_t psiconv_read_string(const psiconv_config config, + const psiconv_buffer buf,int lev, psiconv_u32 off,int *length, int *status) { - int stringlen,i,leng,len,localstatus; - psiconv_string_t result; - char *res_copy; - - psiconv_progress(config,lev+1,off,"Going to read a string"); - - stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus); - if (localstatus) - goto ERROR1; - psiconv_debug(config,lev+2,off,"Length: %i",stringlen); - len = leng; - - result = malloc(stringlen + 1); - if (!result) - goto ERROR1; - for (i = 0; (i < stringlen) && !localstatus; i++) - result[i] = psiconv_read_u8(config,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(config,lev+2,off,"Contents: `%s'",res_copy); - free(res_copy); - - if (length) - *length = len; - - if (status) - *status = 0; - - psiconv_progress(config,lev+1,off+len-1,"End of string (total length: %08x)",len); + return psiconv_read_string_aux(config,buf,lev,off,length,status,-1); +} - return result; +psiconv_string_t psiconv_read_short_string(const psiconv_config config, + const psiconv_buffer buf,int lev, + psiconv_u32 off,int *length, int *status) +{ + return psiconv_read_string_aux(config,buf,lev,off,length,status,-2); +} -ERROR2: - free(result); -ERROR1: - psiconv_warn(config,lev+1,off,"Reading of string failed"); - if (status) - *status = localstatus; - if (length) - *length = 0; - return NULL; +psiconv_string_t psiconv_read_charlist(const psiconv_config config, + const psiconv_buffer buf, int lev, + psiconv_u32 off, int nrofchars, + int *status) +{ + int length; + if (nrofchars <= 0) { + psiconv_error(config,lev,off, + "psiconv_read_charlist called with non-positive nrofchars"); + if (status) + *status = -PSICONV_E_OTHER; + return NULL; + } + return psiconv_read_string_aux(config,buf,lev,off,&length,status,nrofchars); } -psiconv_string_t psiconv_read_short_string(const psiconv_config config, const psiconv_buffer buf, - int lev, - psiconv_u32 off,int *length, int *status) + +psiconv_string_t psiconv_read_string_aux(const psiconv_config config, + const psiconv_buffer buf,int lev, + psiconv_u32 off,int *length, int *status, + int kind) { - int stringlen,i,len,localstatus; + int stringlen,i,leng,len,localstatus; psiconv_string_t result; char *res_copy; + psiconv_u8 temp; - psiconv_progress(config,lev+1,off,"Going to read a short string"); + psiconv_progress(config,lev+1,off,"Going to read a string"); - stringlen = psiconv_read_u8(config,buf,lev+2,off,&localstatus); + if (kind == -1) + stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus); + else if (kind == -2) { + stringlen = psiconv_read_u8(config,buf,lev+2,off,&localstatus); + leng = 1; + } else { + stringlen = kind; + leng = 0; + localstatus = 0; + } if (localstatus) goto ERROR1; psiconv_debug(config,lev+2,off,"Length: %i",stringlen); - len = 1; + len = leng; - result = malloc(stringlen + 1); + result = malloc(sizeof(*result) * (stringlen + 1)); if (!result) goto ERROR1; - for (i = 0; (i < stringlen) && !localstatus; i++) - result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus); - if (localstatus) - goto ERROR2; + for (i = 0; i < stringlen; i++) { + temp = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus); + if (localstatus) + goto ERROR2; + result[i] = psiconv_unicode_from_char(config,temp); + } result[stringlen] = 0; len += stringlen; - res_copy = psiconv_make_printable(result); + res_copy = psiconv_make_printable(config,result); if (!res_copy) goto ERROR2; psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy); @@ -374,16 +373,14 @@ if (status) *status = 0; - psiconv_progress(config,lev+1,off+len-1,"End of short string (total length: %08x)", - len); + psiconv_progress(config,lev+1,off+len-1,"End of string (total length: %08x)",len); return result; - ERROR2: free(result); ERROR1: - psiconv_warn(config,lev+1,off,"Reading of short string failed"); + psiconv_error(config,lev+1,off,"Reading of string failed"); if (status) *status = localstatus; if (length) @@ -425,7 +422,7 @@ *status = res; return result; ERROR: - psiconv_warn(config,lev+1,off,"Reading of float failed"); + psiconv_error(config,lev+1,off,"Reading of float failed"); if (length) *length = 0; if (*status)