| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pg_wchar.h |
| 4 |
* multibyte-character support |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* src/include/mb/pg_wchar.h |
| 10 |
* |
| 11 |
* NOTES |
| 12 |
* This is used both by the backend and by frontends, but should not be |
| 13 |
* included by libpq client programs. In particular, a libpq client |
| 14 |
* should not assume that the encoding IDs used by the version of libpq |
| 15 |
* it's linked to match up with the IDs declared here. |
| 16 |
* To help prevent mistakes, relevant functions that are exported by |
| 17 |
* libpq have a physically different name when being referenced |
| 18 |
* statically. |
| 19 |
* |
| 20 |
*------------------------------------------------------------------------- |
| 21 |
*/ |
| 22 |
#ifndef PG_WCHAR_H |
| 23 |
#define PG_WCHAR_H |
| 24 |
|
| 25 |
/* |
| 26 |
* The pg_wchar type |
| 27 |
*/ |
| 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; |
| 358 |
pg_enc encoding; |
| 359 |
#ifdef WIN32 |
| 360 |
unsigned codepage; /* codepage for WIN32 */ |
| 361 |
#endif |
| 362 |
} pg_enc2name; |
| 363 |
|
| 364 |
extern PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[]; |
| 365 |
|
| 366 |
/* |
| 367 |
* Encoding names for gettext |
| 368 |
*/ |
| 369 |
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 |
|
| 405 |
extern PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[]; |
| 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 |
|
| 488 |
} pg_mb_radix_tree; |
| 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 */ |
| 498 |
} pg_utf_to_local_combined; |
| 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 */ |
| 508 |
} pg_local_to_utf_combined; |
| 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 |
| 535 |
is_valid_unicode_codepoint(pg_wchar c) |
| 536 |
{ |
| 537 |
return (c > 0 && c <= 0x10FFFF); |
| 538 |
} |
| 539 |
|
| 540 |
static inline bool |
| 541 |
is_utf16_surrogate_first(pg_wchar c) |
| 542 |
{ |
| 543 |
return (c >= 0xD800 && c <= 0xDBFF); |
| 544 |
} |
| 545 |
|
| 546 |
static inline bool |
| 547 |
is_utf16_surrogate_second(pg_wchar c) |
| 548 |
{ |
| 549 |
return (c >= 0xDC00 && c <= 0xDFFF); |
| 550 |
} |
| 551 |
|
| 552 |
static inline pg_wchar |
| 553 |
surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second) |
| 554 |
{ |
| 555 |
return ((first & 0x3FF) << 10) + 0x10000 + (second & 0x3FF); |
| 556 |
} |
| 557 |
|
| 558 |
/* |
| 559 |
* Convert a UTF-8 character to a Unicode code point. |
| 560 |
* This is a one-character version of pg_utf2wchar_with_len. |
| 561 |
* |
| 562 |
* No error checks here, c must point to a long-enough string. |
| 563 |
*/ |
| 564 |
static inline pg_wchar |
| 565 |
utf8_to_unicode(const unsigned char *c) |
| 566 |
{ |
| 567 |
if ((*c & 0x80) == 0) |
| 568 |
return (pg_wchar) c[0]; |
| 569 |
else if ((*c & 0xe0) == 0xc0) |
| 570 |
return (pg_wchar) (((c[0] & 0x1f) << 6) | |
| 571 |
(c[1] & 0x3f)); |
| 572 |
else if ((*c & 0xf0) == 0xe0) |
| 573 |
return (pg_wchar) (((c[0] & 0x0f) << 12) | |
| 574 |
((c[1] & 0x3f) << 6) | |
| 575 |
(c[2] & 0x3f)); |
| 576 |
else if ((*c & 0xf8) == 0xf0) |
| 577 |
return (pg_wchar) (((c[0] & 0x07) << 18) | |
| 578 |
((c[1] & 0x3f) << 12) | |
| 579 |
((c[2] & 0x3f) << 6) | |
| 580 |
(c[3] & 0x3f)); |
| 581 |
else |
| 582 |
/* that is an invalid code on purpose */ |
| 583 |
return 0xffffffff; |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* Map a Unicode code point to UTF-8. utf8string must have at least |
| 588 |
* unicode_utf8len(c) bytes available. |
| 589 |
*/ |
| 590 |
static inline unsigned char * |
| 591 |
unicode_to_utf8(pg_wchar c, unsigned char *utf8string) |
| 592 |
{ |
| 593 |
if (c <= 0x7F) |
| 594 |
{ |
| 595 |
utf8string[0] = c; |
| 596 |
} |
| 597 |
else if (c <= 0x7FF) |
| 598 |
{ |
| 599 |
utf8string[0] = 0xC0 | ((c >> 6) & 0x1F); |
| 600 |
utf8string[1] = 0x80 | (c & 0x3F); |
| 601 |
} |
| 602 |
else if (c <= 0xFFFF) |
| 603 |
{ |
| 604 |
utf8string[0] = 0xE0 | ((c >> 12) & 0x0F); |
| 605 |
utf8string[1] = 0x80 | ((c >> 6) & 0x3F); |
| 606 |
utf8string[2] = 0x80 | (c & 0x3F); |
| 607 |
} |
| 608 |
else |
| 609 |
{ |
| 610 |
utf8string[0] = 0xF0 | ((c >> 18) & 0x07); |
| 611 |
utf8string[1] = 0x80 | ((c >> 12) & 0x3F); |
| 612 |
utf8string[2] = 0x80 | ((c >> 6) & 0x3F); |
| 613 |
utf8string[3] = 0x80 | (c & 0x3F); |
| 614 |
} |
| 615 |
|
| 616 |
return utf8string; |
| 617 |
} |
| 618 |
|
| 619 |
/* |
| 620 |
* Number of bytes needed to represent the given char in UTF8. |
| 621 |
*/ |
| 622 |
static inline int |
| 623 |
unicode_utf8len(pg_wchar c) |
| 624 |
{ |
| 625 |
if (c <= 0x7F) |
| 626 |
return 1; |
| 627 |
else if (c <= 0x7FF) |
| 628 |
return 2; |
| 629 |
else if (c <= 0xFFFF) |
| 630 |
return 3; |
| 631 |
else |
| 632 |
return 4; |
| 633 |
} |
| 634 |
|
| 635 |
/* |
| 636 |
* The functions in this list are exported by libpq, and we need to be sure |
| 637 |
* that we know which calls are satisfied by libpq and which are satisfied |
| 638 |
* by static linkage to libpgcommon. (This is because we might be using a |
| 639 |
* libpq.so that's of a different major version and has encoding IDs that |
| 640 |
* differ from the current version's.) The nominal function names are what |
| 641 |
* are actually used in and exported by libpq, while the names exported by |
| 642 |
* libpgcommon.a and libpgcommon_srv.a end in "_private". |
| 643 |
*/ |
| 644 |
#if defined(USE_PRIVATE_ENCODING_FUNCS) || !defined(FRONTEND) |
| 645 |
#define pg_char_to_encoding pg_char_to_encoding_private |
| 646 |
#define pg_encoding_to_char pg_encoding_to_char_private |
| 647 |
#define pg_valid_server_encoding pg_valid_server_encoding_private |
| 648 |
#define pg_valid_server_encoding_id pg_valid_server_encoding_id_private |
| 649 |
#define pg_utf_mblen pg_utf_mblen_private |
| 650 |
#endif |
| 651 |
|
| 652 |
/* |
| 653 |
* These functions are considered part of libpq's exported API and |
| 654 |
* are also declared in libpq-fe.h. |
| 655 |
*/ |
| 656 |
extern int pg_char_to_encoding(const char *name); |
| 657 |
extern const char *pg_encoding_to_char(int encoding); |
| 658 |
extern int pg_valid_server_encoding_id(int encoding); |
| 659 |
|
| 660 |
/* |
| 661 |
* These functions are available to frontend code that links with libpgcommon |
| 662 |
* (in addition to the ones just above). The constant tables declared |
| 663 |
* earlier in this file are also available from libpgcommon. |
| 664 |
*/ |
| 665 |
extern void pg_encoding_set_invalid(int encoding, char *dst); |
| 666 |
extern int pg_encoding_mblen(int encoding, const char *mbstr); |
| 667 |
extern int pg_encoding_mblen_or_incomplete(int encoding, const char *mbstr, |
| 668 |
size_t remaining); |
| 669 |
extern int pg_encoding_mblen_bounded(int encoding, const char *mbstr); |
| 670 |
extern int pg_encoding_dsplen(int encoding, const char *mbstr); |
| 671 |
extern int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len); |
| 672 |
extern int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len); |
| 673 |
extern int pg_encoding_max_length(int encoding); |
| 674 |
extern int pg_valid_client_encoding(const char *name); |
| 675 |
extern int pg_valid_server_encoding(const char *name); |
| 676 |
extern bool is_encoding_supported_by_icu(int encoding); |
| 677 |
extern const char *get_encoding_name_for_icu(int encoding); |
| 678 |
|
| 679 |
extern unsigned char *unicode_to_utf8(pg_wchar c, unsigned char *utf8string); |
| 680 |
extern pg_wchar utf8_to_unicode(const unsigned char *c); |
| 681 |
extern bool pg_utf8_islegal(const unsigned char *source, int length); |
| 682 |
extern int pg_utf_mblen(const unsigned char *s); |
| 683 |
extern int pg_mule_mblen(const unsigned char *s); |
| 684 |
|
| 685 |
/* |
| 686 |
* The remaining functions are backend-only. |
| 687 |
*/ |
| 688 |
extern int pg_mb2wchar(const char *from, pg_wchar *to); |
| 689 |
extern int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len); |
| 690 |
extern int pg_encoding_mb2wchar_with_len(int encoding, |
| 691 |
const char *from, pg_wchar *to, int len); |
| 692 |
extern int pg_wchar2mb(const pg_wchar *from, char *to); |
| 693 |
extern int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len); |
| 694 |
extern int pg_encoding_wchar2mb_with_len(int encoding, |
| 695 |
const pg_wchar *from, char *to, int len); |
| 696 |
extern int pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2); |
| 697 |
extern int pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n); |
| 698 |
extern int pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n); |
| 699 |
extern size_t pg_wchar_strlen(const pg_wchar *str); |
| 700 |
extern int pg_mblen_cstr(const char *mbstr); |
| 701 |
extern int pg_mblen_range(const char *mbstr, const char *end); |
| 702 |
extern int pg_mblen_with_len(const char *mbstr, int limit); |
| 703 |
extern int pg_mblen_unbounded(const char *mbstr); |
| 704 |
|
| 705 |
/* deprecated */ |
| 706 |
extern int pg_mblen(const char *mbstr); |
| 707 |
|
| 708 |
extern int pg_dsplen(const char *mbstr); |
| 709 |
extern int pg_mbstrlen(const char *mbstr); |
| 710 |
extern int pg_mbstrlen_with_len(const char *mbstr, int limit); |
| 711 |
extern int pg_mbcliplen(const char *mbstr, int len, int limit); |
| 712 |
extern int pg_encoding_mbcliplen(int encoding, const char *mbstr, |
| 713 |
int len, int limit); |
| 714 |
extern int pg_mbcharcliplen(const char *mbstr, int len, int limit); |
| 715 |
extern int pg_database_encoding_max_length(void); |
| 716 |
extern mbcharacter_incrementer pg_database_encoding_character_incrementer(void); |
| 717 |
|
| 718 |
extern int PrepareClientEncoding(int encoding); |
| 719 |
extern int SetClientEncoding(int encoding); |
| 720 |
extern void InitializeClientEncoding(void); |
| 721 |
extern int pg_get_client_encoding(void); |
| 722 |
extern const char *pg_get_client_encoding_name(void); |
| 723 |
|
| 724 |
extern void SetDatabaseEncoding(int encoding); |
| 725 |
extern int GetDatabaseEncoding(void); |
| 726 |
extern const char *GetDatabaseEncodingName(void); |
| 727 |
extern void SetMessageEncoding(int encoding); |
| 728 |
extern int GetMessageEncoding(void); |
| 729 |
|
| 730 |
#ifdef ENABLE_NLS |
| 731 |
extern int pg_bind_textdomain_codeset(const char *domainname); |
| 732 |
#endif |
| 733 |
|
| 734 |
extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len, |
| 735 |
int src_encoding, |
| 736 |
int dest_encoding); |
| 737 |
extern int pg_do_encoding_conversion_buf(Oid proc, |
| 738 |
int src_encoding, |
| 739 |
int dest_encoding, |
| 740 |
unsigned char *src, int srclen, |
| 741 |
unsigned char *dest, int destlen, |
| 742 |
bool noError); |
| 743 |
|
| 744 |
extern char *pg_client_to_server(const char *s, int len); |
| 745 |
extern char *pg_server_to_client(const char *s, int len); |
| 746 |
extern char *pg_any_to_server(const char *s, int len, int encoding); |
| 747 |
extern char *pg_server_to_any(const char *s, int len, int encoding); |
| 748 |
|
| 749 |
extern void pg_unicode_to_server(pg_wchar c, unsigned char *s); |
| 750 |
extern bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s); |
| 751 |
|
| 752 |
extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc); |
| 753 |
extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc); |
| 754 |
|
| 755 |
extern int UtfToLocal(const unsigned char *utf, int len, |
| 756 |
unsigned char *iso, |
| 757 |
const pg_mb_radix_tree *map, |
| 758 |
const pg_utf_to_local_combined *cmap, int cmapsize, |
| 759 |
utf_local_conversion_func conv_func, |
| 760 |
int encoding, bool noError); |
| 761 |
extern int LocalToUtf(const unsigned char *iso, int len, |
| 762 |
unsigned char *utf, |
| 763 |
const pg_mb_radix_tree *map, |
| 764 |
const pg_local_to_utf_combined *cmap, int cmapsize, |
| 765 |
utf_local_conversion_func conv_func, |
| 766 |
int encoding, bool noError); |
| 767 |
|
| 768 |
extern bool pg_verifymbstr(const char *mbstr, int len, bool noError); |
| 769 |
extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len, |
| 770 |
bool noError); |
| 771 |
extern int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, |
| 772 |
bool noError); |
| 773 |
|
| 774 |
extern void check_encoding_conversion_args(int src_encoding, |
| 775 |
int dest_encoding, |
| 776 |
int len, |
| 777 |
int expected_src_encoding, |
| 778 |
int expected_dest_encoding); |
| 779 |
|
| 780 |
pg_noreturn extern void report_invalid_encoding(int encoding, const char *mbstr, int len); |
| 781 |
pg_noreturn extern void report_untranslatable_char(int src_encoding, int dest_encoding, |
| 782 |
const char *mbstr, int len); |
| 783 |
|
| 784 |
extern int local2local(const unsigned char *l, unsigned char *p, int len, |
| 785 |
int src_encoding, int dest_encoding, |
| 786 |
const unsigned char *tab, bool noError); |
| 787 |
extern int latin2mic(const unsigned char *l, unsigned char *p, int len, |
| 788 |
int lc, int encoding, bool noError); |
| 789 |
extern int mic2latin(const unsigned char *mic, unsigned char *p, int len, |
| 790 |
int lc, int encoding, bool noError); |
| 791 |
extern int latin2mic_with_table(const unsigned char *l, unsigned char *p, |
| 792 |
int len, int lc, int encoding, |
| 793 |
const unsigned char *tab, bool noError); |
| 794 |
extern int mic2latin_with_table(const unsigned char *mic, unsigned char *p, |
| 795 |
int len, int lc, int encoding, |
| 796 |
const unsigned char *tab, bool noError); |
| 797 |
|
| 798 |
#ifdef WIN32 |
| 799 |
extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len); |
| 800 |
#endif |
| 801 |
|
| 802 |
#endif /* PG_WCHAR_H */ |