2 * conversion functions between pg_wchar and multibyte streams.
4 * $Id: wchar.c,v 1.30 2002/09/04 20:31:31 momjian Exp $
6 * WIN1250 client encoding updated by Pavel Behal
9 /* can be used in either frontend or backend */
11 #include "postgres_fe.h"
12 #define Assert(condition)
17 #include "mb/pg_wchar.h"
21 * conversion to pg_wchar is done by "table driven."
22 * to add an encoding support, define mb2wchar_with_len(), mblen()
23 * for the particular encoding. Note that if the encoding is only
24 * supported in the client, you don't need to define
25 * mb2wchar_with_len() function (SJIS is the case).
31 static int pg_ascii2wchar_with_len
32 (const unsigned char *from, pg_wchar *to, int len)
36 while (len > 0 && *from)
47 pg_ascii_mblen(const unsigned char *s)
56 static int pg_euc2wchar_with_len
57 (const unsigned char *from, pg_wchar *to, int len)
61 while (len > 0 && *from)
63 if (*from == SS2 && len >= 2)
69 else if (*from == SS3 && len >= 3)
73 *to |= 0x3f & *from++;
76 else if ((*from & 0x80) && len >= 2)
95 pg_euc_mblen(const unsigned char *s)
113 static int pg_eucjp2wchar_with_len
114 (const unsigned char *from, pg_wchar *to, int len)
116 return (pg_euc2wchar_with_len(from, to, len));
120 pg_eucjp_mblen(const unsigned char *s)
122 return (pg_euc_mblen(s));
128 static int pg_euckr2wchar_with_len
129 (const unsigned char *from, pg_wchar *to, int len)
131 return (pg_euc2wchar_with_len(from, to, len));
135 pg_euckr_mblen(const unsigned char *s)
137 return (pg_euc_mblen(s));
143 static int pg_euccn2wchar_with_len
144 (const unsigned char *from, pg_wchar *to, int len)
148 while (len > 0 && *from)
150 if (*from == SS2 && len >= 3)
153 *to = 0x3f00 & (*from++ << 8);
157 else if (*from == SS3 && len >= 3)
161 *to |= 0x3f & *from++;
164 else if ((*from & 0x80) && len >= 2)
183 pg_euccn_mblen(const unsigned char *s)
197 static int pg_euctw2wchar_with_len
198 (const unsigned char *from, pg_wchar *to, int len)
202 while (len > 0 && *from)
204 if (*from == SS2 && len >= 4)
212 else if (*from == SS3 && len >= 3)
216 *to |= 0x3f & *from++;
219 else if ((*from & 0x80) && len >= 2)
238 pg_euctw_mblen(const unsigned char *s)
257 pg_johab2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
259 return (pg_euc2wchar_with_len(from, to, len));
263 pg_johab_mblen(const unsigned char *s)
265 return (pg_euc_mblen(s));
269 * convert UTF-8 string to pg_wchar (UCS-2)
270 * caller should allocate enough space for "to"
271 * len: length of from.
272 * "from" not necessarily null terminated.
275 pg_utf2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
282 while (len > 0 && *from)
284 if ((*from & 0x80) == 0)
289 else if ((*from & 0xe0) == 0xc0 && len >= 2)
297 else if ((*from & 0xe0) == 0xe0 && len >= 3)
320 * returns the byte length of a UTF-8 word pointed to by s
323 pg_utf_mblen(const unsigned char *s)
327 if ((*s & 0x80) == 0)
329 else if ((*s & 0xe0) == 0xc0)
331 else if ((*s & 0xe0) == 0xe0)
337 * convert mule internal code to pg_wchar
338 * caller should allocate enough space for "to"
339 * len: length of from.
340 * "from" not necessarily null terminated.
343 pg_mule2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
347 while (len > 0 && *from)
349 if (IS_LC1(*from) && len >= 2)
355 else if (IS_LCPRV1(*from) && len >= 3)
362 else if (IS_LC2(*from) && len >= 3)
369 else if (IS_LCPRV2(*from) && len >= 4)
379 *to = (unsigned char) *from++;
390 pg_mule_mblen(const unsigned char *s)
396 else if (IS_LCPRV1(*s))
400 else if (IS_LCPRV2(*s))
413 pg_latin12wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
417 while (len > 0 && *from)
428 pg_latin1_mblen(const unsigned char *s)
437 pg_sjis_mblen(const unsigned char *s)
441 if (*s >= 0xa1 && *s <= 0xdf)
450 { /* should be ASCII */
460 pg_big5_mblen(const unsigned char *s)
469 { /* should be ASCII */
479 pg_gbk_mblen(const unsigned char *s)
488 { /* should be ASCII */
498 pg_uhc_mblen(const unsigned char *s)
507 { /* should be ASCII */
515 * * Added by Bill Huang <bhuang@redhat.com>,<bill_huanghb@ybb.ne.jp>
518 pg_gb18030_mblen(const unsigned char *s)
528 if ((*(s + 1) >= 0x40 && *(s + 1) <= 0x7e) || (*(s + 1) >= 0x80 && *(s + 1) <= 0xfe))
530 else if (*(s + 1) >= 0x30 && *(s + 1) <= 0x39)
539 pg_wchar_tbl pg_wchar_table[] = {
540 {pg_ascii2wchar_with_len, pg_ascii_mblen, 1}, /* 0; PG_SQL_ASCII */
541 {pg_eucjp2wchar_with_len, pg_eucjp_mblen, 3}, /* 1; PG_EUC_JP */
542 {pg_euccn2wchar_with_len, pg_euccn_mblen, 3}, /* 2; PG_EUC_CN */
543 {pg_euckr2wchar_with_len, pg_euckr_mblen, 3}, /* 3; PG_EUC_KR */
544 {pg_euctw2wchar_with_len, pg_euctw_mblen, 3}, /* 4; PG_EUC_TW */
545 {pg_johab2wchar_with_len, pg_johab_mblen, 3}, /* 5; PG_JOHAB */
546 {pg_utf2wchar_with_len, pg_utf_mblen, 3}, /* 6; PG_UNICODE */
547 {pg_mule2wchar_with_len, pg_mule_mblen, 3}, /* 7; PG_MULE_INTERNAL */
548 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 8; PG_LATIN1 */
549 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 9; PG_LATIN2 */
550 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 10; PG_LATIN3 */
551 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 11; PG_LATIN4 */
552 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 12; PG_LATIN5 */
553 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 13; PG_LATIN6 */
554 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 14; PG_LATIN7 */
555 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 15; PG_LATIN8 */
556 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 16; PG_LATIN9 */
557 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 17; PG_LATIN10 */
558 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 18; PG_WIN1256 */
559 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 19; PG_TCVN */
560 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 20; PG_WIN874 */
561 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 21; PG_KOI8 */
562 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 22; PG_WIN1251 */
563 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 23; PG_ALT */
564 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 24; ISO-8859-5 */
565 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 25; ISO-8859-6 */
566 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 26; ISO-8859-7 */
567 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 27; ISO-8859-8 */
568 {0, pg_sjis_mblen, 2}, /* 28; PG_SJIS */
569 {0, pg_big5_mblen, 2}, /* 29; PG_BIG5 */
570 {0, pg_gbk_mblen, 2}, /* 30; PG_GBK */
571 {0, pg_uhc_mblen, 2}, /* 31; PG_UHC */
572 {pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 32; PG_WIN1250 */
573 {0, pg_gb18030_mblen, 2} /* 33; PG_GB18030 */
576 /* returns the byte length of a word for mule internal code */
578 pg_mic_mblen(const unsigned char *mbstr)
580 return (pg_mule_mblen(mbstr));
584 * Returns the byte length of a multibyte word.
587 pg_encoding_mblen(int encoding, const unsigned char *mbstr)
589 Assert(PG_VALID_ENCODING(encoding));
591 return ((encoding >= 0 &&
592 encoding < sizeof(pg_wchar_table) / sizeof(pg_wchar_tbl)) ?
593 ((*pg_wchar_table[encoding].mblen) (mbstr)) :
594 ((*pg_wchar_table[PG_SQL_ASCII].mblen) (mbstr)));
598 * fetch maximum length of a char encoding
601 pg_encoding_max_length(int encoding)
603 Assert(PG_VALID_ENCODING(encoding));
605 return pg_wchar_table[encoding].maxmblen;
610 * Verify mbstr to make sure that it has a valid character sequence.
611 * mbstr is not necessarily NULL terminated. length of mbstr is
612 * specified by len. If an error was found, returns an error message.
613 * Note that the message is kept in a static buffer, the next invocation
614 * might break the message.
615 * If no error was found, this function returns NULL.
618 pg_verifymbstr(const unsigned char *mbstr, int len)
623 static char buf[256];
626 /* we do not check single byte encodings */
627 if (pg_database_encoding_max_length() <= 1)
630 while (len > 0 && *mbstr)
632 /* special UTF-8 check */
633 if (GetDatabaseEncoding() == PG_UTF8 &&
634 (*mbstr & 0xf8) == 0xf0)
636 snprintf(buf, sizeof(buf), "Unicode >= 0x10000 is not supoorted");
642 /* multibyte letter? */
645 for (i = 1; i < l; i++)
647 if (i > len || *(mbstr + i) == '\0' ||
650 * we assume that every multibyte letter consists of bytes
651 * being the 8th bit set
653 ((*(mbstr + i) & 0x80) == 0))
655 int remains = sizeof(buf);
658 slen = snprintf(p, remains, "Invalid %s character sequence found (0x",
659 GetDatabaseEncodingName());
663 i = ((*(mbstr + i) & 0x80) == 0) ? l : i;
665 for (j = 0; j < i; j++)
667 slen = snprintf(p, remains, "%02x",
672 snprintf(p, remains, ")");
684 * fetch maximum length of a char encoding for the current database
687 pg_database_encoding_max_length(void)
689 return pg_wchar_table[GetDatabaseEncoding()].maxmblen;