--- parser3/src/main/pa_charset.C 2014/08/28 17:09:36 1.96 +++ parser3/src/main/pa_charset.C 2024/12/11 21:57:35 1.115 @@ -1,8 +1,8 @@ /** @file Parser: Charset connection implementation. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexander Petrosyan(http://paf.design.ru) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "pa_charset.h" @@ -11,9 +11,10 @@ // we are using some pcre_internal.h stuff as well #include "../lib/pcre/pa_pcre_internal.h" -volatile const char * IDENT_PA_CHARSET_C="$Id: pa_charset.C,v 1.96 2014/08/28 17:09:36 moko Exp $" IDENT_PA_CHARSET_H; +volatile const char * IDENT_PA_CHARSET_C="$Id: pa_charset.C,v 1.115 2024/12/11 21:57:35 moko Exp $" IDENT_PA_CHARSET_H; #ifdef XML +#include "libxml/xmlmemory.h" #include "libxml/encoding.h" #endif @@ -46,8 +47,7 @@ inline void prepare_case_tables(unsigned for(int i=0; i<0x100; i++) lcc_table[i]=fcc_table[i]=(unsigned char)i; } -inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr, - unsigned char bit) { +inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr, unsigned char bit) { unsigned char *ctypes_table=tables+ctypes_offset; ctypes_table[0]=bit; for(; *cstr; cstr++) { @@ -66,8 +66,7 @@ inline unsigned int to_wchar_code(const inline bool to_bool(const char* cstr) { return cstr && *cstr!=0; } -static void element2ctypes(unsigned char c, bool belongs, - unsigned char *tables, unsigned char bit, int group_offset=-1) { +static void element2ctypes(unsigned char c, bool belongs, unsigned char *tables, unsigned char bit, int group_offset=-1) { if(!belongs) return; @@ -77,8 +76,7 @@ static void element2ctypes(unsigned char if(group_offset>=0) tables[cbits_offset+group_offset+c/8] |= 1<<(c%8); } -static void element2case(unsigned char from, unsigned char to, - unsigned char *tables) { +static void element2case(unsigned char from, unsigned char to, unsigned char *tables) { if(!to) return; @@ -112,13 +110,13 @@ inline XMLByte *append_hex_16(XMLByte *d // methods -Charset::Charset(Request_charsets* charsets, const String::Body ANAME, const String* afile_spec): +Charset::Charset(Request_charsets* acharsets, const String::Body ANAME, const String* afile_spec): FNAME(ANAME), FNAME_CSTR(ANAME.cstrm()) { if(afile_spec) { fisUTF8=false; - load_definition(*charsets, *afile_spec); + load_definition(*acharsets, *afile_spec); #ifdef XML addEncoding(FNAME_CSTR); #endif @@ -133,7 +131,7 @@ Charset::Charset(Request_charsets* chars #endif } -void Charset::load_definition(Request_charsets& charsets, const String& afile_spec) { +void Charset::load_definition(Request_charsets& acharsets, const String& afile_spec) { // pcre_tables // lowcase, flipcase, bits digit+word+whitespace, masks @@ -147,7 +145,7 @@ void Charset::load_definition(Request_ch memset(&tables, 0, sizeof(tables)); // loading text - char *data=file_read_text(charsets, afile_spec); + char *data=file_read_text(acharsets, afile_spec); // ignore header getrow(&data); @@ -176,9 +174,7 @@ void Charset::load_definition(Request_ch case 8: // charset if(tables.toTableSize>MAX_CHARSET_UNI_CODES) - throw Exception(PARSER_RUNTIME, - &afile_spec, - "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES); + throw Exception(PARSER_RUNTIME, &afile_spec, "charset must contain not more than %d unicode values", MAX_CHARSET_UNI_CODES); XMLCh unicode=(XMLCh)to_wchar_code(cell); if(!unicode && column==7/*unicode1 column*/) @@ -216,15 +212,10 @@ static int sort_cmp_Trans_rec_intCh(cons void Charset::sort_ToTable() { qsort(tables.toTable, tables.toTableSize, sizeof(*tables.toTable), sort_cmp_Trans_rec_intCh); - //FILE *f=fopen("c:\\temp\\a", "wb"); - //fwrite(tables.toTable, tables.toTableSize, sizeof(*tables.toTable), f); - //fclose(f); } // @todo: precache for spedup searching -static XMLByte xlatOneTo(const XMLCh toXlat, - const Charset::Tables& tables, - XMLByte not_found) { +static XMLByte xlatOneTo(const XMLCh toXlat, const Charset::Tables& tables, XMLByte not_found) { int lo = 0; int hi = tables.toTableSize - 1; while(lo<=hi) { @@ -243,9 +234,7 @@ static XMLByte xlatOneTo(const XMLCh toX return not_found; } -String::C Charset::transcode(const String::C src, - const Charset& source_charset, - const Charset& dest_charset) { +String::C Charset::transcode(const String::C src, const Charset& source_charset, const Charset& dest_charset) { if(!src.length) return String::C("", 0); @@ -302,9 +291,7 @@ static const XMLByte gFirstByteMark[7] = 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; -static int transcodeToUTF8(const XMLByte* srcData, int& srcLen, - XMLByte *toFill, int& toFillLen, - const Charset::Tables& tables) { +static int transcodeToUTF8(const XMLByte* srcData, int& srcLen, XMLByte *toFill, int& toFillLen, const Charset::Tables& tables) { const XMLByte* srcPtr=srcData; const XMLByte* srcEnd=srcData+srcLen; XMLByte* outPtr=toFill; @@ -385,9 +372,7 @@ of ocetes consumed. return 0; } /// @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, int& srcLen, - XMLByte* toFill, int& toFillLen, - const Charset::Tables& tables) { +static int transcodeFromUTF8(const XMLByte* srcData, int& srcLen, XMLByte* toFill, int& toFillLen, const Charset::Tables& tables) { const XMLByte* srcPtr=srcData; const XMLByte* srcEnd=srcData+srcLen; XMLByte* outPtr=toFill; @@ -426,9 +411,7 @@ static int transcodeFromUTF8(const XMLBy break; default: - throw Exception(0, - 0, - "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); // never + throw Exception(0, 0, "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); // never } tmpVal-=gUTFOffsets[trailingBytes]; @@ -807,9 +790,7 @@ const String::C Charset::transcodeToUTF8 (XMLByte *)src.str, src_length, dest_body, dest_length, tables)<0) - throw Exception(0, - 0, - "Charset::transcodeToUTF8 buffer overflow"); + throw Exception(0, 0, "Charset::transcodeToUTF8 buffer overflow"); assert(dest_length<=saved_dest_length); dest_body[dest_length]=0; // terminator @@ -884,13 +865,11 @@ static void store_UTF8(XMLCh src, XMLByt outPtr+= encodedBytes; } -static void change_case_UTF8(XMLCh src, XMLByte*& outPtr, - const Charset::UTF8CaseTable& table) { +static void change_case_UTF8(XMLCh src, XMLByte*& outPtr, const Charset::UTF8CaseTable& table) { store_UTF8(change_case_UTF8(src, table), outPtr); -}; -void change_case_UTF8(const XMLByte* srcData, size_t srcLen, - XMLByte* toFill, size_t toFillLen, - const Charset::UTF8CaseTable& table) { +} + +void change_case_UTF8(const XMLByte* srcData, size_t srcLen, XMLByte* toFill, size_t toFillLen, const Charset::UTF8CaseTable& table) { const XMLByte* srcPtr=srcData; const XMLByte* srcEnd=srcData+srcLen; XMLByte* outPtr=toFill; @@ -922,9 +901,7 @@ void change_case_UTF8(const XMLByte* src break; default: - throw Exception(0, - 0, - "change_case_UTF8 error: wrong trailingBytes value(%d)", trailingBytes); + throw Exception(0, 0, "change_case_UTF8 error: wrong trailingBytes value(%d)", trailingBytes); } tmpVal-=gUTFOffsets[trailingBytes]; @@ -934,15 +911,11 @@ void change_case_UTF8(const XMLByte* src if(!(tmpVal & 0xFFFF0000)) change_case_UTF8(tmpVal, outPtr, table); else - throw Exception(0, - 0, - "change_case_UTF8 error: too big tmpVal(0x%08X)", tmpVal); + throw Exception(0, 0, "change_case_UTF8 error: too big tmpVal(0x%08X)", tmpVal); } if(srcPtr!=outPtr) - throw Exception(0, - 0, - "change_case_UTF8 error: end pointers do not match"); + throw Exception(0, 0, "change_case_UTF8 error: end pointers do not match"); } static size_t getDecNumLength(XMLCh UTF8Char){ @@ -981,9 +954,7 @@ const String::C Charset::transcodeFromUT (XMLByte *)src.str, src_length, dest_body, dest_length, tables)<0) - throw Exception(0, - 0, - "Charset::transcodeFromUTF8 buffer overflow"); + throw Exception(0, 0, "Charset::transcodeFromUTF8 buffer overflow"); assert(dest_length<=saved_dest_length); dest_body[dest_length]=0; // terminator @@ -991,8 +962,7 @@ const String::C Charset::transcodeFromUT } /// transcode using both charsets -const String::C Charset::transcodeToCharset(const String::C src, - const Charset& dest_charset) const { +const String::C Charset::transcodeToCharset(const String::C src, const Charset& dest_charset) const { if(&dest_charset==this) return src; else { @@ -1023,6 +993,7 @@ void Charset::store_Char(XMLByte*& outPt #ifdef XML static const Charset::Tables* tables[MAX_CHARSETS]; +static xmlCharEncodingHandler xml_encoding_handlers[MAX_CHARSETS]; #ifdef PA_PATCHED_LIBXML_BACKWARD @@ -1091,12 +1062,9 @@ static size_t handlers_count=0; void Charset::addEncoding(char *name_cstr) { if(handlers_count==MAX_CHARSETS) - throw Exception(0, - 0, - "already allocated %d handlers, no space for new encoding '%s'", - MAX_CHARSETS, name_cstr); + throw Exception(0, 0, "already allocated %d handlers, no space for new encoding '%s'", MAX_CHARSETS, name_cstr); - xmlCharEncodingHandler* handler=new(UseGC) xmlCharEncodingHandler; + xmlCharEncodingHandler* handler=&xml_encoding_handlers[handlers_count]; { handler->name=name_cstr; handler->input=inputFuncs[handlers_count]; @@ -1116,9 +1084,7 @@ void Charset::initTranscoder(const Strin xmlCharEncodingHandler& Charset::transcoder(const String::Body NAME) { if(!ftranscoder) - throw Exception(PARSER_RUNTIME, - new String(NAME, String::L_TAINTED), - "unsupported encoding"); + throw Exception(PARSER_RUNTIME, new String(NAME, String::L_TAINTED), "unsupported encoding"); return *ftranscoder; } @@ -1147,9 +1113,7 @@ String::C Charset::transcode_cstr(const error=0; } if(error<0) - throw Exception(0, - 0, - "transcode_cstr failed (%d)", error); + throw Exception(0, 0, "transcode_cstr failed (%d)", error); assert(outlen<=saved_outlen); out[outlen]=0; return String::C(out, outlen); @@ -1191,9 +1155,7 @@ xmlChar* Charset::transcode_buf2xchar(co } if(error<0) - throw Exception(0, - 0, - "transcode_buf failed (%d)", error); + throw Exception(0, 0, "transcode_buf failed (%d)", error); assert(outlen<=saved_outlen); out[outlen]=0; return out; @@ -1209,30 +1171,18 @@ xmlChar* Charset::transcode(const String } #endif -String::Body Charset::transcode(const String::Body src, - const Charset& source_transcoder, - const Charset& dest_transcoder) { - - const char *src_ptr=src.cstr(); - size_t src_size=src.length(); - - String::C dest=Charset::transcode(String::C(src_ptr, src_size), source_transcoder, dest_transcoder); - - return String::Body(dest.length ? dest.str:0); +String::Body Charset::transcode(const String::Body src, const Charset& source_transcoder, const Charset& dest_transcoder) { + return String::Body(Charset::transcode(String::C(src.cstr(), src.length()), source_transcoder, dest_transcoder)); } -String& Charset::transcode(const String& src, - const Charset& source_transcoder, - const Charset& dest_transcoder) { +String& Charset::transcode(const String& src, const Charset& source_transcoder, const Charset& dest_transcoder) { if(src.is_empty()) return *new String(); return *new String(transcode((String::Body)src, source_transcoder, dest_transcoder), String::L_CLEAN); } -void Charset::transcode(ArrayString& src, - const Charset& source_transcoder, - const Charset& dest_transcoder) { +void Charset::transcode(ArrayString& src, const Charset& source_transcoder, const Charset& dest_transcoder) { for(size_t i=0; isource_transcoder, - *info->dest_transcoder); +static void transcode_pair(HashStringValue::key_type /*akey*/, String::Body& avalue, Transcode_pair_info* info) { + avalue=Charset::transcode(avalue, *info->source_transcoder, *info->dest_transcoder); } -void Charset::transcode(HashStringString& src, - const Charset& source_transcoder, - const Charset& dest_transcoder) { +void Charset::transcode(HashStringString& src, const Charset& source_transcoder, const Charset& dest_transcoder) { Transcode_pair_info info={&source_transcoder, &dest_transcoder}; src.for_each_ref(transcode_pair, &info); } @@ -1276,9 +1220,7 @@ size_t getUTF8CharPos(const XMLByte* src } // scan till end but position in bytes still too low - throw Exception(0, - 0, - "Error convertion byte pos to char pos"); + throw Exception(0, 0, "Error conversion byte pos to char pos"); } size_t lengthUTF8(const XMLByte* srcBegin, const XMLByte* srcEnd){ @@ -1306,7 +1248,7 @@ const char *fixUTF8(const char *src){ do { if(error_offset){ - strncpy(dst, src, error_offset); + memcpy(dst, src, error_offset); dst+=error_offset; src+=error_offset;