pg_wchar.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pg_wchar.h
  4. * multibyte-character support
  5. *
  6. * Portions Copyright (c) 1996-2022, 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. *
  17. *-------------------------------------------------------------------------
  18. */
  19. #ifndef PG_WCHAR_H
  20. #define PG_WCHAR_H
  21. /*
  22. * The pg_wchar type
  23. */
  24. typedef unsigned int pg_wchar;
  25. /*
  26. * Maximum byte length of multibyte characters in any backend encoding
  27. */
  28. #define MAX_MULTIBYTE_CHAR_LEN 4
  29. /*
  30. * various definitions for EUC
  31. */
  32. #define SS2 0x8e /* single shift 2 (JIS0201) */
  33. #define SS3 0x8f /* single shift 3 (JIS0212) */
  34. /*
  35. * SJIS validation macros
  36. */
  37. #define ISSJISHEAD(c) (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc))
  38. #define ISSJISTAIL(c) (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc))
  39. /*----------------------------------------------------
  40. * MULE Internal Encoding (MIC)
  41. *
  42. * This encoding follows the design used within XEmacs; it is meant to
  43. * subsume many externally-defined character sets. Each character includes
  44. * identification of the character set it belongs to, so the encoding is
  45. * general but somewhat bulky.
  46. *
  47. * Currently PostgreSQL supports 5 types of MULE character sets:
  48. *
  49. * 1) 1-byte ASCII characters. Each byte is below 0x80.
  50. *
  51. * 2) "Official" single byte charsets such as ISO-8859-1 (Latin1).
  52. * Each MULE character consists of 2 bytes: LC1 + C1, where LC1 is
  53. * an identifier for the charset (in the range 0x81 to 0x8d) and C1
  54. * is the character code (in the range 0xa0 to 0xff).
  55. *
  56. * 3) "Private" single byte charsets such as SISHENG. Each MULE
  57. * character consists of 3 bytes: LCPRV1 + LC12 + C1, where LCPRV1
  58. * is a private-charset flag, LC12 is an identifier for the charset,
  59. * and C1 is the character code (in the range 0xa0 to 0xff).
  60. * LCPRV1 is either 0x9a (if LC12 is in the range 0xa0 to 0xdf)
  61. * or 0x9b (if LC12 is in the range 0xe0 to 0xef).
  62. *
  63. * 4) "Official" multibyte charsets such as JIS X0208. Each MULE
  64. * character consists of 3 bytes: LC2 + C1 + C2, where LC2 is
  65. * an identifier for the charset (in the range 0x90 to 0x99) and C1
  66. * and C2 form the character code (each in the range 0xa0 to 0xff).
  67. *
  68. * 5) "Private" multibyte charsets such as CNS 11643-1992 Plane 3.
  69. * Each MULE character consists of 4 bytes: LCPRV2 + LC22 + C1 + C2,
  70. * where LCPRV2 is a private-charset flag, LC22 is an identifier for
  71. * the charset, and C1 and C2 form the character code (each in the range
  72. * 0xa0 to 0xff). LCPRV2 is either 0x9c (if LC22 is in the range 0xf0
  73. * to 0xf4) or 0x9d (if LC22 is in the range 0xf5 to 0xfe).
  74. *
  75. * "Official" encodings are those that have been assigned code numbers by
  76. * the XEmacs project; "private" encodings have Postgres-specific charset
  77. * identifiers.
  78. *
  79. * See the "XEmacs Internals Manual", available at http://www.xemacs.org,
  80. * for more details. Note that for historical reasons, Postgres'
  81. * private-charset flag values do not match what XEmacs says they should be,
  82. * so this isn't really exactly MULE (not that private charsets would be
  83. * interoperable anyway).
  84. *
  85. * Note that XEmacs's implementation is different from what emacs does.
  86. * We follow emacs's implementation, rather than XEmacs's.
  87. *----------------------------------------------------
  88. */
  89. /*
  90. * Charset identifiers (also called "leading bytes" in the MULE documentation)
  91. */
  92. /*
  93. * Charset IDs for official single byte encodings (0x81-0x8e)
  94. */
  95. #define LC_ISO8859_1 0x81 /* ISO8859 Latin 1 */
  96. #define LC_ISO8859_2 0x82 /* ISO8859 Latin 2 */
  97. #define LC_ISO8859_3 0x83 /* ISO8859 Latin 3 */
  98. #define LC_ISO8859_4 0x84 /* ISO8859 Latin 4 */
  99. #define LC_TIS620 0x85 /* Thai (not supported yet) */
  100. #define LC_ISO8859_7 0x86 /* Greek (not supported yet) */
  101. #define LC_ISO8859_6 0x87 /* Arabic (not supported yet) */
  102. #define LC_ISO8859_8 0x88 /* Hebrew (not supported yet) */
  103. #define LC_JISX0201K 0x89 /* Japanese 1 byte kana */
  104. #define LC_JISX0201R 0x8a /* Japanese 1 byte Roman */
  105. /* Note that 0x8b seems to be unused as of Emacs 20.7.
  106. * However, there might be a chance that 0x8b could be used
  107. * in later versions of Emacs.
  108. */
  109. #define LC_KOI8_R 0x8b /* Cyrillic KOI8-R */
  110. #define LC_ISO8859_5 0x8c /* ISO8859 Cyrillic */
  111. #define LC_ISO8859_9 0x8d /* ISO8859 Latin 5 (not supported yet) */
  112. #define LC_ISO8859_15 0x8e /* ISO8859 Latin 15 (not supported yet) */
  113. /* #define CONTROL_1 0x8f control characters (unused) */
  114. /* Is a leading byte for "official" single byte encodings? */
  115. #define IS_LC1(c) ((unsigned char)(c) >= 0x81 && (unsigned char)(c) <= 0x8d)
  116. /*
  117. * Charset IDs for official multibyte encodings (0x90-0x99)
  118. * 0x9a-0x9d are free. 0x9e and 0x9f are reserved.
  119. */
  120. #define LC_JISX0208_1978 0x90 /* Japanese Kanji, old JIS (not supported) */
  121. #define LC_GB2312_80 0x91 /* Chinese */
  122. #define LC_JISX0208 0x92 /* Japanese Kanji (JIS X 0208) */
  123. #define LC_KS5601 0x93 /* Korean */
  124. #define LC_JISX0212 0x94 /* Japanese Kanji (JIS X 0212) */
  125. #define LC_CNS11643_1 0x95 /* CNS 11643-1992 Plane 1 */
  126. #define LC_CNS11643_2 0x96 /* CNS 11643-1992 Plane 2 */
  127. #define LC_JISX0213_1 0x97 /* Japanese Kanji (JIS X 0213 Plane 1)
  128. * (not supported) */
  129. #define LC_BIG5_1 0x98 /* Plane 1 Chinese traditional (not
  130. * supported) */
  131. #define LC_BIG5_2 0x99 /* Plane 1 Chinese traditional (not
  132. * supported) */
  133. /* Is a leading byte for "official" multibyte encodings? */
  134. #define IS_LC2(c) ((unsigned char)(c) >= 0x90 && (unsigned char)(c) <= 0x99)
  135. /*
  136. * Postgres-specific prefix bytes for "private" single byte encodings
  137. * (According to the MULE docs, we should be using 0x9e for this)
  138. */
  139. #define LCPRV1_A 0x9a
  140. #define LCPRV1_B 0x9b
  141. #define IS_LCPRV1(c) ((unsigned char)(c) == LCPRV1_A || (unsigned char)(c) == LCPRV1_B)
  142. #define IS_LCPRV1_A_RANGE(c) \
  143. ((unsigned char)(c) >= 0xa0 && (unsigned char)(c) <= 0xdf)
  144. #define IS_LCPRV1_B_RANGE(c) \
  145. ((unsigned char)(c) >= 0xe0 && (unsigned char)(c) <= 0xef)
  146. /*
  147. * Postgres-specific prefix bytes for "private" multibyte encodings
  148. * (According to the MULE docs, we should be using 0x9f for this)
  149. */
  150. #define LCPRV2_A 0x9c
  151. #define LCPRV2_B 0x9d
  152. #define IS_LCPRV2(c) ((unsigned char)(c) == LCPRV2_A || (unsigned char)(c) == LCPRV2_B)
  153. #define IS_LCPRV2_A_RANGE(c) \
  154. ((unsigned char)(c) >= 0xf0 && (unsigned char)(c) <= 0xf4)
  155. #define IS_LCPRV2_B_RANGE(c) \
  156. ((unsigned char)(c) >= 0xf5 && (unsigned char)(c) <= 0xfe)
  157. /*
  158. * Charset IDs for private single byte encodings (0xa0-0xef)
  159. */
  160. #define LC_SISHENG 0xa0 /* Chinese SiSheng characters for
  161. * PinYin/ZhuYin (not supported) */
  162. #define LC_IPA 0xa1 /* IPA (International Phonetic
  163. * Association) (not supported) */
  164. #define LC_VISCII_LOWER 0xa2 /* Vietnamese VISCII1.1 lower-case (not
  165. * supported) */
  166. #define LC_VISCII_UPPER 0xa3 /* Vietnamese VISCII1.1 upper-case (not
  167. * supported) */
  168. #define LC_ARABIC_DIGIT 0xa4 /* Arabic digit (not supported) */
  169. #define LC_ARABIC_1_COLUMN 0xa5 /* Arabic 1-column (not supported) */
  170. #define LC_ASCII_RIGHT_TO_LEFT 0xa6 /* ASCII (left half of ISO8859-1) with
  171. * right-to-left direction (not
  172. * supported) */
  173. #define LC_LAO 0xa7 /* Lao characters (ISO10646 0E80..0EDF)
  174. * (not supported) */
  175. #define LC_ARABIC_2_COLUMN 0xa8 /* Arabic 1-column (not supported) */
  176. /*
  177. * Charset IDs for private multibyte encodings (0xf0-0xff)
  178. */
  179. #define LC_INDIAN_1_COLUMN 0xf0 /* Indian charset for 1-column width
  180. * glyphs (not supported) */
  181. #define LC_TIBETAN_1_COLUMN 0xf1 /* Tibetan 1-column width glyphs (not
  182. * supported) */
  183. #define LC_UNICODE_SUBSET_2 0xf2 /* Unicode characters of the range
  184. * U+2500..U+33FF. (not supported) */
  185. #define LC_UNICODE_SUBSET_3 0xf3 /* Unicode characters of the range
  186. * U+E000..U+FFFF. (not supported) */
  187. #define LC_UNICODE_SUBSET 0xf4 /* Unicode characters of the range
  188. * U+0100..U+24FF. (not supported) */
  189. #define LC_ETHIOPIC 0xf5 /* Ethiopic characters (not supported) */
  190. #define LC_CNS11643_3 0xf6 /* CNS 11643-1992 Plane 3 */
  191. #define LC_CNS11643_4 0xf7 /* CNS 11643-1992 Plane 4 */
  192. #define LC_CNS11643_5 0xf8 /* CNS 11643-1992 Plane 5 */
  193. #define LC_CNS11643_6 0xf9 /* CNS 11643-1992 Plane 6 */
  194. #define LC_CNS11643_7 0xfa /* CNS 11643-1992 Plane 7 */
  195. #define LC_INDIAN_2_COLUMN 0xfb /* Indian charset for 2-column width
  196. * glyphs (not supported) */
  197. #define LC_TIBETAN 0xfc /* Tibetan (not supported) */
  198. /* #define FREE 0xfd free (unused) */
  199. /* #define FREE 0xfe free (unused) */
  200. /* #define FREE 0xff free (unused) */
  201. /*----------------------------------------------------
  202. * end of MULE stuff
  203. *----------------------------------------------------
  204. */
  205. /*
  206. * PostgreSQL encoding identifiers
  207. *
  208. * WARNING: the order of this enum must be same as order of entries
  209. * in the pg_enc2name_tbl[] array (in src/common/encnames.c), and
  210. * in the pg_wchar_table[] array (in src/common/wchar.c)!
  211. *
  212. * If you add some encoding don't forget to check
  213. * PG_ENCODING_BE_LAST macro.
  214. *
  215. * PG_SQL_ASCII is default encoding and must be = 0.
  216. *
  217. * XXX We must avoid renumbering any backend encoding until libpq's major
  218. * version number is increased beyond 5; it turns out that the backend
  219. * encoding IDs are effectively part of libpq's ABI as far as 8.2 initdb and
  220. * psql are concerned.
  221. */
  222. typedef enum pg_enc
  223. {
  224. PG_SQL_ASCII = 0, /* SQL/ASCII */
  225. PG_EUC_JP, /* EUC for Japanese */
  226. PG_EUC_CN, /* EUC for Chinese */
  227. PG_EUC_KR, /* EUC for Korean */
  228. PG_EUC_TW, /* EUC for Taiwan */
  229. PG_EUC_JIS_2004, /* EUC-JIS-2004 */
  230. PG_UTF8, /* Unicode UTF8 */
  231. PG_MULE_INTERNAL, /* Mule internal code */
  232. PG_LATIN1, /* ISO-8859-1 Latin 1 */
  233. PG_LATIN2, /* ISO-8859-2 Latin 2 */
  234. PG_LATIN3, /* ISO-8859-3 Latin 3 */
  235. PG_LATIN4, /* ISO-8859-4 Latin 4 */
  236. PG_LATIN5, /* ISO-8859-9 Latin 5 */
  237. PG_LATIN6, /* ISO-8859-10 Latin6 */
  238. PG_LATIN7, /* ISO-8859-13 Latin7 */
  239. PG_LATIN8, /* ISO-8859-14 Latin8 */
  240. PG_LATIN9, /* ISO-8859-15 Latin9 */
  241. PG_LATIN10, /* ISO-8859-16 Latin10 */
  242. PG_WIN1256, /* windows-1256 */
  243. PG_WIN1258, /* Windows-1258 */
  244. PG_WIN866, /* (MS-DOS CP866) */
  245. PG_WIN874, /* windows-874 */
  246. PG_KOI8R, /* KOI8-R */
  247. PG_WIN1251, /* windows-1251 */
  248. PG_WIN1252, /* windows-1252 */
  249. PG_ISO_8859_5, /* ISO-8859-5 */
  250. PG_ISO_8859_6, /* ISO-8859-6 */
  251. PG_ISO_8859_7, /* ISO-8859-7 */
  252. PG_ISO_8859_8, /* ISO-8859-8 */
  253. PG_WIN1250, /* windows-1250 */
  254. PG_WIN1253, /* windows-1253 */
  255. PG_WIN1254, /* windows-1254 */
  256. PG_WIN1255, /* windows-1255 */
  257. PG_WIN1257, /* windows-1257 */
  258. PG_KOI8U, /* KOI8-U */
  259. /* PG_ENCODING_BE_LAST points to the above entry */
  260. /* followings are for client encoding only */
  261. PG_SJIS, /* Shift JIS (Windows-932) */
  262. PG_BIG5, /* Big5 (Windows-950) */
  263. PG_GBK, /* GBK (Windows-936) */
  264. PG_UHC, /* UHC (Windows-949) */
  265. PG_GB18030, /* GB18030 */
  266. PG_JOHAB, /* EUC for Korean JOHAB */
  267. PG_SHIFT_JIS_2004, /* Shift-JIS-2004 */
  268. _PG_LAST_ENCODING_ /* mark only */
  269. } pg_enc;
  270. #define PG_ENCODING_BE_LAST PG_KOI8U
  271. /*
  272. * Please use these tests before access to pg_enc2name_tbl[]
  273. * or to other places...
  274. */
  275. #define PG_VALID_BE_ENCODING(_enc) \
  276. ((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST)
  277. #define PG_ENCODING_IS_CLIENT_ONLY(_enc) \
  278. ((_enc) > PG_ENCODING_BE_LAST && (_enc) < _PG_LAST_ENCODING_)
  279. #define PG_VALID_ENCODING(_enc) \
  280. ((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_)
  281. /* On FE are possible all encodings */
  282. #define PG_VALID_FE_ENCODING(_enc) PG_VALID_ENCODING(_enc)
  283. /*
  284. * When converting strings between different encodings, we assume that space
  285. * for converted result is 4-to-1 growth in the worst case. The rate for
  286. * currently supported encoding pairs are within 3 (SJIS JIS X0201 half width
  287. * kana -> UTF8 is the worst case). So "4" should be enough for the moment.
  288. *
  289. * Note that this is not the same as the maximum character width in any
  290. * particular encoding.
  291. */
  292. #define MAX_CONVERSION_GROWTH 4
  293. /*
  294. * Maximum byte length of a string that's required in any encoding to convert
  295. * at least one character to any other encoding. In other words, if you feed
  296. * MAX_CONVERSION_INPUT_LENGTH bytes to any encoding conversion function, it
  297. * is guaranteed to be able to convert something without needing more input
  298. * (assuming the input is valid).
  299. *
  300. * Currently, the maximum case is the conversion UTF8 -> SJIS JIS X0201 half
  301. * width kana, where a pair of UTF-8 characters is converted into a single
  302. * SHIFT_JIS_2004 character (the reverse of the worst case for
  303. * MAX_CONVERSION_GROWTH). It needs 6 bytes of input. In theory, a
  304. * user-defined conversion function might have more complicated cases, although
  305. * for the reverse mapping you would probably also need to bump up
  306. * MAX_CONVERSION_GROWTH. But there is no need to be stingy here, so make it
  307. * generous.
  308. */
  309. #define MAX_CONVERSION_INPUT_LENGTH 16
  310. /*
  311. * Maximum byte length of the string equivalent to any one Unicode code point,
  312. * in any backend encoding. The current value assumes that a 4-byte UTF-8
  313. * character might expand by MAX_CONVERSION_GROWTH, which is a huge
  314. * overestimate. But in current usage we don't allocate large multiples of
  315. * this, so there's little point in being stingy.
  316. */
  317. #define MAX_UNICODE_EQUIVALENT_STRING 16
  318. /*
  319. * Table for mapping an encoding number to official encoding name and
  320. * possibly other subsidiary data. Be careful to check encoding number
  321. * before accessing a table entry!
  322. *
  323. * if (PG_VALID_ENCODING(encoding))
  324. * pg_enc2name_tbl[ encoding ];
  325. */
  326. typedef struct pg_enc2name
  327. {
  328. const char *name;
  329. pg_enc encoding;
  330. #ifdef WIN32
  331. unsigned codepage; /* codepage for WIN32 */
  332. #endif
  333. } pg_enc2name;
  334. extern PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[];
  335. /*
  336. * Encoding names for gettext
  337. */
  338. typedef struct pg_enc2gettext
  339. {
  340. pg_enc encoding;
  341. const char *name;
  342. } pg_enc2gettext;
  343. extern PGDLLIMPORT const pg_enc2gettext pg_enc2gettext_tbl[];
  344. /*
  345. * pg_wchar stuff
  346. */
  347. typedef int (*mb2wchar_with_len_converter) (const unsigned char *from,
  348. pg_wchar *to,
  349. int len);
  350. typedef int (*wchar2mb_with_len_converter) (const pg_wchar *from,
  351. unsigned char *to,
  352. int len);
  353. typedef int (*mblen_converter) (const unsigned char *mbstr);
  354. typedef int (*mbdisplaylen_converter) (const unsigned char *mbstr);
  355. typedef bool (*mbcharacter_incrementer) (unsigned char *mbstr, int len);
  356. typedef int (*mbchar_verifier) (const unsigned char *mbstr, int len);
  357. typedef int (*mbstr_verifier) (const unsigned char *mbstr, int len);
  358. typedef struct
  359. {
  360. mb2wchar_with_len_converter mb2wchar_with_len; /* convert a multibyte
  361. * string to a wchar */
  362. wchar2mb_with_len_converter wchar2mb_with_len; /* convert a wchar string
  363. * to a multibyte */
  364. mblen_converter mblen; /* get byte length of a char */
  365. mbdisplaylen_converter dsplen; /* get display width of a char */
  366. mbchar_verifier mbverifychar; /* verify multibyte character */
  367. mbstr_verifier mbverifystr; /* verify multibyte string */
  368. int maxmblen; /* max bytes for a char in this encoding */
  369. } pg_wchar_tbl;
  370. extern PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[];
  371. /*
  372. * Data structures for conversions between UTF-8 and other encodings
  373. * (UtfToLocal() and LocalToUtf()). In these data structures, characters of
  374. * either encoding are represented by uint32 words; hence we can only support
  375. * characters up to 4 bytes long. For example, the byte sequence 0xC2 0x89
  376. * would be represented by 0x0000C289, and 0xE8 0xA2 0xB4 by 0x00E8A2B4.
  377. *
  378. * There are three possible ways a character can be mapped:
  379. *
  380. * 1. Using a radix tree, from source to destination code.
  381. * 2. Using a sorted array of source -> destination code pairs. This
  382. * method is used for "combining" characters. There are so few of
  383. * them that building a radix tree would be wasteful.
  384. * 3. Using a conversion function.
  385. */
  386. /*
  387. * Radix tree for character conversion.
  388. *
  389. * Logically, this is actually four different radix trees, for 1-byte,
  390. * 2-byte, 3-byte and 4-byte inputs. The 1-byte tree is a simple lookup
  391. * table from source to target code. The 2-byte tree consists of two levels:
  392. * one lookup table for the first byte, where the value in the lookup table
  393. * points to a lookup table for the second byte. And so on.
  394. *
  395. * Physically, all the trees are stored in one big array, in 'chars16' or
  396. * 'chars32', depending on the maximum value that needs to be represented. For
  397. * each level in each tree, we also store lower and upper bound of allowed
  398. * values - values outside those bounds are considered invalid, and are left
  399. * out of the tables.
  400. *
  401. * In the intermediate levels of the trees, the values stored are offsets
  402. * into the chars[16|32] array.
  403. *
  404. * In the beginning of the chars[16|32] array, there is always a number of
  405. * zeros, so that you safely follow an index from an intermediate table
  406. * without explicitly checking for a zero. Following a zero any number of
  407. * times will always bring you to the dummy, all-zeros table in the
  408. * beginning. This helps to shave some cycles when looking up values.
  409. */
  410. typedef struct
  411. {
  412. /*
  413. * Array containing all the values. Only one of chars16 or chars32 is
  414. * used, depending on how wide the values we need to represent are.
  415. */
  416. const uint16 *chars16;
  417. const uint32 *chars32;
  418. /* Radix tree for 1-byte inputs */
  419. uint32 b1root; /* offset of table in the chars[16|32] array */
  420. uint8 b1_lower; /* min allowed value for a single byte input */
  421. uint8 b1_upper; /* max allowed value for a single byte input */
  422. /* Radix tree for 2-byte inputs */
  423. uint32 b2root; /* offset of 1st byte's table */
  424. uint8 b2_1_lower; /* min/max allowed value for 1st input byte */
  425. uint8 b2_1_upper;
  426. uint8 b2_2_lower; /* min/max allowed value for 2nd input byte */
  427. uint8 b2_2_upper;
  428. /* Radix tree for 3-byte inputs */
  429. uint32 b3root; /* offset of 1st byte's table */
  430. uint8 b3_1_lower; /* min/max allowed value for 1st input byte */
  431. uint8 b3_1_upper;
  432. uint8 b3_2_lower; /* min/max allowed value for 2nd input byte */
  433. uint8 b3_2_upper;
  434. uint8 b3_3_lower; /* min/max allowed value for 3rd input byte */
  435. uint8 b3_3_upper;
  436. /* Radix tree for 4-byte inputs */
  437. uint32 b4root; /* offset of 1st byte's table */
  438. uint8 b4_1_lower; /* min/max allowed value for 1st input byte */
  439. uint8 b4_1_upper;
  440. uint8 b4_2_lower; /* min/max allowed value for 2nd input byte */
  441. uint8 b4_2_upper;
  442. uint8 b4_3_lower; /* min/max allowed value for 3rd input byte */
  443. uint8 b4_3_upper;
  444. uint8 b4_4_lower; /* min/max allowed value for 4th input byte */
  445. uint8 b4_4_upper;
  446. } pg_mb_radix_tree;
  447. /*
  448. * UTF-8 to local code conversion map (for combined characters)
  449. */
  450. typedef struct
  451. {
  452. uint32 utf1; /* UTF-8 code 1 */
  453. uint32 utf2; /* UTF-8 code 2 */
  454. uint32 code; /* local code */
  455. } pg_utf_to_local_combined;
  456. /*
  457. * local code to UTF-8 conversion map (for combined characters)
  458. */
  459. typedef struct
  460. {
  461. uint32 code; /* local code */
  462. uint32 utf1; /* UTF-8 code 1 */
  463. uint32 utf2; /* UTF-8 code 2 */
  464. } pg_local_to_utf_combined;
  465. /*
  466. * callback function for algorithmic encoding conversions (in either direction)
  467. *
  468. * if function returns zero, it does not know how to convert the code
  469. */
  470. typedef uint32 (*utf_local_conversion_func) (uint32 code);
  471. /*
  472. * Support macro for encoding conversion functions to validate their
  473. * arguments. (This could be made more compact if we included fmgr.h
  474. * here, but we don't want to do that because this header file is also
  475. * used by frontends.)
  476. */
  477. #define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
  478. check_encoding_conversion_args(PG_GETARG_INT32(0), \
  479. PG_GETARG_INT32(1), \
  480. PG_GETARG_INT32(4), \
  481. (srcencoding), \
  482. (destencoding))
  483. /*
  484. * Some handy functions for Unicode-specific tests.
  485. */
  486. static inline bool
  487. is_valid_unicode_codepoint(pg_wchar c)
  488. {
  489. return (c > 0 && c <= 0x10FFFF);
  490. }
  491. static inline bool
  492. is_utf16_surrogate_first(pg_wchar c)
  493. {
  494. return (c >= 0xD800 && c <= 0xDBFF);
  495. }
  496. static inline bool
  497. is_utf16_surrogate_second(pg_wchar c)
  498. {
  499. return (c >= 0xDC00 && c <= 0xDFFF);
  500. }
  501. static inline pg_wchar
  502. surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second)
  503. {
  504. return ((first & 0x3FF) << 10) + 0x10000 + (second & 0x3FF);
  505. }
  506. /*
  507. * These functions are considered part of libpq's exported API and
  508. * are also declared in libpq-fe.h.
  509. */
  510. extern int pg_char_to_encoding(const char *name);
  511. extern const char *pg_encoding_to_char(int encoding);
  512. extern int pg_valid_server_encoding_id(int encoding);
  513. /*
  514. * These functions are available to frontend code that links with libpgcommon
  515. * (in addition to the ones just above). The constant tables declared
  516. * earlier in this file are also available from libpgcommon.
  517. */
  518. extern int pg_encoding_mblen(int encoding, const char *mbstr);
  519. extern int pg_encoding_mblen_bounded(int encoding, const char *mbstr);
  520. extern int pg_encoding_dsplen(int encoding, const char *mbstr);
  521. extern int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len);
  522. extern int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len);
  523. extern int pg_encoding_max_length(int encoding);
  524. extern int pg_valid_client_encoding(const char *name);
  525. extern int pg_valid_server_encoding(const char *name);
  526. extern bool is_encoding_supported_by_icu(int encoding);
  527. extern const char *get_encoding_name_for_icu(int encoding);
  528. extern unsigned char *unicode_to_utf8(pg_wchar c, unsigned char *utf8string);
  529. extern pg_wchar utf8_to_unicode(const unsigned char *c);
  530. extern bool pg_utf8_islegal(const unsigned char *source, int length);
  531. extern int pg_utf_mblen(const unsigned char *s);
  532. extern int pg_mule_mblen(const unsigned char *s);
  533. /*
  534. * The remaining functions are backend-only.
  535. */
  536. extern int pg_mb2wchar(const char *from, pg_wchar *to);
  537. extern int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
  538. extern int pg_encoding_mb2wchar_with_len(int encoding,
  539. const char *from, pg_wchar *to, int len);
  540. extern int pg_wchar2mb(const pg_wchar *from, char *to);
  541. extern int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
  542. extern int pg_encoding_wchar2mb_with_len(int encoding,
  543. const pg_wchar *from, char *to, int len);
  544. extern int pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
  545. extern int pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n);
  546. extern int pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n);
  547. extern size_t pg_wchar_strlen(const pg_wchar *wstr);
  548. extern int pg_mblen(const char *mbstr);
  549. extern int pg_dsplen(const char *mbstr);
  550. extern int pg_mbstrlen(const char *mbstr);
  551. extern int pg_mbstrlen_with_len(const char *mbstr, int len);
  552. extern int pg_mbcliplen(const char *mbstr, int len, int limit);
  553. extern int pg_encoding_mbcliplen(int encoding, const char *mbstr,
  554. int len, int limit);
  555. extern int pg_mbcharcliplen(const char *mbstr, int len, int limit);
  556. extern int pg_database_encoding_max_length(void);
  557. extern mbcharacter_incrementer pg_database_encoding_character_incrementer(void);
  558. extern int PrepareClientEncoding(int encoding);
  559. extern int SetClientEncoding(int encoding);
  560. extern void InitializeClientEncoding(void);
  561. extern int pg_get_client_encoding(void);
  562. extern const char *pg_get_client_encoding_name(void);
  563. extern void SetDatabaseEncoding(int encoding);
  564. extern int GetDatabaseEncoding(void);
  565. extern const char *GetDatabaseEncodingName(void);
  566. extern void SetMessageEncoding(int encoding);
  567. extern int GetMessageEncoding(void);
  568. #ifdef ENABLE_NLS
  569. extern int pg_bind_textdomain_codeset(const char *domainname);
  570. #endif
  571. extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
  572. int src_encoding,
  573. int dest_encoding);
  574. extern int pg_do_encoding_conversion_buf(Oid proc,
  575. int src_encoding,
  576. int dest_encoding,
  577. unsigned char *src, int srclen,
  578. unsigned char *dst, int dstlen,
  579. bool noError);
  580. extern char *pg_client_to_server(const char *s, int len);
  581. extern char *pg_server_to_client(const char *s, int len);
  582. extern char *pg_any_to_server(const char *s, int len, int encoding);
  583. extern char *pg_server_to_any(const char *s, int len, int encoding);
  584. extern void pg_unicode_to_server(pg_wchar c, unsigned char *s);
  585. extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
  586. extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
  587. extern int UtfToLocal(const unsigned char *utf, int len,
  588. unsigned char *iso,
  589. const pg_mb_radix_tree *map,
  590. const pg_utf_to_local_combined *cmap, int cmapsize,
  591. utf_local_conversion_func conv_func,
  592. int encoding, bool noError);
  593. extern int LocalToUtf(const unsigned char *iso, int len,
  594. unsigned char *utf,
  595. const pg_mb_radix_tree *map,
  596. const pg_local_to_utf_combined *cmap, int cmapsize,
  597. utf_local_conversion_func conv_func,
  598. int encoding, bool noError);
  599. extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
  600. extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
  601. bool noError);
  602. extern int pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
  603. bool noError);
  604. extern void check_encoding_conversion_args(int src_encoding,
  605. int dest_encoding,
  606. int len,
  607. int expected_src_encoding,
  608. int expected_dest_encoding);
  609. extern void report_invalid_encoding(int encoding, const char *mbstr, int len) pg_attribute_noreturn();
  610. extern void report_untranslatable_char(int src_encoding, int dest_encoding,
  611. const char *mbstr, int len) pg_attribute_noreturn();
  612. extern int local2local(const unsigned char *l, unsigned char *p, int len,
  613. int src_encoding, int dest_encoding,
  614. const unsigned char *tab, bool noError);
  615. extern int latin2mic(const unsigned char *l, unsigned char *p, int len,
  616. int lc, int encoding, bool noError);
  617. extern int mic2latin(const unsigned char *mic, unsigned char *p, int len,
  618. int lc, int encoding, bool noError);
  619. extern int latin2mic_with_table(const unsigned char *l, unsigned char *p,
  620. int len, int lc, int encoding,
  621. const unsigned char *tab, bool noError);
  622. extern int mic2latin_with_table(const unsigned char *mic, unsigned char *p,
  623. int len, int lc, int encoding,
  624. const unsigned char *tab, bool noError);
  625. #ifdef WIN32
  626. extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
  627. #endif
  628. /*
  629. * Verify a chunk of bytes for valid ASCII.
  630. *
  631. * Returns false if the input contains any zero bytes or bytes with the
  632. * high-bit set. Input len must be a multiple of 8.
  633. */
  634. static inline bool
  635. is_valid_ascii(const unsigned char *s, int len)
  636. {
  637. uint64 chunk,
  638. highbit_cum = UINT64CONST(0),
  639. zero_cum = UINT64CONST(0x8080808080808080);
  640. Assert(len % sizeof(chunk) == 0);
  641. while (len > 0)
  642. {
  643. memcpy(&chunk, s, sizeof(chunk));
  644. /*
  645. * Capture any zero bytes in this chunk.
  646. *
  647. * First, add 0x7f to each byte. This sets the high bit in each byte,
  648. * unless it was a zero. If any resulting high bits are zero, the
  649. * corresponding high bits in the zero accumulator will be cleared.
  650. *
  651. * If none of the bytes in the chunk had the high bit set, the max
  652. * value each byte can have after the addition is 0x7f + 0x7f = 0xfe,
  653. * and we don't need to worry about carrying over to the next byte. If
  654. * any input bytes did have the high bit set, it doesn't matter
  655. * because we check for those separately.
  656. */
  657. zero_cum &= (chunk + UINT64CONST(0x7f7f7f7f7f7f7f7f));
  658. /* Capture any set bits in this chunk. */
  659. highbit_cum |= chunk;
  660. s += sizeof(chunk);
  661. len -= sizeof(chunk);
  662. }
  663. /* Check if any high bits in the high bit accumulator got set. */
  664. if (highbit_cum & UINT64CONST(0x8080808080808080))
  665. return false;
  666. /* Check if any high bits in the zero accumulator got cleared. */
  667. if (zero_cum != UINT64CONST(0x8080808080808080))
  668. return false;
  669. return true;
  670. }
  671. #endif /* PG_WCHAR_H */