|
|
| version 1.40, 2003/11/20 15:35:31 | version 1.52, 2005/08/09 08:14:51 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Charset connection implementation. | Parser: Charset connection implementation. |
| Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan<paf@design.ru>(http://paf.design.ru) | Author: Alexander Petrosyan<paf@design.ru>(http://paf.design.ru) |
| */ | */ |
| static const char* IDENT_CHARSET_C="$Date$"; | static const char * const IDENT_CHARSET_C="$Date$"; |
| #include "pa_charset.h" | #include "pa_charset.h" |
| #include "pa_charsets.h" | #include "pa_charsets.h" |
| Line 14 static const char* IDENT_CHARSET_C="$Dat | Line 14 static const char* IDENT_CHARSET_C="$Dat |
| #include "libxml/encoding.h" | #include "libxml/encoding.h" |
| #endif | #endif |
| //#define PA_PATCHED_LIBXML_BACKWARD | |
| // globals | // globals |
| Charset::UTF8CaseTable::Rec UTF8CaseToUpperRecords[]={ | Charset::UTF8CaseTable::Rec UTF8CaseToUpperRecords[]={ |
| Line 126 void Charset::load_definition(Request_ch | Line 128 void Charset::load_definition(Request_ch |
| // parse cells | // parse cells |
| char *row; | char *row; |
| while(row=getrow(&data)) { | while((row=getrow(&data))) { |
| // remove empty&comment lines | // remove empty&comment lines |
| if(!*row || *row=='#') | if(!*row || *row=='#') |
| continue; | continue; |
| Line 134 void Charset::load_definition(Request_ch | Line 136 void Charset::load_definition(Request_ch |
| // char white-space digit hex-digit letter word lowercase unicode1 unicode2 | // char white-space digit hex-digit letter word lowercase unicode1 unicode2 |
| unsigned int c=0; | unsigned int c=0; |
| char *cell; | char *cell; |
| for(int column=0; cell=lsplit(&row, '\t'); column++) { | for(int column=0; (cell=lsplit(&row, '\t')); column++) { |
| switch(column) { | switch(column) { |
| case 0: c=to_wchar_code(cell); break; | case 0: c=to_wchar_code(cell); break; |
| // pcre_tables | // pcre_tables |
| Line 391 static int transcodeFromUTF8(const XMLBy | Line 393 static int transcodeFromUTF8(const XMLBy |
| default: | default: |
| throw Exception(0, | throw Exception(0, |
| 0, | 0, |
| "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); | "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); // never |
| } | } |
| tmpVal-=gUTFOffsets[trailingBytes]; | tmpVal-=gUTFOffsets[trailingBytes]; |
| Line 401 static int transcodeFromUTF8(const XMLBy | Line 403 static int transcodeFromUTF8(const XMLBy |
| if(!(tmpVal & 0xFFFF0000)) { | if(!(tmpVal & 0xFFFF0000)) { |
| if(XMLByte xlat=xlatOneTo(tmpVal, tables, 0)) | if(XMLByte xlat=xlatOneTo(tmpVal, tables, 0)) |
| *outPtr++=xlat; | *outPtr++=xlat; |
| else | else { |
| outPtr+=sprintf((char *)outPtr, "&#%d;", tmpVal); // &#decimal; | outPtr+=sprintf((char *)outPtr, "&#%u;", tmpVal); // &#decimal; |
| } else | } |
| throw Exception(0, | } else { |
| 0, | const XMLByte* recoverPtr=srcPtr-trailingBytes-1; |
| "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal); | for(uint i=0; i<=trailingBytes; i++) |
| outPtr+=sprintf((char*)outPtr, "%%%02X", *recoverPtr++); | |
| } | |
| } | } |
| // Update the bytes eaten | // Update the bytes eaten |
| Line 439 const String::C Charset::transcodeToUTF8 | Line 443 const String::C Charset::transcodeToUTF8 |
| (XMLByte *)src.str, src_length, | (XMLByte *)src.str, src_length, |
| dest_body, dest_length, | dest_body, dest_length, |
| tables)<0) | tables)<0) |
| throw(0, 0, | throw Exception(0, |
| 0, | 0, |
| "Charset::transcodeToUTF8 buffer overflow"); | "Charset::transcodeToUTF8 buffer overflow"); |
| Line 519 static void change_case_UTF8(XMLCh src, | Line 523 static void change_case_UTF8(XMLCh src, |
| const Charset::UTF8CaseTable& table) { | const Charset::UTF8CaseTable& table) { |
| store_UTF8(change_case_UTF8(src, table), outPtr); | store_UTF8(change_case_UTF8(src, table), outPtr); |
| }; | }; |
| void change_case_UTF8(const XMLByte* srcData, XMLByte* toFill, | void change_case_UTF8(const XMLByte* srcData, size_t srcLen, |
| const Charset::UTF8CaseTable& table) { | XMLByte* toFill, size_t toFillLen, |
| const Charset::UTF8CaseTable& table) { | |
| const XMLByte* srcPtr=srcData; | const XMLByte* srcPtr=srcData; |
| const XMLByte* srcEnd=srcData+srcLen; | |
| XMLByte* outPtr=toFill; | XMLByte* outPtr=toFill; |
| XMLByte* outEnd=toFill+toFillLen; | |
| // 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; | |
| // Get the next leading byte out | |
| while (const XMLByte firstByte = *srcPtr) { | |
| if(firstByte<= 127) { | if(firstByte<= 127) { |
| change_case_UTF8(firstByte, outPtr, table); | change_case_UTF8(firstByte, outPtr, table); |
| srcPtr++; | srcPtr++; |
| Line 583 const String::C Charset::transcodeFromUT | Line 593 const String::C Charset::transcodeFromUT |
| (XMLByte *)src.str, src_length, | (XMLByte *)src.str, src_length, |
| dest_body, dest_length, | dest_body, dest_length, |
| tables)<0) | tables)<0) |
| throw(0, 0, | throw Exception(0, |
| 0, | 0, |
| "Charset::transcodeFromUTF8 buffer overflow"); | "Charset::transcodeFromUTF8 buffer overflow"); |
| Line 618 const String::C Charset::transcodeToChar | Line 628 const String::C Charset::transcodeToChar |
| static const Charset::Tables* tables[MAX_CHARSETS]; | static const Charset::Tables* tables[MAX_CHARSETS]; |
| #ifdef PA_PATCHED_LIBXML_BACKWARD | |
| #define declareXml256ioFuncs(i) \ | |
| static int xml256CharEncodingInputFunc##i( \ | |
| unsigned char *out, int *outlen, \ | |
| const unsigned char *in, int *inlen, void*) { \ | |
| 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, void*) { \ | |
| return transcodeFromUTF8( \ | |
| in, *(size_t*)inlen, \ | |
| out, *(size_t*)outlen, \ | |
| *tables[i]); \ | |
| } | |
| #else | |
| #define declareXml256ioFuncs(i) \ | #define declareXml256ioFuncs(i) \ |
| static int xml256CharEncodingInputFunc##i( \ | static int xml256CharEncodingInputFunc##i( \ |
| unsigned char *out, int *outlen, \ | unsigned char *out, int *outlen, \ |
| Line 636 static const Charset::Tables* tables[MAX | Line 668 static const Charset::Tables* tables[MAX |
| *tables[i]); \ | *tables[i]); \ |
| } | } |
| #endif | |
| declareXml256ioFuncs(0) declareXml256ioFuncs(1) | declareXml256ioFuncs(0) declareXml256ioFuncs(1) |
| declareXml256ioFuncs(2) declareXml256ioFuncs(3) | declareXml256ioFuncs(2) declareXml256ioFuncs(3) |
| declareXml256ioFuncs(4) declareXml256ioFuncs(5) | declareXml256ioFuncs(4) declareXml256ioFuncs(5) |
| Line 665 void Charset::addEncoding(char *name_cst | Line 700 void Charset::addEncoding(char *name_cst |
| "already allocated %d handlers, no space for new encoding '%s'", | "already allocated %d handlers, no space for new encoding '%s'", |
| MAX_CHARSETS, name_cstr); | MAX_CHARSETS, name_cstr); |
| xmlCharEncodingHandler* handler=new(PointerFreeGC) xmlCharEncodingHandler; | xmlCharEncodingHandler* handler=new(UseGC) xmlCharEncodingHandler; |
| { | { |
| handler->name=name_cstr; | handler->name=name_cstr; |
| handler->input=inputFuncs[handlers_count]; | handler->input=inputFuncs[handlers_count]; |
| Line 696 String::C Charset::transcode_cstr(xmlCha | Line 731 String::C Charset::transcode_cstr(xmlCha |
| return String::C("", 0); | return String::C("", 0); |
| int inlen=strlen((const char*)s); | int inlen=strlen((const char*)s); |
| int outlen=inlen; // max | int outlen=inlen*6/*strlen("ÿ")*/; // max |
| #ifndef NDEBUG | #ifndef NDEBUG |
| int saved_outlen=outlen; | int saved_outlen=outlen; |
| #endif | #endif |
| Line 706 String::C Charset::transcode_cstr(xmlCha | Line 741 String::C Charset::transcode_cstr(xmlCha |
| if(xmlCharEncodingOutputFunc output=transcoder(FNAME).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 |
| #ifdef PA_PATCHED_LIBXML_BACKWARD | |
| ,0 | |
| #endif | |
| ); | |
| } else { | } else { |
| memcpy(out, s, outlen=inlen); | memcpy(out, s, outlen=inlen); |
| error=0; | error=0; |
| Line 741 xmlChar* Charset::transcode_buf2xchar(co | Line 780 xmlChar* Charset::transcode_buf2xchar(co |
| int saved_outlen; | int saved_outlen; |
| #endif | #endif |
| if(xmlCharEncodingInputFunc input=transcoder(FNAME).input) { | if(xmlCharEncodingInputFunc input=transcoder(FNAME).input) { |
| outlen=buf_size*6/*max*/; | outlen=buf_size*6/*max UTF8 bytes per char*/; |
| #ifndef NDEBUG | #ifndef NDEBUG |
| saved_outlen=outlen; | saved_outlen=outlen; |
| #endif | #endif |
| out=(xmlChar*)xmlMalloc(outlen+1); | out=(xmlChar*)xmlMalloc(outlen+1); |
| error=input( | error=input( |
| out, &outlen, | out, &outlen, |
| (const unsigned char*)buf, (int*)&buf_size); | (const unsigned char*)buf, (int*)&buf_size |
| #ifdef PA_PATCHED_LIBXML_BACKWARD | |
| ,0 | |
| #endif | |
| ); | |
| } else { | } else { |
| outlen=buf_size; | outlen=buf_size; |
| #ifndef NDEBUG | #ifndef NDEBUG |