--- psiconv/trunk/lib/psiconv/parse_simple.c 2004/02/04 12:19:09 196 +++ psiconv/trunk/lib/psiconv/parse_simple.c 2004/02/22 22:24:39 217 @@ -327,46 +327,71 @@ psiconv_u32 off,int *length, int *status, int kind) { - int stringlen,i,leng,len,localstatus; + int bytecount,i,leng,len,localstatus; psiconv_string_t result; char *res_copy; - psiconv_u8 temp; + psiconv_list string; + psiconv_ucs2 nextchar; + psiconv_ucs2 *nextcharptr; psiconv_progress(config,lev+1,off,"Going to read a string"); if (kind == -1) - stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus); + bytecount = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus); else if (kind == -2) { - stringlen = psiconv_read_u8(config,buf,lev+2,off,&localstatus); + bytecount = psiconv_read_u8(config,buf,lev+2,off,&localstatus); leng = 1; } else { - stringlen = kind; + bytecount = kind; leng = 0; localstatus = 0; } if (localstatus) goto ERROR1; - psiconv_debug(config,lev+2,off,"Length: %i",stringlen); + psiconv_debug(config,lev+2,off,"Length: %i",bytecount); len = leng; - result = malloc(sizeof(*result) * (stringlen + 1)); - if (!result) + if (!(string = psiconv_list_new(sizeof(*result)))) goto ERROR1; - for (i = 0; i < stringlen; i++) { - temp = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus); + + /* Read the string into a temporary list */ + i = 0; + while (i < bytecount) { + nextchar = psiconv_unicode_read_char(config,buf,lev,off+i+len, + &leng,&localstatus); if (localstatus) goto ERROR2; - result[i] = psiconv_unicode_from_char(config,temp); + if ((localstatus = psiconv_list_add(string,&nextchar))) + goto ERROR2; + i += leng; + } + if (i > bytecount) { + psiconv_error(config,lev,off+i+len,"Malformed string"); + localstatus = PSICONV_E_PARSE; + goto ERROR2; } - result[stringlen] = 0; - len += stringlen; + len += bytecount; + + /* Copy the list to the actual string */ + if (!(result = malloc(sizeof(*result) * (psiconv_list_length(string) + 1)))) + goto ERROR2; + for (i = 0; i < psiconv_list_length(string); i++) { + if (!(nextcharptr = psiconv_list_get(string,i))) { + psiconv_error(config,lev,off+i+len,"Internal data corruption"); + goto ERROR3; + } + result[i] = *nextcharptr; + } + result[i] = 0; res_copy = psiconv_make_printable(config,result); if (!res_copy) - goto ERROR2; + goto ERROR3; psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy); free(res_copy); + psiconv_list_free(string); + if (length) *length = len; @@ -377,8 +402,10 @@ return result; -ERROR2: +ERROR3: free(result); +ERROR2: + psiconv_list_free(string); ERROR1: psiconv_error(config,lev+1,off,"Reading of string failed"); if (status)