1 /*-------------------------------------------------------------------------
4 * multibyte-character support
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
12 * This is used both by the backend and by libpq, 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.
17 *-------------------------------------------------------------------------
22 #include <sys/types.h>
27 typedef unsigned int pg_wchar
;
30 * various definitions for EUC
32 #define SS2 0x8e /* single shift 2 (JIS0201) */
33 #define SS3 0x8f /* single shift 3 (JIS0212) */
36 * SJIS validation macros
38 #define ISSJISHEAD(c) (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc))
39 #define ISSJISTAIL(c) (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc))
42 * Leading byte types or leading prefix byte for MULE internal code.
43 * See http://www.xemacs.org for more details. (there is a doc titled
44 * "XEmacs Internals Manual", "MULE Character Sets and Encodings"
48 * Is a leading byte for "official" single byte encodings?
50 #define IS_LC1(c) ((unsigned char)(c) >= 0x81 && (unsigned char)(c) <= 0x8d)
52 * Is a prefix byte for "private" single byte encodings?
54 #define IS_LCPRV1(c) ((unsigned char)(c) == 0x9a || (unsigned char)(c) == 0x9b)
56 * Is a leading byte for "official" multibyte encodings?
58 #define IS_LC2(c) ((unsigned char)(c) >= 0x90 && (unsigned char)(c) <= 0x99)
60 * Is a prefix byte for "private" multibyte encodings?
62 #define IS_LCPRV2(c) ((unsigned char)(c) == 0x9c || (unsigned char)(c) == 0x9d)
64 /*----------------------------------------------------
66 *----------------------------------------------------
70 * Official single byte encodings (0x81-0x8e)
72 #define LC_ISO8859_1 0x81 /* ISO8859 Latin 1 */
73 #define LC_ISO8859_2 0x82 /* ISO8859 Latin 2 */
74 #define LC_ISO8859_3 0x83 /* ISO8859 Latin 3 */
75 #define LC_ISO8859_4 0x84 /* ISO8859 Latin 4 */
76 #define LC_TIS620 0x85 /* Thai (not supported yet) */
77 #define LC_ISO8859_7 0x86 /* Greek (not supported yet) */
78 #define LC_ISO8859_6 0x87 /* Arabic (not supported yet) */
79 #define LC_ISO8859_8 0x88 /* Hebrew (not supported yet) */
80 #define LC_JISX0201K 0x89 /* Japanese 1 byte kana */
81 #define LC_JISX0201R 0x8a /* Japanese 1 byte Roman */
82 /* Note that 0x8b seems to be unused as of Emacs 20.7.
83 * However, there might be a chance that 0x8b could be used
84 * in later version of Emacs.
86 #define LC_KOI8_R 0x8b /* Cyrillic KOI8-R */
87 #define LC_KOI8_U 0x8b /* Cyrillic KOI8-U */
88 #define LC_ISO8859_5 0x8c /* ISO8859 Cyrillic */
89 #define LC_ISO8859_9 0x8d /* ISO8859 Latin 5 (not supported yet) */
90 /* #define FREE 0x8e free (unused) */
95 #define CONTROL_1 0x8f /* control characters (unused) */
98 * Official multibyte byte encodings (0x90-0x99)
99 * 0x9a-0x9d are free. 0x9e and 0x9f are reserved.
101 #define LC_JISX0208_1978 0x90 /* Japanese Kanji, old JIS (not supported) */
102 /* #define FREE 0x90 free (unused) */
103 #define LC_GB2312_80 0x91 /* Chinese */
104 #define LC_JISX0208 0x92 /* Japanese Kanji (JIS X 0208) */
105 #define LC_KS5601 0x93 /* Korean */
106 #define LC_JISX0212 0x94 /* Japanese Kanji (JIS X 0212) */
107 #define LC_CNS11643_1 0x95 /* CNS 11643-1992 Plane 1 */
108 #define LC_CNS11643_2 0x96 /* CNS 11643-1992 Plane 2 */
109 /* #define FREE 0x97 free (unused) */
110 #define LC_BIG5_1 0x98 /* Plane 1 Chinese traditional (not supported) */
111 #define LC_BIG5_2 0x99 /* Plane 1 Chinese traditional (not supported) */
114 * Private single byte encodings (0xa0-0xef)
116 #define LC_SISHENG 0xa0/* Chinese SiSheng characters for
117 * PinYin/ZhuYin (not supported) */
118 #define LC_IPA 0xa1/* IPA (International Phonetic Association)
120 #define LC_VISCII_LOWER 0xa2/* Vietnamese VISCII1.1 lower-case (not
122 #define LC_VISCII_UPPER 0xa3/* Vietnamese VISCII1.1 upper-case (not
124 #define LC_ARABIC_DIGIT 0xa4 /* Arabic digit (not supported) */
125 #define LC_ARABIC_1_COLUMN 0xa5 /* Arabic 1-column (not supported) */
126 #define LC_ASCII_RIGHT_TO_LEFT 0xa6 /* ASCII (left half of ISO8859-1) with
127 * right-to-left direction (not
129 #define LC_LAO 0xa7/* Lao characters (ISO10646 0E80..0EDF) (not
131 #define LC_ARABIC_2_COLUMN 0xa8 /* Arabic 1-column (not supported) */
134 * Private multibyte encodings (0xf0-0xff)
136 #define LC_INDIAN_1_COLUMN 0xf0/* Indian charset for 1-column width glypps
138 #define LC_TIBETAN_1_COLUMN 0xf1 /* Tibetan 1 column glyph (not supported) */
139 #define LC_ETHIOPIC 0xf5 /* Ethiopic characters (not supported) */
140 #define LC_CNS11643_3 0xf6 /* CNS 11643-1992 Plane 3 */
141 #define LC_CNS11643_4 0xf7 /* CNS 11643-1992 Plane 4 */
142 #define LC_CNS11643_5 0xf8 /* CNS 11643-1992 Plane 5 */
143 #define LC_CNS11643_6 0xf9 /* CNS 11643-1992 Plane 6 */
144 #define LC_CNS11643_7 0xfa /* CNS 11643-1992 Plane 7 */
145 #define LC_INDIAN_2_COLUMN 0xfb/* Indian charset for 2-column width glypps
147 #define LC_TIBETAN 0xfc /* Tibetan (not supported) */
148 /* #define FREE 0xfd free (unused) */
149 /* #define FREE 0xfe free (unused) */
150 /* #define FREE 0xff free (unused) */
153 * PostgreSQL encoding identifiers
155 * WARNING: the order of this enum must be same as order of entries
156 * in the pg_enc2name_tbl[] array (in mb/encnames.c), and
157 * in the pg_wchar_table[] array (in mb/wchar.c)!
159 * If you add some encoding don't forget to check
160 * PG_ENCODING_BE_LAST macro.
162 * PG_SQL_ASCII is default encoding and must be = 0.
164 * XXX We must avoid renumbering any backend encoding until libpq's major
165 * version number is increased beyond 5; it turns out that the backend
166 * encoding IDs are effectively part of libpq's ABI as far as 8.2 initdb and
167 * psql are concerned.
171 PG_SQL_ASCII
= 0, /* SQL/ASCII */
172 PG_EUC_JP
, /* EUC for Japanese */
173 PG_EUC_CN
, /* EUC for Chinese */
174 PG_EUC_KR
, /* EUC for Korean */
175 PG_EUC_TW
, /* EUC for Taiwan */
176 PG_EUC_JIS_2004
, /* EUC-JIS-2004 */
177 PG_UTF8
, /* Unicode UTF8 */
178 PG_MULE_INTERNAL
, /* Mule internal code */
179 PG_LATIN1
, /* ISO-8859-1 Latin 1 */
180 PG_LATIN2
, /* ISO-8859-2 Latin 2 */
181 PG_LATIN3
, /* ISO-8859-3 Latin 3 */
182 PG_LATIN4
, /* ISO-8859-4 Latin 4 */
183 PG_LATIN5
, /* ISO-8859-9 Latin 5 */
184 PG_LATIN6
, /* ISO-8859-10 Latin6 */
185 PG_LATIN7
, /* ISO-8859-13 Latin7 */
186 PG_LATIN8
, /* ISO-8859-14 Latin8 */
187 PG_LATIN9
, /* ISO-8859-15 Latin9 */
188 PG_LATIN10
, /* ISO-8859-16 Latin10 */
189 PG_WIN1256
, /* windows-1256 */
190 PG_WIN1258
, /* Windows-1258 */
191 PG_WIN866
, /* (MS-DOS CP866) */
192 PG_WIN874
, /* windows-874 */
193 PG_KOI8R
, /* KOI8-R */
194 PG_WIN1251
, /* windows-1251 */
195 PG_WIN1252
, /* windows-1252 */
196 PG_ISO_8859_5
, /* ISO-8859-5 */
197 PG_ISO_8859_6
, /* ISO-8859-6 */
198 PG_ISO_8859_7
, /* ISO-8859-7 */
199 PG_ISO_8859_8
, /* ISO-8859-8 */
200 PG_WIN1250
, /* windows-1250 */
201 PG_WIN1253
, /* windows-1253 */
202 PG_WIN1254
, /* windows-1254 */
203 PG_WIN1255
, /* windows-1255 */
204 PG_WIN1257
, /* windows-1257 */
205 PG_KOI8U
, /* KOI8-U */
206 /* PG_ENCODING_BE_LAST points to the above entry */
208 /* followings are for client encoding only */
209 PG_SJIS
, /* Shift JIS (Winindows-932) */
210 PG_BIG5
, /* Big5 (Windows-950) */
211 PG_GBK
, /* GBK (Windows-936) */
212 PG_UHC
, /* UHC (Windows-949) */
213 PG_GB18030
, /* GB18030 */
214 PG_JOHAB
, /* EUC for Korean JOHAB */
215 PG_SHIFT_JIS_2004
, /* Shift-JIS-2004 */
216 _PG_LAST_ENCODING_
/* mark only */
220 #define PG_ENCODING_BE_LAST PG_KOI8U
223 * Please use these tests before access to pg_encconv_tbl[]
224 * or to other places...
226 #define PG_VALID_BE_ENCODING(_enc) \
227 ((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST)
229 #define PG_ENCODING_IS_CLIENT_ONLY(_enc) \
230 ((_enc) > PG_ENCODING_BE_LAST && (_enc) < _PG_LAST_ENCODING_)
232 #define PG_VALID_ENCODING(_enc) \
233 ((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_)
235 /* On FE are possible all encodings */
236 #define PG_VALID_FE_ENCODING(_enc) PG_VALID_ENCODING(_enc)
239 * Encoding names with all aliases
241 typedef struct pg_encname
247 extern pg_encname pg_encname_tbl
[];
248 extern unsigned int pg_encname_tbl_sz
;
253 * if (PG_VALID_ENCODING(encoding))
254 * pg_enc2name_tbl[ encoding ];
256 typedef struct pg_enc2name
262 extern pg_enc2name pg_enc2name_tbl
[];
265 * Encoding names for gettext
267 typedef struct pg_enc2gettext
273 extern pg_enc2gettext pg_enc2gettext_tbl
[];
278 typedef int (*mb2wchar_with_len_converter
) (const unsigned char *from
,
282 typedef int (*mblen_converter
) (const unsigned char *mbstr
);
284 typedef int (*mbdisplaylen_converter
) (const unsigned char *mbstr
);
286 typedef int (*mbverifier
) (const unsigned char *mbstr
, int len
);
290 mb2wchar_with_len_converter mb2wchar_with_len
; /* convert a multibyte
291 * string to a wchar */
292 mblen_converter mblen
; /* get byte length of a char */
293 mbdisplaylen_converter dsplen
; /* get display width of a char */
294 mbverifier mbverify
; /* verify multibyte sequence */
295 int maxmblen
; /* max bytes for a char in this encoding */
298 extern pg_wchar_tbl pg_wchar_table
[];
301 * UTF-8 to local code conversion map
302 * Note that we limit the max length of UTF-8 to 4 bytes,
303 * which is UCS-4 00010000-001FFFFF range.
307 uint32 utf
; /* UTF-8 */
308 uint32 code
; /* local code */
312 * local code to UTF-8 conversion map
316 uint32 code
; /* local code */
317 uint32 utf
; /* UTF-8 */
321 * UTF-8 to local code conversion map(combined characters)
325 uint32 utf1
; /* UTF-8 code 1 */
326 uint32 utf2
; /* UTF-8 code 2 */
327 uint32 code
; /* local code */
328 } pg_utf_to_local_combined
;
331 * local code to UTF-8 conversion map(combined characters)
335 uint32 code
; /* local code */
336 uint32 utf1
; /* UTF-8 code 1 */
337 uint32 utf2
; /* UTF-8 code 2 */
338 } pg_local_to_utf_combined
;
341 * Support macro for encoding conversion functions to validate their
342 * arguments. (This could be made more compact if we included fmgr.h
343 * here, but we don't want to do that because this header file is also
344 * used by frontends.)
346 #define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
347 check_encoding_conversion_args(PG_GETARG_INT32(0), \
348 PG_GETARG_INT32(1), \
349 PG_GETARG_INT32(4), \
355 * These functions are considered part of libpq's exported API and
356 * are also declared in libpq-fe.h.
358 extern int pg_char_to_encoding(const char *name
);
359 extern const char *pg_encoding_to_char(int encoding
);
360 extern int pg_valid_server_encoding_id(int encoding
);
363 * Remaining functions are not considered part of libpq's API, though many
364 * of them do exist inside libpq.
366 extern pg_encname
*pg_char_to_encname_struct(const char *name
);
368 extern int pg_mb2wchar(const char *from
, pg_wchar
*to
);
369 extern int pg_mb2wchar_with_len(const char *from
, pg_wchar
*to
, int len
);
370 extern int pg_encoding_mb2wchar_with_len(int encoding
,
371 const char *from
, pg_wchar
*to
, int len
);
372 extern int pg_char_and_wchar_strcmp(const char *s1
, const pg_wchar
*s2
);
373 extern int pg_wchar_strncmp(const pg_wchar
*s1
, const pg_wchar
*s2
, size_t n
);
374 extern int pg_char_and_wchar_strncmp(const char *s1
, const pg_wchar
*s2
, size_t n
);
375 extern size_t pg_wchar_strlen(const pg_wchar
*wstr
);
376 extern int pg_mblen(const char *mbstr
);
377 extern int pg_dsplen(const char *mbstr
);
378 extern int pg_encoding_mblen(int encoding
, const char *mbstr
);
379 extern int pg_encoding_dsplen(int encoding
, const char *mbstr
);
380 extern int pg_encoding_verifymb(int encoding
, const char *mbstr
, int len
);
381 extern int pg_mule_mblen(const unsigned char *mbstr
);
382 extern int pg_mic_mblen(const unsigned char *mbstr
);
383 extern int pg_mbstrlen(const char *mbstr
);
384 extern int pg_mbstrlen_with_len(const char *mbstr
, int len
);
385 extern int pg_mbcliplen(const char *mbstr
, int len
, int limit
);
386 extern int pg_encoding_mbcliplen(int encoding
, const char *mbstr
,
388 extern int pg_mbcharcliplen(const char *mbstr
, int len
, int imit
);
389 extern int pg_encoding_max_length(int encoding
);
390 extern int pg_database_encoding_max_length(void);
392 #ifdef USE_WIDE_UPPER_LOWER
393 extern size_t wchar2char(char *to
, const wchar_t *from
, size_t tolen
);
394 extern size_t char2wchar(wchar_t *to
, size_t tolen
, const char *from
, size_t fromlen
);
397 extern int SetClientEncoding(int encoding
, bool doit
);
398 extern void InitializeClientEncoding(void);
399 extern int pg_get_client_encoding(void);
400 extern const char *pg_get_client_encoding_name(void);
402 extern void SetDatabaseEncoding(int encoding
);
403 extern int GetDatabaseEncoding(void);
404 extern const char *GetDatabaseEncodingName(void);
405 extern void pg_bind_textdomain_codeset(const char *domainname
);
407 extern int pg_valid_client_encoding(const char *name
);
408 extern int pg_valid_server_encoding(const char *name
);
410 extern unsigned char *unicode_to_utf8(pg_wchar c
, unsigned char *utf8string
);
411 extern int pg_utf_mblen(const unsigned char *);
412 extern unsigned char *pg_do_encoding_conversion(unsigned char *src
, int len
,
416 extern char *pg_client_to_server(const char *s
, int len
);
417 extern char *pg_server_to_client(const char *s
, int len
);
419 extern unsigned short BIG5toCNS(unsigned short big5
, unsigned char *lc
);
420 extern unsigned short CNStoBIG5(unsigned short cns
, unsigned char lc
);
422 extern void LocalToUtf(const unsigned char *iso
, unsigned char *utf
,
423 const pg_local_to_utf
*map
, const pg_local_to_utf_combined
*cmap
,
424 int size1
, int size2
, int encoding
, int len
);
426 extern void UtfToLocal(const unsigned char *utf
, unsigned char *iso
,
427 const pg_utf_to_local
*map
, const pg_utf_to_local_combined
*cmap
,
428 int size1
, int size2
, int encoding
, int len
);
430 extern bool pg_verifymbstr(const char *mbstr
, int len
, bool noError
);
431 extern bool pg_verify_mbstr(int encoding
, const char *mbstr
, int len
,
433 extern int pg_verify_mbstr_len(int encoding
, const char *mbstr
, int len
,
436 extern void check_encoding_conversion_args(int src_encoding
,
439 int expected_src_encoding
,
440 int expected_dest_encoding
);
442 extern void report_invalid_encoding(int encoding
, const char *mbstr
, int len
);
443 extern void report_untranslatable_char(int src_encoding
, int dest_encoding
,
444 const char *mbstr
, int len
);
446 extern void pg_ascii2mic(const unsigned char *l
, unsigned char *p
, int len
);
447 extern void pg_mic2ascii(const unsigned char *mic
, unsigned char *p
, int len
);
448 extern void latin2mic(const unsigned char *l
, unsigned char *p
, int len
,
449 int lc
, int encoding
);
450 extern void mic2latin(const unsigned char *mic
, unsigned char *p
, int len
,
451 int lc
, int encoding
);
452 extern void latin2mic_with_table(const unsigned char *l
, unsigned char *p
,
453 int len
, int lc
, int encoding
,
454 const unsigned char *tab
);
455 extern void mic2latin_with_table(const unsigned char *mic
, unsigned char *p
,
456 int len
, int lc
, int encoding
,
457 const unsigned char *tab
);
459 extern bool pg_utf8_islegal(const unsigned char *source
, int length
);
461 #endif /* PG_WCHAR_H */