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-2024, 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  */
28 typedef 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 
173 /*
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) */
192 
193 /*
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 
224 /*
225  * PostgreSQL encoding identifiers
226  *
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.
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;
359 #ifdef WIN32
360  unsigned codepage; /* codepage for WIN32 */
361 #endif
363 
365 
366 /*
367  * Encoding names for gettext
368  */
369 extern PGDLLIMPORT const char *pg_enc2gettext_tbl[];
370 
371 /*
372  * pg_wchar stuff
373  */
374 typedef int (*mb2wchar_with_len_converter) (const unsigned char *from,
375  pg_wchar *to,
376  int len);
377 
378 typedef int (*wchar2mb_with_len_converter) (const pg_wchar *from,
379  unsigned char *to,
380  int len);
381 
382 typedef int (*mblen_converter) (const unsigned char *mbstr);
383 
384 typedef int (*mbdisplaylen_converter) (const unsigned char *mbstr);
385 
386 typedef bool (*mbcharacter_incrementer) (unsigned char *mbstr, int len);
387 
388 typedef int (*mbchar_verifier) (const unsigned char *mbstr, int len);
389 
390 typedef int (*mbstr_verifier) (const unsigned char *mbstr, int len);
391 
392 typedef 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 */
403 } pg_wchar_tbl;
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.
431  *
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.
437  *
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  */
447 typedef struct
448 {
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;
455 
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;
467 
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;
487 
489 
490 /*
491  * UTF-8 to local code conversion map (for combined characters)
492  */
493 typedef struct
494 {
495  uint32 utf1; /* UTF-8 code 1 */
496  uint32 utf2; /* UTF-8 code 2 */
497  uint32 code; /* local code */
499 
500 /*
501  * local code to UTF-8 conversion map (for combined characters)
502  */
503 typedef 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  */
515 typedef 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 
531 /*
532  * Some handy functions for Unicode-specific tests.
533  */
534 static inline bool
536 {
537  return (c > 0 && c <= 0x10FFFF);
538 }
539 
540 static inline bool
542 {
543  return (c >= 0xD800 && c <= 0xDBFF);
544 }
545 
546 static inline bool
548 {
549  return (c >= 0xDC00 && c <= 0xDFFF);
550 }
551 
552 static inline pg_wchar
554 {
555  return ((first & 0x3FF) << 10) + 0x10000 + (second & 0x3FF);
556 }
557 
558 /*
559  * Number of bytes needed to represent the given char in UTF8.
560  */
561 static inline int
563 {
564  if (c <= 0x7F)
565  return 1;
566  else if (c <= 0x7FF)
567  return 2;
568  else if (c <= 0xFFFF)
569  return 3;
570  else
571  return 4;
572 }
573 
574 /*
575  * The functions in this list are exported by libpq, and we need to be sure
576  * that we know which calls are satisfied by libpq and which are satisfied
577  * by static linkage to libpgcommon. (This is because we might be using a
578  * libpq.so that's of a different major version and has encoding IDs that
579  * differ from the current version's.) The nominal function names are what
580  * are actually used in and exported by libpq, while the names exported by
581  * libpgcommon.a and libpgcommon_srv.a end in "_private".
582  */
583 #if defined(USE_PRIVATE_ENCODING_FUNCS) || !defined(FRONTEND)
584 #define pg_char_to_encoding pg_char_to_encoding_private
585 #define pg_encoding_to_char pg_encoding_to_char_private
586 #define pg_valid_server_encoding pg_valid_server_encoding_private
587 #define pg_valid_server_encoding_id pg_valid_server_encoding_id_private
588 #define pg_utf_mblen pg_utf_mblen_private
589 #endif
590 
591 /*
592  * These functions are considered part of libpq's exported API and
593  * are also declared in libpq-fe.h.
594  */
595 extern int pg_char_to_encoding(const char *name);
596 extern const char *pg_encoding_to_char(int encoding);
597 extern int pg_valid_server_encoding_id(int encoding);
598 
599 /*
600  * These functions are available to frontend code that links with libpgcommon
601  * (in addition to the ones just above). The constant tables declared
602  * earlier in this file are also available from libpgcommon.
603  */
604 extern int pg_encoding_mblen(int encoding, const char *mbstr);
605 extern int pg_encoding_mblen_bounded(int encoding, const char *mbstr);
606 extern int pg_encoding_dsplen(int encoding, const char *mbstr);
607 extern int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len);
608 extern int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len);
609 extern int pg_encoding_max_length(int encoding);
610 extern int pg_valid_client_encoding(const char *name);
611 extern int pg_valid_server_encoding(const char *name);
612 extern bool is_encoding_supported_by_icu(int encoding);
613 extern const char *get_encoding_name_for_icu(int encoding);
614 
615 extern unsigned char *unicode_to_utf8(pg_wchar c, unsigned char *utf8string);
616 extern pg_wchar utf8_to_unicode(const unsigned char *c);
617 extern bool pg_utf8_islegal(const unsigned char *source, int length);
618 extern int pg_utf_mblen(const unsigned char *s);
619 extern int pg_mule_mblen(const unsigned char *s);
620 
621 /*
622  * The remaining functions are backend-only.
623  */
624 extern int pg_mb2wchar(const char *from, pg_wchar *to);
625 extern int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
627  const char *from, pg_wchar *to, int len);
628 extern int pg_wchar2mb(const pg_wchar *from, char *to);
629 extern int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
631  const pg_wchar *from, char *to, int len);
632 extern int pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
633 extern int pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n);
634 extern int pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n);
635 extern size_t pg_wchar_strlen(const pg_wchar *str);
636 extern int pg_mblen(const char *mbstr);
637 extern int pg_dsplen(const char *mbstr);
638 extern int pg_mbstrlen(const char *mbstr);
639 extern int pg_mbstrlen_with_len(const char *mbstr, int limit);
640 extern int pg_mbcliplen(const char *mbstr, int len, int limit);
641 extern int pg_encoding_mbcliplen(int encoding, const char *mbstr,
642  int len, int limit);
643 extern int pg_mbcharcliplen(const char *mbstr, int len, int limit);
644 extern int pg_database_encoding_max_length(void);
646 
647 extern int PrepareClientEncoding(int encoding);
648 extern int SetClientEncoding(int encoding);
649 extern void InitializeClientEncoding(void);
650 extern int pg_get_client_encoding(void);
651 extern const char *pg_get_client_encoding_name(void);
652 
653 extern void SetDatabaseEncoding(int encoding);
654 extern int GetDatabaseEncoding(void);
655 extern const char *GetDatabaseEncodingName(void);
656 extern void SetMessageEncoding(int encoding);
657 extern int GetMessageEncoding(void);
658 
659 #ifdef ENABLE_NLS
660 extern int pg_bind_textdomain_codeset(const char *domainname);
661 #endif
662 
663 extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
664  int src_encoding,
665  int dest_encoding);
666 extern int pg_do_encoding_conversion_buf(Oid proc,
667  int src_encoding,
668  int dest_encoding,
669  unsigned char *src, int srclen,
670  unsigned char *dest, int destlen,
671  bool noError);
672 
673 extern char *pg_client_to_server(const char *s, int len);
674 extern char *pg_server_to_client(const char *s, int len);
675 extern char *pg_any_to_server(const char *s, int len, int encoding);
676 extern char *pg_server_to_any(const char *s, int len, int encoding);
677 
678 extern void pg_unicode_to_server(pg_wchar c, unsigned char *s);
679 extern bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s);
680 
681 extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
682 extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
683 
684 extern int UtfToLocal(const unsigned char *utf, int len,
685  unsigned char *iso,
686  const pg_mb_radix_tree *map,
687  const pg_utf_to_local_combined *cmap, int cmapsize,
688  utf_local_conversion_func conv_func,
689  int encoding, bool noError);
690 extern int LocalToUtf(const unsigned char *iso, int len,
691  unsigned char *utf,
692  const pg_mb_radix_tree *map,
693  const pg_local_to_utf_combined *cmap, int cmapsize,
694  utf_local_conversion_func conv_func,
695  int encoding, bool noError);
696 
697 extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
698 extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
699  bool noError);
700 extern int pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
701  bool noError);
702 
703 extern void check_encoding_conversion_args(int src_encoding,
704  int dest_encoding,
705  int len,
706  int expected_src_encoding,
707  int expected_dest_encoding);
708 
709 extern void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn();
710 extern void report_untranslatable_char(int src_encoding, int dest_encoding,
711  const char *mbstr, int len) pg_attribute_noreturn();
712 
713 extern int local2local(const unsigned char *l, unsigned char *p, int len,
714  int src_encoding, int dest_encoding,
715  const unsigned char *tab, bool noError);
716 extern int latin2mic(const unsigned char *l, unsigned char *p, int len,
717  int lc, int encoding, bool noError);
718 extern int mic2latin(const unsigned char *mic, unsigned char *p, int len,
719  int lc, int encoding, bool noError);
720 extern int latin2mic_with_table(const unsigned char *l, unsigned char *p,
721  int len, int lc, int encoding,
722  const unsigned char *tab, bool noError);
723 extern int mic2latin_with_table(const unsigned char *mic, unsigned char *p,
724  int len, int lc, int encoding,
725  const unsigned char *tab, bool noError);
726 
727 #ifdef WIN32
728 extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
729 #endif
730 
731 #endif /* PG_WCHAR_H */
unsigned short uint16
Definition: c.h:492
unsigned int uint32
Definition: c.h:493
#define PGDLLIMPORT
Definition: c.h:1303
#define pg_attribute_noreturn()
Definition: c.h:204
unsigned char bool
Definition: c.h:443
unsigned char uint8
Definition: c.h:491
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
const char * pg_get_client_encoding_name(void)
Definition: mbutils.c:345
int pg_encoding_mblen_bounded(int encoding, const char *mbstr)
Definition: wchar.c:2142
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:356
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:2014
bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s)
Definition: mbutils.c:926
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676
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:572
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
const char * get_encoding_name_for_icu(int encoding)
Definition: encnames.c:472
uint32(* utf_local_conversion_func)(uint32 code)
Definition: pg_wchar.h:499
unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: wchar.c:484
int(* wchar2mb_with_len_converter)(const pg_wchar *from, unsigned char *to, int len)
Definition: pg_wchar.h:362
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
char * pg_client_to_server(const char *s, int len)
Definition: mbutils.c:660
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
int(* mbchar_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:372
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
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:833
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
pg_wchar utf8_to_unicode(const unsigned char *c)
Definition: wchar.c:680
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
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
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:546
int pg_valid_client_encoding(const char *name)
Definition: encnames.c:485
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition: wchar.c:2151
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1267
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
char * pg_server_to_client(const char *s, int len)
Definition: mbutils.c:738
unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc)
Definition: big5.c:292
#define pg_encoding_to_char
Definition: pg_wchar.h:569
#define pg_valid_server_encoding_id
Definition: pg_wchar.h:571
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
Definition: wchar.c:2177
int pg_get_client_encoding(void)
Definition: mbutils.c:336
#define pg_valid_server_encoding
Definition: pg_wchar.h:570
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
#define pg_char_to_encoding
Definition: pg_wchar.h:568
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:2188
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2130
PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:2076
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
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:2164
int pg_encoding_mb2wchar_with_len(int encoding, const char *from, pg_wchar *to, int len)
Definition: mbutils.c:993
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:749
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:31
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