--- parser3/src/main/pa_charset.C 2002/01/21 17:17:27 1.17 +++ parser3/src/main/pa_charset.C 2003/03/25 10:25:25 1.33.2.19.2.9 @@ -1,25 +1,19 @@ /** @file Parser: Charset connection implementation. - Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) + Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan(http://paf.design.ru) - - $Id: pa_charset.C,v 1.17 2002/01/21 17:17:27 paf Exp $ */ +static const char* IDENT_CHARSET_C="$Date: 2003/03/25 10:25:25 $"; + #include "pa_charset.h" +#include "pa_charsets.h" #ifdef XML #include "libxml/encoding.h" #endif -// globals - - -// consts - -#define MAX_CHARSET_UNI_CODES 500 - // helpers inline void prepare_case_tables(unsigned char *tables) { @@ -37,7 +31,7 @@ inline void cstr2ctypes(unsigned char *t ctypes_table[c]|=bit; } } -inline unsigned int to_wchar_code(const char *cstr) { +inline unsigned int to_wchar_code(const char* cstr) { if(!cstr || !*cstr) return 0; if(cstr[1]==0) @@ -46,7 +40,7 @@ inline unsigned int to_wchar_code(const char *error_pos; return(unsigned int)strtol(cstr, &error_pos, 0); } -inline bool to_bool(const char *cstr) { +inline bool to_bool(const char* cstr) { return cstr && *cstr!=0; } static void element2ctypes(unsigned char c, bool belongs, @@ -74,18 +68,18 @@ static void element2case(unsigned char f // methods extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c -Charset::Charset(Pool& apool, const String& aname, const String *request_file_spec): Pooled(apool), - fname(aname) { +Charset::Charset(const String& aname, const String* afile_spec): + fname(aname), + fname_cstr(aname.cstrm()) { - char *name_cstr=fname.cstr(); - for(char *c=name_cstr; *c; c++) - *c = toupper(*c); + for(char *c=fname_cstr; *c; c++) + *c = toupper(*c); - if(request_file_spec) { + if(afile_spec) { fisUTF8=false; - loadDefinition(*request_file_spec); + load_definition(*afile_spec); #ifdef XML - addEncoding(name_cstr); + addEncoding(fname_cstr); #endif } else { fisUTF8=true; @@ -94,17 +88,18 @@ Charset::Charset(Pool& apool, const Stri } #ifdef XML - initTranscoder(&aname, name_cstr); + initTranscoder(fname, fname_cstr); #endif } Charset::~Charset() { + // @todonow unregister encodings #ifdef XML // not deleting transcoder, that's not our business #endif } -void Charset::loadDefinition(const String& request_file_spec) { +void Charset::load_definition(const String& afile_spec) { // pcre_tables // lowcase, flipcase, bits digit+word+whitespace, masks @@ -115,16 +110,14 @@ void Charset::loadDefinition(const Strin cstr2ctypes(pcre_tables,(const unsigned char *)"*+?{^.$|()[", ctype_meta); // charset - memset(tables.fromTable, 0, sizeof(tables.fromTable)); - tables.toTable=(Charset_TransRec *)calloc(sizeof(Charset_TransRec)*MAX_CHARSET_UNI_CODES); - tables.toTableSize=0; + memset(&tables, 0, sizeof(tables)); // strangly vital tables.toTable[tables.toTableSize].intCh=0; tables.toTable[tables.toTableSize].extCh=(XMLByte)0; tables.toTableSize++; // loading text - char *data=file_read_text(pool(), request_file_spec); + char *data=file_read_text(UTF8_charset, afile_spec); // ignore header getrow(&data); @@ -153,8 +146,8 @@ void Charset::loadDefinition(const Strin case 8: // charset if(tables.toTableSize>MAX_CHARSET_UNI_CODES) - throw Exception(0, 0, - &request_file_spec, + throw Exception("parser.runtime", + &afile_spec, "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); XMLCh unicode=(XMLCh)to_wchar_code(cell); @@ -191,7 +184,8 @@ void Charset::sort_ToTable() { } static XMLByte xlatOneTo(const XMLCh toXlat, - const Charset::Tables& tables) { + const Charset::Tables& tables, + XMLByte not_found) { unsigned int lowOfs = 0; unsigned int hiOfs = tables.toTableSize - 1; XMLByte curByte = 0; @@ -210,11 +204,11 @@ static XMLByte xlatOneTo(const XMLCh toX return tables.toTable[midOfs].extCh; } while(lowOfs+1>= 6; - case 5: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL); + case 5: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL); curVal>>= 6; - case 4: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL); + case 4: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL); curVal>>= 6; - case 3: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL); + case 3: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL); curVal>>= 6; - case 2: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL); + case 2: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL); curVal>>= 6; - case 1: *--outPtr = (XMLByte)(curVal | gFirstByteMark[encodedBytes]); + case 1: *--outPtr = XMLByte(curVal | gFirstByteMark[encodedBytes]); } // Add the encoded bytes back in again to indicate we've eaten them @@ -360,10 +354,19 @@ static int transcodeToUTF8( // Return the characters read toFillLen = outPtr - toFill; - return srcPtr==srcEnd?(int)toFillLen:-1; + //return srcPtr==srcEnd?(int)toFillLen:-1; +/* +xmlCharEncodingInputFunc +Returns : +the number of byte written, or -1 by lack of space, or -2 if the transcoding failed. The value of inlen after return is the +number of octets consumed as the return value is positive, else unpredictiable. The value of outlen after return is the number +of ocetes consumed. +*/ + return 0; } -static size_t transcodeFromUTF8( - const XMLByte *srcData, size_t& srcLen, +/// @todo digital entites only when xml/html output [at output in html/xml mode, in html part of a letter] +static int transcodeFromUTF8( + const XMLByte* srcData, size_t& srcLen, XMLByte* toFill, size_t& toFillLen, const Charset::Tables& tables) { const XMLByte* srcPtr=srcData; @@ -374,7 +377,7 @@ static size_t transcodeFromUTF8( // We now loop until we either run out of input data, or room to store while ((srcPtr < srcEnd) && (outPtr < outEnd)) { // Get the next leading byte out - const XMLByte firstByte = *srcPtr; + const XMLByte firstByte =* srcPtr; // Special-case ASCII, which is a leading byte value of<= 127 if(firstByte<= 127) { @@ -404,7 +407,7 @@ static size_t transcodeFromUTF8( break; default: - throw Exception(0, 0, + throw Exception(0, 0, "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); } @@ -413,10 +416,13 @@ static size_t transcodeFromUTF8( // If it will fit into a single char, then put it in. Otherwise // fail [*encode it as a surrogate pair. If its not valid, use the // replacement char.*] - if(!(tmpVal & 0xFFFF0000)) - *outPtr++= xlatOneTo(tmpVal, tables); - else - throw Exception(0, 0, + if(!(tmpVal & 0xFFFF0000)) { + if(XMLByte xlat=xlatOneTo(tmpVal, tables, 0)) + *outPtr++=xlat; + else + outPtr+=sprintf((char *)outPtr, "&#%d;", tmpVal); // &#decimal; + } else + throw Exception(0, 0, "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal); } @@ -427,15 +433,23 @@ static size_t transcodeFromUTF8( // Return the characters read toFillLen = outPtr - toFill; - return srcPtr==srcEnd?(int)toFillLen:-1; + //return srcPtr==srcEnd?(int)toFillLen:-1; +/* +xmlCharEncodingOutputFunc +Returns : +the number of byte written, or -1 by lack of space, or -2 if the transcoding failed. The value of inlen after return is the +number of octets consumed as the return value is positive, else unpredictiable. The value of outlen after return is the number +of ocetes consumed. +*/ + return 0; } /// @todo not so memory-hungry with prescan -void Charset::transcodeToUTF8(Pool& pool, - const void *source_body, size_t source_content_length, +void Charset::transcodeToUTF8( + const void* source_body, size_t source_content_length, const void *& adest_body, size_t& dest_content_length) const { - dest_content_length=source_content_length*6/*so that surly enough*/; - XMLByte *dest_body=(XMLByte*)pool.malloc(dest_content_length); + dest_content_length=source_content_length*6/*so that surly enough, max utf8 seq len=6*/; + XMLByte *dest_body=new XMLByte[dest_content_length]; if(::transcodeToUTF8( (XMLByte *)source_body, source_content_length, @@ -448,12 +462,11 @@ void Charset::transcodeToUTF8(Pool& pool // return adest_body=dest_body; } -/// @test buf overflow -void Charset::transcodeFromUTF8(Pool& pool, - const void *source_body, size_t source_content_length, +void Charset::transcodeFromUTF8( + const void* source_body, size_t source_content_length, const void *& adest_body, size_t& dest_content_length) const { - dest_content_length=source_content_length/*surly enough*/; - XMLByte *dest_body=(XMLByte*)pool.malloc(dest_content_length); + dest_content_length=source_content_length*6/*so that surly enough, "ÿ" has max ratio */; + XMLByte *dest_body=new XMLByte[dest_content_length]; if(::transcodeFromUTF8( (XMLByte *)source_body, source_content_length, @@ -468,16 +481,16 @@ void Charset::transcodeFromUTF8(Pool& po } /// transcode using both charsets -void Charset::transcodeToCharset(Pool& pool, +void Charset::transcodeToCharset( const Charset& dest_charset, - const void *source_body, size_t source_content_length, + const void* source_body, size_t source_content_length, const void *& adest_body, size_t& adest_content_length) const { if(&dest_charset==this) { adest_body=source_body; adest_content_length=source_content_length; } else { size_t dest_content_length=source_content_length; - unsigned char *dest_body=(unsigned char *)pool.malloc(dest_content_length); + unsigned char *dest_body=new unsigned char[dest_content_length]; const XMLByte* srcPtr=(XMLByte *)source_body; const XMLByte* srcEnd=(XMLByte *)source_body+source_content_length; @@ -485,7 +498,7 @@ void Charset::transcodeToCharset(Pool& p for(XMLByte* outPtr=dest_body; srcPtrname=name_cstr; handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables; handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables; @@ -533,78 +545,104 @@ void Charset::addEncoding(char *name_cst xmlRegisterCharEncodingHandler(handler); } -void Charset::initTranscoder(const String *source, const char *name_cstr) { +void Charset::initTranscoder(const String& name, const char* name_cstr) { ftranscoder=xmlFindCharEncodingHandler(name_cstr); - transcoder(source); // check right way + transcoder(name); // check right way } -xmlCharEncodingHandler *Charset::transcoder(const String *source) { +xmlCharEncodingHandler& Charset::transcoder(const String& name) { if(!ftranscoder) - throw Exception(0, 0, - source, + throw Exception("parser.runtime", + &name, "unsupported encoding"); - return ftranscoder; + return *ftranscoder; } -const char *Charset::transcode_cstr(xmlChar *s) { +const char* Charset::transcode_cstr(xmlChar* s) { if(!s) return ""; - int inlen=strlen((const char *)s); + int inlen=strlen((const char* )s); int outlen=inlen+1; // max - char *out=(char *)malloc(outlen*sizeof(char)); + char *out=new(PointerFreeGC) char[outlen]; - int size; - if(xmlCharEncodingOutputFunc output=transcoder(0)->output) { - size=output( + int error; + if(xmlCharEncodingOutputFunc output=transcoder(fname).output) { + error=output( (unsigned char*)out, &outlen, (const unsigned char*)s, &inlen, - transcoder(0)->outputInfo); - } else - memcpy(out, s, size=inlen); - if(size<0) - throw Exception(0, 0, + transcoder(fname).outputInfo); + } else { + memcpy(out, s, outlen=inlen); + error=0; + } + if(error<0) + throw Exception(0, 0, - "transcode_cstr failed (%d)", size); + "transcode_cstr failed (%d)", error); - out[size]=0; + out[outlen/*surely would be less then on input*/]=0; return out; } -String& Charset::transcode(xmlChar *s) { - return *NEW String(pool(), transcode_cstr(s)); +const String& Charset::transcode(xmlChar* s) { + return *new String(transcode_cstr(s), 0/*auto-size*/, String::L_CLEAN); } -const char *Charset::transcode_cstr(GdomeDOMString *s) { +const char* Charset::transcode_cstr(GdomeDOMString* s) { return s?transcode_cstr(BAD_CAST s->str):""; } -String& Charset::transcode(GdomeDOMString *s) { - return *NEW String(pool(), transcode_cstr(s)); +const String& Charset::transcode(GdomeDOMString* s) { + return *new String(transcode_cstr(s), 0/*auto-size*/, String::L_CLEAN); } /// @test less memory using -maybe- xmlParserInputBufferCreateMem -GdomeDOMString_auto_ptr Charset::transcode_buf(const char *buf, size_t buf_size) { - int outlen=buf_size*6/*max*/+1; - unsigned char *out=(unsigned char*)malloc(outlen*sizeof(unsigned char)); - - int size; - if(xmlCharEncodingInputFunc input=transcoder(0)->input) { - size=input( +void* Charset::transcode_buf2mchar(transcode_buf_malloc_func malloc_func, + const char* buf, size_t buf_size) { + unsigned char* out; + int outlen; + int error; + if(xmlCharEncodingInputFunc input=transcoder(fname).input) { + outlen=buf_size*6/*max*/; + out=(unsigned char*)malloc_func(outlen+1); + error=input( out, &outlen, (const unsigned char *)buf, (int *)&buf_size, - transcoder(0)->inputInfo); - } else - memcpy(out, buf, size=buf_size); + transcoder(fname).inputInfo); + } else { + outlen=buf_size; + out=(unsigned char*)malloc_func(outlen+1); + memcpy(out, buf, outlen); + error=0; + } - if(size<0) - throw Exception(0, 0, + if(error<0) + throw Exception(0, 0, - "transcode_buf failed (%d)", size); + "transcode_buf failed (%d)", error); + + out[outlen/*surely would be less then on input*/]=0; + return out; +} - out[size]=0; - return GdomeDOMString_auto_ptr((gchar*)out); +xmlChar* Charset::transcode_buf2xchar(const char* buf, size_t buf_size) { + return static_cast(transcode_buf2mchar(xmlMalloc, buf, buf_size)); +} +static void* g_malloc_wrapper(size_t size) { + return g_malloc(size); +} +gchar* Charset::transcode_buf2gchar(const char* buf, size_t buf_size) { + return static_cast(transcode_buf2mchar(g_malloc_wrapper, buf, buf_size)); +} +GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char* buf, size_t buf_size) { + return GdomeDOMString_auto_ptr(transcode_buf2gchar(buf, buf_size)); } GdomeDOMString_auto_ptr Charset::transcode(const String& s) { - const char *cstr=s.cstr(String::UL_UNSPECIFIED); + const char* cstr=s.cstr(String::L_UNSPECIFIED); + + return transcode_buf2dom(cstr, strlen(cstr)); +} +GdomeDOMString_auto_ptr Charset::transcode(const StringBody s) { + const char* cstr=s.cstr(); - return transcode_buf(cstr, strlen(cstr)); + return transcode_buf2dom(cstr, s.length()); } #endif