2
* conversion functions between pg_wchar and multibyte streams.
7
/* can be used in either frontend or backend */
9
#include "postgres_fe.h"
10
#define Assert(condition)
15
#include "mb/pg_wchar.h"
19
* conversion to pg_wchar is done by "table driven."
20
* to add an encoding support, define mb2wchar_with_len(), mblen(), dsplen()
21
* for the particular encoding. Note that if the encoding is only
22
* supported in the client, you don't need to define
23
* mb2wchar_with_len() function (SJIS is the case).
25
* These functions generally assume that their input is validly formed.
26
* The "verifier" functions, further down in the file, have to be more
27
* paranoid. We expect that mblen() does not need to examine more than
28
* the first byte of the character to discover the correct length.
30
* Note: for the display output of psql to work properly, the return values
31
* of the dsplen functions must conform to the Unicode standard. In particular
32
* the NUL character is zero width and control characters are generally
33
* width -1. It is recommended that non-ASCII encodings refer their ASCII
34
* subset to the ASCII routines to ensure consistency.
41
pg_ascii2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
45
while (len > 0 && *from)
56
pg_ascii_mblen(const unsigned char *s)
62
pg_ascii_dsplen(const unsigned char *s)
66
if (*s < 0x20 || *s == 0x7f)
76
pg_euc2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
80
while (len > 0 && *from)
82
if (*from == SS2 && len >= 2) /* JIS X 0201 (so called "1 byte
86
*to = (SS2 << 8) | *from++;
89
else if (*from == SS3 && len >= 3) /* JIS X 0212 KANJI */
92
*to = (SS3 << 16) | (*from++ << 8);
96
else if (IS_HIGHBIT_SET(*from) && len >= 2) /* JIS X 0208 KANJI */
116
pg_euc_mblen(const unsigned char *s)
124
else if (IS_HIGHBIT_SET(*s))
132
pg_euc_dsplen(const unsigned char *s)
140
else if (IS_HIGHBIT_SET(*s))
143
len = pg_ascii_dsplen(s);
151
pg_eucjp2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
153
return pg_euc2wchar_with_len(from, to, len);
157
pg_eucjp_mblen(const unsigned char *s)
159
return pg_euc_mblen(s);
163
pg_eucjp_dsplen(const unsigned char *s)
171
else if (IS_HIGHBIT_SET(*s))
174
len = pg_ascii_dsplen(s);
182
pg_euckr2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
184
return pg_euc2wchar_with_len(from, to, len);
188
pg_euckr_mblen(const unsigned char *s)
190
return pg_euc_mblen(s);
194
pg_euckr_dsplen(const unsigned char *s)
196
return pg_euc_dsplen(s);
204
pg_euccn2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
208
while (len > 0 && *from)
210
if (*from == SS2 && len >= 3) /* code set 2 (unused?) */
213
*to = (SS2 << 16) | (*from++ << 8);
217
else if (*from == SS3 && len >= 3) /* code set 3 (unsed ?) */
220
*to = (SS3 << 16) | (*from++ << 8);
224
else if (IS_HIGHBIT_SET(*from) && len >= 2) /* code set 1 */
243
pg_euccn_mblen(const unsigned char *s)
247
if (IS_HIGHBIT_SET(*s))
255
pg_euccn_dsplen(const unsigned char *s)
259
if (IS_HIGHBIT_SET(*s))
262
len = pg_ascii_dsplen(s);
271
pg_euctw2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
275
while (len > 0 && *from)
277
if (*from == SS2 && len >= 4) /* code set 2 */
280
*to = (((uint32) SS2) << 24) | (*from++ << 16);
285
else if (*from == SS3 && len >= 3) /* code set 3 (unused?) */
288
*to = (SS3 << 16) | (*from++ << 8);
292
else if (IS_HIGHBIT_SET(*from) && len >= 2) /* code set 2 */
311
pg_euctw_mblen(const unsigned char *s)
319
else if (IS_HIGHBIT_SET(*s))
327
pg_euctw_dsplen(const unsigned char *s)
335
else if (IS_HIGHBIT_SET(*s))
338
len = pg_ascii_dsplen(s);
346
pg_johab_mblen(const unsigned char *s)
348
return pg_euc_mblen(s);
352
pg_johab_dsplen(const unsigned char *s)
354
return pg_euc_dsplen(s);
358
* convert UTF8 string to pg_wchar (UCS-4)
359
* caller must allocate enough space for "to", including a trailing zero!
360
* len: length of from.
361
* "from" not necessarily null terminated.
364
pg_utf2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
372
while (len > 0 && *from)
374
if ((*from & 0x80) == 0)
379
else if ((*from & 0xe0) == 0xc0)
382
break; /* drop trailing incomplete char */
385
*to = (c1 << 6) | c2;
388
else if ((*from & 0xf0) == 0xe0)
391
break; /* drop trailing incomplete char */
395
*to = (c1 << 12) | (c2 << 6) | c3;
398
else if ((*from & 0xf8) == 0xf0)
401
break; /* drop trailing incomplete char */
406
*to = (c1 << 18) | (c2 << 12) | (c3 << 6) | c4;
411
/* treat a bogus char as length 1; not ours to raise error */
424
* Map a Unicode code point to UTF-8. utf8string must have 4 bytes of
428
unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
436
utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
437
utf8string[1] = 0x80 | (c & 0x3F);
439
else if (c <= 0xFFFF)
441
utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
442
utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
443
utf8string[2] = 0x80 | (c & 0x3F);
447
utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
448
utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
449
utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
450
utf8string[3] = 0x80 | (c & 0x3F);
458
* Return the byte length of a UTF8 character pointed to by s
460
* Note: in the current implementation we do not support UTF8 sequences
461
* of more than 4 bytes; hence do NOT return a value larger than 4.
462
* We return "1" for any leading byte that is either flat-out illegal or
463
* indicates a length larger than we support.
465
* pg_utf2wchar_with_len(), utf2ucs(), pg_utf8_islegal(), and perhaps
466
* other places would need to be fixed to change this.
469
pg_utf_mblen(const unsigned char *s)
473
if ((*s & 0x80) == 0)
475
else if ((*s & 0xe0) == 0xc0)
477
else if ((*s & 0xf0) == 0xe0)
479
else if ((*s & 0xf8) == 0xf0)
482
else if ((*s & 0xfc) == 0xf8)
484
else if ((*s & 0xfe) == 0xfc)
493
* This is an implementation of wcwidth() and wcswidth() as defined in
494
* "The Single UNIX Specification, Version 2, The Open Group, 1997"
495
* <http://www.UNIX-systems.org/online.html>
497
* Markus Kuhn -- 2001-09-08 -- public domain
499
* customised for PostgreSQL
501
* original available at : http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
506
unsigned short first;
510
/* auxiliary function for binary search in interval table */
512
mbbisearch(pg_wchar ucs, const struct mbinterval * table, int max)
517
if (ucs < table[0].first || ucs > table[max].last)
521
mid = (min + max) / 2;
522
if (ucs > table[mid].last)
524
else if (ucs < table[mid].first)
534
/* The following functions define the column width of an ISO 10646
535
* character as follows:
537
* - The null character (U+0000) has a column width of 0.
539
* - Other C0/C1 control characters and DEL will lead to a return
542
* - Non-spacing and enclosing combining characters (general
543
* category code Mn or Me in the Unicode database) have a
546
* - Other format characters (general category code Cf in the Unicode
547
* database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
549
* - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
550
* have a column width of 0.
552
* - Spacing characters in the East Asian Wide (W) or East Asian
553
* FullWidth (F) category as defined in Unicode Technical
554
* Report #11 have a column width of 2.
556
* - All remaining characters (including all printable
557
* ISO 8859-1 and WGL4 characters, Unicode control characters,
558
* etc.) have a column width of 1.
560
* This implementation assumes that wchar_t characters are encoded
565
ucs_wcwidth(pg_wchar ucs)
567
/* sorted list of non-overlapping intervals of non-spacing characters */
568
static const struct mbinterval combining[] = {
569
{0x0300, 0x034E}, {0x0360, 0x0362}, {0x0483, 0x0486},
570
{0x0488, 0x0489}, {0x0591, 0x05A1}, {0x05A3, 0x05B9},
571
{0x05BB, 0x05BD}, {0x05BF, 0x05BF}, {0x05C1, 0x05C2},
572
{0x05C4, 0x05C4}, {0x064B, 0x0655}, {0x0670, 0x0670},
573
{0x06D6, 0x06E4}, {0x06E7, 0x06E8}, {0x06EA, 0x06ED},
574
{0x070F, 0x070F}, {0x0711, 0x0711}, {0x0730, 0x074A},
575
{0x07A6, 0x07B0}, {0x0901, 0x0902}, {0x093C, 0x093C},
576
{0x0941, 0x0948}, {0x094D, 0x094D}, {0x0951, 0x0954},
577
{0x0962, 0x0963}, {0x0981, 0x0981}, {0x09BC, 0x09BC},
578
{0x09C1, 0x09C4}, {0x09CD, 0x09CD}, {0x09E2, 0x09E3},
579
{0x0A02, 0x0A02}, {0x0A3C, 0x0A3C}, {0x0A41, 0x0A42},
580
{0x0A47, 0x0A48}, {0x0A4B, 0x0A4D}, {0x0A70, 0x0A71},
581
{0x0A81, 0x0A82}, {0x0ABC, 0x0ABC}, {0x0AC1, 0x0AC5},
582
{0x0AC7, 0x0AC8}, {0x0ACD, 0x0ACD}, {0x0B01, 0x0B01},
583
{0x0B3C, 0x0B3C}, {0x0B3F, 0x0B3F}, {0x0B41, 0x0B43},
584
{0x0B4D, 0x0B4D}, {0x0B56, 0x0B56}, {0x0B82, 0x0B82},
585
{0x0BC0, 0x0BC0}, {0x0BCD, 0x0BCD}, {0x0C3E, 0x0C40},
586
{0x0C46, 0x0C48}, {0x0C4A, 0x0C4D}, {0x0C55, 0x0C56},
587
{0x0CBF, 0x0CBF}, {0x0CC6, 0x0CC6}, {0x0CCC, 0x0CCD},
588
{0x0D41, 0x0D43}, {0x0D4D, 0x0D4D}, {0x0DCA, 0x0DCA},
589
{0x0DD2, 0x0DD4}, {0x0DD6, 0x0DD6}, {0x0E31, 0x0E31},
590
{0x0E34, 0x0E3A}, {0x0E47, 0x0E4E}, {0x0EB1, 0x0EB1},
591
{0x0EB4, 0x0EB9}, {0x0EBB, 0x0EBC}, {0x0EC8, 0x0ECD},
592
{0x0F18, 0x0F19}, {0x0F35, 0x0F35}, {0x0F37, 0x0F37},
593
{0x0F39, 0x0F39}, {0x0F71, 0x0F7E}, {0x0F80, 0x0F84},
594
{0x0F86, 0x0F87}, {0x0F90, 0x0F97}, {0x0F99, 0x0FBC},
595
{0x0FC6, 0x0FC6}, {0x102D, 0x1030}, {0x1032, 0x1032},
596
{0x1036, 0x1037}, {0x1039, 0x1039}, {0x1058, 0x1059},
597
{0x1160, 0x11FF}, {0x17B7, 0x17BD}, {0x17C6, 0x17C6},
598
{0x17C9, 0x17D3}, {0x180B, 0x180E}, {0x18A9, 0x18A9},
599
{0x200B, 0x200F}, {0x202A, 0x202E}, {0x206A, 0x206F},
600
{0x20D0, 0x20E3}, {0x302A, 0x302F}, {0x3099, 0x309A},
601
{0xFB1E, 0xFB1E}, {0xFE20, 0xFE23}, {0xFEFF, 0xFEFF},
605
/* test for 8-bit control characters */
609
if (ucs < 0x20 || (ucs >= 0x7f && ucs < 0xa0) || ucs > 0x0010ffff)
612
/* binary search in table of non-spacing characters */
613
if (mbbisearch(ucs, combining,
614
sizeof(combining) / sizeof(struct mbinterval) - 1))
618
* if we arrive here, ucs is not a combining or C0/C1 control character
623
(ucs <= 0x115f || /* Hangul Jamo init. consonants */
624
(ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
625
ucs != 0x303f) || /* CJK ... Yi */
626
(ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
627
(ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility
629
(ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
630
(ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
631
(ucs >= 0xffe0 && ucs <= 0xffe6) ||
632
(ucs >= 0x20000 && ucs <= 0x2ffff)));
636
utf2ucs(const unsigned char *c)
639
* one char version of pg_utf2wchar_with_len. no control here, c must
640
* point to a large enough string
642
if ((*c & 0x80) == 0)
643
return (pg_wchar) c[0];
644
else if ((*c & 0xe0) == 0xc0)
645
return (pg_wchar) (((c[0] & 0x1f) << 6) |
647
else if ((*c & 0xf0) == 0xe0)
648
return (pg_wchar) (((c[0] & 0x0f) << 12) |
649
((c[1] & 0x3f) << 6) |
651
else if ((*c & 0xf8) == 0xf0)
652
return (pg_wchar) (((c[0] & 0x07) << 18) |
653
((c[1] & 0x3f) << 12) |
654
((c[2] & 0x3f) << 6) |
657
/* that is an invalid code on purpose */
662
pg_utf_dsplen(const unsigned char *s)
664
return ucs_wcwidth(utf2ucs(s));
668
* convert mule internal code to pg_wchar
669
* caller should allocate enough space for "to"
670
* len: length of from.
671
* "from" not necessarily null terminated.
674
pg_mule2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
678
while (len > 0 && *from)
680
if (IS_LC1(*from) && len >= 2)
686
else if (IS_LCPRV1(*from) && len >= 3)
693
else if (IS_LC2(*from) && len >= 3)
700
else if (IS_LCPRV2(*from) && len >= 4)
710
*to = (unsigned char) *from++;
721
pg_mule_mblen(const unsigned char *s)
727
else if (IS_LCPRV1(*s))
731
else if (IS_LCPRV2(*s))
734
len = 1; /* assume ASCII */
739
pg_mule_dsplen(const unsigned char *s)
745
else if (IS_LCPRV1(*s))
749
else if (IS_LCPRV2(*s))
752
len = 1; /* assume ASCII */
761
pg_latin12wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
765
while (len > 0 && *from)
776
pg_latin1_mblen(const unsigned char *s)
782
pg_latin1_dsplen(const unsigned char *s)
784
return pg_ascii_dsplen(s);
791
pg_sjis_mblen(const unsigned char *s)
795
if (*s >= 0xa1 && *s <= 0xdf)
796
len = 1; /* 1 byte kana? */
797
else if (IS_HIGHBIT_SET(*s))
798
len = 2; /* kanji? */
800
len = 1; /* should be ASCII */
805
pg_sjis_dsplen(const unsigned char *s)
809
if (*s >= 0xa1 && *s <= 0xdf)
810
len = 1; /* 1 byte kana? */
811
else if (IS_HIGHBIT_SET(*s))
812
len = 2; /* kanji? */
814
len = pg_ascii_dsplen(s); /* should be ASCII */
822
pg_big5_mblen(const unsigned char *s)
826
if (IS_HIGHBIT_SET(*s))
827
len = 2; /* kanji? */
829
len = 1; /* should be ASCII */
834
pg_big5_dsplen(const unsigned char *s)
838
if (IS_HIGHBIT_SET(*s))
839
len = 2; /* kanji? */
841
len = pg_ascii_dsplen(s); /* should be ASCII */
849
pg_gbk_mblen(const unsigned char *s)
853
if (IS_HIGHBIT_SET(*s))
854
len = 2; /* kanji? */
856
len = 1; /* should be ASCII */
861
pg_gbk_dsplen(const unsigned char *s)
865
if (IS_HIGHBIT_SET(*s))
866
len = 2; /* kanji? */
868
len = pg_ascii_dsplen(s); /* should be ASCII */
876
pg_uhc_mblen(const unsigned char *s)
880
if (IS_HIGHBIT_SET(*s))
881
len = 2; /* 2byte? */
883
len = 1; /* should be ASCII */
888
pg_uhc_dsplen(const unsigned char *s)
892
if (IS_HIGHBIT_SET(*s))
893
len = 2; /* 2byte? */
895
len = pg_ascii_dsplen(s); /* should be ASCII */
901
* * Added by Bill Huang <bhuang@redhat.com>,<bill_huanghb@ybb.ne.jp>
904
pg_gb18030_mblen(const unsigned char *s)
908
if (!IS_HIGHBIT_SET(*s))
912
if ((*(s + 1) >= 0x40 && *(s + 1) <= 0x7e) || (*(s + 1) >= 0x80 && *(s + 1) <= 0xfe))
914
else if (*(s + 1) >= 0x30 && *(s + 1) <= 0x39)
923
pg_gb18030_dsplen(const unsigned char *s)
927
if (IS_HIGHBIT_SET(*s))
930
len = pg_ascii_dsplen(s); /* ASCII */
935
*-------------------------------------------------------------------
936
* multibyte sequence validators
938
* These functions accept "s", a pointer to the first byte of a string,
939
* and "len", the remaining length of the string. If there is a validly
940
* encoded character beginning at *s, return its length in bytes; else
943
* The functions can assume that len > 0 and that *s != '\0', but they must
944
* test for and reject zeroes in any additional bytes of a multibyte character.
946
* Note that this definition allows the function for a single-byte
947
* encoding to be just "return 1".
948
*-------------------------------------------------------------------
952
pg_ascii_verifier(const unsigned char *s, int len)
957
#define IS_EUC_RANGE_VALID(c) ((c) >= 0xa1 && (c) <= 0xfe)
960
pg_eucjp_verifier(const unsigned char *s, int len)
970
case SS2: /* JIS X 0201 */
975
if (c2 < 0xa1 || c2 > 0xdf)
979
case SS3: /* JIS X 0212 */
984
if (!IS_EUC_RANGE_VALID(c2))
987
if (!IS_EUC_RANGE_VALID(c2))
992
if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
997
if (!IS_EUC_RANGE_VALID(c1))
1000
if (!IS_EUC_RANGE_VALID(c2))
1015
pg_euckr_verifier(const unsigned char *s, int len)
1023
if (IS_HIGHBIT_SET(c1))
1028
if (!IS_EUC_RANGE_VALID(c1))
1031
if (!IS_EUC_RANGE_VALID(c2))
1043
/* EUC-CN byte sequences are exactly same as EUC-KR */
1044
#define pg_euccn_verifier pg_euckr_verifier
1047
pg_euctw_verifier(const unsigned char *s, int len)
1057
case SS2: /* CNS 11643 Plane 1-7 */
1062
if (c2 < 0xa1 || c2 > 0xa7)
1065
if (!IS_EUC_RANGE_VALID(c2))
1068
if (!IS_EUC_RANGE_VALID(c2))
1072
case SS3: /* unused */
1076
if (IS_HIGHBIT_SET(c1)) /* CNS 11643 Plane 1 */
1081
/* no further range check on c1? */
1083
if (!IS_EUC_RANGE_VALID(c2))
1097
pg_johab_verifier(const unsigned char *s, int len)
1103
l = mbl = pg_johab_mblen(s);
1108
if (!IS_HIGHBIT_SET(*s))
1114
if (!IS_EUC_RANGE_VALID(c))
1121
pg_mule_verifier(const unsigned char *s, int len)
1127
l = mbl = pg_mule_mblen(s);
1135
if (!IS_HIGHBIT_SET(c))
1142
pg_latin1_verifier(const unsigned char *s, int len)
1148
pg_sjis_verifier(const unsigned char *s, int len)
1155
l = mbl = pg_sjis_mblen(s);
1160
if (l == 1) /* pg_sjis_mblen already verified it */
1165
if (!ISSJISHEAD(c1) || !ISSJISTAIL(c2))
1171
pg_big5_verifier(const unsigned char *s, int len)
1176
l = mbl = pg_big5_mblen(s);
1191
pg_gbk_verifier(const unsigned char *s, int len)
1196
l = mbl = pg_gbk_mblen(s);
1211
pg_uhc_verifier(const unsigned char *s, int len)
1216
l = mbl = pg_uhc_mblen(s);
1231
pg_gb18030_verifier(const unsigned char *s, int len)
1236
l = mbl = pg_gb18030_mblen(s);
1251
pg_utf8_verifier(const unsigned char *s, int len)
1253
int l = pg_utf_mblen(s);
1258
if (!pg_utf8_islegal(s, l))
1265
* Check for validity of a single UTF-8 encoded character
1267
* This directly implements the rules in RFC3629. The bizarre-looking
1268
* restrictions on the second byte are meant to ensure that there isn't
1269
* more than one encoding of a given Unicode character point; that is,
1270
* you may not use a longer-than-necessary byte sequence with high order
1271
* zero bits to represent a character that would fit in fewer bytes.
1272
* To do otherwise is to create security hazards (eg, create an apparent
1273
* non-ASCII character that decodes to plain ASCII).
1275
* length is assumed to have been obtained by pg_utf_mblen(), and the
1276
* caller must have checked that that many bytes are present in the buffer.
1279
pg_utf8_islegal(const unsigned char *source, int length)
1286
/* reject lengths 5 and 6 for now */
1290
if (a < 0x80 || a > 0xBF)
1295
if (a < 0x80 || a > 0xBF)
1303
if (a < 0xA0 || a > 0xBF)
1307
if (a < 0x80 || a > 0x9F)
1311
if (a < 0x90 || a > 0xBF)
1315
if (a < 0x80 || a > 0x8F)
1319
if (a < 0x80 || a > 0xBF)
1326
if (a >= 0x80 && a < 0xC2)
1336
*-------------------------------------------------------------------
1337
* encoding info table
1338
* XXX must be sorted by the same order as enum pg_enc (in mb/pg_wchar.h)
1339
*-------------------------------------------------------------------
1341
pg_wchar_tbl pg_wchar_table[] = {
1342
{pg_ascii2wchar_with_len, pg_ascii_mblen, pg_ascii_dsplen, pg_ascii_verifier, 1}, /* PG_SQL_ASCII */
1343
{pg_eucjp2wchar_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifier, 3}, /* PG_EUC_JP */
1344
{pg_euccn2wchar_with_len, pg_euccn_mblen, pg_euccn_dsplen, pg_euccn_verifier, 2}, /* PG_EUC_CN */
1345
{pg_euckr2wchar_with_len, pg_euckr_mblen, pg_euckr_dsplen, pg_euckr_verifier, 3}, /* PG_EUC_KR */
1346
{pg_euctw2wchar_with_len, pg_euctw_mblen, pg_euctw_dsplen, pg_euctw_verifier, 4}, /* PG_EUC_TW */
1347
{pg_eucjp2wchar_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifier, 3}, /* PG_EUC_JIS_2004 */
1348
{pg_utf2wchar_with_len, pg_utf_mblen, pg_utf_dsplen, pg_utf8_verifier, 4}, /* PG_UTF8 */
1349
{pg_mule2wchar_with_len, pg_mule_mblen, pg_mule_dsplen, pg_mule_verifier, 4}, /* PG_MULE_INTERNAL */
1350
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN1 */
1351
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN2 */
1352
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN3 */
1353
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN4 */
1354
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN5 */
1355
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN6 */
1356
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN7 */
1357
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN8 */
1358
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN9 */
1359
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_LATIN10 */
1360
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1256 */
1361
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1258 */
1362
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN866 */
1363
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN874 */
1364
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_KOI8R */
1365
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1251 */
1366
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1252 */
1367
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-5 */
1368
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-6 */
1369
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-7 */
1370
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* ISO-8859-8 */
1371
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1250 */
1372
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1253 */
1373
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1254 */
1374
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1255 */
1375
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_WIN1257 */
1376
{pg_latin12wchar_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifier, 1}, /* PG_KOI8U */
1377
{0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifier, 2}, /* PG_SJIS */
1378
{0, pg_big5_mblen, pg_big5_dsplen, pg_big5_verifier, 2}, /* PG_BIG5 */
1379
{0, pg_gbk_mblen, pg_gbk_dsplen, pg_gbk_verifier, 2}, /* PG_GBK */
1380
{0, pg_uhc_mblen, pg_uhc_dsplen, pg_uhc_verifier, 2}, /* PG_UHC */
1381
{0, pg_gb18030_mblen, pg_gb18030_dsplen, pg_gb18030_verifier, 4}, /* PG_GB18030 */
1382
{0, pg_johab_mblen, pg_johab_dsplen, pg_johab_verifier, 3}, /* PG_JOHAB */
1383
{0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifier, 2} /* PG_SHIFT_JIS_2004 */
1386
/* returns the byte length of a word for mule internal code */
1388
pg_mic_mblen(const unsigned char *mbstr)
1390
return pg_mule_mblen(mbstr);
1394
* Returns the byte length of a multibyte character.
1397
pg_encoding_mblen(int encoding, const char *mbstr)
1399
Assert(PG_VALID_ENCODING(encoding));
1401
return ((encoding >= 0 &&
1402
encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ?
1403
((*pg_wchar_table[encoding].mblen) ((const unsigned char *) mbstr)) :
1404
((*pg_wchar_table[PG_SQL_ASCII].mblen) ((const unsigned char *) mbstr)));
1408
* Returns the display length of a multibyte character.
1411
pg_encoding_dsplen(int encoding, const char *mbstr)
1413
Assert(PG_VALID_ENCODING(encoding));
1415
return ((encoding >= 0 &&
1416
encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ?
1417
((*pg_wchar_table[encoding].dsplen) ((const unsigned char *) mbstr)) :
1418
((*pg_wchar_table[PG_SQL_ASCII].dsplen) ((const unsigned char *) mbstr)));
1422
* Verify the first multibyte character of the given string.
1423
* Return its byte length if good, -1 if bad. (See comments above for
1424
* full details of the mbverify API.)
1427
pg_encoding_verifymb(int encoding, const char *mbstr, int len)
1429
Assert(PG_VALID_ENCODING(encoding));
1431
return ((encoding >= 0 &&
1432
encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ?
1433
((*pg_wchar_table[encoding].mbverify) ((const unsigned char *) mbstr, len)) :
1434
((*pg_wchar_table[PG_SQL_ASCII].mbverify) ((const unsigned char *) mbstr, len)));
1438
* fetch maximum length of a given encoding
1441
pg_encoding_max_length(int encoding)
1443
Assert(PG_VALID_ENCODING(encoding));
1445
return pg_wchar_table[encoding].maxmblen;
1451
* fetch maximum length of the encoding for the current database
1454
pg_database_encoding_max_length(void)
1456
return pg_wchar_table[GetDatabaseEncoding()].maxmblen;
1460
* Verify mbstr to make sure that it is validly encoded in the current
1461
* database encoding. Otherwise same as pg_verify_mbstr().
1464
pg_verifymbstr(const char *mbstr, int len, bool noError)
1467
pg_verify_mbstr_len(GetDatabaseEncoding(), mbstr, len, noError) >= 0;
1471
* Verify mbstr to make sure that it is validly encoded in the specified
1475
pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
1477
return pg_verify_mbstr_len(encoding, mbstr, len, noError) >= 0;
1481
* Verify mbstr to make sure that it is validly encoded in the specified
1484
* mbstr is not necessarily zero terminated; length of mbstr is
1487
* If OK, return length of string in the encoding.
1488
* If a problem is found, return -1 when noError is
1489
* true; when noError is false, ereport() a descriptive message.
1492
pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
1494
mbverifier mbverify;
1497
Assert(PG_VALID_ENCODING(encoding));
1500
* In single-byte encodings, we need only reject nulls (\0).
1502
if (pg_encoding_max_length(encoding) <= 1)
1504
const char *nullpos = memchr(mbstr, 0, len);
1506
if (nullpos == NULL)
1510
report_invalid_encoding(encoding, nullpos, 1);
1513
/* fetch function pointer just once */
1514
mbverify = pg_wchar_table[encoding].mbverify;
1522
/* fast path for ASCII-subset characters */
1523
if (!IS_HIGHBIT_SET(*mbstr))
1534
report_invalid_encoding(encoding, mbstr, len);
1537
l = (*mbverify) ((const unsigned char *) mbstr, len);
1543
report_invalid_encoding(encoding, mbstr, len);
1554
* check_encoding_conversion_args: check arguments of a conversion function
1556
* "expected" arguments can be either an encoding ID or -1 to indicate that
1557
* the caller will check whether it accepts the ID.
1559
* Note: the errors here are not really user-facing, so elog instead of
1560
* ereport seems sufficient. Also, we trust that the "expected" encoding
1561
* arguments are valid encoding IDs, but we don't trust the actuals.
1564
check_encoding_conversion_args(int src_encoding,
1567
int expected_src_encoding,
1568
int expected_dest_encoding)
1570
if (!PG_VALID_ENCODING(src_encoding))
1571
elog(ERROR, "invalid source encoding ID: %d", src_encoding);
1572
if (src_encoding != expected_src_encoding && expected_src_encoding >= 0)
1573
elog(ERROR, "expected source encoding \"%s\", but got \"%s\"",
1574
pg_enc2name_tbl[expected_src_encoding].name,
1575
pg_enc2name_tbl[src_encoding].name);
1576
if (!PG_VALID_ENCODING(dest_encoding))
1577
elog(ERROR, "invalid destination encoding ID: %d", dest_encoding);
1578
if (dest_encoding != expected_dest_encoding && expected_dest_encoding >= 0)
1579
elog(ERROR, "expected destination encoding \"%s\", but got \"%s\"",
1580
pg_enc2name_tbl[expected_dest_encoding].name,
1581
pg_enc2name_tbl[dest_encoding].name);
1583
elog(ERROR, "encoding conversion length must not be negative");
1587
* report_invalid_encoding: complain about invalid multibyte character
1589
* note: len is remaining length of string, not length of character;
1590
* len must be greater than zero, as we always examine the first byte.
1593
report_invalid_encoding(int encoding, const char *mbstr, int len)
1595
int l = pg_encoding_mblen(encoding, mbstr);
1596
char buf[8 * 2 + 1];
1601
jlimit = Min(l, len);
1602
jlimit = Min(jlimit, 8); /* prevent buffer overrun */
1604
for (j = 0; j < jlimit; j++)
1605
p += sprintf(p, "%02x", (unsigned char) mbstr[j]);
1608
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1609
errmsg("invalid byte sequence for encoding \"%s\": 0x%s",
1610
pg_enc2name_tbl[encoding].name,
1612
errhint("This error can also happen if the byte sequence does not "
1613
"match the encoding expected by the server, which is controlled "
1614
"by \"client_encoding\".")));
1618
* report_untranslatable_char: complain about untranslatable character
1620
* note: len is remaining length of string, not length of character;
1621
* len must be greater than zero, as we always examine the first byte.
1624
report_untranslatable_char(int src_encoding, int dest_encoding,
1625
const char *mbstr, int len)
1627
int l = pg_encoding_mblen(src_encoding, mbstr);
1628
char buf[8 * 2 + 1];
1633
jlimit = Min(l, len);
1634
jlimit = Min(jlimit, 8); /* prevent buffer overrun */
1636
for (j = 0; j < jlimit; j++)
1637
p += sprintf(p, "%02x", (unsigned char) mbstr[j]);
1640
(errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
1641
errmsg("character 0x%s of encoding \"%s\" has no equivalent in \"%s\"",
1643
pg_enc2name_tbl[src_encoding].name,
1644
pg_enc2name_tbl[dest_encoding].name)));