Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * pg_wchar.h
4 : * multibyte-character support
5 : *
6 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : * src/include/mb/pg_wchar.h
10 : *
11 : * NOTES
12 : * This is used both by the backend and by frontends, but should not be
13 : * included by libpq client programs. In particular, a libpq client
14 : * should not assume that the encoding IDs used by the version of libpq
15 : * it's linked to match up with the IDs declared here.
16 : *
17 : *-------------------------------------------------------------------------
18 : */
19 : #ifndef PG_WCHAR_H
20 : #define PG_WCHAR_H
21 :
22 : #include "port/simd.h"
23 :
24 : /*
25 : * The pg_wchar type
26 : */
27 : typedef unsigned int pg_wchar;
28 :
29 : /*
30 : * Maximum byte length of multibyte characters in any backend encoding
31 : */
32 : #define MAX_MULTIBYTE_CHAR_LEN 4
33 :
34 : /*
35 : * various definitions for EUC
36 : */
37 : #define SS2 0x8e /* single shift 2 (JIS0201) */
38 : #define SS3 0x8f /* single shift 3 (JIS0212) */
39 :
40 : /*
41 : * SJIS validation macros
42 : */
43 : #define ISSJISHEAD(c) (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc))
44 : #define ISSJISTAIL(c) (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc))
45 :
46 : /*----------------------------------------------------
47 : * MULE Internal Encoding (MIC)
48 : *
49 : * This encoding follows the design used within XEmacs; it is meant to
50 : * subsume many externally-defined character sets. Each character includes
51 : * identification of the character set it belongs to, so the encoding is
52 : * general but somewhat bulky.
53 : *
54 : * Currently PostgreSQL supports 5 types of MULE character sets:
55 : *
56 : * 1) 1-byte ASCII characters. Each byte is below 0x80.
57 : *
58 : * 2) "Official" single byte charsets such as ISO-8859-1 (Latin1).
59 : * Each MULE character consists of 2 bytes: LC1 + C1, where LC1 is
60 : * an identifier for the charset (in the range 0x81 to 0x8d) and C1
61 : * is the character code (in the range 0xa0 to 0xff).
62 : *
63 : * 3) "Private" single byte charsets such as SISHENG. Each MULE
64 : * character consists of 3 bytes: LCPRV1 + LC12 + C1, where LCPRV1
65 : * is a private-charset flag, LC12 is an identifier for the charset,
66 : * and C1 is the character code (in the range 0xa0 to 0xff).
67 : * LCPRV1 is either 0x9a (if LC12 is in the range 0xa0 to 0xdf)
68 : * or 0x9b (if LC12 is in the range 0xe0 to 0xef).
69 : *
70 : * 4) "Official" multibyte charsets such as JIS X0208. Each MULE
71 : * character consists of 3 bytes: LC2 + C1 + C2, where LC2 is
72 : * an identifier for the charset (in the range 0x90 to 0x99) and C1
73 : * and C2 form the character code (each in the range 0xa0 to 0xff).
74 : *
75 : * 5) "Private" multibyte charsets such as CNS 11643-1992 Plane 3.
76 : * Each MULE character consists of 4 bytes: LCPRV2 + LC22 + C1 + C2,
77 : * where LCPRV2 is a private-charset flag, LC22 is an identifier for
78 : * the charset, and C1 and C2 form the character code (each in the range
79 : * 0xa0 to 0xff). LCPRV2 is either 0x9c (if LC22 is in the range 0xf0
80 : * to 0xf4) or 0x9d (if LC22 is in the range 0xf5 to 0xfe).
81 : *
82 : * "Official" encodings are those that have been assigned code numbers by
83 : * the XEmacs project; "private" encodings have Postgres-specific charset
84 : * identifiers.
85 : *
86 : * See the "XEmacs Internals Manual", available at http://www.xemacs.org,
87 : * for more details. Note that for historical reasons, Postgres'
88 : * private-charset flag values do not match what XEmacs says they should be,
89 : * so this isn't really exactly MULE (not that private charsets would be
90 : * interoperable anyway).
91 : *
92 : * Note that XEmacs's implementation is different from what emacs does.
93 : * We follow emacs's implementation, rather than XEmacs's.
94 : *----------------------------------------------------
95 : */
96 :
97 : /*
98 : * Charset identifiers (also called "leading bytes" in the MULE documentation)
99 : */
100 :
101 : /*
102 : * Charset IDs for official single byte encodings (0x81-0x8e)
103 : */
104 : #define LC_ISO8859_1 0x81 /* ISO8859 Latin 1 */
105 : #define LC_ISO8859_2 0x82 /* ISO8859 Latin 2 */
106 : #define LC_ISO8859_3 0x83 /* ISO8859 Latin 3 */
107 : #define LC_ISO8859_4 0x84 /* ISO8859 Latin 4 */
108 : #define LC_TIS620 0x85 /* Thai (not supported yet) */
109 : #define LC_ISO8859_7 0x86 /* Greek (not supported yet) */
110 : #define LC_ISO8859_6 0x87 /* Arabic (not supported yet) */
111 : #define LC_ISO8859_8 0x88 /* Hebrew (not supported yet) */
112 : #define LC_JISX0201K 0x89 /* Japanese 1 byte kana */
113 : #define LC_JISX0201R 0x8a /* Japanese 1 byte Roman */
114 : /* Note that 0x8b seems to be unused as of Emacs 20.7.
115 : * However, there might be a chance that 0x8b could be used
116 : * in later versions of Emacs.
117 : */
118 : #define LC_KOI8_R 0x8b /* Cyrillic KOI8-R */
119 : #define LC_ISO8859_5 0x8c /* ISO8859 Cyrillic */
120 : #define LC_ISO8859_9 0x8d /* ISO8859 Latin 5 (not supported yet) */
121 : #define LC_ISO8859_15 0x8e /* ISO8859 Latin 15 (not supported yet) */
122 : /* #define CONTROL_1 0x8f control characters (unused) */
123 :
124 : /* Is a leading byte for "official" single byte encodings? */
125 : #define IS_LC1(c) ((unsigned char)(c) >= 0x81 && (unsigned char)(c) <= 0x8d)
126 :
127 : /*
128 : * Charset IDs for official multibyte encodings (0x90-0x99)
129 : * 0x9a-0x9d are free. 0x9e and 0x9f are reserved.
130 : */
131 : #define LC_JISX0208_1978 0x90 /* Japanese Kanji, old JIS (not supported) */
132 : #define LC_GB2312_80 0x91 /* Chinese */
133 : #define LC_JISX0208 0x92 /* Japanese Kanji (JIS X 0208) */
134 : #define LC_KS5601 0x93 /* Korean */
135 : #define LC_JISX0212 0x94 /* Japanese Kanji (JIS X 0212) */
136 : #define LC_CNS11643_1 0x95 /* CNS 11643-1992 Plane 1 */
137 : #define LC_CNS11643_2 0x96 /* CNS 11643-1992 Plane 2 */
138 : #define LC_JISX0213_1 0x97 /* Japanese Kanji (JIS X 0213 Plane 1)
139 : * (not supported) */
140 : #define LC_BIG5_1 0x98 /* Plane 1 Chinese traditional (not
141 : * supported) */
142 : #define LC_BIG5_2 0x99 /* Plane 1 Chinese traditional (not
143 : * supported) */
144 :
145 : /* Is a leading byte for "official" multibyte encodings? */
146 : #define IS_LC2(c) ((unsigned char)(c) >= 0x90 && (unsigned char)(c) <= 0x99)
147 :
148 : /*
149 : * Postgres-specific prefix bytes for "private" single byte encodings
150 : * (According to the MULE docs, we should be using 0x9e for this)
151 : */
152 : #define LCPRV1_A 0x9a
153 : #define LCPRV1_B 0x9b
154 : #define IS_LCPRV1(c) ((unsigned char)(c) == LCPRV1_A || (unsigned char)(c) == LCPRV1_B)
155 : #define IS_LCPRV1_A_RANGE(c) \
156 : ((unsigned char)(c) >= 0xa0 && (unsigned char)(c) <= 0xdf)
157 : #define IS_LCPRV1_B_RANGE(c) \
158 : ((unsigned char)(c) >= 0xe0 && (unsigned char)(c) <= 0xef)
159 :
160 : /*
161 : * Postgres-specific prefix bytes for "private" multibyte encodings
162 : * (According to the MULE docs, we should be using 0x9f for this)
163 : */
164 : #define LCPRV2_A 0x9c
165 : #define LCPRV2_B 0x9d
166 : #define IS_LCPRV2(c) ((unsigned char)(c) == LCPRV2_A || (unsigned char)(c) == LCPRV2_B)
167 : #define IS_LCPRV2_A_RANGE(c) \
168 : ((unsigned char)(c) >= 0xf0 && (unsigned char)(c) <= 0xf4)
169 : #define IS_LCPRV2_B_RANGE(c) \
170 : ((unsigned char)(c) >= 0xf5 && (unsigned char)(c) <= 0xfe)
171 :
172 : /*
173 : * Charset IDs for private single byte encodings (0xa0-0xef)
174 : */
175 : #define LC_SISHENG 0xa0 /* Chinese SiSheng characters for
176 : * PinYin/ZhuYin (not supported) */
177 : #define LC_IPA 0xa1 /* IPA (International Phonetic
178 : * Association) (not supported) */
179 : #define LC_VISCII_LOWER 0xa2 /* Vietnamese VISCII1.1 lower-case (not
180 : * supported) */
181 : #define LC_VISCII_UPPER 0xa3 /* Vietnamese VISCII1.1 upper-case (not
182 : * supported) */
183 : #define LC_ARABIC_DIGIT 0xa4 /* Arabic digit (not supported) */
184 : #define LC_ARABIC_1_COLUMN 0xa5 /* Arabic 1-column (not supported) */
185 : #define LC_ASCII_RIGHT_TO_LEFT 0xa6 /* ASCII (left half of ISO8859-1) with
186 : * right-to-left direction (not
187 : * supported) */
188 : #define LC_LAO 0xa7 /* Lao characters (ISO10646 0E80..0EDF)
189 : * (not supported) */
190 : #define LC_ARABIC_2_COLUMN 0xa8 /* Arabic 1-column (not supported) */
191 :
192 : /*
193 : * Charset IDs for private multibyte encodings (0xf0-0xff)
194 : */
195 : #define LC_INDIAN_1_COLUMN 0xf0 /* Indian charset for 1-column width
196 : * glyphs (not supported) */
197 : #define LC_TIBETAN_1_COLUMN 0xf1 /* Tibetan 1-column width glyphs (not
198 : * supported) */
199 : #define LC_UNICODE_SUBSET_2 0xf2 /* Unicode characters of the range
200 : * U+2500..U+33FF. (not supported) */
201 : #define LC_UNICODE_SUBSET_3 0xf3 /* Unicode characters of the range
202 : * U+E000..U+FFFF. (not supported) */
203 : #define LC_UNICODE_SUBSET 0xf4 /* Unicode characters of the range
204 : * U+0100..U+24FF. (not supported) */
205 : #define LC_ETHIOPIC 0xf5 /* Ethiopic characters (not supported) */
206 : #define LC_CNS11643_3 0xf6 /* CNS 11643-1992 Plane 3 */
207 : #define LC_CNS11643_4 0xf7 /* CNS 11643-1992 Plane 4 */
208 : #define LC_CNS11643_5 0xf8 /* CNS 11643-1992 Plane 5 */
209 : #define LC_CNS11643_6 0xf9 /* CNS 11643-1992 Plane 6 */
210 : #define LC_CNS11643_7 0xfa /* CNS 11643-1992 Plane 7 */
211 : #define LC_INDIAN_2_COLUMN 0xfb /* Indian charset for 2-column width
212 : * glyphs (not supported) */
213 : #define LC_TIBETAN 0xfc /* Tibetan (not supported) */
214 : /* #define FREE 0xfd free (unused) */
215 : /* #define FREE 0xfe free (unused) */
216 : /* #define FREE 0xff free (unused) */
217 :
218 : /*----------------------------------------------------
219 : * end of MULE stuff
220 : *----------------------------------------------------
221 : */
222 :
223 : /*
224 : * PostgreSQL encoding identifiers
225 : *
226 : * WARNING: the order of this enum must be same as order of entries
227 : * in the pg_enc2name_tbl[] array (in src/common/encnames.c), and
228 : * in the pg_wchar_table[] array (in src/common/wchar.c)!
229 : *
230 : * If you add some encoding don't forget to check
231 : * PG_ENCODING_BE_LAST macro.
232 : *
233 : * PG_SQL_ASCII is default encoding and must be = 0.
234 : *
235 : * XXX We must avoid renumbering any backend encoding until libpq's major
236 : * version number is increased beyond 5; it turns out that the backend
237 : * encoding IDs are effectively part of libpq's ABI as far as 8.2 initdb and
238 : * psql are concerned.
239 : */
240 : typedef enum pg_enc
241 : {
242 : PG_SQL_ASCII = 0, /* SQL/ASCII */
243 : PG_EUC_JP, /* EUC for Japanese */
244 : PG_EUC_CN, /* EUC for Chinese */
245 : PG_EUC_KR, /* EUC for Korean */
246 : PG_EUC_TW, /* EUC for Taiwan */
247 : PG_EUC_JIS_2004, /* EUC-JIS-2004 */
248 : PG_UTF8, /* Unicode UTF8 */
249 : PG_MULE_INTERNAL, /* Mule internal code */
250 : PG_LATIN1, /* ISO-8859-1 Latin 1 */
251 : PG_LATIN2, /* ISO-8859-2 Latin 2 */
252 : PG_LATIN3, /* ISO-8859-3 Latin 3 */
253 : PG_LATIN4, /* ISO-8859-4 Latin 4 */
254 : PG_LATIN5, /* ISO-8859-9 Latin 5 */
255 : PG_LATIN6, /* ISO-8859-10 Latin6 */
256 : PG_LATIN7, /* ISO-8859-13 Latin7 */
257 : PG_LATIN8, /* ISO-8859-14 Latin8 */
258 : PG_LATIN9, /* ISO-8859-15 Latin9 */
259 : PG_LATIN10, /* ISO-8859-16 Latin10 */
260 : PG_WIN1256, /* windows-1256 */
261 : PG_WIN1258, /* Windows-1258 */
262 : PG_WIN866, /* (MS-DOS CP866) */
263 : PG_WIN874, /* windows-874 */
264 : PG_KOI8R, /* KOI8-R */
265 : PG_WIN1251, /* windows-1251 */
266 : PG_WIN1252, /* windows-1252 */
267 : PG_ISO_8859_5, /* ISO-8859-5 */
268 : PG_ISO_8859_6, /* ISO-8859-6 */
269 : PG_ISO_8859_7, /* ISO-8859-7 */
270 : PG_ISO_8859_8, /* ISO-8859-8 */
271 : PG_WIN1250, /* windows-1250 */
272 : PG_WIN1253, /* windows-1253 */
273 : PG_WIN1254, /* windows-1254 */
274 : PG_WIN1255, /* windows-1255 */
275 : PG_WIN1257, /* windows-1257 */
276 : PG_KOI8U, /* KOI8-U */
277 : /* PG_ENCODING_BE_LAST points to the above entry */
278 :
279 : /* followings are for client encoding only */
280 : PG_SJIS, /* Shift JIS (Windows-932) */
281 : PG_BIG5, /* Big5 (Windows-950) */
282 : PG_GBK, /* GBK (Windows-936) */
283 : PG_UHC, /* UHC (Windows-949) */
284 : PG_GB18030, /* GB18030 */
285 : PG_JOHAB, /* EUC for Korean JOHAB */
286 : PG_SHIFT_JIS_2004, /* Shift-JIS-2004 */
287 : _PG_LAST_ENCODING_ /* mark only */
288 :
289 : } pg_enc;
290 :
291 : #define PG_ENCODING_BE_LAST PG_KOI8U
292 :
293 : /*
294 : * Please use these tests before access to pg_enc2name_tbl[]
295 : * or to other places...
296 : */
297 : #define PG_VALID_BE_ENCODING(_enc) \
298 : ((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST)
299 :
300 : #define PG_ENCODING_IS_CLIENT_ONLY(_enc) \
301 : ((_enc) > PG_ENCODING_BE_LAST && (_enc) < _PG_LAST_ENCODING_)
302 :
303 : #define PG_VALID_ENCODING(_enc) \
304 : ((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_)
305 :
306 : /* On FE are possible all encodings */
307 : #define PG_VALID_FE_ENCODING(_enc) PG_VALID_ENCODING(_enc)
308 :
309 : /*
310 : * When converting strings between different encodings, we assume that space
311 : * for converted result is 4-to-1 growth in the worst case. The rate for
312 : * currently supported encoding pairs are within 3 (SJIS JIS X0201 half width
313 : * kana -> UTF8 is the worst case). So "4" should be enough for the moment.
314 : *
315 : * Note that this is not the same as the maximum character width in any
316 : * particular encoding.
317 : */
318 : #define MAX_CONVERSION_GROWTH 4
319 :
320 : /*
321 : * Maximum byte length of a string that's required in any encoding to convert
322 : * at least one character to any other encoding. In other words, if you feed
323 : * MAX_CONVERSION_INPUT_LENGTH bytes to any encoding conversion function, it
324 : * is guaranteed to be able to convert something without needing more input
325 : * (assuming the input is valid).
326 : *
327 : * Currently, the maximum case is the conversion UTF8 -> SJIS JIS X0201 half
328 : * width kana, where a pair of UTF-8 characters is converted into a single
329 : * SHIFT_JIS_2004 character (the reverse of the worst case for
330 : * MAX_CONVERSION_GROWTH). It needs 6 bytes of input. In theory, a
331 : * user-defined conversion function might have more complicated cases, although
332 : * for the reverse mapping you would probably also need to bump up
333 : * MAX_CONVERSION_GROWTH. But there is no need to be stingy here, so make it
334 : * generous.
335 : */
336 : #define MAX_CONVERSION_INPUT_LENGTH 16
337 :
338 : /*
339 : * Maximum byte length of the string equivalent to any one Unicode code point,
340 : * in any backend encoding. The current value assumes that a 4-byte UTF-8
341 : * character might expand by MAX_CONVERSION_GROWTH, which is a huge
342 : * overestimate. But in current usage we don't allocate large multiples of
343 : * this, so there's little point in being stingy.
344 : */
345 : #define MAX_UNICODE_EQUIVALENT_STRING 16
346 :
347 : /*
348 : * Table for mapping an encoding number to official encoding name and
349 : * possibly other subsidiary data. Be careful to check encoding number
350 : * before accessing a table entry!
351 : *
352 : * if (PG_VALID_ENCODING(encoding))
353 : * pg_enc2name_tbl[ encoding ];
354 : */
355 : typedef struct pg_enc2name
356 : {
357 : const char *name;
358 : pg_enc encoding;
359 : #ifdef WIN32
360 : unsigned codepage; /* codepage for WIN32 */
361 : #endif
362 : } pg_enc2name;
363 :
364 : extern PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[];
365 :
366 : /*
367 : * Encoding names for gettext
368 : */
369 : typedef struct pg_enc2gettext
370 : {
371 : pg_enc encoding;
372 : const char *name;
373 : } pg_enc2gettext;
374 :
375 : extern PGDLLIMPORT const pg_enc2gettext pg_enc2gettext_tbl[];
376 :
377 : /*
378 : * pg_wchar stuff
379 : */
380 : typedef int (*mb2wchar_with_len_converter) (const unsigned char *from,
381 : pg_wchar *to,
382 : int len);
383 :
384 : typedef int (*wchar2mb_with_len_converter) (const pg_wchar *from,
385 : unsigned char *to,
386 : int len);
387 :
388 : typedef int (*mblen_converter) (const unsigned char *mbstr);
389 :
390 : typedef int (*mbdisplaylen_converter) (const unsigned char *mbstr);
391 :
392 : typedef bool (*mbcharacter_incrementer) (unsigned char *mbstr, int len);
393 :
394 : typedef int (*mbchar_verifier) (const unsigned char *mbstr, int len);
395 :
396 : typedef int (*mbstr_verifier) (const unsigned char *mbstr, int len);
397 :
398 : typedef struct
399 : {
400 : mb2wchar_with_len_converter mb2wchar_with_len; /* convert a multibyte
401 : * string to a wchar */
402 : wchar2mb_with_len_converter wchar2mb_with_len; /* convert a wchar string
403 : * to a multibyte */
404 : mblen_converter mblen; /* get byte length of a char */
405 : mbdisplaylen_converter dsplen; /* get display width of a char */
406 : mbchar_verifier mbverifychar; /* verify multibyte character */
407 : mbstr_verifier mbverifystr; /* verify multibyte string */
408 : int maxmblen; /* max bytes for a char in this encoding */
409 : } pg_wchar_tbl;
410 :
411 : extern PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[];
412 :
413 : /*
414 : * Data structures for conversions between UTF-8 and other encodings
415 : * (UtfToLocal() and LocalToUtf()). In these data structures, characters of
416 : * either encoding are represented by uint32 words; hence we can only support
417 : * characters up to 4 bytes long. For example, the byte sequence 0xC2 0x89
418 : * would be represented by 0x0000C289, and 0xE8 0xA2 0xB4 by 0x00E8A2B4.
419 : *
420 : * There are three possible ways a character can be mapped:
421 : *
422 : * 1. Using a radix tree, from source to destination code.
423 : * 2. Using a sorted array of source -> destination code pairs. This
424 : * method is used for "combining" characters. There are so few of
425 : * them that building a radix tree would be wasteful.
426 : * 3. Using a conversion function.
427 : */
428 :
429 : /*
430 : * Radix tree for character conversion.
431 : *
432 : * Logically, this is actually four different radix trees, for 1-byte,
433 : * 2-byte, 3-byte and 4-byte inputs. The 1-byte tree is a simple lookup
434 : * table from source to target code. The 2-byte tree consists of two levels:
435 : * one lookup table for the first byte, where the value in the lookup table
436 : * points to a lookup table for the second byte. And so on.
437 : *
438 : * Physically, all the trees are stored in one big array, in 'chars16' or
439 : * 'chars32', depending on the maximum value that needs to be represented. For
440 : * each level in each tree, we also store lower and upper bound of allowed
441 : * values - values outside those bounds are considered invalid, and are left
442 : * out of the tables.
443 : *
444 : * In the intermediate levels of the trees, the values stored are offsets
445 : * into the chars[16|32] array.
446 : *
447 : * In the beginning of the chars[16|32] array, there is always a number of
448 : * zeros, so that you safely follow an index from an intermediate table
449 : * without explicitly checking for a zero. Following a zero any number of
450 : * times will always bring you to the dummy, all-zeros table in the
451 : * beginning. This helps to shave some cycles when looking up values.
452 : */
453 : typedef struct
454 : {
455 : /*
456 : * Array containing all the values. Only one of chars16 or chars32 is
457 : * used, depending on how wide the values we need to represent are.
458 : */
459 : const uint16 *chars16;
460 : const uint32 *chars32;
461 :
462 : /* Radix tree for 1-byte inputs */
463 : uint32 b1root; /* offset of table in the chars[16|32] array */
464 : uint8 b1_lower; /* min allowed value for a single byte input */
465 : uint8 b1_upper; /* max allowed value for a single byte input */
466 :
467 : /* Radix tree for 2-byte inputs */
468 : uint32 b2root; /* offset of 1st byte's table */
469 : uint8 b2_1_lower; /* min/max allowed value for 1st input byte */
470 : uint8 b2_1_upper;
471 : uint8 b2_2_lower; /* min/max allowed value for 2nd input byte */
472 : uint8 b2_2_upper;
473 :
474 : /* Radix tree for 3-byte inputs */
475 : uint32 b3root; /* offset of 1st byte's table */
476 : uint8 b3_1_lower; /* min/max allowed value for 1st input byte */
477 : uint8 b3_1_upper;
478 : uint8 b3_2_lower; /* min/max allowed value for 2nd input byte */
479 : uint8 b3_2_upper;
480 : uint8 b3_3_lower; /* min/max allowed value for 3rd input byte */
481 : uint8 b3_3_upper;
482 :
483 : /* Radix tree for 4-byte inputs */
484 : uint32 b4root; /* offset of 1st byte's table */
485 : uint8 b4_1_lower; /* min/max allowed value for 1st input byte */
486 : uint8 b4_1_upper;
487 : uint8 b4_2_lower; /* min/max allowed value for 2nd input byte */
488 : uint8 b4_2_upper;
489 : uint8 b4_3_lower; /* min/max allowed value for 3rd input byte */
490 : uint8 b4_3_upper;
491 : uint8 b4_4_lower; /* min/max allowed value for 4th input byte */
492 : uint8 b4_4_upper;
493 :
494 : } pg_mb_radix_tree;
495 :
496 : /*
497 : * UTF-8 to local code conversion map (for combined characters)
498 : */
499 : typedef struct
500 : {
501 : uint32 utf1; /* UTF-8 code 1 */
502 : uint32 utf2; /* UTF-8 code 2 */
503 : uint32 code; /* local code */
504 : } pg_utf_to_local_combined;
505 :
506 : /*
507 : * local code to UTF-8 conversion map (for combined characters)
508 : */
509 : typedef struct
510 : {
511 : uint32 code; /* local code */
512 : uint32 utf1; /* UTF-8 code 1 */
513 : uint32 utf2; /* UTF-8 code 2 */
514 : } pg_local_to_utf_combined;
515 :
516 : /*
517 : * callback function for algorithmic encoding conversions (in either direction)
518 : *
519 : * if function returns zero, it does not know how to convert the code
520 : */
521 : typedef uint32 (*utf_local_conversion_func) (uint32 code);
522 :
523 : /*
524 : * Support macro for encoding conversion functions to validate their
525 : * arguments. (This could be made more compact if we included fmgr.h
526 : * here, but we don't want to do that because this header file is also
527 : * used by frontends.)
528 : */
529 : #define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
530 : check_encoding_conversion_args(PG_GETARG_INT32(0), \
531 : PG_GETARG_INT32(1), \
532 : PG_GETARG_INT32(4), \
533 : (srcencoding), \
534 : (destencoding))
535 :
536 :
537 : /*
538 : * Some handy functions for Unicode-specific tests.
539 : */
540 : static inline bool
1129 tgl 541 GIC 634 : is_valid_unicode_codepoint(pg_wchar c)
542 : {
1129 tgl 543 CBC 634 : return (c > 0 && c <= 0x10FFFF);
544 : }
1129 tgl 545 ECB :
546 : static inline bool
1182 tgl 547 GIC 490 : is_utf16_surrogate_first(pg_wchar c)
548 : {
1182 tgl 549 CBC 490 : return (c >= 0xD800 && c <= 0xDBFF);
550 : }
1182 tgl 551 ECB :
552 : static inline bool
1182 tgl 553 GIC 433 : is_utf16_surrogate_second(pg_wchar c)
554 : {
1182 tgl 555 CBC 433 : return (c >= 0xDC00 && c <= 0xDFFF);
556 : }
1182 tgl 557 ECB :
558 : static inline pg_wchar
1182 tgl 559 GIC 30 : surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second)
560 : {
1182 tgl 561 CBC 30 : return ((first & 0x3FF) << 10) + 0x10000 + (second & 0x3FF);
562 : }
1182 tgl 563 ECB :
564 :
565 : /*
566 : * These functions are considered part of libpq's exported API and
567 : * are also declared in libpq-fe.h.
568 : */
569 : extern int pg_char_to_encoding(const char *name);
570 : extern const char *pg_encoding_to_char(int encoding);
571 : extern int pg_valid_server_encoding_id(int encoding);
572 :
573 : /*
574 : * These functions are available to frontend code that links with libpgcommon
575 : * (in addition to the ones just above). The constant tables declared
576 : * earlier in this file are also available from libpgcommon.
577 : */
578 : extern int pg_encoding_mblen(int encoding, const char *mbstr);
579 : extern int pg_encoding_mblen_bounded(int encoding, const char *mbstr);
580 : extern int pg_encoding_dsplen(int encoding, const char *mbstr);
581 : extern int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len);
582 : extern int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len);
583 : extern int pg_encoding_max_length(int encoding);
584 : extern int pg_valid_client_encoding(const char *name);
585 : extern int pg_valid_server_encoding(const char *name);
586 : extern bool is_encoding_supported_by_icu(int encoding);
587 : extern const char *get_encoding_name_for_icu(int encoding);
588 :
589 : extern unsigned char *unicode_to_utf8(pg_wchar c, unsigned char *utf8string);
590 : extern pg_wchar utf8_to_unicode(const unsigned char *c);
591 : extern bool pg_utf8_islegal(const unsigned char *source, int length);
592 : extern int pg_utf_mblen(const unsigned char *s);
593 : extern int pg_mule_mblen(const unsigned char *s);
594 :
595 : /*
596 : * The remaining functions are backend-only.
597 : */
598 : extern int pg_mb2wchar(const char *from, pg_wchar *to);
599 : extern int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
600 : extern int pg_encoding_mb2wchar_with_len(int encoding,
601 : const char *from, pg_wchar *to, int len);
602 : extern int pg_wchar2mb(const pg_wchar *from, char *to);
603 : extern int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
604 : extern int pg_encoding_wchar2mb_with_len(int encoding,
605 : const pg_wchar *from, char *to, int len);
606 : extern int pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
607 : extern int pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n);
608 : extern int pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n);
609 : extern size_t pg_wchar_strlen(const pg_wchar *str);
610 : extern int pg_mblen(const char *mbstr);
611 : extern int pg_dsplen(const char *mbstr);
612 : extern int pg_mbstrlen(const char *mbstr);
613 : extern int pg_mbstrlen_with_len(const char *mbstr, int limit);
614 : extern int pg_mbcliplen(const char *mbstr, int len, int limit);
615 : extern int pg_encoding_mbcliplen(int encoding, const char *mbstr,
616 : int len, int limit);
617 : extern int pg_mbcharcliplen(const char *mbstr, int len, int limit);
618 : extern int pg_database_encoding_max_length(void);
619 : extern mbcharacter_incrementer pg_database_encoding_character_incrementer(void);
620 :
621 : extern int PrepareClientEncoding(int encoding);
622 : extern int SetClientEncoding(int encoding);
623 : extern void InitializeClientEncoding(void);
624 : extern int pg_get_client_encoding(void);
625 : extern const char *pg_get_client_encoding_name(void);
626 :
627 : extern void SetDatabaseEncoding(int encoding);
628 : extern int GetDatabaseEncoding(void);
629 : extern const char *GetDatabaseEncodingName(void);
630 : extern void SetMessageEncoding(int encoding);
631 : extern int GetMessageEncoding(void);
632 :
633 : #ifdef ENABLE_NLS
634 : extern int pg_bind_textdomain_codeset(const char *domainname);
635 : #endif
636 :
637 : extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
638 : int src_encoding,
639 : int dest_encoding);
640 : extern int pg_do_encoding_conversion_buf(Oid proc,
641 : int src_encoding,
642 : int dest_encoding,
643 : unsigned char *src, int srclen,
644 : unsigned char *dest, int destlen,
645 : bool noError);
646 :
647 : extern char *pg_client_to_server(const char *s, int len);
648 : extern char *pg_server_to_client(const char *s, int len);
649 : extern char *pg_any_to_server(const char *s, int len, int encoding);
650 : extern char *pg_server_to_any(const char *s, int len, int encoding);
651 :
652 : extern void pg_unicode_to_server(pg_wchar c, unsigned char *s);
653 : extern bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s);
654 :
655 : extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
656 : extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
657 :
658 : extern int UtfToLocal(const unsigned char *utf, int len,
659 : unsigned char *iso,
660 : const pg_mb_radix_tree *map,
661 : const pg_utf_to_local_combined *cmap, int cmapsize,
662 : utf_local_conversion_func conv_func,
663 : int encoding, bool noError);
664 : extern int LocalToUtf(const unsigned char *iso, int len,
665 : unsigned char *utf,
666 : const pg_mb_radix_tree *map,
667 : const pg_local_to_utf_combined *cmap, int cmapsize,
668 : utf_local_conversion_func conv_func,
669 : int encoding, bool noError);
670 :
671 : extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
672 : extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
673 : bool noError);
674 : extern int pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
675 : bool noError);
676 :
677 : extern void check_encoding_conversion_args(int src_encoding,
678 : int dest_encoding,
679 : int len,
680 : int expected_src_encoding,
681 : int expected_dest_encoding);
682 :
683 : extern void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn();
684 : extern void report_untranslatable_char(int src_encoding, int dest_encoding,
685 : const char *mbstr, int len) pg_attribute_noreturn();
686 :
687 : extern int local2local(const unsigned char *l, unsigned char *p, int len,
688 : int src_encoding, int dest_encoding,
689 : const unsigned char *tab, bool noError);
690 : extern int latin2mic(const unsigned char *l, unsigned char *p, int len,
691 : int lc, int encoding, bool noError);
692 : extern int mic2latin(const unsigned char *mic, unsigned char *p, int len,
693 : int lc, int encoding, bool noError);
694 : extern int latin2mic_with_table(const unsigned char *l, unsigned char *p,
695 : int len, int lc, int encoding,
696 : const unsigned char *tab, bool noError);
697 : extern int mic2latin_with_table(const unsigned char *mic, unsigned char *p,
698 : int len, int lc, int encoding,
699 : const unsigned char *tab, bool noError);
700 :
701 : #ifdef WIN32
702 : extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
703 : #endif
704 :
705 :
706 : /*
707 : * Verify a chunk of bytes for valid ASCII.
708 : *
709 : * Returns false if the input contains any zero bytes or bytes with the
710 : * high-bit set. Input len must be a multiple of the chunk size (8 or 16).
711 : */
712 : static inline bool
537 john.naylor 713 GIC 4159412 : is_valid_ascii(const unsigned char *s, int len)
714 : {
247 john.naylor 715 GNC 4159412 : const unsigned char *const s_end = s + len;
716 : Vector8 chunk;
226 717 4159412 : Vector8 highbit_cum = vector8_broadcast(0);
718 : #ifdef USE_NO_SIMD
719 : Vector8 zero_cum = vector8_broadcast(0x80);
720 : #endif
537 john.naylor 721 ECB :
537 john.naylor 722 GIC 4159412 : Assert(len % sizeof(chunk) == 0);
537 john.naylor 723 ECB :
247 john.naylor 724 GNC 12478236 : while (s < s_end)
725 : {
226 726 8318824 : vector8_load(&chunk, s);
727 :
728 : /* Capture any zero bytes in this chunk. */
729 : #ifdef USE_NO_SIMD
730 :
537 john.naylor 731 ECB : /*
732 : * First, add 0x7f to each byte. This sets the high bit in each byte,
733 : * unless it was a zero. If any resulting high bits are zero, the
734 : * corresponding high bits in the zero accumulator will be cleared.
735 : *
736 : * If none of the bytes in the chunk had the high bit set, the max
737 : * value each byte can have after the addition is 0x7f + 0x7f = 0xfe,
738 : * and we don't need to worry about carrying over to the next byte. If
739 : * any input bytes did have the high bit set, it doesn't matter
740 : * because we check for those separately.
741 : */
742 : zero_cum &= (chunk + vector8_broadcast(0x7F));
743 : #else
744 :
745 : /*
746 : * Set all bits in each lane of the highbit accumulator where input
747 : * bytes are zero.
748 : */
226 john.naylor 749 GNC 8318824 : highbit_cum = vector8_or(highbit_cum,
750 : vector8_eq(chunk, vector8_broadcast(0)));
751 : #endif
752 :
753 : /* Capture all set bits in this chunk. */
754 8318824 : highbit_cum = vector8_or(highbit_cum, chunk);
755 :
537 john.naylor 756 GIC 8318824 : s += sizeof(chunk);
757 : }
758 :
759 : /* Check if any high bits in the high bit accumulator got set. */
226 john.naylor 760 GNC 4159412 : if (vector8_is_highbit_set(highbit_cum))
537 john.naylor 761 GIC 472 : return false;
762 :
763 : #ifdef USE_NO_SIMD
764 : /* Check if any high bits in the zero accumulator got cleared. */
765 : if (zero_cum != vector8_broadcast(0x80))
766 : return false;
767 : #endif
768 :
769 4158940 : return true;
770 : }
537 john.naylor 771 ECB :
772 : #endif /* PG_WCHAR_H */
|