|
|
| version 1.33.2.19.2.6, 2003/03/24 13:43:41 | version 1.33.2.19.2.16, 2003/04/08 12:47:31 |
|---|---|
| Line 92 Charset::Charset(const String& aname, co | Line 92 Charset::Charset(const String& aname, co |
| #endif | #endif |
| } | } |
| Charset::~Charset() { | |
| // @todonow unregister encodings | |
| #ifdef XML | |
| // not deleting transcoder, that's not our business | |
| #endif | |
| } | |
| void Charset::load_definition(const String& afile_spec) { | void Charset::load_definition(const String& afile_spec) { |
| // pcre_tables | // pcre_tables |
| // lowcase, flipcase, bits digit+word+whitespace, masks | // lowcase, flipcase, bits digit+word+whitespace, masks |
| Line 207 static XMLByte xlatOneTo(const XMLCh toX | Line 200 static XMLByte xlatOneTo(const XMLCh toX |
| return not_found; | return not_found; |
| } | } |
| void Charset::transcode( | String::C Charset::transcode(const String::C src, |
| const Charset& source_charset, const void* source_body, size_t source_content_length, | const Charset& source_charset, |
| const Charset& dest_charset, const void *& dest_body, size_t& dest_content_length | const Charset& dest_charset) { |
| ) { | if(!src.length) |
| if(!source_content_length) { | return String::C(0, 0); |
| dest_body=0; | |
| dest_content_length=0; | |
| return; | |
| } | |
| switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) { | switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) { |
| default: // 0x00 | default: // 0x00 |
| source_charset.transcodeToCharset(dest_charset, | return source_charset.transcodeToCharset(src, dest_charset); |
| source_body, source_content_length, | |
| dest_body, dest_content_length); | |
| break; | |
| case 0x01: | case 0x01: |
| source_charset.transcodeToUTF8( | return source_charset.transcodeToUTF8(src); |
| source_body, source_content_length, | |
| dest_body, dest_content_length); | |
| break; | |
| case 0x10: | case 0x10: |
| dest_charset.transcodeFromUTF8( | return dest_charset.transcodeFromUTF8(src); |
| source_body, source_content_length, | |
| dest_body, dest_content_length); | |
| break; | |
| case 0x11: | case 0x11: |
| dest_body=source_body; | return src; |
| dest_content_length=source_content_length; | |
| break; | |
| } | } |
| } | } |
| Line 282 static const XMLByte gFirstByteMark[7] = | Line 260 static const XMLByte gFirstByteMark[7] = |
| }; | }; |
| static int transcodeToUTF8( | static int transcodeToUTF8( |
| const XMLByte* srcData, size_t& srcLen, | const XMLByte* srcData, size_t& srcLen, |
| XMLByte *toFill, size_t& toFillLen, | XMLByte *toFill, size_t& toFillLen, |
| const Charset::Tables& tables) { | const Charset::Tables& tables) { |
| const XMLByte* srcPtr=srcData; | const XMLByte* srcPtr=srcData; |
| const XMLByte* srcEnd=srcData+srcLen; | const XMLByte* srcEnd=srcData+srcLen; |
| XMLByte* outPtr=toFill; | XMLByte* outPtr=toFill; |
| Line 445 of ocetes consumed. | Line 423 of ocetes consumed. |
| } | } |
| /// @todo not so memory-hungry with prescan | /// @todo not so memory-hungry with prescan |
| void Charset::transcodeToUTF8( | const String::C Charset::transcodeToUTF8(const String::C src) const { |
| const void* source_body, size_t source_content_length, | size_t src_length=src.length; |
| const void *& adest_body, size_t& dest_content_length) const { | size_t dest_length=src.length*6/*so that surly enough, max utf8 seq len=6*/; |
| dest_content_length=source_content_length*6/*so that surly enough, max utf8 seq len=6*/; | XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/]; |
| XMLByte *dest_body=new XMLByte[dest_content_length]; | |
| if(::transcodeToUTF8( | if(::transcodeToUTF8( |
| (XMLByte *)source_body, source_content_length, | (XMLByte *)src.str, src_length, |
| dest_body, dest_content_length, | dest_body, dest_length, |
| tables)<0) | tables)<0) |
| throw(0, 0, | throw(0, 0, |
| 0, | 0, |
| "Charset::transcodeToUTF8 buffer overflow"); | "Charset::transcodeToUTF8 buffer overflow"); |
| // return | dest_body[dest_length]=0; // terminator |
| adest_body=dest_body; | return String::C((char*)dest_body, dest_length); |
| } | } |
| void Charset::transcodeFromUTF8( | const String::C Charset::transcodeFromUTF8(const String::C src) const { |
| const void* source_body, size_t source_content_length, | size_t src_length=src.length; |
| const void *& adest_body, size_t& dest_content_length) const { | size_t dest_length=src.length*6/*so that surly enough, "ÿ" has max ratio */; |
| dest_content_length=source_content_length*6/*so that surly enough, "ÿ" has max ratio */; | XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/]; |
| XMLByte *dest_body=new XMLByte[dest_content_length]; | |
| if(::transcodeFromUTF8( | if(::transcodeFromUTF8( |
| (XMLByte *)source_body, source_content_length, | (XMLByte *)src.str, src_length, |
| dest_body, dest_content_length, | dest_body, dest_length, |
| tables)<0) | tables)<0) |
| throw(0, 0, | throw(0, 0, |
| 0, | 0, |
| "Charset::transcodeToUTF8 buffer overflow"); | "Charset::transcodeFromUTF8 buffer overflow"); |
| // return | dest_body[dest_length]=0; // terminator |
| adest_body=dest_body; | return String::C((char*)dest_body, dest_length); |
| } | } |
| /// transcode using both charsets | /// transcode using both charsets |
| void Charset::transcodeToCharset( | const String::C Charset::transcodeToCharset(const String::C src, |
| const Charset& dest_charset, | const Charset& dest_charset) const { |
| const void* source_body, size_t source_content_length, | if(&dest_charset==this) |
| const void *& adest_body, size_t& adest_content_length) const { | return src; |
| if(&dest_charset==this) { | else { |
| adest_body=source_body; | size_t dest_length=src.length; |
| adest_content_length=source_content_length; | XMLByte* dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/]; |
| } else { | |
| size_t dest_content_length=source_content_length; | XMLByte* output=dest_body; |
| unsigned char *dest_body=new unsigned char[dest_content_length]; | const XMLByte* input=(XMLByte *)src.str; |
| while(char c=*input++) { | |
| const XMLByte* srcPtr=(XMLByte *)source_body; | XMLCh curVal = tables.fromTable[c]; |
| const XMLByte* srcEnd=(XMLByte *)source_body+source_content_length; | *output++=curVal? |
| xlatOneTo(curVal, dest_charset.tables, '?') // OK | |
| for(XMLByte* outPtr=dest_body; srcPtr<srcEnd; srcPtr++) { | :'?'; // use the replacement character |
| XMLCh curVal = tables.fromTable[*srcPtr]; | |
| if(curVal) | |
| *outPtr++=xlatOneTo(curVal, dest_charset.tables, '?'); | |
| else { | |
| // use the replacement character | |
| *outPtr++= '?'; | |
| } | |
| } | } |
| adest_body=dest_body; | dest_body[dest_length]=0; // terminator |
| adest_content_length=dest_content_length; | return String::C((char*)dest_body, dest_length); |
| } | } |
| } | } |
| #ifdef XML | #ifdef XML |
| static int xml256CharEncodingInputFunc ( | |
| unsigned char *out, | |
| int *outlen, | |
| const unsigned char *in, | |
| int *inlen, | |
| void *info) { | |
| return transcodeToUTF8( | |
| in, *(size_t*)inlen, | |
| out, *(size_t*)outlen, | |
| *(const Charset::Tables *)info); | |
| } | |
| static int xml256CharEncodingOutputFunc ( | |
| unsigned char *out, | |
| int *outlen, | |
| const unsigned char *in, | |
| int *inlen, | |
| void *info) { | |
| return transcodeFromUTF8( | |
| in, *(size_t*)inlen, | |
| out, *(size_t*)outlen, | |
| *(const Charset::Tables *)info); | |
| } | |
| static const Charset::Tables* tables[MAX_CHARSETS]; | |
| #define declareXml256ioFuncs(i) \ | |
| static int xml256CharEncodingInputFunc##i( \ | |
| unsigned char *out, int *outlen, \ | |
| const unsigned char *in, int *inlen) { \ | |
| return transcodeToUTF8( \ | |
| in, *(size_t*)inlen, \ | |
| out, *(size_t*)outlen, \ | |
| *tables[i]); \ | |
| } \ | |
| static int xml256CharEncodingOutputFunc##i( \ | |
| unsigned char *out, int *outlen, \ | |
| const unsigned char *in, int *inlen) { \ | |
| return transcodeFromUTF8( \ | |
| in, *(size_t*)inlen, \ | |
| out, *(size_t*)outlen, \ | |
| *tables[i]); \ | |
| } | |
| declareXml256ioFuncs(0) declareXml256ioFuncs(1) | |
| declareXml256ioFuncs(2) declareXml256ioFuncs(3) | |
| declareXml256ioFuncs(4) declareXml256ioFuncs(5) | |
| declareXml256ioFuncs(6) declareXml256ioFuncs(7) | |
| declareXml256ioFuncs(8) declareXml256ioFuncs(9) | |
| static xmlCharEncodingInputFunc inputFuncs[MAX_CHARSETS]={ | |
| xml256CharEncodingInputFunc0, xml256CharEncodingInputFunc1, | |
| xml256CharEncodingInputFunc2, xml256CharEncodingInputFunc3, | |
| xml256CharEncodingInputFunc4, xml256CharEncodingInputFunc5, | |
| xml256CharEncodingInputFunc6, xml256CharEncodingInputFunc7, | |
| xml256CharEncodingInputFunc8, xml256CharEncodingInputFunc9 | |
| }; | |
| static xmlCharEncodingOutputFunc outputFuncs[MAX_CHARSETS]={ | |
| xml256CharEncodingOutputFunc0, xml256CharEncodingOutputFunc1, | |
| xml256CharEncodingOutputFunc2, xml256CharEncodingOutputFunc3, | |
| xml256CharEncodingOutputFunc4, xml256CharEncodingOutputFunc5, | |
| xml256CharEncodingOutputFunc6, xml256CharEncodingOutputFunc7, | |
| xml256CharEncodingOutputFunc8, xml256CharEncodingOutputFunc9 | |
| }; | |
| static size_t handlers_count=0; | |
| void Charset::addEncoding(char *name_cstr) { | void Charset::addEncoding(char *name_cstr) { |
| xmlCharEncodingHandler* handler=new xmlCharEncodingHandler; | if(handlers_count==MAX_CHARSETS) |
| fcreated_handler=xmlCharEncodingHandlerPtr(handler); | throw Exception(0, |
| 0, | |
| "already allocated %d handlers, no space for new encoding '%s'", | |
| MAX_CHARSETS, name_cstr); | |
| handler->name=name_cstr; | xmlCharEncodingHandler* handler=new(PointerFreeGC) xmlCharEncodingHandler; |
| handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables; | { |
| handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables; | handler->name=name_cstr; |
| handler->input=inputFuncs[handlers_count]; | |
| handler->output=outputFuncs[handlers_count]; | |
| ::tables[handlers_count]=&tables; | |
| handlers_count++; | |
| } | |
| xmlRegisterCharEncodingHandler(handler); | 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); | 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) | if(!ftranscoder) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| source, | &name, |
| "unsupported encoding"); | "unsupported encoding"); |
| return *ftranscoder; | return *ftranscoder; |
| } | } |
| const char* Charset::transcode_cstrxmlChar* s) { | const char* Charset::transcode_cstr(xmlChar* s) { |
| if(!s) | if(!s) |
| return ""; | return ""; |
| Line 569 const char* Charset::transcode_cstrxmlCh | Line 565 const char* Charset::transcode_cstrxmlCh |
| char *out=new(PointerFreeGC) char[outlen]; | char *out=new(PointerFreeGC) char[outlen]; |
| int error; | int error; |
| if(xmlCharEncodingOutputFunc output=transcoder(0).output) { | if(xmlCharEncodingOutputFunc output=transcoder(fname).output) { |
| error=output( | error=output( |
| (unsigned char*)out, &outlen, | (unsigned char*)out, &outlen, |
| (const unsigned char*)s, &inlen, | (const unsigned char*)s, &inlen); |
| transcoder(0).outputInfo); | |
| } else { | } else { |
| memcpy(out, s, outlen=inlen); | memcpy(out, s, outlen=inlen); |
| error=0; | error=0; |
| Line 586 const char* Charset::transcode_cstrxmlCh | Line 581 const char* Charset::transcode_cstrxmlCh |
| out[outlen/*surely would be less then on input*/]=0; | out[outlen/*surely would be less then on input*/]=0; |
| return out; | return out; |
| } | } |
| const String& Charset::transcodexmlChar* s | const String& Charset::transcode(xmlChar* s) { |
| #ifndef NO_STRING_ORIGIN | return *new String(transcode_cstr(s), 0/*auto-size*/, true); |
| , const String& origin | |
| #endif | |
| ) { | |
| const String& result(new String()); | |
| result->APPEND_CLEAN( | |
| transcode_cstr(s), 0/*auto-size*/, | |
| origin->origin().file, origin->origin().line); | |
| return result; | |
| } | } |
| const char* Charset::transcode_cstrGdomeDOMString* s) { | const char* Charset::transcode_cstr(GdomeDOMString* s) { |
| return s?transcode_cstr(BAD_CAST s->str):""; | return s?transcode_cstr(BAD_CAST s->str):""; |
| } | } |
| const String& Charset::transcodeGdomeDOMString* s | const String& Charset::transcode(GdomeDOMString* s) { |
| #ifndef NO_STRING_ORIGIN | return *new String(transcode_cstr(s), 0/*auto-size*/, true); |
| , const String& origin | |
| #endif | |
| ) { | |
| const String& result(new String()); | |
| result->APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); | |
| return result; | |
| } | } |
| /// @test less memory using -maybe- xmlParserInputBufferCreateMem | /// @test less memory using -maybe- xmlParserInputBufferCreateMem |
| void* Charset::transcode_buf2mchar(transcode_buf_malloc_func malloc_func, | void* Charset::transcode_buf2mchar(transcode_buf_malloc_func malloc_func, |
| const char* buf, size_t buf_size) { | const char* buf, size_t buf_size) { |
| unsigned char* out; | unsigned char* out; |
| int outlen; | int outlen; |
| int error; | int error; |
| if(xmlCharEncodingInputFunc input=transcoder(0).input) { | if(xmlCharEncodingInputFunc input=transcoder(fname).input) { |
| outlen=buf_size*6/*max*/; | outlen=buf_size*6/*max*/; |
| out=(unsigned char*)malloc_func(outlen+1); | out=(unsigned char*)malloc_func(outlen+1); |
| error=input( | error=input( |
| out, &outlen, | out, &outlen, |
| (const unsigned char *)buf, (int *)&buf_size, | (const unsigned char *)buf, (int *)&buf_size); |
| transcoder(0).inputInfo); | |
| } else { | } else { |
| outlen=buf_size; | outlen=buf_size; |
| out=(unsigned char*)malloc_func(outlen+1); | out=(unsigned char*)malloc_func(outlen+1); |
| Line 652 GdomeDOMString_auto_ptr Charset::transco | Line 632 GdomeDOMString_auto_ptr Charset::transco |
| return GdomeDOMString_auto_ptr(transcode_buf2gchar(buf, buf_size)); | return GdomeDOMString_auto_ptr(transcode_buf2gchar(buf, buf_size)); |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode(const String& s) { | GdomeDOMString_auto_ptr Charset::transcode(const String& s) { |
| const char* cstr=s->cstr(String::L_UNSPECIFIED); | const char* cstr=s.cstr(String::L_UNSPECIFIED); |
| return transcode_buf2dom(cstr, strlen(cstr)); | return transcode_buf2dom(cstr, strlen(cstr)); |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode(const StringBody s) { | |
| const char* cstr=s.cstr(); | |
| return transcode_buf2dom(cstr, s.length()); | |
| } | |
| #endif | #endif |