Annotation of parser3/src/include/pa_charset.h, revision 1.63
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.63 ! moko 11: #define IDENT_PA_CHARSET_H "$Id: pa_charset.h,v 1.62 2025/06/28 15:38:04 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.41 misha 38: #ifndef XMLCh
39: typedef unsigned int XMLCh;
40: #endif
41: #ifndef XMLByte
42: typedef unsigned char XMLByte;
43: #endif
1.3 paf 44:
1.23 paf 45: // helpers
46:
1.42 misha 47: typedef HashString<String::Body> HashStringString;
1.23 paf 48:
1.1 paf 49: /** charset holds name & transcode tables
1.23 paf 50: registers libxml transcoders
1.1 paf 51: */
1.23 paf 52: class Charset: public PA_Object {
1.1 paf 53: public:
54:
1.24 paf 55: Charset(Request_charsets* charsets, const String::Body ANAME, const String* afile_spec);
1.1 paf 56:
1.24 paf 57: const String::Body NAME() const { return FNAME; }
1.30 paf 58: const char* NAME_CSTR() const { return FNAME_CSTR; }
1.1 paf 59:
60: bool isUTF8() const { return fisUTF8; }
61:
1.53 moko 62: static String::C transcode(const String::C src, const Charset& source_charset, const Charset& dest_charset);
63: static String::Body transcode(const String::Body src, const Charset& source_transcoder, const Charset& dest_transcoder);
1.61 moko 64: 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 65: static String& transcode(const String& src, const Charset& source_transcoder, const Charset& dest_transcoder);
66: static void transcode(ArrayString& src, const Charset& source_transcoder, const Charset& dest_transcoder);
67: static void transcode(HashStringString& src, const Charset& source_transcoder, const Charset& dest_transcoder);
68:
69: static String::C escape(const String::C src, const Charset& source_charset);
70: static String::Body escape(const String::Body src, const Charset& source_charset);
71: static String& escape(const String& src, const Charset& source_charset);
72:
73: static String::C escape_JSON(const String::C src, const Charset& source_charset);
74: static String::Body escape_JSON(const String::Body src, const Charset& source_charset);
75: static String& escape_JSON(const String& src, const Charset& source_charset);
1.38 misha 76:
1.35 misha 77: void store_Char(XMLByte*& outPtr, XMLCh src, XMLByte not_found);
1.33 misha 78:
1.1 paf 79: public:
80:
81: unsigned char pcre_tables[tables_length];
82:
83: private:
84:
1.23 paf 85: void load_definition(Request_charsets& charsets, const String& afile_spec);
1.1 paf 86: void sort_ToTable();
87:
1.23 paf 88: const String::C transcodeToUTF8(const String::C src) const;
89: const String::C transcodeFromUTF8(const String::C src) const;
90:
91: const String::C transcodeToCharset(const String::C src,
92: const Charset& dest_transcoder) const;
1.1 paf 93:
1.4 paf 94: public:
95:
96: struct Tables {
1.25 paf 97: struct Rec {
98: XMLCh intCh;
99: XMLByte extCh;
100: };
101:
1.4 paf 102: XMLCh fromTable[0x100];
1.25 paf 103: Rec toTable[MAX_CHARSET_UNI_CODES];
1.4 paf 104: uint toTableSize;
105: };
106:
1.25 paf 107: struct UTF8CaseTable {
108: struct Rec {
109: XMLCh from, to;
110: };
111:
112: uint size;
113: Rec* records;
114: };
115:
1.1 paf 116: private:
117:
1.24 paf 118: const String::Body FNAME;
1.23 paf 119: char* FNAME_CSTR;
1.1 paf 120: bool fisUTF8;
1.4 paf 121: Tables tables;
1.1 paf 122:
1.45 misha 123: static size_t calc_escaped_length_UTF8(XMLByte* src, size_t src_length);
1.46 moko 124: static size_t calc_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables);
1.45 misha 125: static size_t calc_escaped_length(const String::C src, const Charset& source_charset);
126: static size_t escape_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest);
127: static size_t escape(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables);
128:
129: static size_t calc_JSON_escaped_length_UTF8(XMLByte* src, size_t src_length);
1.46 moko 130: static size_t calc_JSON_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables);
1.45 misha 131: static size_t calc_JSON_escaped_length(const String::C src, const Charset& source_charset);
132: static size_t escape_JSON_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest);
133: static size_t escape_JSON(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables);
134:
1.47 misha 135: #ifdef XML
136:
137: private:
138: void addEncoding(char* name_cstr);
139:
1.1 paf 140: public:
1.23 paf 141: /// converts xmlChar* null-terminated string to char*
1.32 paf 142: String::C transcode_cstr(const xmlChar* s);
1.8 paf 143: /// converts xmlChar* null-terminated string to parser String
1.32 paf 144: const String& transcode(const xmlChar* s);
1.23 paf 145:
146: /** converts sized char* to xmlChar*
147: @returns xmlChar* WHICH CALLER SHOULD FREE
1.16 paf 148: */
1.23 paf 149: xmlChar* transcode_buf2xchar(const char* buf, size_t buf_size);
1.32 paf 150: /// converts parser String to xmlChar*
151: xmlChar* transcode(const String& s);
152: /// converts parser String::Body to xmlChar*
153: xmlChar* transcode(const String::Body s);
1.1 paf 154:
155: private:
156:
1.62 moko 157: xmlCharEncodingInputFunc ftranscoder_input;
158: xmlCharEncodingOutputFunc ftranscoder_output;
1.1 paf 159:
160: #endif
161:
1.5 paf 162: };
1.25 paf 163:
164:
165: // externs
166:
167: extern Charset::UTF8CaseTable UTF8CaseToUpper;
168: extern Charset::UTF8CaseTable UTF8CaseToLower;
1.28 paf 169: void change_case_UTF8(const XMLByte* srcData, size_t srcLen,
1.43 misha 170: XMLByte* toFill, size_t toFillLen,
171: const Charset::UTF8CaseTable& table);
1.37 misha 172: size_t getUTF8BytePos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t charPos/*position in characters*/);
173: size_t getUTF8CharPos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t bytePos/*position in bytes*/);
174: size_t lengthUTF8(const XMLByte* srcBegin, const XMLByte* srcEnd);
1.44 misha 175: unsigned int lengthUTF8Char(const XMLByte c);
1.25 paf 176:
1.52 moko 177: const char *fixUTF8(const char *src);
1.43 misha 178:
179: class UTF8_string_iterator {
180: public:
181: UTF8_string_iterator(const String& astring): fsrcPtr((XMLByte*)astring.cstr()), fsrcEnd(fsrcPtr + astring.length()) {}
182: UTF8_string_iterator(XMLByte* asrcPtr, size_t length): fsrcPtr(asrcPtr), fsrcEnd(fsrcPtr + length) {}
183:
184: bool has_next();
185: XMLCh next() { return fUTF8Char; }
186: XMLByte getFirstByte(){ return ffirstByte; }
187: size_t getCharSize(){ return fcharSize; }
188: private:
189: const XMLByte* fsrcPtr;
190: const XMLByte* fsrcEnd;
191: size_t fcharSize;
192: XMLByte ffirstByte;
193: XMLCh fUTF8Char;
194: };
195:
1.1 paf 196: #endif
E-mail: