|
|
| version 1.33.2.12, 2003/02/26 10:17:30 | version 1.33.2.18, 2003/03/07 15:01:04 |
|---|---|
| Line 8 | Line 8 |
| static const char* IDENT_CHARSET_C="$Date$"; | static const char* IDENT_CHARSET_C="$Date$"; |
| #include "pa_charset.h" | #include "pa_charset.h" |
| #include "pa_charsets.h" | |
| #ifdef XML | #ifdef XML |
| #include "libxml/encoding.h" | #include "libxml/encoding.h" |
| #endif | #endif |
| // globals | |
| #define CHARSET_UTF8_NAME "UTF-8" | |
| CharsetPtr UTF8_charset(new Charset(StringPtr(new String(CHARSET_UTF8_NAME)), | |
| StringPtr(0)/*no file=system*/)); | |
| // helpers | // helpers |
| inline void prepare_case_tables(unsigned char *tables) { | inline void prepare_case_tables(unsigned char *tables) { |
| Line 215 static XMLByte xlatOneTo(const XMLCh toX | Line 209 static XMLByte xlatOneTo(const XMLCh toX |
| } | } |
| void Charset::transcode(Pool& pool, | void Charset::transcode(Pool& pool, |
| const Charset& source_charset, const void *source_body, size_t source_content_length, | const Charset& source_charset, const void* source_body, size_t source_content_length, |
| const Charset& dest_charset, const void *& dest_body, size_t& dest_content_length | const Charset& dest_charset, const void *& dest_body, size_t& dest_content_length |
| ) { | ) { |
| if(!source_content_length) { | if(!source_content_length) { |
| Line 373 of ocetes consumed. | Line 367 of ocetes consumed. |
| } | } |
| /// @todo digital entites only when xml/html output [at output in html/xml mode, in html part of a letter] | /// @todo digital entites only when xml/html output [at output in html/xml mode, in html part of a letter] |
| static int transcodeFromUTF8( | static int transcodeFromUTF8( |
| 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; |
| Line 384 static int transcodeFromUTF8( | Line 378 static int transcodeFromUTF8( |
| // We now loop until we either run out of input data, or room to store | // We now loop until we either run out of input data, or room to store |
| while ((srcPtr < srcEnd) && (outPtr < outEnd)) { | while ((srcPtr < srcEnd) && (outPtr < outEnd)) { |
| // Get the next leading byte out | // 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 | // Special-case ASCII, which is a leading byte value of<= 127 |
| if(firstByte<= 127) { | if(firstByte<= 127) { |
| Line 453 of ocetes consumed. | Line 447 of ocetes consumed. |
| /// @todo not so memory-hungry with prescan | /// @todo not so memory-hungry with prescan |
| void Charset::transcodeToUTF8(Pool& pool, | void Charset::transcodeToUTF8(Pool& pool, |
| const void *source_body, size_t source_content_length, | const void* source_body, size_t source_content_length, |
| const void *& adest_body, size_t& dest_content_length) const { | const void *& adest_body, size_t& dest_content_length) const { |
| dest_content_length=source_content_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(pool) XMLByte[dest_content_length]; | XMLByte *dest_body=new(pool) XMLByte[dest_content_length]; |
| Line 470 void Charset::transcodeToUTF8(Pool& pool | Line 464 void Charset::transcodeToUTF8(Pool& pool |
| adest_body=dest_body; | adest_body=dest_body; |
| } | } |
| void Charset::transcodeFromUTF8(Pool& pool, | void Charset::transcodeFromUTF8(Pool& pool, |
| const void *source_body, size_t source_content_length, | const void* source_body, size_t source_content_length, |
| const void *& adest_body, size_t& dest_content_length) const { | const void *& adest_body, size_t& dest_content_length) const { |
| dest_content_length=source_content_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(pool) XMLByte[dest_content_length]; | XMLByte *dest_body=new(pool) XMLByte[dest_content_length]; |
| Line 490 void Charset::transcodeFromUTF8(Pool& po | Line 484 void Charset::transcodeFromUTF8(Pool& po |
| /// transcode using both charsets | /// transcode using both charsets |
| void Charset::transcodeToCharset(Pool& pool, | void Charset::transcodeToCharset(Pool& pool, |
| const Charset& dest_charset, | 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 { | const void *& adest_body, size_t& adest_content_length) const { |
| if(&dest_charset==this) { | if(&dest_charset==this) { |
| adest_body=source_body; | adest_body=source_body; |
| Line 544 static int xml256CharEncodingOut | Line 538 static int xml256CharEncodingOut |
| void Charset::addEncoding(char *name_cstr) { | void Charset::addEncoding(char *name_cstr) { |
| xmlCharEncodingHandler *handler= | xmlCharEncodingHandler* handler=new xmlCharEncodingHandler; |
| (xmlCharEncodingHandler *)[ADD to local array of autoptrs to autofree]new ...(sizeof(xmlCharEncodingHandler)); | fcreated_handler=xmlCharEncodingHandlerPtr(handler); |
| handler->name=name_cstr; | handler->name=name_cstr; |
| handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables; | handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables; |
| handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables; | handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables; |
| Line 553 void Charset::addEncoding(char *name_cst | Line 548 void Charset::addEncoding(char *name_cst |
| xmlRegisterCharEncodingHandler(handler); | xmlRegisterCharEncodingHandler(handler); |
| } | } |
| void Charset::initTranscoder(const String *source, const char* name_cstr) { | void Charset::initTranscoder(StringPtr source, const char* name_cstr) { |
| ftranscoder=xmlFindCharEncodingHandler(name_cstr); | ftranscoder=xmlFindCharEncodingHandler(name_cstr); |
| transcoder(source); // check right way | transcoder(source); // check right way |
| } | } |
| xmlCharEncodingHandler *Charset::transcoder(const String *source) { | xmlCharEncodingHandler& Charset::transcoder(StringPtr source) { |
| if(!ftranscoder) | if(!ftranscoder) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| source, | source, |
| "unsupported encoding"); | "unsupported encoding"); |
| return ftranscoder; | return *ftranscoder; |
| } | } |
| CharPtr Charset::transcode_cstr(xmlChar *s) { | const char* Charset::transcode_cstr(Pool& pool, xmlChar* s) { |
| if(!s) | if(!s) |
| return ""; | return ""; |
| int inlen=strlen((const char* )s); | int inlen=strlen((const char* )s); |
| int outlen=inlen+1; // max | int outlen=inlen+1; // max |
| char *out=new char[outlen]; | char *out=new(pool) char[outlen]; |
| int error; | int error; |
| if(xmlCharEncodingOutputFunc output=transcoder(0)->output) { | if(xmlCharEncodingOutputFunc output=transcoder(Exception::undefined_source).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); | transcoder(Exception::undefined_source).outputInfo); |
| } else { | } else { |
| memcpy(out, s, outlen=inlen); | memcpy(out, s, outlen=inlen); |
| error=0; | error=0; |
| } | } |
| if(error<0) | if(error<0) |
| throw Exception(0, | throw Exception(Exception::undefined_type, |
| 0, | Exception::undefined_source, |
| "transcode_cstr failed (%d)", error); | "transcode_cstr failed (%d)", error); |
| out[outlen/*surely would be less then on input*/]=0; | out[outlen/*surely would be less then on input*/]=0; |
| return out; | return out; |
| } | } |
| String& Charset::transcode(xmlChar *s | StringPtr Charset::transcode(Pool& pool, xmlChar* s |
| #ifndef NO_STRING_ORIGIN | #ifndef NO_STRING_ORIGIN |
| , const String *origin | , StringPtr origin |
| #endif | #endif |
| ) { | ) { |
| String& result=*NEW String(pool()); | StringPtr result(new String()); |
| result.APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); | result->APPEND_CLEAN( |
| transcode_cstr(pool, s), 0/*auto-size*/, | |
| origin->origin().file, origin->origin().line); | |
| return result; | return result; |
| } | } |
| const char* Charset::transcode_cstr(GdomeDOMString *s) { | const char* Charset::transcode_cstr(Pool& pool, GdomeDOMString* s) { |
| return s?transcode_cstr(BAD_CAST s->str):""; | return s?transcode_cstr(pool, BAD_CAST s->str):""; |
| } | } |
| String& Charset::transcode(GdomeDOMString *s | StringPtr Charset::transcode(Pool& pool, GdomeDOMString* s |
| #ifndef NO_STRING_ORIGIN | #ifndef NO_STRING_ORIGIN |
| , const String *origin | , StringPtr origin |
| #endif | #endif |
| ) { | ) { |
| String& result=*NEW String(pool()); | StringPtr result(new String()); |
| result.APPEND_CLEAN(transcode_cstr(s), 0/*auto-size*/, origin->origin().file, origin->origin().line); | result->APPEND_CLEAN(transcode_cstr(pool, s), 0/*auto-size*/, origin->origin().file, origin->origin().line); |
| return result; | return result; |
| } | } |
| /// @test less memory using -maybe- xmlParserInputBufferCreateMem | /// @test less memory using -maybe- xmlParserInputBufferCreateMem |
| xmlChar *Charset::transcode_buf2xchar(Pool& pool, const char* buf, size_t buf_size) { | void* Charset::transcode_buf2mchar(transcode_buf_malloc_func malloc_func, |
| unsigned char *out; | const char* buf, size_t buf_size) { |
| unsigned char* out; | |
| int outlen; | int outlen; |
| int error; | int error; |
| if(xmlCharEncodingInputFunc input=transcoder(0)->input) { | if(xmlCharEncodingInputFunc input=transcoder(Exception::undefined_source).input) { |
| outlen=buf_size*6/*max*/; | outlen=buf_size*6/*max*/; |
| out=new(pool) unsigned char[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); | transcoder(Exception::undefined_source).inputInfo); |
| } else { | } else { |
| outlen=buf_size; | outlen=buf_size; |
| out=new(pool) unsigned char[outlen+1]; | out=(unsigned char*)malloc_func(outlen+1); |
| memcpy(out, buf, outlen); | memcpy(out, buf, outlen); |
| error=0; | error=0; |
| } | } |
| if(error<0) | if(error<0) |
| throw Exception(0, | throw Exception(Exception::undefined_type, |
| 0, | Exception::undefined_source, |
| "transcode_buf failed (%d)", error); | "transcode_buf failed (%d)", error); |
| out[outlen/*surely would be less then on input*/]=0; | out[outlen/*surely would be less then on input*/]=0; |
| return (xmlChar *)out; | return out; |
| } | |
| xmlChar* Charset::transcode_buf2xchar(const char* buf, size_t buf_size) { | |
| return static_cast<xmlChar*>(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<gchar*>(transcode_buf2mchar(g_malloc_wrapper, buf, buf_size)); | |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char* buf, size_t buf_size) { | GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char* buf, size_t buf_size) { |
| return GdomeDOMString_auto_ptr((gchar*)transcode_buf2xchar(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(StringPtr s) { |
| CharPtr cstr=s.cstr(String::UL_UNSPECIFIED); | CharPtr cstr=s->cstr(String::UL_UNSPECIFIED); |
| return transcode_buf2dom(cstr, strlen(cstr)); | return transcode_buf2dom(cstr, strlen(cstr)); |
| } | } |