|
|
| version 1.41, 2003/11/20 16:34:26 | version 1.59, 2008/06/05 13:23:22 |
|---|---|
| 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) |
| */ | */ |
| Line 14 static const char * const IDENT_CHARSET_ | Line 14 static const char * const IDENT_CHARSET_ |
| #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 36 inline void prepare_case_tables(unsigned | Line 38 inline void prepare_case_tables(unsigned |
| unsigned char *lcc_table=tables+lcc_offset; | unsigned char *lcc_table=tables+lcc_offset; |
| unsigned char *fcc_table=tables+fcc_offset; | unsigned char *fcc_table=tables+fcc_offset; |
| for(int i=0; i<0x100; i++) | for(int i=0; i<0x100; i++) |
| lcc_table[i]=fcc_table[i]=i; | lcc_table[i]=fcc_table[i]=(unsigned char)i; |
| } | } |
| inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr, | inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr, |
| unsigned char bit) { | unsigned char bit) { |
| 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; |
| // 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 char 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=(unsigned char)to_wchar_code(cell); break; |
| // pcre_tables | // pcre_tables |
| case 1: element2ctypes(c, to_bool(cell), pcre_tables, ctype_space, cbit_space); break; | case 1: element2ctypes(c, to_bool(cell), pcre_tables, ctype_space, cbit_space); break; |
| case 2: element2ctypes(c, to_bool(cell), pcre_tables, ctype_digit, cbit_digit); break; | case 2: element2ctypes(c, to_bool(cell), pcre_tables, ctype_digit, cbit_digit); break; |
| case 3: element2ctypes(c, to_bool(cell), pcre_tables, ctype_xdigit); break; | case 3: element2ctypes(c, to_bool(cell), pcre_tables, ctype_xdigit); break; |
| case 4: element2ctypes(c, to_bool(cell), pcre_tables, ctype_letter); break; | case 4: element2ctypes(c, to_bool(cell), pcre_tables, ctype_letter); break; |
| case 5: element2ctypes(c, to_bool(cell), pcre_tables, ctype_word, cbit_word); break; | case 5: element2ctypes(c, to_bool(cell), pcre_tables, ctype_word, cbit_word); break; |
| case 6: element2case(c, to_wchar_code(cell), pcre_tables); break; | case 6: element2case(c, (unsigned char)to_wchar_code(cell), pcre_tables); break; |
| case 7: | case 7: |
| case 8: | case 8: |
| // charset | // charset |
| if(tables.toTableSize>MAX_CHARSET_UNI_CODES) | if(tables.toTableSize>MAX_CHARSET_UNI_CODES) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| &afile_spec, | &afile_spec, |
| "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); | "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); |
| 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 467 static XMLCh change_case_UTF8(const XMLC | Line 471 static XMLCh change_case_UTF8(const XMLC |
| return src; | return src; |
| } | } |
| static void store_UTF8(XMLCh src, XMLByte*& outPtr ) { | static void store_UTF8(XMLCh src, XMLByte*& outPtr){ |
| if(!src) { | if(!src) { |
| // use the replacement character | // use the replacement character |
| *outPtr++= '?'; | *outPtr++= '?'; |
| 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 614 const String::C Charset::transcodeToChar | Line 624 const String::C Charset::transcodeToChar |
| } | } |
| } | } |
| void Charset::store_Char(XMLByte*& outPtr, XMLCh src, XMLByte not_found){ | |
| if(isUTF8()) | |
| store_UTF8(src, outPtr); | |
| else if(char ch=xlatOneTo(src, tables, not_found)) | |
| *outPtr++=ch; | |
| } | |
| #ifdef XML | #ifdef XML |
| 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 675 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 707 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 685 void Charset::initTranscoder(const Strin | Line 727 void Charset::initTranscoder(const Strin |
| xmlCharEncodingHandler& Charset::transcoder(const String::Body NAME) { | xmlCharEncodingHandler& Charset::transcoder(const String::Body NAME) { |
| if(!ftranscoder) | if(!ftranscoder) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| new String(NAME, String::L_TAINTED), | new String(NAME, String::L_TAINTED), |
| "unsupported encoding"); | "unsupported encoding"); |
| return *ftranscoder; | return *ftranscoder; |
| } | } |
| String::C Charset::transcode_cstr(xmlChar* s) { | String::C Charset::transcode_cstr(const xmlChar* s) { |
| if(!s) | if(!s) |
| 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 748 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 719 String::C Charset::transcode_cstr(xmlCha | Line 765 String::C Charset::transcode_cstr(xmlCha |
| assert(outlen<=saved_outlen); out[outlen]=0; | assert(outlen<=saved_outlen); out[outlen]=0; |
| return String::C(out, outlen); | return String::C(out, outlen); |
| } | } |
| const String& Charset::transcode(xmlChar* s) { | const String& Charset::transcode(const xmlChar* s) { |
| String::C cstr=transcode_cstr(s); | |
| return *new String(cstr.str, cstr.length, true); | |
| } | |
| String::C Charset::transcode_cstr(GdomeDOMString* s) { | |
| return s?transcode_cstr(BAD_CAST s->str) | |
| :String::C("", 0); | |
| } | |
| const String& Charset::transcode(GdomeDOMString* s) { | |
| String::C cstr=transcode_cstr(s); | String::C cstr=transcode_cstr(s); |
| return *new String(cstr.str, cstr.length, true); | return *new String(cstr.str, cstr.length, true); |
| } | } |
| Line 741 xmlChar* Charset::transcode_buf2xchar(co | Line 779 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 |
| Line 767 xmlChar* Charset::transcode_buf2xchar(co | Line 809 xmlChar* Charset::transcode_buf2xchar(co |
| assert(outlen<=saved_outlen); out[outlen]=0; | assert(outlen<=saved_outlen); out[outlen]=0; |
| return out; | return out; |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char* buf, size_t buf_size) { | xmlChar* Charset::transcode(const String& s) { |
| return GdomeDOMString_auto_ptr(transcode_buf2xchar(buf, buf_size)); | |
| } | |
| 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_buf2xchar(cstr, strlen(cstr)); |
| } | } |
| GdomeDOMString_auto_ptr Charset::transcode(const String::Body s) { | xmlChar* Charset::transcode(const String::Body s) { |
| const char* cstr=s.cstr(); | const char* cstr=s.cstr(); |
| return transcode_buf2dom(cstr, s.length()); | return transcode_buf2xchar(cstr, s.length()); |
| } | } |
| #endif | #endif |
| Line 829 void Charset::transcode(HashStringString | Line 868 void Charset::transcode(HashStringString |
| const Charset& source_transcoder, | const Charset& source_transcoder, |
| const Charset& dest_transcoder) { | const Charset& dest_transcoder) { |
| Transcode_pair_info info={&source_transcoder, &dest_transcoder}; | Transcode_pair_info info={&source_transcoder, &dest_transcoder}; |
| src.for_each_ref(transcode_pair, &info); | src.for_each_ref<Transcode_pair_info*>(transcode_pair, &info); |
| } | } |