2 * conversion functions between pg_wchar and multibyte streams.
4 * src/backend/utils/mb/wchar.c
7 /* can be used in either frontend or backend */
9 #include "postgres_fe.h"
14 #include "mb/pg_wchar.h"
18 * Operations on multi-byte encodings are driven by a table of helper
21 * To add an encoding support, define mblen(), dsplen() and verifier() for
22 * the encoding. For server-encodings, also define mb2wchar() and wchar2mb()
23 * conversion functions.
25 * These functions generally assume that their input is validly formed.
26 * The "verifier" functions, further down in the file, have to be more
29 * We expect that mblen() does not need to examine more than the first byte
30 * of the character to discover the correct length. GB18030 is an exception
31 * to that rule, though, as it also looks at second byte. But even that
32 * behaves in a predictable way, if you only pass the first byte: it will
33 * treat 4-byte encoded characters as two 2-byte encoded characters, which is
34 * good enough for all current uses.
36 * Note: for the display output of psql to work properly, the return values
37 * of the dsplen functions must conform to the Unicode standard. In particular
38 * the NUL character is zero width and control characters are generally
39 * width -1. It is recommended that non-ASCII encodings refer their ASCII
40 * subset to the ASCII routines to ensure consistency.
47 pg_ascii2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
51 while (len > 0 && *from)
62 pg_ascii_mblen(const unsigned char *s)
68 pg_ascii_dsplen(const unsigned char *s)
72 if (*s < 0x20 || *s == 0x7f)
82 pg_euc2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
86 while (len > 0 && *from)
88 if (*from == SS2 && len >= 2) /* JIS X 0201 (so called "1 byte
92 *to = (SS2 << 8) | *from++;
95 else if (*from == SS3 && len >= 3) /* JIS X 0212 KANJI */
98 *to = (SS3 << 16) | (*from++ << 8);
102 else if (IS_HIGHBIT_SET(*from) && len >= 2) /* JIS X 0208 KANJI */
108 else /* must be ASCII */
121 pg_euc_mblen(const unsigned char *s)
129 else if (IS_HIGHBIT_SET(*s))
137 pg_euc_dsplen(const unsigned char *s)
145 else if (IS_HIGHBIT_SET(*s))
148 len = pg_ascii_dsplen(s);
156 pg_eucjp2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
158 return pg_euc2wchar_with_len(from, to, len);
162 pg_eucjp_mblen(const unsigned char *s)
164 return pg_euc_mblen(s);
168 pg_eucjp_dsplen(const unsigned char *s)
176 else if (IS_HIGHBIT_SET(*s))
179 len = pg_ascii_dsplen(s);
187 pg_euckr2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
189 return pg_euc2wchar_with_len(from, to, len);
193 pg_euckr_mblen(const unsigned char *s)
195 return pg_euc_mblen(s);
199 pg_euckr_dsplen(const unsigned char *s)
201 return pg_euc_dsplen(s);
209 pg_euccn2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
213 while (len > 0 && *from)
215 if (*from == SS2 && len >= 3) /* code set 2 (unused?) */
218 *to = (SS2 << 16) | (*from++ << 8);
222 else if (*from == SS3 && len >= 3) /* code set 3 (unused ?) */
225 *to = (SS3 << 16) | (*from++ << 8);
229 else if (IS_HIGHBIT_SET(*from) && len >= 2) /* code set 1 */
248 pg_euccn_mblen(const unsigned char *s)
252 if (IS_HIGHBIT_SET(*s))
260 pg_euccn_dsplen(const unsigned char *s)
264 if (IS_HIGHBIT_SET(*s))
267 len = pg_ascii_dsplen(s);
276 pg_euctw2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
280 while (len > 0 && *from)
282 if (*from == SS2 && len >= 4) /* code set 2 */
285 *to = (((uint32) SS2) << 24) | (*from++ << 16);
290 else if (*from == SS3 && len >= 3) /* code set 3 (unused?) */
293 *to = (SS3 << 16) | (*from++ << 8);
297 else if (IS_HIGHBIT_SET(*from) && len >= 2) /* code set 2 */
316 pg_euctw_mblen(const unsigned char *s)
324 else if (IS_HIGHBIT_SET(*s))
332 pg_euctw_dsplen(const unsigned char *s)
340 else if (IS_HIGHBIT_SET(*s))
343 len = pg_ascii_dsplen(s);
348 * Convert pg_wchar to EUC_* encoding.
349 * caller must allocate enough space for "to", including a trailing zero!
350 * len: length of from.
351 * "from" not necessarily null terminated.
354 pg_wchar2euc_with_len(const pg_wchar *from, unsigned char *to, int len)
358 while (len > 0 && *from)
362 if ((c = (*from >> 24)))
365 *to++ = (*from >> 16) & 0xff;
366 *to++ = (*from >> 8) & 0xff;
367 *to++ = *from & 0xff;
370 else if ((c = (*from >> 16)))
373 *to++ = (*from >> 8) & 0xff;
374 *to++ = *from & 0xff;
377 else if ((c = (*from >> 8)))
380 *to++ = *from & 0xff;
400 pg_johab_mblen(const unsigned char *s)
402 return pg_euc_mblen(s);
406 pg_johab_dsplen(const unsigned char *s)
408 return pg_euc_dsplen(s);
412 * convert UTF8 string to pg_wchar (UCS-4)
413 * caller must allocate enough space for "to", including a trailing zero!
414 * len: length of from.
415 * "from" not necessarily null terminated.
418 pg_utf2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
426 while (len > 0 && *from)
428 if ((*from & 0x80) == 0)
433 else if ((*from & 0xe0) == 0xc0)
436 break; /* drop trailing incomplete char */
439 *to = (c1 << 6) | c2;
442 else if ((*from & 0xf0) == 0xe0)
445 break; /* drop trailing incomplete char */
449 *to = (c1 << 12) | (c2 << 6) | c3;
452 else if ((*from & 0xf8) == 0xf0)
455 break; /* drop trailing incomplete char */
460 *to = (c1 << 18) | (c2 << 12) | (c3 << 6) | c4;
465 /* treat a bogus char as length 1; not ours to raise error */
478 * Map a Unicode code point to UTF-8. utf8string must have 4 bytes of
482 unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
490 utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
491 utf8string[1] = 0x80 | (c & 0x3F);
493 else if (c <= 0xFFFF)
495 utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
496 utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
497 utf8string[2] = 0x80 | (c & 0x3F);
501 utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
502 utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
503 utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
504 utf8string[3] = 0x80 | (c & 0x3F);
511 * Trivial conversion from pg_wchar to UTF-8.
512 * caller should allocate enough space for "to"
513 * len: length of from.
514 * "from" not necessarily null terminated.
517 pg_wchar2utf_with_len(const pg_wchar *from, unsigned char *to, int len)
521 while (len > 0 && *from)
525 unicode_to_utf8(*from, to);
526 char_len = pg_utf_mblen(to);
537 * Return the byte length of a UTF8 character pointed to by s
539 * Note: in the current implementation we do not support UTF8 sequences
540 * of more than 4 bytes; hence do NOT return a value larger than 4.
541 * We return "1" for any leading byte that is either flat-out illegal or
542 * indicates a length larger than we support.
544 * pg_utf2wchar_with_len(), utf8_to_unicode(), pg_utf8_islegal(), and perhaps
545 * other places would need to be fixed to change this.
548 pg_utf_mblen(const unsigned char *s)
552 if ((*s & 0x80) == 0)
554 else if ((*s & 0xe0) == 0xc0)
556 else if ((*s & 0xf0) == 0xe0)
558 else if ((*s & 0xf8) == 0xf0)
561 else if ((*s & 0xfc) == 0xf8)
563 else if ((*s & 0xfe) == 0xfc)
572 * This is an implementation of wcwidth() and wcswidth() as defined in
573 * "The Single UNIX Specification, Version 2, The Open Group, 1997"
574 * <http://www.unix.org/online.html>
576 * Markus Kuhn -- 2001-09-08 -- public domain
578 * customised for PostgreSQL
580 * original available at : http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
585 unsigned short first;
589 /* auxiliary function for binary search in interval table */
591 mbbisearch(pg_wchar ucs, const struct mbinterval *table, int max)
596 if (ucs < table[0].first || ucs > table[max].last)
600 mid = (min + max) / 2;
601 if (ucs > table[mid].last)
603 else if (ucs < table[mid].first)
613 /* The following functions define the column width of an ISO 10646
614 * character as follows:
616 * - The null character (U+0000) has a column width of 0.
618 * - Other C0/C1 control characters and DEL will lead to a return
621 * - Non-spacing and enclosing combining characters (general
622 * category code Mn or Me in the Unicode database) have a
625 * - Other format characters (general category code Cf in the Unicode
626 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
628 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
629 * have a column width of 0.
631 * - Spacing characters in the East Asian Wide (W) or East Asian
632 * FullWidth (F) category as defined in Unicode Technical
633 * Report #11 have a column width of 2.
635 * - All remaining characters (including all printable
636 * ISO 8859-1 and WGL4 characters, Unicode control characters,
637 * etc.) have a column width of 1.
639 * This implementation assumes that wchar_t characters are encoded
644 ucs_wcwidth(pg_wchar ucs)
646 /* sorted list of non-overlapping intervals of non-spacing characters */
647 static const struct mbinterval combining[] = {
648 {0x0300, 0x036F}, {0x0483, 0x0489}, {0x0591, 0x05BD},
649 {0x05BF, 0x05BF}, {0x05C1, 0x05C2}, {0x05C4, 0x05C5},
650 {0x05C7, 0x05C7}, {0x0610, 0x061A}, {0x064B, 0x065F},
651 {0x0670, 0x0670}, {0x06D6, 0x06DC}, {0x06DF, 0x06E4},
652 {0x06E7, 0x06E8}, {0x06EA, 0x06ED}, {0x0711, 0x0711},
653 {0x0730, 0x074A}, {0x07A6, 0x07B0}, {0x07EB, 0x07F3},
654 {0x07FD, 0x07FD}, {0x0816, 0x0819}, {0x081B, 0x0823},
655 {0x0825, 0x0827}, {0x0829, 0x082D}, {0x0859, 0x085B},
656 {0x08D3, 0x08E1}, {0x08E3, 0x0902}, {0x093A, 0x093A},
657 {0x093C, 0x093C}, {0x0941, 0x0948}, {0x094D, 0x094D},
658 {0x0951, 0x0957}, {0x0962, 0x0963}, {0x0981, 0x0981},
659 {0x09BC, 0x09BC}, {0x09C1, 0x09C4}, {0x09CD, 0x09CD},
660 {0x09E2, 0x09E3}, {0x09FE, 0x0A02}, {0x0A3C, 0x0A3C},
661 {0x0A41, 0x0A51}, {0x0A70, 0x0A71}, {0x0A75, 0x0A75},
662 {0x0A81, 0x0A82}, {0x0ABC, 0x0ABC}, {0x0AC1, 0x0AC8},
663 {0x0ACD, 0x0ACD}, {0x0AE2, 0x0AE3}, {0x0AFA, 0x0B01},
664 {0x0B3C, 0x0B3C}, {0x0B3F, 0x0B3F}, {0x0B41, 0x0B44},
665 {0x0B4D, 0x0B56}, {0x0B62, 0x0B63}, {0x0B82, 0x0B82},
666 {0x0BC0, 0x0BC0}, {0x0BCD, 0x0BCD}, {0x0C00, 0x0C00},
667 {0x0C04, 0x0C04}, {0x0C3E, 0x0C40}, {0x0C46, 0x0C56},
668 {0x0C62, 0x0C63}, {0x0C81, 0x0C81}, {0x0CBC, 0x0CBC},
669 {0x0CBF, 0x0CBF}, {0x0CC6, 0x0CC6}, {0x0CCC, 0x0CCD},
670 {0x0CE2, 0x0CE3}, {0x0D00, 0x0D01}, {0x0D3B, 0x0D3C},
671 {0x0D41, 0x0D44}, {0x0D4D, 0x0D4D}, {0x0D62, 0x0D63},
672 {0x0DCA, 0x0DCA}, {0x0DD2, 0x0DD6}, {0x0E31, 0x0E31},
673 {0x0E34, 0x0E3A}, {0x0E47, 0x0E4E}, {0x0EB1, 0x0EB1},
674 {0x0EB4, 0x0EBC}, {0x0EC8, 0x0ECD}, {0x0F18, 0x0F19},
675 {0x0F35, 0x0F35}, {0x0F37, 0x0F37}, {0x0F39, 0x0F39},
676 {0x0F71, 0x0F7E}, {0x0F80, 0x0F84}, {0x0F86, 0x0F87},
677 {0x0F8D, 0x0FBC}, {0x0FC6, 0x0FC6}, {0x102D, 0x1030},
678 {0x1032, 0x1037}, {0x1039, 0x103A}, {0x103D, 0x103E},
679 {0x1058, 0x1059}, {0x105E, 0x1060}, {0x1071, 0x1074},
680 {0x1082, 0x1082}, {0x1085, 0x1086}, {0x108D, 0x108D},
681 {0x109D, 0x109D}, {0x135D, 0x135F}, {0x1712, 0x1714},
682 {0x1732, 0x1734}, {0x1752, 0x1753}, {0x1772, 0x1773},
683 {0x17B4, 0x17B5}, {0x17B7, 0x17BD}, {0x17C6, 0x17C6},
684 {0x17C9, 0x17D3}, {0x17DD, 0x17DD}, {0x180B, 0x180D},
685 {0x1885, 0x1886}, {0x18A9, 0x18A9}, {0x1920, 0x1922},
686 {0x1927, 0x1928}, {0x1932, 0x1932}, {0x1939, 0x193B},
687 {0x1A17, 0x1A18}, {0x1A1B, 0x1A1B}, {0x1A56, 0x1A56},
688 {0x1A58, 0x1A60}, {0x1A62, 0x1A62}, {0x1A65, 0x1A6C},
689 {0x1A73, 0x1A7F}, {0x1AB0, 0x1B03}, {0x1B34, 0x1B34},
690 {0x1B36, 0x1B3A}, {0x1B3C, 0x1B3C}, {0x1B42, 0x1B42},
691 {0x1B6B, 0x1B73}, {0x1B80, 0x1B81}, {0x1BA2, 0x1BA5},
692 {0x1BA8, 0x1BA9}, {0x1BAB, 0x1BAD}, {0x1BE6, 0x1BE6},
693 {0x1BE8, 0x1BE9}, {0x1BED, 0x1BED}, {0x1BEF, 0x1BF1},
694 {0x1C2C, 0x1C33}, {0x1C36, 0x1C37}, {0x1CD0, 0x1CD2},
695 {0x1CD4, 0x1CE0}, {0x1CE2, 0x1CE8}, {0x1CED, 0x1CED},
696 {0x1CF4, 0x1CF4}, {0x1CF8, 0x1CF9}, {0x1DC0, 0x1DFF},
697 {0x20D0, 0x20F0}, {0x2CEF, 0x2CF1}, {0x2D7F, 0x2D7F},
698 {0x2DE0, 0x2DFF}, {0x302A, 0x302D}, {0x3099, 0x309A},
699 {0xA66F, 0xA672}, {0xA674, 0xA67D}, {0xA69E, 0xA69F},
700 {0xA6F0, 0xA6F1}, {0xA802, 0xA802}, {0xA806, 0xA806},
701 {0xA80B, 0xA80B}, {0xA825, 0xA826}, {0xA8C4, 0xA8C5},
702 {0xA8E0, 0xA8F1}, {0xA8FF, 0xA8FF}, {0xA926, 0xA92D},
703 {0xA947, 0xA951}, {0xA980, 0xA982}, {0xA9B3, 0xA9B3},
704 {0xA9B6, 0xA9B9}, {0xA9BC, 0xA9BD}, {0xA9E5, 0xA9E5},
705 {0xAA29, 0xAA2E}, {0xAA31, 0xAA32}, {0xAA35, 0xAA36},
706 {0xAA43, 0xAA43}, {0xAA4C, 0xAA4C}, {0xAA7C, 0xAA7C},
707 {0xAAB0, 0xAAB0}, {0xAAB2, 0xAAB4}, {0xAAB7, 0xAAB8},
708 {0xAABE, 0xAABF}, {0xAAC1, 0xAAC1}, {0xAAEC, 0xAAED},
709 {0xAAF6, 0xAAF6}, {0xABE5, 0xABE5}, {0xABE8, 0xABE8},
710 {0xABED, 0xABED}, {0xFB1E, 0xFB1E}, {0xFE00, 0xFE0F},
714 /* test for 8-bit control characters */
718 if (ucs < 0x20 || (ucs >= 0x7f && ucs < 0xa0) || ucs > 0x0010ffff)
721 /* binary search in table of non-spacing characters */
722 if (mbbisearch(ucs, combining,
723 sizeof(combining) / sizeof(struct mbinterval) - 1))
727 * if we arrive here, ucs is not a combining or C0/C1 control character
732 (ucs <= 0x115f || /* Hangul Jamo init. consonants */
733 (ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
734 ucs != 0x303f) || /* CJK ... Yi */
735 (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
736 (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility
738 (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
739 (ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
740 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
741 (ucs >= 0x20000 && ucs <= 0x2ffff)));
745 * Convert a UTF-8 character to a Unicode code point.
746 * This is a one-character version of pg_utf2wchar_with_len.
748 * No error checks here, c must point to a long-enough string.
751 utf8_to_unicode(const unsigned char *c)
753 if ((*c & 0x80) == 0)
754 return (pg_wchar) c[0];
755 else if ((*c & 0xe0) == 0xc0)
756 return (pg_wchar) (((c[0] & 0x1f) << 6) |
758 else if ((*c & 0xf0) == 0xe0)
759 return (pg_wchar) (((c[0] & 0x0f) << 12) |
760 ((c[1] & 0x3f) << 6) |
762 else if ((*c & 0xf8) == 0xf0)
763 return (pg_wchar) (((c[0] & 0x07) << 18) |
764 ((c[1] & 0x3f) << 12) |
765 ((c[2] & 0x3f) << 6) |
768 /* that is an invalid code on purpose */
773 pg_utf_dsplen(const unsigned char *s)
775 return ucs_wcwidth(utf8_to_unicode(s));
779 * convert mule internal code to pg_wchar
780 * caller should allocate enough space for "to"
781 * len: length of from.
782 * "from" not necessarily null terminated.
785 pg_mule2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
789 while (len > 0 && *from)
791 if (IS_LC1(*from) && len >= 2)
797 else if (IS_LCPRV1(*from) && len >= 3)
804 else if (IS_LC2(*from) && len >= 3)
811 else if (IS_LCPRV2(*from) && len >= 4)
821 *to = (unsigned char) *from++;
832 * convert pg_wchar to mule internal code
833 * caller should allocate enough space for "to"
834 * len: length of from.
835 * "from" not necessarily null terminated.
838 pg_wchar2mule_with_len(const pg_wchar *from, unsigned char *to, int len)
842 while (len > 0 && *from)
846 lb = (*from >> 16) & 0xff;
850 *to++ = *from & 0xff;
856 *to++ = (*from >> 8) & 0xff;
857 *to++ = *from & 0xff;
860 else if (IS_LCPRV1_A_RANGE(lb))
864 *to++ = *from & 0xff;
867 else if (IS_LCPRV1_B_RANGE(lb))
871 *to++ = *from & 0xff;
874 else if (IS_LCPRV2_A_RANGE(lb))
878 *to++ = (*from >> 8) & 0xff;
879 *to++ = *from & 0xff;
882 else if (IS_LCPRV2_B_RANGE(lb))
886 *to++ = (*from >> 8) & 0xff;
887 *to++ = *from & 0xff;
892 *to++ = *from & 0xff;
903 pg_mule_mblen(const unsigned char *s)
909 else if (IS_LCPRV1(*s))
913 else if (IS_LCPRV2(*s))
916 len = 1; /* assume ASCII */
921 pg_mule_dsplen(const unsigned char *s)
926 * Note: it's not really appropriate to assume that all multibyte charsets
927 * are double-wide on screen. But this seems an okay approximation for
928 * the MULE charsets we currently support.
933 else if (IS_LCPRV1(*s))
937 else if (IS_LCPRV2(*s))
940 len = 1; /* assume ASCII */
949 pg_latin12wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
953 while (len > 0 && *from)
964 * Trivial conversion from pg_wchar to single byte encoding. Just ignores
966 * caller should allocate enough space for "to"
967 * len: length of from.
968 * "from" not necessarily null terminated.
971 pg_wchar2single_with_len(const pg_wchar *from, unsigned char *to, int len)
975 while (len > 0 && *from)
986 pg_latin1_mblen(const unsigned char *s)
992 pg_latin1_dsplen(const unsigned char *s)
994 return pg_ascii_dsplen(s);
1001 pg_sjis_mblen(const unsigned char *s)
1005 if (*s >= 0xa1 && *s <= 0xdf)
1006 len = 1; /* 1 byte kana? */
1007 else if (IS_HIGHBIT_SET(*s))
1008 len = 2; /* kanji? */
1010 len = 1; /* should be ASCII */
1015 pg_sjis_dsplen(const unsigned char *s)
1019 if (*s >= 0xa1 && *s <= 0xdf)
1020 len = 1; /* 1 byte kana? */
1021 else if (IS_HIGHBIT_SET(*s))
1022 len = 2; /* kanji? */
1024 len = pg_ascii_dsplen(s); /* should be ASCII */
1032 pg_big5_mblen(const unsigned char *s)
1036 if (IS_HIGHBIT_SET(*s))
1037 len = 2; /* kanji? */
1039 len = 1; /* should be ASCII */
1044 pg_big5_dsplen(const unsigned char *s)
1048 if (IS_HIGHBIT_SET(*s))
1049 len = 2; /* kanji? */
1051 len = pg_ascii_dsplen(s); /* should be ASCII */
1059 pg_gbk_mblen(const unsigned char *s)
1063 if (IS_HIGHBIT_SET(*s))
1064 len = 2; /* kanji? */
1066 len = 1; /* should be ASCII */
1071 pg_gbk_dsplen(const unsigned char *s)
1075 if (IS_HIGHBIT_SET(*s))
1076 len = 2; /* kanji? */
1078 len = pg_ascii_dsplen(s); /* should be ASCII */
1086 pg_uhc_mblen(const unsigned char *s)
1090 if (IS_HIGHBIT_SET(*s))
1091 len = 2; /* 2byte? */
1093 len = 1; /* should be ASCII */
1098 pg_uhc_dsplen(const unsigned char *s)
1102 if (IS_HIGHBIT_SET(*s))
1103 len = 2; /* 2byte? */
1105 len = pg_ascii_dsplen(s); /* should be ASCII */
1111 * Added by Bill Huang <bhuang@redhat.com>,<bill_huanghb@ybb.ne.jp>
1115 * Unlike all other mblen() functions, this also looks at the second byte of
1116 * the input. However, if you only pass the first byte of a multi-byte
1117 * string, and \0 as the second byte, this still works in a predictable way:
1118 * a 4-byte character will be reported as two 2-byte characters. That's
1119 * enough for all current uses, as a client-only encoding. It works that
1120 * way, because in any valid 4-byte GB18030-encoded character, the third and
1121 * fourth byte look like a 2-byte encoded character, when looked at
1125 pg_gb18030_mblen(const unsigned char *s)
1129 if (!IS_HIGHBIT_SET(*s))
1130 len = 1; /* ASCII */
1131 else if (*(s + 1) >= 0x30 && *(s + 1) <= 0x39)
1139 pg_gb18030_dsplen(const unsigned char *s)
1143 if (IS_HIGHBIT_SET(*s))
1146 len = pg_ascii_dsplen(s); /* ASCII */
1151 *-------------------------------------------------------------------
1152 * multibyte sequence validators
1154 * These functions accept "s", a pointer to the first byte of a string,
1155 * and "len", the remaining length of the string. If there is a validly
1156 * encoded character beginning at *s, return its length in bytes; else
1159 * The functions can assume that len > 0 and that *s != '\0', but they must
1160 * test for and reject zeroes in any additional bytes of a multibyte character.
1162 * Note that this definition allows the function for a single-byte
1163 * encoding to be just "return 1".
1164 *-------------------------------------------------------------------
1168 pg_ascii_verifier(const unsigned char *s, int len)
1173 #define IS_EUC_RANGE_VALID(c) ((c) >= 0xa1 && (c) <= 0xfe)
1176 pg_eucjp_verifier(const unsigned char *s, int len)
1186 case SS2: /* JIS X 0201 */
1191 if (c2 < 0xa1 || c2 > 0xdf)
1195 case SS3: /* JIS X 0212 */
1200 if (!IS_EUC_RANGE_VALID(c2))
1203 if (!IS_EUC_RANGE_VALID(c2))
1208 if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
1213 if (!IS_EUC_RANGE_VALID(c1))
1216 if (!IS_EUC_RANGE_VALID(c2))
1231 pg_euckr_verifier(const unsigned char *s, int len)
1239 if (IS_HIGHBIT_SET(c1))
1244 if (!IS_EUC_RANGE_VALID(c1))
1247 if (!IS_EUC_RANGE_VALID(c2))
1259 /* EUC-CN byte sequences are exactly same as EUC-KR */
1260 #define pg_euccn_verifier pg_euckr_verifier
1263 pg_euctw_verifier(const unsigned char *s, int len)
1273 case SS2: /* CNS 11643 Plane 1-7 */
1278 if (c2 < 0xa1 || c2 > 0xa7)
1281 if (!IS_EUC_RANGE_VALID(c2))
1284 if (!IS_EUC_RANGE_VALID(c2))
1288 case SS3: /* unused */
1292 if (IS_HIGHBIT_SET(c1)) /* CNS 11643 Plane 1 */
1297 /* no further range check on c1? */
1299 if (!IS_EUC_RANGE_VALID(c2))
1313 pg_johab_verifier(const unsigned char *s, int len)
1319 l = mbl = pg_johab_mblen(s);
1324 if (!IS_HIGHBIT_SET(*s))
1330 if (!IS_EUC_RANGE_VALID(c))
1337 pg_mule_verifier(const unsigned char *s, int len)
1343 l = mbl = pg_mule_mblen(s);
1351 if (!IS_HIGHBIT_SET(c))
1358 pg_latin1_verifier(const unsigned char *s, int len)
1364 pg_sjis_verifier(const unsigned char *s, int len)
1371 l = mbl = pg_sjis_mblen(s);
1376 if (l == 1) /* pg_sjis_mblen already verified it */
1381 if (!ISSJISHEAD(c1) || !ISSJISTAIL(c2))
1387 pg_big5_verifier(const unsigned char *s, int len)
1392 l = mbl = pg_big5_mblen(s);
1407 pg_gbk_verifier(const unsigned char *s, int len)
1412 l = mbl = pg_gbk_mblen(s);
1427 pg_uhc_verifier(const unsigned char *s, int len)
1432 l = mbl = pg_uhc_mblen(s);
1447 pg_gb18030_verifier(const unsigned char *s, int len)
1451 if (!IS_HIGHBIT_SET(*s))
1453 else if (len >= 4 && *(s + 1) >= 0x30 && *(s + 1) <= 0x39)
1455 /* Should be 4-byte, validate remaining bytes */
1456 if (*s >= 0x81 && *s <= 0xfe &&
1457 *(s + 2) >= 0x81 && *(s + 2) <= 0xfe &&
1458 *(s + 3) >= 0x30 && *(s + 3) <= 0x39)
1463 else if (len >= 2 && *s >= 0x81 && *s <= 0xfe)
1465 /* Should be 2-byte, validate */
1466 if ((*(s + 1) >= 0x40 && *(s + 1) <= 0x7e) ||
1467 (*(s + 1) >= 0x80 && *(s + 1) <= 0xfe))
1478 pg_utf8_verifier(const unsigned char *s, int len)
1480 int l = pg_utf_mblen(s);
1485 if (!pg_utf8_islegal(s, l))
1492 * Check for validity of a single UTF-8 encoded character
1494 * This directly implements the rules in RFC3629. The bizarre-looking
1495 * restrictions on the second byte are meant to ensure that there isn't
1496 * more than one encoding of a given Unicode character point; that is,
1497 * you may not use a longer-than-necessary byte sequence with high order
1498 * zero bits to represent a character that would fit in fewer bytes.
1499 * To do otherwise is to create security hazards (eg, create an apparent
1500 * non-ASCII character that decodes to plain ASCII).
1502 * length is assumed to have been obtained by pg_utf_mblen(), and the
1503 * caller must have checked that that many bytes are present in the buffer.
1506 pg_utf8_islegal(const unsigned char *source, int length)
1513 /* reject lengths 5 and 6 for now */
1517 if (a < 0x80 || a > 0xBF)
1522 if (a < 0x80 || a > 0xBF)
1530 if (a < 0xA0 || a > 0xBF)
1534 if (a < 0x80 || a > 0x9F)
1538 if (a < 0x90 || a > 0xBF)
1542 if (a < 0x80 || a > 0x8F)
1546 if (a < 0x80 || a > 0xBF)
1553 if (a >= 0x80 && a < 0xC2)
1565 * Generic character incrementer function.
1567 * Not knowing anything about the properties of the encoding in use, we just
1568 * keep incrementing the last byte until we get a validly-encoded result,
1569 * or we run out of values to try. We don't bother to try incrementing
1570 * higher-order bytes, so there's no growth in runtime for wider characters.
1571 * (If we did try to do that, we'd need to consider the likelihood that 255
1572 * is not a valid final byte in the encoding.)
1575 pg_generic_charinc(unsigned char *charptr, int len)
1577 unsigned char *lastbyte = charptr + len - 1;
1578 mbverifier mbverify;
1580 /* We can just invoke the character verifier directly. */
1581 mbverify = pg_wchar_table[GetDatabaseEncoding()].mbverify;
1583 while (*lastbyte < (unsigned char) 255)
1586 if ((*mbverify) (charptr, len) == len)
1594 * UTF-8 character incrementer function.
1596 * For a one-byte character less than 0x7F, we just increment the byte.
1598 * For a multibyte character, every byte but the first must fall between 0x80
1599 * and 0xBF; and the first byte must be between 0xC0 and 0xF4. We increment
1600 * the last byte that's not already at its maximum value. If we can't find a
1601 * byte that's less than the maximum allowable value, we simply fail. We also
1602 * need some special-case logic to skip regions used for surrogate pair
1603 * handling, as those should not occur in valid UTF-8.
1605 * Note that we don't reset lower-order bytes back to their minimums, since
1606 * we can't afford to make an exhaustive search (see make_greater_string).
1609 pg_utf8_increment(unsigned char *charptr, int length)
1612 unsigned char limit;
1617 /* reject lengths 5 and 6 for now */
1657 if (a == 0x7F || a == 0xDF || a == 0xEF || a == 0xF4)
1667 * EUC-JP character incrementer function.
1669 * If the sequence starts with SS2 (0x8e), it must be a two-byte sequence
1670 * representing JIS X 0201 characters with the second byte ranging between
1671 * 0xa1 and 0xdf. We just increment the last byte if it's less than 0xdf,
1672 * and otherwise rewrite the whole sequence to 0xa1 0xa1.
1674 * If the sequence starts with SS3 (0x8f), it must be a three-byte sequence
1675 * in which the last two bytes range between 0xa1 and 0xfe. The last byte
1676 * is incremented if possible, otherwise the second-to-last byte.
1678 * If the sequence starts with a value other than the above and its MSB
1679 * is set, it must be a two-byte sequence representing JIS X 0208 characters
1680 * with both bytes ranging between 0xa1 and 0xfe. The last byte is
1681 * incremented if possible, otherwise the second-to-last byte.
1683 * Otherwise, the sequence is a single-byte ASCII character. It is
1684 * incremented up to 0x7f.
1687 pg_eucjp_increment(unsigned char *charptr, int length)
1697 case SS2: /* JIS X 0201 */
1704 charptr[0] = charptr[1] = 0xa1;
1711 case SS3: /* JIS X 0212 */
1715 for (i = 2; i > 0; i--)
1730 /* Out of 3-byte code region */
1734 if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
1739 for (i = 1; i >= 0; i--)
1754 /* Out of 2 byte code region */
1758 { /* ASCII, single byte */
1768 #endif /* !FRONTEND */
1772 *-------------------------------------------------------------------
1773 * encoding info table
1774 * XXX must be sorted by the same order as enum pg_enc (in mb/pg_wchar.h)
1775 *-------------------------------------------------------------------
1777 const pg_wchar_tbl pg_wchar_table[] = {
1778 {pg_ascii2wchar_with_len, pg_wchar2single_with_len, pg_ascii_mblen, pg_ascii_dsplen, pg_ascii_verifier, 1}, /* PG_SQL_ASCII */
1779 {pg_eucjp2wchar_with_len, pg_wchar2euc_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifier, 3}, /* PG_EUC_JP */
1780 {pg_euccn2wchar_with_len, pg_wchar2euc_with_len, pg_euccn_mblen, pg_euccn_dsplen, pg_euccn_verifier, 2}, /* PG_EUC_CN */
1781 {pg_euckr2wchar_with_len, pg_wchar2euc_with_len, pg_euckr_mblen, pg_euckr_dsplen, pg_euckr_verifier, 3}, /* PG_EUC_KR */
1782 {pg_euctw2wchar_with_len, pg_wchar2euc_with_len, pg_euctw_mblen, pg_euctw_dsplen, pg_euctw_verifier, 4}, /* PG_EUC_TW */
1783 {pg_eucjp2wchar_with_len, pg_wchar2euc_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifier, 3}, /* PG_EUC_JIS_2004 */
1784 {pg_utf2wchar_with_len, pg_wchar2utf_with_len, pg_utf_mblen, pg_utf_dsplen, pg_utf8_verifier, 4}, /* PG_UTF8 */
1785 {pg_mule2wchar_with_len, pg_wchar2mule_with_len, pg_mule_mblen, pg_mule_dsplen, pg_mule_verifier, 4}, /* PG_MULE_INTERNAL */
1786 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN1 */
1787 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN2 */
1788 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN3 */
1789 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN4 */
1790 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN5 */
1791 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN6 */
1792 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN7 */
1793 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN8 */
1794 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN9 */
1795 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN10 */
1796 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1256 */
1797 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1258 */
1798 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN866 */
1799 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN874 */
1800 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_KOI8R */
1801 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1251 */
1802 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1252 */
1803 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-5 */
1804 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-6 */
1805 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-7 */
1806 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-8 */
1807 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1250 */
1808 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1253 */
1809 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1254 */
1810 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1255 */
1811 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1257 */
1812 {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_KOI8U */
1813 {0, 0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifier, 2}, /* PG_SJIS */
1814 {0, 0, pg_big5_mblen, pg_big5_dsplen, pg_big5_verifier, 2}, /* PG_BIG5 */
1815 {0, 0, pg_gbk_mblen, pg_gbk_dsplen, pg_gbk_verifier, 2}, /* PG_GBK */
1816 {0, 0, pg_uhc_mblen, pg_uhc_dsplen, pg_uhc_verifier, 2}, /* PG_UHC */
1817 {0, 0, pg_gb18030_mblen, pg_gb18030_dsplen, pg_gb18030_verifier, 4}, /* PG_GB18030 */
1818 {0, 0, pg_johab_mblen, pg_johab_dsplen, pg_johab_verifier, 3}, /* PG_JOHAB */
1819 {0, 0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifier, 2} /* PG_SHIFT_JIS_2004 */
1822 /* returns the byte length of a word for mule internal code */
1824 pg_mic_mblen(const unsigned char *mbstr)
1826 return pg_mule_mblen(mbstr);
1830 * Returns the byte length of a multibyte character.
1833 pg_encoding_mblen(int encoding, const char *mbstr)
1835 return (PG_VALID_ENCODING(encoding) ?
1836 pg_wchar_table[encoding].mblen((const unsigned char *) mbstr) :
1837 pg_wchar_table[PG_SQL_ASCII].mblen((const unsigned char *) mbstr));
1841 * Returns the display length of a multibyte character.
1844 pg_encoding_dsplen(int encoding, const char *mbstr)
1846 return (PG_VALID_ENCODING(encoding) ?
1847 pg_wchar_table[encoding].dsplen((const unsigned char *) mbstr) :
1848 pg_wchar_table[PG_SQL_ASCII].dsplen((const unsigned char *) mbstr));
1852 * Verify the first multibyte character of the given string.
1853 * Return its byte length if good, -1 if bad. (See comments above for
1854 * full details of the mbverify API.)
1857 pg_encoding_verifymb(int encoding, const char *mbstr, int len)
1859 return (PG_VALID_ENCODING(encoding) ?
1860 pg_wchar_table[encoding].mbverify((const unsigned char *) mbstr, len) :
1861 pg_wchar_table[PG_SQL_ASCII].mbverify((const unsigned char *) mbstr, len));
1865 * fetch maximum length of a given encoding
1868 pg_encoding_max_length(int encoding)
1870 Assert(PG_VALID_ENCODING(encoding));
1872 return pg_wchar_table[encoding].maxmblen;
1878 * fetch maximum length of the encoding for the current database
1881 pg_database_encoding_max_length(void)
1883 return pg_wchar_table[GetDatabaseEncoding()].maxmblen;
1887 * get the character incrementer for the encoding for the current database
1889 mbcharacter_incrementer
1890 pg_database_encoding_character_incrementer(void)
1893 * Eventually it might be best to add a field to pg_wchar_table[], but for
1894 * now we just use a switch.
1896 switch (GetDatabaseEncoding())
1899 return pg_utf8_increment;
1902 return pg_eucjp_increment;
1905 return pg_generic_charinc;
1910 * Verify mbstr to make sure that it is validly encoded in the current
1911 * database encoding. Otherwise same as pg_verify_mbstr().
1914 pg_verifymbstr(const char *mbstr, int len, bool noError)
1917 pg_verify_mbstr_len(GetDatabaseEncoding(), mbstr, len, noError) >= 0;
1921 * Verify mbstr to make sure that it is validly encoded in the specified
1925 pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
1927 return pg_verify_mbstr_len(encoding, mbstr, len, noError) >= 0;
1931 * Verify mbstr to make sure that it is validly encoded in the specified
1934 * mbstr is not necessarily zero terminated; length of mbstr is
1937 * If OK, return length of string in the encoding.
1938 * If a problem is found, return -1 when noError is
1939 * true; when noError is false, ereport() a descriptive message.
1942 pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
1944 mbverifier mbverify;
1947 Assert(PG_VALID_ENCODING(encoding));
1950 * In single-byte encodings, we need only reject nulls (\0).
1952 if (pg_encoding_max_length(encoding) <= 1)
1954 const char *nullpos = memchr(mbstr, 0, len);
1956 if (nullpos == NULL)
1960 report_invalid_encoding(encoding, nullpos, 1);
1963 /* fetch function pointer just once */
1964 mbverify = pg_wchar_table[encoding].mbverify;
1972 /* fast path for ASCII-subset characters */
1973 if (!IS_HIGHBIT_SET(*mbstr))
1984 report_invalid_encoding(encoding, mbstr, len);
1987 l = (*mbverify) ((const unsigned char *) mbstr, len);
1993 report_invalid_encoding(encoding, mbstr, len);
2004 * check_encoding_conversion_args: check arguments of a conversion function
2006 * "expected" arguments can be either an encoding ID or -1 to indicate that
2007 * the caller will check whether it accepts the ID.
2009 * Note: the errors here are not really user-facing, so elog instead of
2010 * ereport seems sufficient. Also, we trust that the "expected" encoding
2011 * arguments are valid encoding IDs, but we don't trust the actuals.
2014 check_encoding_conversion_args(int src_encoding,
2017 int expected_src_encoding,
2018 int expected_dest_encoding)
2020 if (!PG_VALID_ENCODING(src_encoding))
2021 elog(ERROR, "invalid source encoding ID: %d", src_encoding);
2022 if (src_encoding != expected_src_encoding && expected_src_encoding >= 0)
2023 elog(ERROR, "expected source encoding \"%s\", but got \"%s\"",
2024 pg_enc2name_tbl[expected_src_encoding].name,
2025 pg_enc2name_tbl[src_encoding].name);
2026 if (!PG_VALID_ENCODING(dest_encoding))
2027 elog(ERROR, "invalid destination encoding ID: %d", dest_encoding);
2028 if (dest_encoding != expected_dest_encoding && expected_dest_encoding >= 0)
2029 elog(ERROR, "expected destination encoding \"%s\", but got \"%s\"",
2030 pg_enc2name_tbl[expected_dest_encoding].name,
2031 pg_enc2name_tbl[dest_encoding].name);
2033 elog(ERROR, "encoding conversion length must not be negative");
2037 * report_invalid_encoding: complain about invalid multibyte character
2039 * note: len is remaining length of string, not length of character;
2040 * len must be greater than zero, as we always examine the first byte.
2043 report_invalid_encoding(int encoding, const char *mbstr, int len)
2045 int l = pg_encoding_mblen(encoding, mbstr);
2046 char buf[8 * 5 + 1];
2051 jlimit = Min(l, len);
2052 jlimit = Min(jlimit, 8); /* prevent buffer overrun */
2054 for (j = 0; j < jlimit; j++)
2056 p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
2058 p += sprintf(p, " ");
2062 (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
2063 errmsg("invalid byte sequence for encoding \"%s\": %s",
2064 pg_enc2name_tbl[encoding].name,
2069 * report_untranslatable_char: complain about untranslatable character
2071 * note: len is remaining length of string, not length of character;
2072 * len must be greater than zero, as we always examine the first byte.
2075 report_untranslatable_char(int src_encoding, int dest_encoding,
2076 const char *mbstr, int len)
2078 int l = pg_encoding_mblen(src_encoding, mbstr);
2079 char buf[8 * 5 + 1];
2084 jlimit = Min(l, len);
2085 jlimit = Min(jlimit, 8); /* prevent buffer overrun */
2087 for (j = 0; j < jlimit; j++)
2089 p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
2091 p += sprintf(p, " ");
2095 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
2096 errmsg("character with byte sequence %s in encoding \"%s\" has no equivalent in encoding \"%s\"",
2098 pg_enc2name_tbl[src_encoding].name,
2099 pg_enc2name_tbl[dest_encoding].name)));
2102 #endif /* !FRONTEND */