--- parser3/src/main/pa_charset.C 2015/10/09 11:49:54 1.99 +++ parser3/src/main/pa_charset.C 2021/11/09 15:10:30 1.110 @@ -1,7 +1,7 @@ /** @file Parser: Charset connection implementation. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexander Petrosyan(http://paf.design.ru) */ @@ -11,7 +11,7 @@ // 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.99 2015/10/09 11:49:54 moko Exp $" IDENT_PA_CHARSET_H; +volatile const char * IDENT_PA_CHARSET_C="$Id: pa_charset.C,v 1.110 2021/11/09 15:10:30 moko Exp $" IDENT_PA_CHARSET_H; #ifdef XML #include "libxml/encoding.h" @@ -109,13 +109,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 @@ -130,7 +130,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 @@ -144,7 +144,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,9 +173,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 then %d unicode values", MAX_CHARSET_UNI_CODES); XMLCh unicode=(XMLCh)to_wchar_code(cell); if(!unicode && column==7/*unicode1 column*/) @@ -213,9 +211,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 @@ -415,9 +410,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]; @@ -796,9 +789,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 @@ -909,9 +900,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]; @@ -921,15 +910,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){ @@ -968,9 +953,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 @@ -1077,12 +1060,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 xmlCharEncodingHandler; + xmlCharEncodingHandler* handler=new(PointerGC) xmlCharEncodingHandler; { handler->name=name_cstr; handler->input=inputFuncs[handlers_count]; @@ -1102,9 +1082,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; } @@ -1133,9 +1111,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); @@ -1177,9 +1153,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; @@ -1196,13 +1170,7 @@ 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); + 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) { @@ -1250,9 +1218,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){ @@ -1280,7 +1246,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;