PostgreSQL Source Code git master
pg_wchar.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_wchar.h
4 * multibyte-character support
5 *
6 * Portions Copyright (c) 1996-2025, 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 * To help prevent mistakes, relevant functions that are exported by
17 * libpq have a physically different name when being referenced
18 * statically.
19 *
20 *-------------------------------------------------------------------------
21 */
22#ifndef PG_WCHAR_H
23#define PG_WCHAR_H
24
25/*
26 * The pg_wchar type
27 */
28typedef unsigned int pg_wchar;
29
30/*
31 * Maximum byte length of multibyte characters in any backend encoding
32 */
33#define MAX_MULTIBYTE_CHAR_LEN 4
34
35/*
36 * various definitions for EUC
37 */
38#define SS2 0x8e /* single shift 2 (JIS0201) */
39#define SS3 0x8f /* single shift 3 (JIS0212) */
40
41/*
42 * SJIS validation macros
43 */
44#define ISSJISHEAD(c) (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc))
45#define ISSJISTAIL(c) (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc))
46
47/*----------------------------------------------------
48 * MULE Internal Encoding (MIC)
49 *
50 * This encoding follows the design used within XEmacs; it is meant to
51 * subsume many externally-defined character sets. Each character includes
52 * identification of the character set it belongs to, so the encoding is
53 * general but somewhat bulky.
54 *
55 * Currently PostgreSQL supports 5 types of MULE character sets:
56 *
57 * 1) 1-byte ASCII characters. Each byte is below 0x80.
58 *
59 * 2) "Official" single byte charsets such as ISO-8859-1 (Latin1).
60 * Each MULE character consists of 2 bytes: LC1 + C1, where LC1 is
61 * an identifier for the charset (in the range 0x81 to 0x8d) and C1
62 * is the character code (in the range 0xa0 to 0xff).
63 *
64 * 3) "Private" single byte charsets such as SISHENG. Each MULE
65 * character consists of 3 bytes: LCPRV1 + LC12 + C1, where LCPRV1
66 * is a private-charset flag, LC12 is an identifier for the charset,
67 * and C1 is the character code (in the range 0xa0 to 0xff).
68 * LCPRV1 is either 0x9a (if LC12 is in the range 0xa0 to 0xdf)
69 * or 0x9b (if LC12 is in the range 0xe0 to 0xef).
70 *
71 * 4) "Official" multibyte charsets such as JIS X0208. Each MULE
72 * character consists of 3 bytes: LC2 + C1 + C2, where LC2 is
73 * an identifier for the charset (in the range 0x90 to 0x99) and C1
74 * and C2 form the character code (each in the range 0xa0 to 0xff).
75 *
76 * 5) "Private" multibyte charsets such as CNS 11643-1992 Plane 3.
77 * Each MULE character consists of 4 bytes: LCPRV2 + LC22 + C1 + C2,
78 * where LCPRV2 is a private-charset flag, LC22 is an identifier for
79 * the charset, and C1 and C2 form the character code (each in the range
80 * 0xa0 to 0xff). LCPRV2 is either 0x9c (if LC22 is in the range 0xf0
81 * to 0xf4) or 0x9d (if LC22 is in the range 0xf5 to 0xfe).
82 *
83 * "Official" encodings are those that have been assigned code numbers by
84 * the XEmacs project; "private" encodings have Postgres-specific charset
85 * identifiers.
86 *
87 * See the "XEmacs Internals Manual", available at http://www.xemacs.org,
88 * for more details. Note that for historical reasons, Postgres'
89 * private-charset flag values do not match what XEmacs says they should be,
90 * so this isn't really exactly MULE (not that private charsets would be
91 * interoperable anyway).
92 *
93 * Note that XEmacs's implementation is different from what emacs does.
94 * We follow emacs's implementation, rather than XEmacs's.
95 *----------------------------------------------------
96 */
97
98/*
99 * Charset identifiers (also called "leading bytes" in the MULE documentation)
100 */
101
102/*
103 * Charset IDs for official single byte encodings (0x81-0x8e)
104 */
105#define LC_ISO8859_1 0x81 /* ISO8859 Latin 1 */
106#define LC_ISO8859_2 0x82 /* ISO8859 Latin 2 */
107#define LC_ISO8859_3 0x83 /* ISO8859 Latin 3 */
108#define LC_ISO8859_4 0x84 /* ISO8859 Latin 4 */
109#define LC_TIS620 0x85 /* Thai (not supported yet) */
110#define LC_ISO8859_7 0x86 /* Greek (not supported yet) */
111#define LC_ISO8859_6 0x87 /* Arabic (not supported yet) */
112#define LC_ISO8859_8 0x88 /* Hebrew (not supported yet) */
113#define LC_JISX0201K 0x89 /* Japanese 1 byte kana */
114#define LC_JISX0201R 0x8a /* Japanese 1 byte Roman */
115/* Note that 0x8b seems to be unused as of Emacs 20.7.
116 * However, there might be a chance that 0x8b could be used
117 * in later versions of Emacs.
118 */
119#define LC_KOI8_R 0x8b /* Cyrillic KOI8-R */
120#define LC_ISO8859_5 0x8c /* ISO8859 Cyrillic */
121#define LC_ISO8859_9 0x8d /* ISO8859 Latin 5 (not supported yet) */
122#define LC_ISO8859_15 0x8e /* ISO8859 Latin 15 (not supported yet) */
123/* #define CONTROL_1 0x8f control characters (unused) */
124
125/* Is a leading byte for "official" single byte encodings? */
126#define IS_LC1(c) ((unsigned char)(c) >= 0x81 && (unsigned char)(c) <= 0x8d)
127
128/*
129 * Charset IDs for official multibyte encodings (0x90-0x99)
130 * 0x9a-0x9d are free. 0x9e and 0x9f are reserved.
131 */
132#define LC_JISX0208_1978 0x90 /* Japanese Kanji, old JIS (not supported) */
133#define LC_GB2312_80 0x91 /* Chinese */
134#define LC_JISX0208 0x92 /* Japanese Kanji (JIS X 0208) */
135#define LC_KS5601 0x93 /* Korean */
136#define LC_JISX0212 0x94 /* Japanese Kanji (JIS X 0212) */
137#define LC_CNS11643_1 0x95 /* CNS 11643-1992 Plane 1 */
138#define LC_CNS11643_2 0x96 /* CNS 11643-1992 Plane 2 */
139#define LC_JISX0213_1 0x97 /* Japanese Kanji (JIS X 0213 Plane 1)
140 * (not supported) */
141#define LC_BIG5_1 0x98 /* Plane 1 Chinese traditional (not
142 * supported) */
143#define LC_BIG5_2 0x99 /* Plane 1 Chinese traditional (not
144 * supported) */
145
146/* Is a leading byte for "official" multibyte encodings? */
147#define IS_LC2(c) ((unsigned char)(c) >= 0x90 && (unsigned char)(c) <= 0x99)
148
149/*
150 * Postgres-specific prefix bytes for "private" single byte encodings
151 * (According to the MULE docs, we should be using 0x9e for this)
152 */
153#define LCPRV1_A 0x9a
154#define LCPRV1_B 0x9b
155#define IS_LCPRV1(c) ((unsigned char)(c) == LCPRV1_A || (unsigned char)(c) == LCPRV1_B)
156#define IS_LCPRV1_A_RANGE(c) \
157 ((unsigned char)(c) >= 0xa0 && (unsigned char)(c) <= 0xdf)
158#define IS_LCPRV1_B_RANGE(c) \
159 ((unsigned char)(c) >= 0xe0 && (unsigned char)(c) <= 0xef)
160
161/*
162 * Postgres-specific prefix bytes for "private" multibyte encodings
163 * (According to the MULE docs, we should be using 0x9f for this)
164 */
165#define LCPRV2_A 0x9c
166#define LCPRV2_B 0x9d
167#define IS_LCPRV2(c) ((unsigned char)(c) == LCPRV2_A || (unsigned char)(c) == LCPRV2_B)
168#define IS_LCPRV2_A_RANGE(c) \
169 ((unsigned char)(c) >= 0xf0 && (unsigned char)(c) <= 0xf4)
170#define IS_LCPRV2_B_RANGE(c) \
171 ((unsigned char)(c) >= 0xf5 && (unsigned char)(c) <= 0xfe)
172
174 * Charset IDs for private single byte encodings (0xa0-0xef)
175 */
176#define LC_SISHENG 0xa0 /* Chinese SiSheng characters for
177 * PinYin/ZhuYin (not supported) */
178#define LC_IPA 0xa1 /* IPA (International Phonetic
179 * Association) (not supported) */
180#define LC_VISCII_LOWER 0xa2 /* Vietnamese VISCII1.1 lower-case (not
181 * supported) */
182#define LC_VISCII_UPPER 0xa3 /* Vietnamese VISCII1.1 upper-case (not
183 * supported) */
184#define LC_ARABIC_DIGIT 0xa4 /* Arabic digit (not supported) */
185#define LC_ARABIC_1_COLUMN 0xa5 /* Arabic 1-column (not supported) */
186#define LC_ASCII_RIGHT_TO_LEFT 0xa6 /* ASCII (left half of ISO8859-1) with
187 * right-to-left direction (not
188 * supported) */
189#define LC_LAO 0xa7 /* Lao characters (ISO10646 0E80..0EDF)
190 * (not supported) */
191#define LC_ARABIC_2_COLUMN 0xa8 /* Arabic 1-column (not supported) */
194 * Charset IDs for private multibyte encodings (0xf0-0xff)
195 */
196#define LC_INDIAN_1_COLUMN 0xf0 /* Indian charset for 1-column width
197 * glyphs (not supported) */
198#define LC_TIBETAN_1_COLUMN 0xf1 /* Tibetan 1-column width glyphs (not
199 * supported) */
200#define LC_UNICODE_SUBSET_2 0xf2 /* Unicode characters of the range
201 * U+2500..U+33FF. (not supported) */
202#define LC_UNICODE_SUBSET_3 0xf3 /* Unicode characters of the range
203 * U+E000..U+FFFF. (not supported) */
204#define LC_UNICODE_SUBSET 0xf4 /* Unicode characters of the range
205 * U+0100..U+24FF. (not supported) */
206#define LC_ETHIOPIC 0xf5 /* Ethiopic characters (not supported) */
207#define LC_CNS11643_3 0xf6 /* CNS 11643-1992 Plane 3 */
208#define LC_CNS11643_4 0xf7 /* CNS 11643-1992 Plane 4 */
209#define LC_CNS11643_5 0xf8 /* CNS 11643-1992 Plane 5 */
210#define LC_CNS11643_6 0xf9 /* CNS 11643-1992 Plane 6 */
211#define LC_CNS11643_7 0xfa /* CNS 11643-1992 Plane 7 */
212#define LC_INDIAN_2_COLUMN 0xfb /* Indian charset for 2-column width
213 * glyphs (not supported) */
214#define LC_TIBETAN 0xfc /* Tibetan (not supported) */
215/* #define FREE 0xfd free (unused) */
216/* #define FREE 0xfe free (unused) */
217/* #define FREE 0xff free (unused) */
218
219/*----------------------------------------------------
220 * end of MULE stuff
221 *----------------------------------------------------
222 */
223
225 * PostgreSQL encoding identifiers
227 * WARNING: If you add some encoding don't forget to update
228 * the pg_enc2name_tbl[] array (in src/common/encnames.c),
229 * the pg_enc2gettext_tbl[] array (in src/common/encnames.c) and
230 * the pg_wchar_table[] array (in src/common/wchar.c) and to check
231 * PG_ENCODING_BE_LAST macro.
233 * PG_SQL_ASCII is default encoding and must be = 0.
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 */
240typedef enum pg_enc
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_)
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
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
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 */
355typedef struct pg_enc2name
356{
357 const char *name;
359#ifdef WIN32
360 unsigned codepage; /* codepage for WIN32 */
361#endif
363
365
367 * Encoding names for gettext
368 */
369extern PGDLLIMPORT const char *pg_enc2gettext_tbl[];
371/*
372 * pg_wchar stuff
373 */
374typedef int (*mb2wchar_with_len_converter) (const unsigned char *from,
375 pg_wchar *to,
376 int len);
377
378typedef int (*wchar2mb_with_len_converter) (const pg_wchar *from,
379 unsigned char *to,
380 int len);
381
382typedef int (*mblen_converter) (const unsigned char *mbstr);
384typedef int (*mbdisplaylen_converter) (const unsigned char *mbstr);
386typedef bool (*mbcharacter_incrementer) (unsigned char *mbstr, int len);
387
388typedef int (*mbchar_verifier) (const unsigned char *mbstr, int len);
389
390typedef int (*mbstr_verifier) (const unsigned char *mbstr, int len);
391
392typedef struct
393{
394 mb2wchar_with_len_converter mb2wchar_with_len; /* convert a multibyte
395 * string to a wchar */
396 wchar2mb_with_len_converter wchar2mb_with_len; /* convert a wchar string
397 * to a multibyte */
398 mblen_converter mblen; /* get byte length of a char */
399 mbdisplaylen_converter dsplen; /* get display width of a char */
400 mbchar_verifier mbverifychar; /* verify multibyte character */
401 mbstr_verifier mbverifystr; /* verify multibyte string */
402 int maxmblen; /* max bytes for a char in this encoding */
404
406
407/*
408 * Data structures for conversions between UTF-8 and other encodings
409 * (UtfToLocal() and LocalToUtf()). In these data structures, characters of
410 * either encoding are represented by uint32 words; hence we can only support
411 * characters up to 4 bytes long. For example, the byte sequence 0xC2 0x89
412 * would be represented by 0x0000C289, and 0xE8 0xA2 0xB4 by 0x00E8A2B4.
413 *
414 * There are three possible ways a character can be mapped:
415 *
416 * 1. Using a radix tree, from source to destination code.
417 * 2. Using a sorted array of source -> destination code pairs. This
418 * method is used for "combining" characters. There are so few of
419 * them that building a radix tree would be wasteful.
420 * 3. Using a conversion function.
421 */
422
423/*
424 * Radix tree for character conversion.
425 *
426 * Logically, this is actually four different radix trees, for 1-byte,
427 * 2-byte, 3-byte and 4-byte inputs. The 1-byte tree is a simple lookup
428 * table from source to target code. The 2-byte tree consists of two levels:
429 * one lookup table for the first byte, where the value in the lookup table
430 * points to a lookup table for the second byte. And so on.
432 * Physically, all the trees are stored in one big array, in 'chars16' or
433 * 'chars32', depending on the maximum value that needs to be represented. For
434 * each level in each tree, we also store lower and upper bound of allowed
435 * values - values outside those bounds are considered invalid, and are left
436 * out of the tables.
438 * In the intermediate levels of the trees, the values stored are offsets
439 * into the chars[16|32] array.
440 *
441 * In the beginning of the chars[16|32] array, there is always a number of
442 * zeros, so that you safely follow an index from an intermediate table
443 * without explicitly checking for a zero. Following a zero any number of
444 * times will always bring you to the dummy, all-zeros table in the
445 * beginning. This helps to shave some cycles when looking up values.
446 */
447typedef struct
449 /*
450 * Array containing all the values. Only one of chars16 or chars32 is
451 * used, depending on how wide the values we need to represent are.
452 */
453 const uint16 *chars16;
454 const uint32 *chars32;
456 /* Radix tree for 1-byte inputs */
457 uint32 b1root; /* offset of table in the chars[16|32] array */
458 uint8 b1_lower; /* min allowed value for a single byte input */
459 uint8 b1_upper; /* max allowed value for a single byte input */
460
461 /* Radix tree for 2-byte inputs */
462 uint32 b2root; /* offset of 1st byte's table */
463 uint8 b2_1_lower; /* min/max allowed value for 1st input byte */
464 uint8 b2_1_upper;
465 uint8 b2_2_lower; /* min/max allowed value for 2nd input byte */
466 uint8 b2_2_upper;
468 /* Radix tree for 3-byte inputs */
469 uint32 b3root; /* offset of 1st byte's table */
470 uint8 b3_1_lower; /* min/max allowed value for 1st input byte */
471 uint8 b3_1_upper;
472 uint8 b3_2_lower; /* min/max allowed value for 2nd input byte */
473 uint8 b3_2_upper;
474 uint8 b3_3_lower; /* min/max allowed value for 3rd input byte */
475 uint8 b3_3_upper;
476
477 /* Radix tree for 4-byte inputs */
478 uint32 b4root; /* offset of 1st byte's table */
479 uint8 b4_1_lower; /* min/max allowed value for 1st input byte */
480 uint8 b4_1_upper;
481 uint8 b4_2_lower; /* min/max allowed value for 2nd input byte */
482 uint8 b4_2_upper;
483 uint8 b4_3_lower; /* min/max allowed value for 3rd input byte */
484 uint8 b4_3_upper;
485 uint8 b4_4_lower; /* min/max allowed value for 4th input byte */
486 uint8 b4_4_upper;
491 * UTF-8 to local code conversion map (for combined characters)
492 */
493typedef struct
494{
495 uint32 utf1; /* UTF-8 code 1 */
496 uint32 utf2; /* UTF-8 code 2 */
497 uint32 code; /* local code */
500/*
501 * local code to UTF-8 conversion map (for combined characters)
502 */
503typedef struct
504{
505 uint32 code; /* local code */
506 uint32 utf1; /* UTF-8 code 1 */
507 uint32 utf2; /* UTF-8 code 2 */
509
510/*
511 * callback function for algorithmic encoding conversions (in either direction)
512 *
513 * if function returns zero, it does not know how to convert the code
514 */
515typedef uint32 (*utf_local_conversion_func) (uint32 code);
516
517/*
518 * Support macro for encoding conversion functions to validate their
519 * arguments. (This could be made more compact if we included fmgr.h
520 * here, but we don't want to do that because this header file is also
521 * used by frontends.)
522 */
523#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
524 check_encoding_conversion_args(PG_GETARG_INT32(0), \
525 PG_GETARG_INT32(1), \
526 PG_GETARG_INT32(4), \
527 (srcencoding), \
528 (destencoding))
529
530
532 * Some handy functions for Unicode-specific tests.
533 */
534static inline bool
536{
537 return (c > 0 && c <= 0x10FFFF);
538}
539
540static inline bool
542{
543 return (c >= 0xD800 && c <= 0xDBFF);
544}
545
546static inline bool
548{
549 return (c >= 0xDC00 && c <= 0xDFFF);
550}
551
552static inline pg_wchar
554{
555 return ((first & 0x3FF) << 10) + 0x10000 + (second & 0x3FF);
556}
557
558/*
559 * Convert a UTF-8 character to a Unicode code point.
560 * This is a one-character version of pg_utf2wchar_with_len.
561 *
562 * No error checks here, c must point to a long-enough string.
563 */
564static inline pg_wchar
565utf8_to_unicode(const unsigned char *c)
566{
567 if ((*c & 0x80) == 0)
568 return (pg_wchar) c[0];
569 else if ((*c & 0xe0) == 0xc0)
570 return (pg_wchar) (((c[0] & 0x1f) << 6) |
571 (c[1] & 0x3f));
572 else if ((*c & 0xf0) == 0xe0)
573 return (pg_wchar) (((c[0] & 0x0f) << 12) |
574 ((c[1] & 0x3f) << 6) |
575 (c[2] & 0x3f));
576 else if ((*c & 0xf8) == 0xf0)
577 return (pg_wchar) (((c[0] & 0x07) << 18) |
578 ((c[1] & 0x3f) << 12) |
579 ((c[2] & 0x3f) << 6) |
580 (c[3] & 0x3f));
581 else
582 /* that is an invalid code on purpose */
583 return 0xffffffff;
584}
585
586/*
587 * Map a Unicode code point to UTF-8. utf8string must have at least
588 * unicode_utf8len(c) bytes available.
589 */
590static inline unsigned char *
591unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
592{
593 if (c <= 0x7F)
594 {
595 utf8string[0] = c;
596 }
597 else if (c <= 0x7FF)
598 {
599 utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
600 utf8string[1] = 0x80 | (c & 0x3F);
601 }
602 else if (c <= 0xFFFF)
603 {
604 utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
605 utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
606 utf8string[2] = 0x80 | (c & 0x3F);
608 else
609 {
610 utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
611 utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
612 utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
613 utf8string[3] = 0x80 | (c & 0x3F);
614 }
615
616 return utf8string;
617}
618
619/*
620 * Number of bytes needed to represent the given char in UTF8.
621 */
622static inline int
624{
625 if (c <= 0x7F)
626 return 1;
627 else if (c <= 0x7FF)
628 return 2;
629 else if (c <= 0xFFFF)
630 return 3;
631 else
632 return 4;
634
635/*
636 * The functions in this list are exported by libpq, and we need to be sure
637 * that we know which calls are satisfied by libpq and which are satisfied
638 * by static linkage to libpgcommon. (This is because we might be using a
639 * libpq.so that's of a different major version and has encoding IDs that
640 * differ from the current version's.) The nominal function names are what
641 * are actually used in and exported by libpq, while the names exported by
642 * libpgcommon.a and libpgcommon_srv.a end in "_private".
643 */
644#if defined(USE_PRIVATE_ENCODING_FUNCS) || !defined(FRONTEND)
645#define pg_char_to_encoding pg_char_to_encoding_private
646#define pg_encoding_to_char pg_encoding_to_char_private
647#define pg_valid_server_encoding pg_valid_server_encoding_private
648#define pg_valid_server_encoding_id pg_valid_server_encoding_id_private
649#define pg_utf_mblen pg_utf_mblen_private
650#endif
651
652/*
653 * These functions are considered part of libpq's exported API and
654 * are also declared in libpq-fe.h.
655 */
656extern int pg_char_to_encoding(const char *name);
657extern const char *pg_encoding_to_char(int encoding);
659
660/*
661 * These functions are available to frontend code that links with libpgcommon
662 * (in addition to the ones just above). The constant tables declared
663 * earlier in this file are also available from libpgcommon.
664 */
665extern int pg_encoding_mblen(int encoding, const char *mbstr);
666extern int pg_encoding_mblen_bounded(int encoding, const char *mbstr);
667extern int pg_encoding_dsplen(int encoding, const char *mbstr);
668extern int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len);
669extern int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len);
670extern int pg_encoding_max_length(int encoding);
671extern int pg_valid_client_encoding(const char *name);
672extern int pg_valid_server_encoding(const char *name);
674extern const char *get_encoding_name_for_icu(int encoding);
675
676extern unsigned char *unicode_to_utf8(pg_wchar c, unsigned char *utf8string);
677extern pg_wchar utf8_to_unicode(const unsigned char *c);
678extern bool pg_utf8_islegal(const unsigned char *source, int length);
679extern int pg_utf_mblen(const unsigned char *s);
680extern int pg_mule_mblen(const unsigned char *s);
681
682/*
683 * The remaining functions are backend-only.
684 */
685extern int pg_mb2wchar(const char *from, pg_wchar *to);
686extern int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
688 const char *from, pg_wchar *to, int len);
689extern int pg_wchar2mb(const pg_wchar *from, char *to);
690extern int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
692 const pg_wchar *from, char *to, int len);
693extern int pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
694extern int pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n);
695extern int pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n);
696extern size_t pg_wchar_strlen(const pg_wchar *str);
697extern int pg_mblen(const char *mbstr);
698extern int pg_dsplen(const char *mbstr);
699extern int pg_mbstrlen(const char *mbstr);
700extern int pg_mbstrlen_with_len(const char *mbstr, int limit);
701extern int pg_mbcliplen(const char *mbstr, int len, int limit);
702extern int pg_encoding_mbcliplen(int encoding, const char *mbstr,
703 int len, int limit);
704extern int pg_mbcharcliplen(const char *mbstr, int len, int limit);
705extern int pg_database_encoding_max_length(void);
707
708extern int PrepareClientEncoding(int encoding);
709extern int SetClientEncoding(int encoding);
710extern void InitializeClientEncoding(void);
711extern int pg_get_client_encoding(void);
712extern const char *pg_get_client_encoding_name(void);
713
714extern void SetDatabaseEncoding(int encoding);
715extern int GetDatabaseEncoding(void);
716extern const char *GetDatabaseEncodingName(void);
717extern void SetMessageEncoding(int encoding);
718extern int GetMessageEncoding(void);
719
720#ifdef ENABLE_NLS
721extern int pg_bind_textdomain_codeset(const char *domainname);
722#endif
723
724extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
725 int src_encoding,
726 int dest_encoding);
727extern int pg_do_encoding_conversion_buf(Oid proc,
728 int src_encoding,
729 int dest_encoding,
730 unsigned char *src, int srclen,
731 unsigned char *dest, int destlen,
732 bool noError);
733
734extern char *pg_client_to_server(const char *s, int len);
735extern char *pg_server_to_client(const char *s, int len);
736extern char *pg_any_to_server(const char *s, int len, int encoding);
737extern char *pg_server_to_any(const char *s, int len, int encoding);
738
739extern void pg_unicode_to_server(pg_wchar c, unsigned char *s);
740extern bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s);
741
742extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
743extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
744
745extern int UtfToLocal(const unsigned char *utf, int len,
746 unsigned char *iso,
747 const pg_mb_radix_tree *map,
748 const pg_utf_to_local_combined *cmap, int cmapsize,
750 int encoding, bool noError);
751extern int LocalToUtf(const unsigned char *iso, int len,
752 unsigned char *utf,
753 const pg_mb_radix_tree *map,
754 const pg_local_to_utf_combined *cmap, int cmapsize,
756 int encoding, bool noError);
757
758extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
759extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
760 bool noError);
761extern int pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
762 bool noError);
763
764extern void check_encoding_conversion_args(int src_encoding,
765 int dest_encoding,
766 int len,
767 int expected_src_encoding,
768 int expected_dest_encoding);
769
770extern void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn();
771extern void report_untranslatable_char(int src_encoding, int dest_encoding,
772 const char *mbstr, int len) pg_attribute_noreturn();
773
774extern int local2local(const unsigned char *l, unsigned char *p, int len,
775 int src_encoding, int dest_encoding,
776 const unsigned char *tab, bool noError);
777extern int latin2mic(const unsigned char *l, unsigned char *p, int len,
778 int lc, int encoding, bool noError);
779extern int mic2latin(const unsigned char *mic, unsigned char *p, int len,
780 int lc, int encoding, bool noError);
781extern int latin2mic_with_table(const unsigned char *l, unsigned char *p,
782 int len, int lc, int encoding,
783 const unsigned char *tab, bool noError);
784extern int mic2latin_with_table(const unsigned char *mic, unsigned char *p,
785 int len, int lc, int encoding,
786 const unsigned char *tab, bool noError);
787
788#ifdef WIN32
789extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
790#endif
791
792#endif /* PG_WCHAR_H */
#define PGDLLIMPORT
Definition: c.h:1277
uint8_t uint8
Definition: c.h:486
#define pg_attribute_noreturn()
Definition: c.h:239
uint16_t uint16
Definition: c.h:487
uint32_t uint32
Definition: c.h:488
const char * str
unsigned int pg_wchar
Definition: mbprint.c:31
const void size_t len
int32 encoding
Definition: pg_database.h:41
static rewind_source * source
Definition: pg_rewind.c:89
int pg_encoding_mblen_bounded(int encoding, const char *mbstr)
Definition: wchar.c:2081
int mic2latin_with_table(const unsigned char *mic, unsigned char *p, int len, int lc, int encoding, const unsigned char *tab, bool noError)
Definition: conv.c:257
bool pg_utf8_islegal(const unsigned char *source, int length)
Definition: wchar.c:1953
char * pg_client_to_server(const char *s, int len)
Definition: mbutils.c:660
bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s)
Definition: mbutils.c:926
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
int pg_encoding_wchar2mb_with_len(int encoding, const pg_wchar *from, char *to, int len)
Definition: mbutils.c:1015
int UtfToLocal(const unsigned char *utf, int len, unsigned char *iso, const pg_mb_radix_tree *map, const pg_utf_to_local_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
Definition: conv.c:507
#define pg_utf_mblen
Definition: pg_wchar.h:633
pg_enc
Definition: pg_wchar.h:225
@ PG_WIN1254
Definition: pg_wchar.h:257
@ PG_LATIN4
Definition: pg_wchar.h:237
@ _PG_LAST_ENCODING_
Definition: pg_wchar.h:271
@ PG_LATIN9
Definition: pg_wchar.h:242
@ PG_JOHAB
Definition: pg_wchar.h:269
@ PG_GB18030
Definition: pg_wchar.h:268
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
@ PG_KOI8R
Definition: pg_wchar.h:248
@ PG_ISO_8859_6
Definition: pg_wchar.h:252
@ PG_WIN1253
Definition: pg_wchar.h:256
@ PG_KOI8U
Definition: pg_wchar.h:260
@ PG_LATIN6
Definition: pg_wchar.h:239
@ PG_MULE_INTERNAL
Definition: pg_wchar.h:233
@ PG_LATIN5
Definition: pg_wchar.h:238
@ PG_EUC_CN
Definition: pg_wchar.h:228
@ PG_UHC
Definition: pg_wchar.h:267
@ PG_LATIN2
Definition: pg_wchar.h:235
@ PG_ISO_8859_5
Definition: pg_wchar.h:251
@ PG_LATIN10
Definition: pg_wchar.h:243
@ PG_WIN1250
Definition: pg_wchar.h:255
@ PG_ISO_8859_7
Definition: pg_wchar.h:253
@ PG_SJIS
Definition: pg_wchar.h:264
@ PG_LATIN8
Definition: pg_wchar.h:241
@ PG_EUC_JP
Definition: pg_wchar.h:227
@ PG_GBK
Definition: pg_wchar.h:266
@ PG_LATIN3
Definition: pg_wchar.h:236
@ PG_WIN1256
Definition: pg_wchar.h:244
@ PG_LATIN1
Definition: pg_wchar.h:234
@ PG_EUC_TW
Definition: pg_wchar.h:230
@ PG_WIN1258
Definition: pg_wchar.h:245
@ PG_SHIFT_JIS_2004
Definition: pg_wchar.h:270
@ PG_WIN1252
Definition: pg_wchar.h:250
@ PG_LATIN7
Definition: pg_wchar.h:240
@ PG_UTF8
Definition: pg_wchar.h:232
@ PG_WIN1255
Definition: pg_wchar.h:258
@ PG_WIN1257
Definition: pg_wchar.h:259
@ PG_WIN1251
Definition: pg_wchar.h:249
@ PG_EUC_KR
Definition: pg_wchar.h:229
@ PG_WIN866
Definition: pg_wchar.h:246
@ PG_ISO_8859_8
Definition: pg_wchar.h:254
@ PG_WIN874
Definition: pg_wchar.h:247
@ PG_EUC_JIS_2004
Definition: pg_wchar.h:231
@ PG_BIG5
Definition: pg_wchar.h:265
uint32(* utf_local_conversion_func)(uint32 code)
Definition: pg_wchar.h:499
int(* wchar2mb_with_len_converter)(const pg_wchar *from, unsigned char *to, int len)
Definition: pg_wchar.h:362
static unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: pg_wchar.h:575
int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1597
void InitializeClientEncoding(void)
Definition: mbutils.c:281
int pg_dsplen(const char *mbstr)
Definition: mbutils.c:1030
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1057
mbcharacter_incrementer pg_database_encoding_character_incrementer(void)
Definition: mbutils.c:1523
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:1008
int latin2mic_with_table(const unsigned char *l, unsigned char *p, int len, int lc, int encoding, const unsigned char *tab, bool noError)
Definition: conv.c:194
int pg_mb2wchar(const char *from, pg_wchar *to)
Definition: mbutils.c:979
bool(* mbcharacter_incrementer)(unsigned char *mbstr, int len)
Definition: pg_wchar.h:370
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
int(* mbchar_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:372
const char * get_encoding_name_for_icu(int encoding)
Definition: encnames.c:472
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1125
int mic2latin(const unsigned char *mic, unsigned char *p, int len, int lc, int encoding, bool noError)
Definition: conv.c:127
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:356
struct pg_enc2name pg_enc2name
unsigned short CNStoBIG5(unsigned short cns, unsigned char lc)
Definition: big5.c:345
size_t pg_wchar_strlen(const pg_wchar *str)
Definition: wstrncmp.c:70
int pg_wchar2mb(const pg_wchar *from, char *to)
Definition: mbutils.c:1001
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len) pg_attribute_noreturn()
Definition: mbutils.c:1730
int pg_mule_mblen(const unsigned char *s)
Definition: wchar.c:772
PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:308
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:1037
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1566
PGDLLIMPORT const char * pg_enc2gettext_tbl[]
Definition: encnames.c:360
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1556
int LocalToUtf(const unsigned char *iso, int len, unsigned char *utf, const pg_mb_radix_tree *map, const pg_local_to_utf_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
Definition: conv.c:717
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1083
int GetMessageEncoding(void)
Definition: mbutils.c:1308
static pg_wchar utf8_to_unicode(const unsigned char *c)
Definition: pg_wchar.h:549
int pg_do_encoding_conversion_buf(Oid proc, int src_encoding, int dest_encoding, unsigned char *src, int srclen, unsigned char *dest, int destlen, bool noError)
Definition: mbutils.c:469
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1267
int SetClientEncoding(int encoding)
Definition: mbutils.c:208
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1171
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Definition: mbutils.c:864
static bool is_valid_unicode_codepoint(pg_wchar c)
Definition: pg_wchar.h:519
char * pg_server_to_client(const char *s, int len)
Definition: mbutils.c:738
unsigned int pg_wchar
Definition: pg_wchar.h:28
void check_encoding_conversion_args(int src_encoding, int dest_encoding, int len, int expected_src_encoding, int expected_dest_encoding)
Definition: mbutils.c:1669
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1546
int PrepareClientEncoding(int encoding)
Definition: mbutils.c:110
static int unicode_utf8len(pg_wchar c)
Definition: pg_wchar.h:607
int pg_valid_client_encoding(const char *name)
Definition: encnames.c:485
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition: wchar.c:2090
void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn()
Definition: mbutils.c:1698
int pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2)
Definition: wstrcmp.c:41
unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc)
Definition: big5.c:292
#define pg_encoding_to_char
Definition: pg_wchar.h:630
#define pg_valid_server_encoding_id
Definition: pg_wchar.h:632
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
Definition: wchar.c:2116
int pg_get_client_encoding(void)
Definition: mbutils.c:336
#define pg_valid_server_encoding
Definition: pg_wchar.h:631
bool is_encoding_supported_by_icu(int encoding)
Definition: encnames.c:461
int(* mb2wchar_with_len_converter)(const unsigned char *from, pg_wchar *to, int len)
Definition: pg_wchar.h:358
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:749
#define pg_char_to_encoding
Definition: pg_wchar.h:629
int(* mbstr_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:374
int local2local(const unsigned char *l, unsigned char *p, int len, int src_encoding, int dest_encoding, const unsigned char *tab, bool noError)
Definition: conv.c:33
int pg_encoding_max_length(int encoding)
Definition: wchar.c:2127
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2069
PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:2015
static pg_wchar surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second)
Definition: pg_wchar.h:537
int pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n)
Definition: wstrncmp.c:40
static bool is_utf16_surrogate_first(pg_wchar c)
Definition: pg_wchar.h:525
void SetDatabaseEncoding(int encoding)
Definition: mbutils.c:1161
int latin2mic(const unsigned char *l, unsigned char *p, int len, int lc, int encoding, bool noError)
Definition: conv.c:89
int pg_encoding_mbcliplen(int encoding, const char *mbstr, int len, int limit)
Definition: mbutils.c:1093
const char * pg_get_client_encoding_name(void)
Definition: mbutils.c:345
int pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n)
Definition: wstrncmp.c:55
int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)
Definition: wchar.c:2103
int pg_encoding_mb2wchar_with_len(int encoding, const char *from, pg_wchar *to, int len)
Definition: mbutils.c:993
int(* mbdisplaylen_converter)(const unsigned char *mbstr)
Definition: pg_wchar.h:368
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:986
static bool is_utf16_surrogate_second(pg_wchar c)
Definition: pg_wchar.h:531
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
int(* mblen_converter)(const unsigned char *mbstr)
Definition: pg_wchar.h:366
unsigned int Oid
Definition: postgres_ext.h:32
char * c
char * s1
char * s2
pg_enc encoding
Definition: pg_wchar.h:342
const char * name
Definition: pg_wchar.h:341
const char * name