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: the order of this enum must be same as order of entries
228  * in the pg_enc2name_tbl[] array (in src/common/encnames.c), and
229  * in the pg_wchar_table[] array (in src/common/wchar.c)!
230  *
231  * If you add some encoding don't forget to check
232  * PG_ENCODING_BE_LAST macro.
233  *
234  * PG_SQL_ASCII is default encoding and must be = 0.
235  *
236  * XXX We must avoid renumbering any backend encoding until libpq's major
237  * version number is increased beyond 5; it turns out that the backend
238  * encoding IDs are effectively part of libpq's ABI as far as 8.2 initdb and
239  * psql are concerned.
240  */
241 typedef enum pg_enc
242 {
243  PG_SQL_ASCII = 0, /* SQL/ASCII */
244  PG_EUC_JP, /* EUC for Japanese */
245  PG_EUC_CN, /* EUC for Chinese */
246  PG_EUC_KR, /* EUC for Korean */
247  PG_EUC_TW, /* EUC for Taiwan */
248  PG_EUC_JIS_2004, /* EUC-JIS-2004 */
249  PG_UTF8, /* Unicode UTF8 */
250  PG_MULE_INTERNAL, /* Mule internal code */
251  PG_LATIN1, /* ISO-8859-1 Latin 1 */
252  PG_LATIN2, /* ISO-8859-2 Latin 2 */
253  PG_LATIN3, /* ISO-8859-3 Latin 3 */
254  PG_LATIN4, /* ISO-8859-4 Latin 4 */
255  PG_LATIN5, /* ISO-8859-9 Latin 5 */
256  PG_LATIN6, /* ISO-8859-10 Latin6 */
257  PG_LATIN7, /* ISO-8859-13 Latin7 */
258  PG_LATIN8, /* ISO-8859-14 Latin8 */
259  PG_LATIN9, /* ISO-8859-15 Latin9 */
260  PG_LATIN10, /* ISO-8859-16 Latin10 */
261  PG_WIN1256, /* windows-1256 */
262  PG_WIN1258, /* Windows-1258 */
263  PG_WIN866, /* (MS-DOS CP866) */
264  PG_WIN874, /* windows-874 */
265  PG_KOI8R, /* KOI8-R */
266  PG_WIN1251, /* windows-1251 */
267  PG_WIN1252, /* windows-1252 */
268  PG_ISO_8859_5, /* ISO-8859-5 */
269  PG_ISO_8859_6, /* ISO-8859-6 */
270  PG_ISO_8859_7, /* ISO-8859-7 */
271  PG_ISO_8859_8, /* ISO-8859-8 */
272  PG_WIN1250, /* windows-1250 */
273  PG_WIN1253, /* windows-1253 */
274  PG_WIN1254, /* windows-1254 */
275  PG_WIN1255, /* windows-1255 */
276  PG_WIN1257, /* windows-1257 */
277  PG_KOI8U, /* KOI8-U */
278  /* PG_ENCODING_BE_LAST points to the above entry */
279 
280  /* followings are for client encoding only */
281  PG_SJIS, /* Shift JIS (Windows-932) */
282  PG_BIG5, /* Big5 (Windows-950) */
283  PG_GBK, /* GBK (Windows-936) */
284  PG_UHC, /* UHC (Windows-949) */
285  PG_GB18030, /* GB18030 */
286  PG_JOHAB, /* EUC for Korean JOHAB */
287  PG_SHIFT_JIS_2004, /* Shift-JIS-2004 */
288  _PG_LAST_ENCODING_ /* mark only */
289 
290 } pg_enc;
291 
292 #define PG_ENCODING_BE_LAST PG_KOI8U
293 
294 /*
295  * Please use these tests before access to pg_enc2name_tbl[]
296  * or to other places...
297  */
298 #define PG_VALID_BE_ENCODING(_enc) \
299  ((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST)
300 
301 #define PG_ENCODING_IS_CLIENT_ONLY(_enc) \
302  ((_enc) > PG_ENCODING_BE_LAST && (_enc) < _PG_LAST_ENCODING_)
303 
304 #define PG_VALID_ENCODING(_enc) \
305  ((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_)
306 
307 /* On FE are possible all encodings */
308 #define PG_VALID_FE_ENCODING(_enc) PG_VALID_ENCODING(_enc)
309 
310 /*
311  * When converting strings between different encodings, we assume that space
312  * for converted result is 4-to-1 growth in the worst case. The rate for
313  * currently supported encoding pairs are within 3 (SJIS JIS X0201 half width
314  * kana -> UTF8 is the worst case). So "4" should be enough for the moment.
315  *
316  * Note that this is not the same as the maximum character width in any
317  * particular encoding.
318  */
319 #define MAX_CONVERSION_GROWTH 4
320 
321 /*
322  * Maximum byte length of a string that's required in any encoding to convert
323  * at least one character to any other encoding. In other words, if you feed
324  * MAX_CONVERSION_INPUT_LENGTH bytes to any encoding conversion function, it
325  * is guaranteed to be able to convert something without needing more input
326  * (assuming the input is valid).
327  *
328  * Currently, the maximum case is the conversion UTF8 -> SJIS JIS X0201 half
329  * width kana, where a pair of UTF-8 characters is converted into a single
330  * SHIFT_JIS_2004 character (the reverse of the worst case for
331  * MAX_CONVERSION_GROWTH). It needs 6 bytes of input. In theory, a
332  * user-defined conversion function might have more complicated cases, although
333  * for the reverse mapping you would probably also need to bump up
334  * MAX_CONVERSION_GROWTH. But there is no need to be stingy here, so make it
335  * generous.
336  */
337 #define MAX_CONVERSION_INPUT_LENGTH 16
338 
339 /*
340  * Maximum byte length of the string equivalent to any one Unicode code point,
341  * in any backend encoding. The current value assumes that a 4-byte UTF-8
342  * character might expand by MAX_CONVERSION_GROWTH, which is a huge
343  * overestimate. But in current usage we don't allocate large multiples of
344  * this, so there's little point in being stingy.
345  */
346 #define MAX_UNICODE_EQUIVALENT_STRING 16
347 
348 /*
349  * Table for mapping an encoding number to official encoding name and
350  * possibly other subsidiary data. Be careful to check encoding number
351  * before accessing a table entry!
352  *
353  * if (PG_VALID_ENCODING(encoding))
354  * pg_enc2name_tbl[ encoding ];
355  */
356 typedef struct pg_enc2name
357 {
358  const char *name;
360 #ifdef WIN32
361  unsigned codepage; /* codepage for WIN32 */
362 #endif
363 } pg_enc2name;
364 
366 
367 /*
368  * Encoding names for gettext
369  */
370 typedef struct pg_enc2gettext
371 {
373  const char *name;
375 
377 
378 /*
379  * pg_wchar stuff
380  */
381 typedef int (*mb2wchar_with_len_converter) (const unsigned char *from,
382  pg_wchar *to,
383  int len);
384 
385 typedef int (*wchar2mb_with_len_converter) (const pg_wchar *from,
386  unsigned char *to,
387  int len);
388 
389 typedef int (*mblen_converter) (const unsigned char *mbstr);
390 
391 typedef int (*mbdisplaylen_converter) (const unsigned char *mbstr);
392 
393 typedef bool (*mbcharacter_incrementer) (unsigned char *mbstr, int len);
394 
395 typedef int (*mbchar_verifier) (const unsigned char *mbstr, int len);
396 
397 typedef int (*mbstr_verifier) (const unsigned char *mbstr, int len);
398 
399 typedef struct
400 {
401  mb2wchar_with_len_converter mb2wchar_with_len; /* convert a multibyte
402  * string to a wchar */
403  wchar2mb_with_len_converter wchar2mb_with_len; /* convert a wchar string
404  * to a multibyte */
405  mblen_converter mblen; /* get byte length of a char */
406  mbdisplaylen_converter dsplen; /* get display width of a char */
407  mbchar_verifier mbverifychar; /* verify multibyte character */
408  mbstr_verifier mbverifystr; /* verify multibyte string */
409  int maxmblen; /* max bytes for a char in this encoding */
410 } pg_wchar_tbl;
411 
413 
414 /*
415  * Data structures for conversions between UTF-8 and other encodings
416  * (UtfToLocal() and LocalToUtf()). In these data structures, characters of
417  * either encoding are represented by uint32 words; hence we can only support
418  * characters up to 4 bytes long. For example, the byte sequence 0xC2 0x89
419  * would be represented by 0x0000C289, and 0xE8 0xA2 0xB4 by 0x00E8A2B4.
420  *
421  * There are three possible ways a character can be mapped:
422  *
423  * 1. Using a radix tree, from source to destination code.
424  * 2. Using a sorted array of source -> destination code pairs. This
425  * method is used for "combining" characters. There are so few of
426  * them that building a radix tree would be wasteful.
427  * 3. Using a conversion function.
428  */
429 
430 /*
431  * Radix tree for character conversion.
432  *
433  * Logically, this is actually four different radix trees, for 1-byte,
434  * 2-byte, 3-byte and 4-byte inputs. The 1-byte tree is a simple lookup
435  * table from source to target code. The 2-byte tree consists of two levels:
436  * one lookup table for the first byte, where the value in the lookup table
437  * points to a lookup table for the second byte. And so on.
438  *
439  * Physically, all the trees are stored in one big array, in 'chars16' or
440  * 'chars32', depending on the maximum value that needs to be represented. For
441  * each level in each tree, we also store lower and upper bound of allowed
442  * values - values outside those bounds are considered invalid, and are left
443  * out of the tables.
444  *
445  * In the intermediate levels of the trees, the values stored are offsets
446  * into the chars[16|32] array.
447  *
448  * In the beginning of the chars[16|32] array, there is always a number of
449  * zeros, so that you safely follow an index from an intermediate table
450  * without explicitly checking for a zero. Following a zero any number of
451  * times will always bring you to the dummy, all-zeros table in the
452  * beginning. This helps to shave some cycles when looking up values.
453  */
454 typedef struct
455 {
456  /*
457  * Array containing all the values. Only one of chars16 or chars32 is
458  * used, depending on how wide the values we need to represent are.
459  */
460  const uint16 *chars16;
461  const uint32 *chars32;
462 
463  /* Radix tree for 1-byte inputs */
464  uint32 b1root; /* offset of table in the chars[16|32] array */
465  uint8 b1_lower; /* min allowed value for a single byte input */
466  uint8 b1_upper; /* max allowed value for a single byte input */
467 
468  /* Radix tree for 2-byte inputs */
469  uint32 b2root; /* offset of 1st byte's table */
470  uint8 b2_1_lower; /* min/max allowed value for 1st input byte */
471  uint8 b2_1_upper;
472  uint8 b2_2_lower; /* min/max allowed value for 2nd input byte */
473  uint8 b2_2_upper;
474 
475  /* Radix tree for 3-byte inputs */
476  uint32 b3root; /* offset of 1st byte's table */
477  uint8 b3_1_lower; /* min/max allowed value for 1st input byte */
478  uint8 b3_1_upper;
479  uint8 b3_2_lower; /* min/max allowed value for 2nd input byte */
480  uint8 b3_2_upper;
481  uint8 b3_3_lower; /* min/max allowed value for 3rd input byte */
482  uint8 b3_3_upper;
483 
484  /* Radix tree for 4-byte inputs */
485  uint32 b4root; /* offset of 1st byte's table */
486  uint8 b4_1_lower; /* min/max allowed value for 1st input byte */
487  uint8 b4_1_upper;
488  uint8 b4_2_lower; /* min/max allowed value for 2nd input byte */
489  uint8 b4_2_upper;
490  uint8 b4_3_lower; /* min/max allowed value for 3rd input byte */
491  uint8 b4_3_upper;
492  uint8 b4_4_lower; /* min/max allowed value for 4th input byte */
493  uint8 b4_4_upper;
494 
496 
497 /*
498  * UTF-8 to local code conversion map (for combined characters)
499  */
500 typedef struct
501 {
502  uint32 utf1; /* UTF-8 code 1 */
503  uint32 utf2; /* UTF-8 code 2 */
504  uint32 code; /* local code */
506 
507 /*
508  * local code to UTF-8 conversion map (for combined characters)
509  */
510 typedef struct
511 {
512  uint32 code; /* local code */
513  uint32 utf1; /* UTF-8 code 1 */
514  uint32 utf2; /* UTF-8 code 2 */
516 
517 /*
518  * callback function for algorithmic encoding conversions (in either direction)
519  *
520  * if function returns zero, it does not know how to convert the code
521  */
522 typedef uint32 (*utf_local_conversion_func) (uint32 code);
523 
524 /*
525  * Support macro for encoding conversion functions to validate their
526  * arguments. (This could be made more compact if we included fmgr.h
527  * here, but we don't want to do that because this header file is also
528  * used by frontends.)
529  */
530 #define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
531  check_encoding_conversion_args(PG_GETARG_INT32(0), \
532  PG_GETARG_INT32(1), \
533  PG_GETARG_INT32(4), \
534  (srcencoding), \
535  (destencoding))
536 
537 
538 /*
539  * Some handy functions for Unicode-specific tests.
540  */
541 static inline bool
543 {
544  return (c > 0 && c <= 0x10FFFF);
545 }
546 
547 static inline bool
549 {
550  return (c >= 0xD800 && c <= 0xDBFF);
551 }
552 
553 static inline bool
555 {
556  return (c >= 0xDC00 && c <= 0xDFFF);
557 }
558 
559 static inline pg_wchar
561 {
562  return ((first & 0x3FF) << 10) + 0x10000 + (second & 0x3FF);
563 }
564 
565 
566 /*
567  * The functions in this list are exported by libpq, and we need to be sure
568  * that we know which calls are satisfied by libpq and which are satisfied
569  * by static linkage to libpgcommon. (This is because we might be using a
570  * libpq.so that's of a different major version and has encoding IDs that
571  * differ from the current version's.) The nominal function names are what
572  * are actually used in and exported by libpq, while the names exported by
573  * libpgcommon.a and libpgcommon_srv.a end in "_private".
574  */
575 #if defined(USE_PRIVATE_ENCODING_FUNCS) || !defined(FRONTEND)
576 #define pg_char_to_encoding pg_char_to_encoding_private
577 #define pg_encoding_to_char pg_encoding_to_char_private
578 #define pg_valid_server_encoding pg_valid_server_encoding_private
579 #define pg_valid_server_encoding_id pg_valid_server_encoding_id_private
580 #define pg_utf_mblen pg_utf_mblen_private
581 #endif
582 
583 /*
584  * These functions are considered part of libpq's exported API and
585  * are also declared in libpq-fe.h.
586  */
587 extern int pg_char_to_encoding(const char *name);
588 extern const char *pg_encoding_to_char(int encoding);
589 extern int pg_valid_server_encoding_id(int encoding);
590 
591 /*
592  * These functions are available to frontend code that links with libpgcommon
593  * (in addition to the ones just above). The constant tables declared
594  * earlier in this file are also available from libpgcommon.
595  */
596 extern int pg_encoding_mblen(int encoding, const char *mbstr);
597 extern int pg_encoding_mblen_bounded(int encoding, const char *mbstr);
598 extern int pg_encoding_dsplen(int encoding, const char *mbstr);
599 extern int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len);
600 extern int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len);
601 extern int pg_encoding_max_length(int encoding);
602 extern int pg_valid_client_encoding(const char *name);
603 extern int pg_valid_server_encoding(const char *name);
604 extern bool is_encoding_supported_by_icu(int encoding);
605 extern const char *get_encoding_name_for_icu(int encoding);
606 
607 extern unsigned char *unicode_to_utf8(pg_wchar c, unsigned char *utf8string);
608 extern pg_wchar utf8_to_unicode(const unsigned char *c);
609 extern bool pg_utf8_islegal(const unsigned char *source, int length);
610 extern int pg_utf_mblen(const unsigned char *s);
611 extern int pg_mule_mblen(const unsigned char *s);
612 
613 /*
614  * The remaining functions are backend-only.
615  */
616 extern int pg_mb2wchar(const char *from, pg_wchar *to);
617 extern int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
619  const char *from, pg_wchar *to, int len);
620 extern int pg_wchar2mb(const pg_wchar *from, char *to);
621 extern int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
623  const pg_wchar *from, char *to, int len);
624 extern int pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
625 extern int pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n);
626 extern int pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n);
627 extern size_t pg_wchar_strlen(const pg_wchar *str);
628 extern int pg_mblen(const char *mbstr);
629 extern int pg_dsplen(const char *mbstr);
630 extern int pg_mbstrlen(const char *mbstr);
631 extern int pg_mbstrlen_with_len(const char *mbstr, int limit);
632 extern int pg_mbcliplen(const char *mbstr, int len, int limit);
633 extern int pg_encoding_mbcliplen(int encoding, const char *mbstr,
634  int len, int limit);
635 extern int pg_mbcharcliplen(const char *mbstr, int len, int limit);
636 extern int pg_database_encoding_max_length(void);
638 
639 extern int PrepareClientEncoding(int encoding);
640 extern int SetClientEncoding(int encoding);
641 extern void InitializeClientEncoding(void);
642 extern int pg_get_client_encoding(void);
643 extern const char *pg_get_client_encoding_name(void);
644 
645 extern void SetDatabaseEncoding(int encoding);
646 extern int GetDatabaseEncoding(void);
647 extern const char *GetDatabaseEncodingName(void);
648 extern void SetMessageEncoding(int encoding);
649 extern int GetMessageEncoding(void);
650 
651 #ifdef ENABLE_NLS
652 extern int pg_bind_textdomain_codeset(const char *domainname);
653 #endif
654 
655 extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
656  int src_encoding,
657  int dest_encoding);
658 extern int pg_do_encoding_conversion_buf(Oid proc,
659  int src_encoding,
660  int dest_encoding,
661  unsigned char *src, int srclen,
662  unsigned char *dest, int destlen,
663  bool noError);
664 
665 extern char *pg_client_to_server(const char *s, int len);
666 extern char *pg_server_to_client(const char *s, int len);
667 extern char *pg_any_to_server(const char *s, int len, int encoding);
668 extern char *pg_server_to_any(const char *s, int len, int encoding);
669 
670 extern void pg_unicode_to_server(pg_wchar c, unsigned char *s);
671 extern bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s);
672 
673 extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
674 extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
675 
676 extern int UtfToLocal(const unsigned char *utf, int len,
677  unsigned char *iso,
678  const pg_mb_radix_tree *map,
679  const pg_utf_to_local_combined *cmap, int cmapsize,
680  utf_local_conversion_func conv_func,
681  int encoding, bool noError);
682 extern int LocalToUtf(const unsigned char *iso, int len,
683  unsigned char *utf,
684  const pg_mb_radix_tree *map,
685  const pg_local_to_utf_combined *cmap, int cmapsize,
686  utf_local_conversion_func conv_func,
687  int encoding, bool noError);
688 
689 extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
690 extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
691  bool noError);
692 extern int pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
693  bool noError);
694 
695 extern void check_encoding_conversion_args(int src_encoding,
696  int dest_encoding,
697  int len,
698  int expected_src_encoding,
699  int expected_dest_encoding);
700 
701 extern void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn();
702 extern void report_untranslatable_char(int src_encoding, int dest_encoding,
703  const char *mbstr, int len) pg_attribute_noreturn();
704 
705 extern int local2local(const unsigned char *l, unsigned char *p, int len,
706  int src_encoding, int dest_encoding,
707  const unsigned char *tab, bool noError);
708 extern int latin2mic(const unsigned char *l, unsigned char *p, int len,
709  int lc, int encoding, bool noError);
710 extern int mic2latin(const unsigned char *mic, unsigned char *p, int len,
711  int lc, int encoding, bool noError);
712 extern int latin2mic_with_table(const unsigned char *l, unsigned char *p,
713  int len, int lc, int encoding,
714  const unsigned char *tab, bool noError);
715 extern int mic2latin_with_table(const unsigned char *mic, unsigned char *p,
716  int len, int lc, int encoding,
717  const unsigned char *tab, bool noError);
718 
719 #ifdef WIN32
720 extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
721 #endif
722 
723 #endif /* PG_WCHAR_H */
unsigned short uint16
Definition: c.h:494
unsigned int uint32
Definition: c.h:495
#define PGDLLIMPORT
Definition: c.h:1305
#define pg_attribute_noreturn()
Definition: c.h:206
unsigned char bool
Definition: c.h:445
unsigned char uint8
Definition: c.h:493
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:346
int pg_encoding_mblen_bounded(int encoding, const char *mbstr)
Definition: wchar.c:2143
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:357
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:927
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:677
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268
int pg_encoding_wchar2mb_with_len(int encoding, const pg_wchar *from, char *to, int len)
Definition: mbutils.c:1016
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:564
pg_enc
Definition: pg_wchar.h:226
@ PG_WIN1254
Definition: pg_wchar.h:258
@ PG_LATIN4
Definition: pg_wchar.h:238
@ _PG_LAST_ENCODING_
Definition: pg_wchar.h:272
@ PG_LATIN9
Definition: pg_wchar.h:243
@ PG_JOHAB
Definition: pg_wchar.h:270
@ PG_GB18030
Definition: pg_wchar.h:269
@ PG_SQL_ASCII
Definition: pg_wchar.h:227
@ PG_KOI8R
Definition: pg_wchar.h:249
@ PG_ISO_8859_6
Definition: pg_wchar.h:253
@ PG_WIN1253
Definition: pg_wchar.h:257
@ PG_KOI8U
Definition: pg_wchar.h:261
@ PG_LATIN6
Definition: pg_wchar.h:240
@ PG_MULE_INTERNAL
Definition: pg_wchar.h:234
@ PG_LATIN5
Definition: pg_wchar.h:239
@ PG_EUC_CN
Definition: pg_wchar.h:229
@ PG_UHC
Definition: pg_wchar.h:268
@ PG_LATIN2
Definition: pg_wchar.h:236
@ PG_ISO_8859_5
Definition: pg_wchar.h:252
@ PG_LATIN10
Definition: pg_wchar.h:244
@ PG_WIN1250
Definition: pg_wchar.h:256
@ PG_ISO_8859_7
Definition: pg_wchar.h:254
@ PG_SJIS
Definition: pg_wchar.h:265
@ PG_LATIN8
Definition: pg_wchar.h:242
@ PG_EUC_JP
Definition: pg_wchar.h:228
@ PG_GBK
Definition: pg_wchar.h:267
@ PG_LATIN3
Definition: pg_wchar.h:237
@ PG_WIN1256
Definition: pg_wchar.h:245
@ PG_LATIN1
Definition: pg_wchar.h:235
@ PG_EUC_TW
Definition: pg_wchar.h:231
@ PG_WIN1258
Definition: pg_wchar.h:246
@ PG_SHIFT_JIS_2004
Definition: pg_wchar.h:271
@ PG_WIN1252
Definition: pg_wchar.h:251
@ PG_LATIN7
Definition: pg_wchar.h:241
@ PG_UTF8
Definition: pg_wchar.h:233
@ PG_WIN1255
Definition: pg_wchar.h:259
@ PG_WIN1257
Definition: pg_wchar.h:260
@ PG_WIN1251
Definition: pg_wchar.h:250
@ PG_EUC_KR
Definition: pg_wchar.h:230
@ PG_WIN866
Definition: pg_wchar.h:247
@ PG_ISO_8859_8
Definition: pg_wchar.h:255
@ PG_WIN874
Definition: pg_wchar.h:248
@ PG_EUC_JIS_2004
Definition: pg_wchar.h:232
@ PG_BIG5
Definition: pg_wchar.h:266
const char * get_encoding_name_for_icu(int encoding)
Definition: encnames.c:473
uint32(* utf_local_conversion_func)(uint32 code)
Definition: pg_wchar.h:506
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:369
int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1604
void InitializeClientEncoding(void)
Definition: mbutils.c:282
int pg_dsplen(const char *mbstr)
Definition: mbutils.c:1031
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1058
mbcharacter_incrementer pg_database_encoding_character_incrementer(void)
Definition: mbutils.c:1530
char * pg_client_to_server(const char *s, int len)
Definition: mbutils.c:661
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
Definition: mbutils.c:1009
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:980
bool(* mbcharacter_incrementer)(unsigned char *mbstr, int len)
Definition: pg_wchar.h:377
int(* mbchar_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:379
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1126
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:1002
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len) pg_attribute_noreturn()
Definition: mbutils.c:1737
int pg_mule_mblen(const unsigned char *s)
Definition: wchar.c:833
PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
int pg_mbstrlen(const char *mbstr)
Definition: mbutils.c:1038
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1573
PGDLLIMPORT const pg_enc2gettext pg_enc2gettext_tbl[]
Definition: encnames.c:361
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1563
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:1084
int GetMessageEncoding(void)
Definition: mbutils.c:1315
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:470
int SetClientEncoding(int encoding)
Definition: mbutils.c:209
void SetMessageEncoding(int encoding)
Definition: mbutils.c:1172
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Definition: mbutils.c:865
static bool is_valid_unicode_codepoint(pg_wchar c)
Definition: pg_wchar.h:526
unsigned int pg_wchar
Definition: pg_wchar.h:28
struct pg_enc2gettext pg_enc2gettext
void check_encoding_conversion_args(int src_encoding, int dest_encoding, int len, int expected_src_encoding, int expected_dest_encoding)
Definition: mbutils.c:1676
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1553
int PrepareClientEncoding(int encoding)
Definition: mbutils.c:111
int pg_valid_client_encoding(const char *name)
Definition: encnames.c:486
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition: wchar.c:2152
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1274
void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn()
Definition: mbutils.c:1705
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:739
unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc)
Definition: big5.c:292
#define pg_encoding_to_char
Definition: pg_wchar.h:561
#define pg_valid_server_encoding_id
Definition: pg_wchar.h:563
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
Definition: wchar.c:2178
int pg_get_client_encoding(void)
Definition: mbutils.c:337
#define pg_valid_server_encoding
Definition: pg_wchar.h:562
bool is_encoding_supported_by_icu(int encoding)
Definition: encnames.c:462
int(* mb2wchar_with_len_converter)(const unsigned char *from, pg_wchar *to, int len)
Definition: pg_wchar.h:365
#define pg_char_to_encoding
Definition: pg_wchar.h:560
int(* mbstr_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:381
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:2189
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2131
PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:2077
static pg_wchar surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second)
Definition: pg_wchar.h:544
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:532
void SetDatabaseEncoding(int encoding)
Definition: mbutils.c:1162
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:1094
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:2165
int pg_encoding_mb2wchar_with_len(int encoding, const char *from, pg_wchar *to, int len)
Definition: mbutils.c:994
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:750
int(* mbdisplaylen_converter)(const unsigned char *mbstr)
Definition: pg_wchar.h:375
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition: mbutils.c:987
static bool is_utf16_surrogate_second(pg_wchar c)
Definition: pg_wchar.h:538
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1024
int(* mblen_converter)(const unsigned char *mbstr)
Definition: pg_wchar.h:373
unsigned int Oid
Definition: postgres_ext.h:31
char * c
char * s1
char * s2
const char * name
Definition: pg_wchar.h:357
pg_enc encoding
Definition: pg_wchar.h:356
pg_enc encoding
Definition: pg_wchar.h:343
const char * name
Definition: pg_wchar.h:342
const char * name