--- parser3/src/main/pa_charset.C 2016/09/21 12:40:20 1.103 +++ parser3/src/main/pa_charset.C 2025/06/28 15:42:00 1.117 @@ -1,8 +1,8 @@ /** @file Parser: Charset connection implementation. - Copyright (c) 2001-2015 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,14 +11,13 @@ // 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.103 2016/09/21 12:40:20 moko Exp $" IDENT_PA_CHARSET_H; +volatile const char * IDENT_PA_CHARSET_C="$Id: pa_charset.C,v 1.117 2025/06/28 15:42:00 moko Exp $" IDENT_PA_CHARSET_H; #ifdef XML +#include "libxml/xmlmemory.h" #include "libxml/encoding.h" #endif -//#define PA_PATCHED_LIBXML_BACKWARD - // reduce memory usage by pre-calculation utf-8 string length #define PRECALCULATE_DEST_LENGTH @@ -124,13 +123,9 @@ Charset::Charset(Request_charsets* achar // grab default onces [for UTF-8 so to be able to make a-z =>A-Z memcpy(pcre_tables, pa_pcre_default_tables, sizeof(pcre_tables)); } - -#ifdef XML - initTranscoder(FNAME, FNAME_CSTR); -#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 @@ -144,7 +139,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); @@ -173,7 +168,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*/) @@ -211,9 +206,6 @@ 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 @@ -996,28 +988,6 @@ void Charset::store_Char(XMLByte*& outPt 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, *inlen, \ - out, *outlen, \ - *tables[i]); \ - } \ - static int xml256CharEncodingOutputFunc##i( \ - unsigned char *out, int *outlen, \ - const unsigned char *in, int *inlen, void*) { \ - return transcodeFromUTF8( \ - in, *inlen, \ - out, *outlen, \ - *tables[i]); \ - } - -#else - #define declareXml256ioFuncs(i) \ static int xml256CharEncodingInputFunc##i( \ unsigned char *out, int *outlen, \ @@ -1036,9 +1006,6 @@ static const Charset::Tables* tables[MAX *tables[i]); \ } -#endif - - declareXml256ioFuncs(0) declareXml256ioFuncs(1) declareXml256ioFuncs(2) declareXml256ioFuncs(3) declareXml256ioFuncs(4) declareXml256ioFuncs(5) @@ -1065,28 +1032,14 @@ void Charset::addEncoding(char *name_cst if(handlers_count==MAX_CHARSETS) throw Exception(0, 0, "already allocated %d handlers, no space for new encoding '%s'", MAX_CHARSETS, name_cstr); - xmlCharEncodingHandler* handler=new xmlCharEncodingHandler; - { - handler->name=name_cstr; - handler->input=inputFuncs[handlers_count]; - handler->output=outputFuncs[handlers_count]; - ::tables[handlers_count]=&tables; - handlers_count++; - } - + ftranscoder_input=inputFuncs[handlers_count]; + ftranscoder_output=outputFuncs[handlers_count]; + ::tables[handlers_count++]=&tables; + + xmlCharEncodingHandler* handler=xmlNewCharEncodingHandler(name_cstr, ftranscoder_input, ftranscoder_output); + if(!handler) + throw Exception(0, new String(name_cstr, String::L_TAINTED), "unable to register XML encoding handler"); xmlRegisterCharEncodingHandler(handler); - -} - -void Charset::initTranscoder(const String::Body NAME, const char* name_cstr) { - ftranscoder=xmlFindCharEncodingHandler(name_cstr); - transcoder(NAME); // check right way -} - -xmlCharEncodingHandler& Charset::transcoder(const String::Body NAME) { - if(!ftranscoder) - throw Exception(PARSER_RUNTIME, new String(NAME, String::L_TAINTED), "unsupported encoding"); - return *ftranscoder; } String::C Charset::transcode_cstr(const xmlChar* s) { @@ -1101,14 +1054,8 @@ String::C Charset::transcode_cstr(const char *out=new(PointerFreeGC) char[outlen+1]; int error; - if(xmlCharEncodingOutputFunc output=transcoder(FNAME).output) { - error=output( - (unsigned char*)out, &outlen, - (const unsigned char*)s, &inlen -#ifdef PA_PATCHED_LIBXML_BACKWARD - ,0 -#endif - ); + if(!fisUTF8) { + error=ftranscoder_output((unsigned char*)out, &outlen, (const unsigned char*)s, &inlen); } else { memcpy(out, s, outlen=inlen); error=0; @@ -1132,19 +1079,13 @@ xmlChar* Charset::transcode_buf2xchar(co #ifndef NDEBUG int saved_outlen; #endif - if(xmlCharEncodingInputFunc input=transcoder(FNAME).input) { + if(!fisUTF8) { outlen=buf_size*6/*max UTF8 bytes per char*/; #ifndef NDEBUG saved_outlen=outlen; #endif out=(xmlChar*)xmlMalloc(outlen+1); - error=input( - out, &outlen, - (const unsigned char*)buf, (int*)&buf_size -#ifdef PA_PATCHED_LIBXML_BACKWARD - ,0 -#endif - ); + error=ftranscoder_input(out, &outlen, (const unsigned char*)buf, (int*)&buf_size); } else { outlen=buf_size; #ifndef NDEBUG @@ -1221,7 +1162,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){ @@ -1249,7 +1190,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;