Annotation of parser3/src/include/pa_charset.h, revision 1.65

1.1       paf         1: /** @file
                      2:        Parser: Charset connection decl.
                      3: 
1.63      moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.57      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_CHARSET_H
                      9: #define PA_CHARSET_H
1.17      paf        10: 
1.65    ! moko       11: #define IDENT_PA_CHARSET_H "$Id: pa_charset.h,v 1.64 2026/11/26 20:30:15 moko Exp $"
1.23      paf        12: 
1.1       paf        13: 
                     14: #include "pa_exception.h"
                     15: #include "pa_common.h"
1.23      paf        16: #include "pa_hash.h"
                     17: #include "pa_array.h"
1.1       paf        18: 
1.58      moko       19: #ifdef HAVE_PCRE2
                     20: #include <pcre2.h>
                     21: #else
                     22: #include <pcre.h>
                     23: #endif
1.51      moko       24: // we are using some pcre_internal.h stuff as well
                     25: #include "../lib/pcre/pa_pcre_internal.h"
1.1       paf        26: 
1.3       paf        27: #ifdef XML
1.60      moko       28: #include "libxml/xmlstring.h"
1.3       paf        29: #include "libxml/encoding.h"
                     30: #endif
                     31: 
1.1       paf        32: // defines
                     33: 
1.23      paf        34: #define MAX_CHARSETS 10
                     35: 
                     36: #define MAX_CHARSET_UNI_CODES 500
                     37: 
1.65    ! moko       38: #define CHARSET_LATIN1_NAME "ISO-8859-1"
        !            39: 
1.41      misha      40: #ifndef XMLCh 
                     41:        typedef unsigned int XMLCh;
                     42: #endif
                     43: #ifndef XMLByte
                     44:        typedef unsigned char XMLByte;
                     45: #endif
1.3       paf        46: 
1.23      paf        47: // helpers
                     48: 
1.42      misha      49: typedef HashString<String::Body> HashStringString;
1.23      paf        50: 
1.1       paf        51: /**    charset holds name & transcode tables 
1.23      paf        52:        registers libxml transcoders
1.1       paf        53: */
1.23      paf        54: class Charset: public PA_Object {
1.1       paf        55: public:
                     56: 
1.24      paf        57:        Charset(Request_charsets* charsets, const String::Body ANAME, const String* afile_spec);
1.1       paf        58:        
1.24      paf        59:        const String::Body NAME() const { return FNAME; }
1.30      paf        60:        const char* NAME_CSTR() const { return FNAME_CSTR; }
1.1       paf        61: 
                     62:        bool isUTF8() const { return fisUTF8; }
                     63: 
1.53      moko       64:        static String::C transcode(const String::C src, const Charset& source_charset, const Charset& dest_charset);
                     65:        static String::Body transcode(const String::Body src, const Charset& source_transcoder, const Charset& dest_transcoder);
1.61      moko       66:        static String::Body transcode(const char* src,const Charset& source_transcoder, const Charset& dest_transcoder){ return transcode(String::Body(src), source_transcoder, dest_transcoder); }
1.53      moko       67:        static String& transcode(const String& src, const Charset& source_transcoder, const Charset& dest_transcoder);
                     68:        static void transcode(ArrayString& src, const Charset& source_transcoder, const Charset& dest_transcoder);
                     69:        static void transcode(HashStringString& src, const Charset& source_transcoder, const Charset& dest_transcoder);
                     70: 
                     71:        static String::C escape(const String::C src, const Charset& source_charset);
                     72:        static String::Body escape(const String::Body src, const Charset& source_charset);
                     73:        static String& escape(const String& src, const Charset& source_charset);
                     74: 
                     75:        static String::C escape_JSON(const String::C src, const Charset& source_charset);
                     76:        static String::Body escape_JSON(const String::Body src, const Charset& source_charset);
                     77:        static String& escape_JSON(const String& src, const Charset& source_charset);
1.38      misha      78: 
1.35      misha      79:        void store_Char(XMLByte*& outPtr, XMLCh src, XMLByte not_found);
1.33      misha      80: 
1.1       paf        81: public:
                     82: 
                     83:        unsigned char pcre_tables[tables_length];
                     84: 
                     85: private:
                     86: 
1.23      paf        87:        void load_definition(Request_charsets& charsets, const String& afile_spec);
1.65    ! moko       88:        void load_latin1();
1.1       paf        89:        void sort_ToTable();
                     90: 
1.23      paf        91:        const String::C transcodeToUTF8(const String::C src) const;
                     92:        const String::C transcodeFromUTF8(const String::C src) const;
                     93:        
                     94:        const String::C transcodeToCharset(const String::C src,
                     95:                const Charset& dest_transcoder) const;
1.1       paf        96: 
1.4       paf        97: public:
                     98: 
                     99:        struct Tables {
1.25      paf       100:                struct Rec {
                    101:                        XMLCh intCh;
                    102:                        XMLByte extCh;
                    103:                };
                    104: 
1.4       paf       105:                XMLCh fromTable[0x100];
1.25      paf       106:                Rec toTable[MAX_CHARSET_UNI_CODES];
1.4       paf       107:                uint toTableSize;
                    108:        };
                    109: 
1.25      paf       110:        struct UTF8CaseTable {
                    111:                struct Rec {
                    112:                        XMLCh from, to;
                    113:                };
                    114: 
                    115:                uint size;
                    116:                Rec* records;
                    117:        };
                    118: 
1.1       paf       119: private:
                    120: 
1.24      paf       121:        const String::Body FNAME;
1.23      paf       122:        char* FNAME_CSTR;
1.1       paf       123:        bool fisUTF8;
1.4       paf       124:        Tables tables;
1.1       paf       125: 
1.45      misha     126:        static size_t calc_escaped_length_UTF8(XMLByte* src, size_t src_length);
1.46      moko      127:        static size_t calc_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables);
1.45      misha     128:        static size_t calc_escaped_length(const String::C src, const Charset& source_charset);
                    129:        static size_t escape_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest);
                    130:        static size_t escape(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables);
                    131: 
                    132:        static size_t calc_JSON_escaped_length_UTF8(XMLByte* src, size_t src_length);
1.46      moko      133:        static size_t calc_JSON_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables);
1.45      misha     134:        static size_t calc_JSON_escaped_length(const String::C src, const Charset& source_charset);
                    135:        static size_t escape_JSON_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest);
                    136:        static size_t escape_JSON(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables);
                    137: 
1.47      misha     138: #ifdef XML
                    139: 
                    140: private:
                    141:        void addEncoding(char* name_cstr);
                    142: 
1.1       paf       143: public:
1.23      paf       144:        /// converts xmlChar* null-terminated string to char* 
1.32      paf       145:        String::C transcode_cstr(const xmlChar* s);
1.8       paf       146:        /// converts xmlChar* null-terminated string to parser String
1.32      paf       147:        const String& transcode(const xmlChar* s);
1.23      paf       148: 
                    149:        /** converts sized char*  to xmlChar*
                    150:                @returns xmlChar*  WHICH CALLER SHOULD FREE
1.16      paf       151:        */
1.23      paf       152:        xmlChar* transcode_buf2xchar(const char* buf, size_t buf_size);
1.32      paf       153:        /// converts parser String to xmlChar*
                    154:        xmlChar* transcode(const String& s);
                    155:        /// converts parser String::Body to xmlChar*
                    156:        xmlChar* transcode(const String::Body s);
1.1       paf       157: 
                    158: #endif
                    159: 
1.5       paf       160: };
1.25      paf       161: 
                    162: 
                    163: // externs
                    164: 
                    165: extern Charset::UTF8CaseTable UTF8CaseToUpper;
                    166: extern Charset::UTF8CaseTable UTF8CaseToLower;
1.28      paf       167: void change_case_UTF8(const XMLByte* srcData, size_t srcLen,
1.43      misha     168:                                        XMLByte* toFill, size_t toFillLen,
                    169:                                        const Charset::UTF8CaseTable& table);
1.37      misha     170: size_t getUTF8BytePos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t charPos/*position in characters*/);
                    171: size_t getUTF8CharPos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t bytePos/*position in bytes*/);
                    172: size_t lengthUTF8(const XMLByte* srcBegin, const XMLByte* srcEnd);
1.44      misha     173: unsigned int lengthUTF8Char(const XMLByte c);
1.25      paf       174: 
1.52      moko      175: const char *fixUTF8(const char *src);
1.43      misha     176: 
                    177: class UTF8_string_iterator {
                    178:        public:
                    179:                UTF8_string_iterator(const String& astring): fsrcPtr((XMLByte*)astring.cstr()), fsrcEnd(fsrcPtr + astring.length()) {}
                    180:                UTF8_string_iterator(XMLByte* asrcPtr, size_t length): fsrcPtr(asrcPtr), fsrcEnd(fsrcPtr + length) {}
                    181: 
                    182:                bool has_next();
                    183:                XMLCh next() { return fUTF8Char; }
                    184:                XMLByte getFirstByte(){ return ffirstByte; }
                    185:                size_t getCharSize(){ return fcharSize; }
                    186:        private:
                    187:                const XMLByte* fsrcPtr;
                    188:                const XMLByte* fsrcEnd;
                    189:                size_t fcharSize;
                    190:                XMLByte ffirstByte;
                    191:                XMLCh fUTF8Char;
                    192: };
                    193: 
1.1       paf       194: #endif

E-mail: