16b5e5868SGarrett D'Amore /*
26b5e5868SGarrett D'Amore * This file and its contents are supplied under the terms of the
36b5e5868SGarrett D'Amore * Common Development and Distribution License ("CDDL"), version 1.0.
45aec55ebSGarrett D'Amore * You may only use this file in accordance with the terms of version
55aec55ebSGarrett D'Amore * 1.0 of the CDDL.
66b5e5868SGarrett D'Amore *
76b5e5868SGarrett D'Amore * A full copy of the text of the CDDL should have accompanied this
86b5e5868SGarrett D'Amore * source. A copy of the CDDL is also available via the Internet at
96b5e5868SGarrett D'Amore * http://www.illumos.org/license/CDDL.
106b5e5868SGarrett D'Amore */
116b5e5868SGarrett D'Amore
126b5e5868SGarrett D'Amore /*
13475b496bSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
14*5080145bSGarrett D'Amore * Copyright 2012 Garrett D'Amore <garrett@damore.org> All rights reserved.
156b5e5868SGarrett D'Amore */
166b5e5868SGarrett D'Amore
176b5e5868SGarrett D'Amore /*
186b5e5868SGarrett D'Amore * The functions in this file convert from the standard multibyte forms
196b5e5868SGarrett D'Amore * to the wide character forms used internally by libc. Unfortunately,
206b5e5868SGarrett D'Amore * this approach means that we need a method for each and every encoding.
216b5e5868SGarrett D'Amore */
226b5e5868SGarrett D'Amore
236b5e5868SGarrett D'Amore #include <stdlib.h>
246b5e5868SGarrett D'Amore #include <wchar.h>
256b5e5868SGarrett D'Amore #include <string.h>
26*5080145bSGarrett D'Amore #include <note.h>
276b5e5868SGarrett D'Amore #include <sys/types.h>
286b5e5868SGarrett D'Amore #include "localedef.h"
296b5e5868SGarrett D'Amore
30*5080145bSGarrett D'Amore static int towide_none(wchar_t *, const char *, unsigned);
31*5080145bSGarrett D'Amore static int towide_utf8(wchar_t *, const char *, unsigned);
32*5080145bSGarrett D'Amore static int towide_big5(wchar_t *, const char *, unsigned);
33*5080145bSGarrett D'Amore static int towide_gbk(wchar_t *, const char *, unsigned);
34*5080145bSGarrett D'Amore static int towide_gb2312(wchar_t *, const char *, unsigned);
35*5080145bSGarrett D'Amore static int towide_gb18030(wchar_t *, const char *, unsigned);
36*5080145bSGarrett D'Amore static int towide_mskanji(wchar_t *, const char *, unsigned);
37*5080145bSGarrett D'Amore static int towide_euccn(wchar_t *, const char *, unsigned);
38*5080145bSGarrett D'Amore static int towide_eucjp(wchar_t *, const char *, unsigned);
39*5080145bSGarrett D'Amore static int towide_euckr(wchar_t *, const char *, unsigned);
40*5080145bSGarrett D'Amore static int towide_euctw(wchar_t *, const char *, unsigned);
416b5e5868SGarrett D'Amore
426b5e5868SGarrett D'Amore static int tomb_none(char *, wchar_t);
436b5e5868SGarrett D'Amore static int tomb_utf8(char *, wchar_t);
446b5e5868SGarrett D'Amore static int tomb_mbs(char *, wchar_t);
456b5e5868SGarrett D'Amore
46*5080145bSGarrett D'Amore static int (*_towide)(wchar_t *, const char *, unsigned) = towide_none;
476b5e5868SGarrett D'Amore static int (*_tomb)(char *, wchar_t) = tomb_none;
486b5e5868SGarrett D'Amore static const char *_encoding = "NONE";
49723fee08SGarrett D'Amore static int _nbits = 7;
506b5e5868SGarrett D'Amore
516b5e5868SGarrett D'Amore /*
526b5e5868SGarrett D'Amore * Table of supported encodings. We only bother to list the multibyte
536b5e5868SGarrett D'Amore * encodings here, because single byte locales are handed by "NONE".
546b5e5868SGarrett D'Amore */
556b5e5868SGarrett D'Amore static struct {
566b5e5868SGarrett D'Amore const char *name;
576b5e5868SGarrett D'Amore /* the name that the underlying libc implemenation uses */
586b5e5868SGarrett D'Amore const char *cname;
59723fee08SGarrett D'Amore /* the maximum number of bits required for priorities */
60723fee08SGarrett D'Amore int nbits;
61*5080145bSGarrett D'Amore int (*towide)(wchar_t *, const char *, unsigned);
626b5e5868SGarrett D'Amore int (*tomb)(char *, wchar_t);
636b5e5868SGarrett D'Amore } mb_encodings[] = {
64723fee08SGarrett D'Amore /*
65723fee08SGarrett D'Amore * UTF8 values max out at 0x1fffff (although in theory there could
66723fee08SGarrett D'Amore * be later extensions, but it won't happen.) This means we only need
67723fee08SGarrett D'Amore * 21 bits to be able to encode the entire range of priorities.
68723fee08SGarrett D'Amore */
69723fee08SGarrett D'Amore { "UTF-8", "UTF-8", 21, towide_utf8, tomb_utf8 },
70723fee08SGarrett D'Amore { "UTF8", "UTF-8", 21, towide_utf8, tomb_utf8 },
71723fee08SGarrett D'Amore { "utf8", "UTF-8", 21, towide_utf8, tomb_utf8 },
72723fee08SGarrett D'Amore { "utf-8", "UTF-8", 21, towide_utf8, tomb_utf8 },
736b5e5868SGarrett D'Amore
74723fee08SGarrett D'Amore { "EUC-CN", "EUC-CN", 16, towide_euccn, tomb_mbs },
75723fee08SGarrett D'Amore { "eucCN", "EUC-CN", 16, towide_euccn, tomb_mbs },
76723fee08SGarrett D'Amore /*
77723fee08SGarrett D'Amore * Becuase the 3-byte form of EUC-JP use the same leading byte,
78723fee08SGarrett D'Amore * only 17 bits required to provide unique priorities. (The low
79723fee08SGarrett D'Amore * bit of that first byte is set.) By setting this value low,
80723fee08SGarrett D'Amore * we can get by with only 3 bytes in the strxfrm expansion.
81723fee08SGarrett D'Amore */
82723fee08SGarrett D'Amore { "EUC-JP", "EUC-JP", 17, towide_eucjp, tomb_mbs },
83723fee08SGarrett D'Amore { "eucJP", "EUC-JP", 17, towide_eucjp, tomb_mbs },
846b5e5868SGarrett D'Amore
85723fee08SGarrett D'Amore { "EUC-KR", "EUC-KR", 16, towide_euckr, tomb_mbs },
86723fee08SGarrett D'Amore { "eucKR", "EUC-KR", 16, towide_euckr, tomb_mbs },
87723fee08SGarrett D'Amore /*
88723fee08SGarrett D'Amore * EUC-TW uses 2 bytes most of the time, but 4 bytes if the
89723fee08SGarrett D'Amore * high order byte is 0x8E. However, with 4 byte encodings,
90723fee08SGarrett D'Amore * the third byte will be A0-B0. So we only need to consider
91723fee08SGarrett D'Amore * the lower order 24 bits for collation.
92723fee08SGarrett D'Amore */
93723fee08SGarrett D'Amore { "EUC-TW", "EUC-TW", 24, towide_euctw, tomb_mbs },
94723fee08SGarrett D'Amore { "eucTW", "EUC-TW", 24, towide_euctw, tomb_mbs },
956b5e5868SGarrett D'Amore
96723fee08SGarrett D'Amore { "MS_Kanji", "MSKanji", 16, towide_mskanji, tomb_mbs },
97723fee08SGarrett D'Amore { "MSKanji", "MSKanji", 16, towide_mskanji, tomb_mbs },
98723fee08SGarrett D'Amore { "PCK", "MSKanji", 16, towide_mskanji, tomb_mbs },
99723fee08SGarrett D'Amore { "SJIS", "MSKanji", 16, towide_mskanji, tomb_mbs },
100723fee08SGarrett D'Amore { "Shift_JIS", "MSKanji", 16, towide_mskanji, tomb_mbs },
1016b5e5868SGarrett D'Amore
102723fee08SGarrett D'Amore { "BIG5", "BIG5", 16, towide_big5, tomb_mbs },
103723fee08SGarrett D'Amore { "big5", "BIG5", 16, towide_big5, tomb_mbs },
104723fee08SGarrett D'Amore { "Big5", "BIG5", 16, towide_big5, tomb_mbs },
1056b5e5868SGarrett D'Amore
106723fee08SGarrett D'Amore { "GBK", "GBK", 16, towide_gbk, tomb_mbs },
1076b5e5868SGarrett D'Amore
108723fee08SGarrett D'Amore /*
109723fee08SGarrett D'Amore * GB18030 can get away with just 31 bits. This is because the
110723fee08SGarrett D'Amore * high order bit is always set for 4 byte values, and the
111723fee08SGarrett D'Amore * at least one of the other bits in that 4 byte value will
112723fee08SGarrett D'Amore * be non-zero.
113723fee08SGarrett D'Amore */
114723fee08SGarrett D'Amore { "GB18030", "GB18030", 31, towide_gb18030, tomb_mbs },
1156b5e5868SGarrett D'Amore
116723fee08SGarrett D'Amore /*
117723fee08SGarrett D'Amore * This should probably be an aliase for euc-cn, or vice versa.
118723fee08SGarrett D'Amore */
119723fee08SGarrett D'Amore { "GB2312", "GB2312", 16, towide_gb2312, tomb_mbs },
1206b5e5868SGarrett D'Amore
1216b5e5868SGarrett D'Amore { NULL, NULL },
1226b5e5868SGarrett D'Amore };
1236b5e5868SGarrett D'Amore
1246b5e5868SGarrett D'Amore static char *
show_mb(const char * mb)1256b5e5868SGarrett D'Amore show_mb(const char *mb)
1266b5e5868SGarrett D'Amore {
1276b5e5868SGarrett D'Amore static char buf[64];
1286b5e5868SGarrett D'Amore
1296b5e5868SGarrett D'Amore /* ASCII stuff we just print */
1306b5e5868SGarrett D'Amore if (isascii(*mb) && isgraph(*mb)) {
1316b5e5868SGarrett D'Amore buf[0] = *mb;
1326b5e5868SGarrett D'Amore buf[1] = 0;
1336b5e5868SGarrett D'Amore return (buf);
1346b5e5868SGarrett D'Amore }
1356b5e5868SGarrett D'Amore buf[0] = 0;
1366b5e5868SGarrett D'Amore while (*mb != 0) {
1376b5e5868SGarrett D'Amore char scr[8];
1386b5e5868SGarrett D'Amore (void) snprintf(scr, sizeof (scr), "\\x%02x", *mb);
1396b5e5868SGarrett D'Amore (void) strlcat(buf, scr, sizeof (buf));
1406b5e5868SGarrett D'Amore mb++;
1416b5e5868SGarrett D'Amore }
1426b5e5868SGarrett D'Amore return (buf);
1436b5e5868SGarrett D'Amore }
1446b5e5868SGarrett D'Amore
1456b5e5868SGarrett D'Amore static char *widemsg;
1466b5e5868SGarrett D'Amore
1476b5e5868SGarrett D'Amore void
werr(const char * fmt,...)1486b5e5868SGarrett D'Amore werr(const char *fmt, ...)
1496b5e5868SGarrett D'Amore {
1506b5e5868SGarrett D'Amore char *msg;
1516b5e5868SGarrett D'Amore
1526b5e5868SGarrett D'Amore va_list va;
1536b5e5868SGarrett D'Amore va_start(va, fmt);
1546b5e5868SGarrett D'Amore (void) vasprintf(&msg, fmt, va);
1556b5e5868SGarrett D'Amore va_end(va);
1566b5e5868SGarrett D'Amore
1576b5e5868SGarrett D'Amore free(widemsg);
1586b5e5868SGarrett D'Amore widemsg = msg;
1596b5e5868SGarrett D'Amore }
1606b5e5868SGarrett D'Amore
1616b5e5868SGarrett D'Amore /*
1626b5e5868SGarrett D'Amore * This is used for 8-bit encodings.
1636b5e5868SGarrett D'Amore */
1646b5e5868SGarrett D'Amore int
towide_none(wchar_t * c,const char * mb,unsigned n)165*5080145bSGarrett D'Amore towide_none(wchar_t *c, const char *mb, unsigned n)
1666b5e5868SGarrett D'Amore {
167*5080145bSGarrett D'Amore _NOTE(ARGUNUSED(n));
168*5080145bSGarrett D'Amore
1696b5e5868SGarrett D'Amore if (mb_cur_max != 1) {
1706b5e5868SGarrett D'Amore werr("invalid or unsupported multibyte locale");
1716b5e5868SGarrett D'Amore return (-1);
1726b5e5868SGarrett D'Amore }
1736b5e5868SGarrett D'Amore *c = (uint8_t)*mb;
1746b5e5868SGarrett D'Amore return (1);
1756b5e5868SGarrett D'Amore }
1766b5e5868SGarrett D'Amore
1776b5e5868SGarrett D'Amore int
tomb_none(char * mb,wchar_t wc)1786b5e5868SGarrett D'Amore tomb_none(char *mb, wchar_t wc)
1796b5e5868SGarrett D'Amore {
1806b5e5868SGarrett D'Amore if (mb_cur_max != 1) {
1816b5e5868SGarrett D'Amore werr("invalid or unsupported multibyte locale");
1826b5e5868SGarrett D'Amore return (-1);
1836b5e5868SGarrett D'Amore }
1846b5e5868SGarrett D'Amore *(uint8_t *)mb = (wc & 0xff);
1856b5e5868SGarrett D'Amore mb[1] = 0;
1866b5e5868SGarrett D'Amore return (1);
1876b5e5868SGarrett D'Amore }
1886b5e5868SGarrett D'Amore
1896b5e5868SGarrett D'Amore /*
1906b5e5868SGarrett D'Amore * UTF-8 stores wide characters in UTF-32 form.
1916b5e5868SGarrett D'Amore */
1926b5e5868SGarrett D'Amore int
towide_utf8(wchar_t * wc,const char * mb,unsigned n)193*5080145bSGarrett D'Amore towide_utf8(wchar_t *wc, const char *mb, unsigned n)
1946b5e5868SGarrett D'Amore {
1956b5e5868SGarrett D'Amore wchar_t c;
1966b5e5868SGarrett D'Amore int nb;
1976b5e5868SGarrett D'Amore int lv; /* lowest legal value */
1986b5e5868SGarrett D'Amore int i;
1996b5e5868SGarrett D'Amore const uint8_t *s = (const uint8_t *)mb;
2006b5e5868SGarrett D'Amore
2016b5e5868SGarrett D'Amore c = *s;
2026b5e5868SGarrett D'Amore
2036b5e5868SGarrett D'Amore if ((c & 0x80) == 0) {
2046b5e5868SGarrett D'Amore /* 7-bit ASCII */
2056b5e5868SGarrett D'Amore *wc = c;
2066b5e5868SGarrett D'Amore return (1);
2076b5e5868SGarrett D'Amore } else if ((c & 0xe0) == 0xc0) {
2086b5e5868SGarrett D'Amore /* u80-u7ff - two bytes encoded */
2096b5e5868SGarrett D'Amore nb = 2;
2106b5e5868SGarrett D'Amore lv = 0x80;
2116b5e5868SGarrett D'Amore c &= ~0xe0;
2126b5e5868SGarrett D'Amore } else if ((c & 0xf0) == 0xe0) {
2136b5e5868SGarrett D'Amore /* u800-uffff - three bytes encoded */
2146b5e5868SGarrett D'Amore nb = 3;
2156b5e5868SGarrett D'Amore lv = 0x800;
2166b5e5868SGarrett D'Amore c &= ~0xf0;
2176b5e5868SGarrett D'Amore } else if ((c & 0xf8) == 0xf0) {
2186b5e5868SGarrett D'Amore /* u1000-u1fffff - four bytes encoded */
2196b5e5868SGarrett D'Amore nb = 4;
2206b5e5868SGarrett D'Amore lv = 0x1000;
2216b5e5868SGarrett D'Amore c &= ~0xf8;
2226b5e5868SGarrett D'Amore } else {
2236b5e5868SGarrett D'Amore /* 5 and 6 byte encodings are not legal unicode */
2246b5e5868SGarrett D'Amore werr("utf8 encoding too large (%s)", show_mb(mb));
2256b5e5868SGarrett D'Amore return (-1);
2266b5e5868SGarrett D'Amore }
2276b5e5868SGarrett D'Amore if (nb > n) {
2286b5e5868SGarrett D'Amore werr("incomplete utf8 sequence (%s)", show_mb(mb));
2296b5e5868SGarrett D'Amore return (-1);
2306b5e5868SGarrett D'Amore }
2316b5e5868SGarrett D'Amore
2326b5e5868SGarrett D'Amore for (i = 1; i < nb; i++) {
2336b5e5868SGarrett D'Amore if (((s[i]) & 0xc0) != 0x80) {
2346b5e5868SGarrett D'Amore werr("illegal utf8 byte (%x)", s[i]);
2356b5e5868SGarrett D'Amore return (-1);
2366b5e5868SGarrett D'Amore }
2376b5e5868SGarrett D'Amore c <<= 6;
2386b5e5868SGarrett D'Amore c |= (s[i] & 0x3f);
2396b5e5868SGarrett D'Amore }
2406b5e5868SGarrett D'Amore
2416b5e5868SGarrett D'Amore if (c < lv) {
2426b5e5868SGarrett D'Amore werr("illegal redundant utf8 encoding (%s)", show_mb(mb));
2436b5e5868SGarrett D'Amore return (-1);
2446b5e5868SGarrett D'Amore }
2456b5e5868SGarrett D'Amore *wc = c;
2466b5e5868SGarrett D'Amore return (nb);
2476b5e5868SGarrett D'Amore }
2486b5e5868SGarrett D'Amore
2496b5e5868SGarrett D'Amore int
tomb_utf8(char * mb,wchar_t wc)2506b5e5868SGarrett D'Amore tomb_utf8(char *mb, wchar_t wc)
2516b5e5868SGarrett D'Amore {
2526b5e5868SGarrett D'Amore uint8_t *s = (uint8_t *)mb;
2536b5e5868SGarrett D'Amore uint8_t msk;
2546b5e5868SGarrett D'Amore int cnt;
2556b5e5868SGarrett D'Amore int i;
2566b5e5868SGarrett D'Amore
2576b5e5868SGarrett D'Amore if (wc <= 0x7f) {
2586b5e5868SGarrett D'Amore s[0] = wc & 0x7f;
2596b5e5868SGarrett D'Amore s[1] = 0;
2606b5e5868SGarrett D'Amore return (1);
2616b5e5868SGarrett D'Amore }
2626b5e5868SGarrett D'Amore if (wc <= 0x7ff) {
2636b5e5868SGarrett D'Amore cnt = 2;
2646b5e5868SGarrett D'Amore msk = 0xc0;
2656b5e5868SGarrett D'Amore } else if (wc <= 0xffff) {
2666b5e5868SGarrett D'Amore cnt = 3;
2676b5e5868SGarrett D'Amore msk = 0xe0;
2686b5e5868SGarrett D'Amore } else if (wc <= 0x1fffff) {
2696b5e5868SGarrett D'Amore cnt = 4;
2706b5e5868SGarrett D'Amore msk = 0xf0;
2716b5e5868SGarrett D'Amore } else {
2726b5e5868SGarrett D'Amore werr("illegal uf8 char (%x)", wc);
2736b5e5868SGarrett D'Amore return (-1);
2746b5e5868SGarrett D'Amore }
2756b5e5868SGarrett D'Amore for (i = cnt - 1; i; i--) {
2766b5e5868SGarrett D'Amore s[i] = (wc & 0x3f) | 0x80;
2776b5e5868SGarrett D'Amore wc >>= 6;
2786b5e5868SGarrett D'Amore }
2796b5e5868SGarrett D'Amore s[0] = (msk) | wc;
2806b5e5868SGarrett D'Amore s[cnt] = 0;
2816b5e5868SGarrett D'Amore return (cnt);
2826b5e5868SGarrett D'Amore }
2836b5e5868SGarrett D'Amore
2846b5e5868SGarrett D'Amore /*
2856b5e5868SGarrett D'Amore * Several encodings share a simplistic dual byte encoding. In these
2866b5e5868SGarrett D'Amore * forms, they all indicate that a two byte sequence is to be used if
2876b5e5868SGarrett D'Amore * the first byte has its high bit set. They all store this simple
2886b5e5868SGarrett D'Amore * encoding as a 16-bit value, although a great many of the possible
2896b5e5868SGarrett D'Amore * code points are not used in most character sets. This gives a possible
2906b5e5868SGarrett D'Amore * set of just over 32,000 valid code points.
2916b5e5868SGarrett D'Amore *
2926b5e5868SGarrett D'Amore * 0x00 - 0x7f - 1 byte encoding
2936b5e5868SGarrett D'Amore * 0x80 - 0x7fff - illegal
2946b5e5868SGarrett D'Amore * 0x8000 - 0xffff - 2 byte encoding
2956b5e5868SGarrett D'Amore */
2966b5e5868SGarrett D'Amore static int
towide_dbcs(wchar_t * wc,const char * mb,unsigned n)297*5080145bSGarrett D'Amore towide_dbcs(wchar_t *wc, const char *mb, unsigned n)
2986b5e5868SGarrett D'Amore {
2996b5e5868SGarrett D'Amore wchar_t c;
3006b5e5868SGarrett D'Amore
3016b5e5868SGarrett D'Amore c = *(uint8_t *)mb;
3026b5e5868SGarrett D'Amore
3036b5e5868SGarrett D'Amore if ((c & 0x80) == 0) {
3046b5e5868SGarrett D'Amore /* 7-bit */
3056b5e5868SGarrett D'Amore *wc = c;
3066b5e5868SGarrett D'Amore return (1);
3076b5e5868SGarrett D'Amore }
3086b5e5868SGarrett D'Amore if (n < 2) {
3096b5e5868SGarrett D'Amore werr("incomplete character sequence (%s)", show_mb(mb));
3106b5e5868SGarrett D'Amore return (-1);
3116b5e5868SGarrett D'Amore }
3126b5e5868SGarrett D'Amore
3136b5e5868SGarrett D'Amore /* Store both bytes as a single 16-bit wide. */
3146b5e5868SGarrett D'Amore c <<= 8;
3156b5e5868SGarrett D'Amore c |= (uint8_t)(mb[1]);
3166b5e5868SGarrett D'Amore *wc = c;
3176b5e5868SGarrett D'Amore return (2);
3186b5e5868SGarrett D'Amore }
3196b5e5868SGarrett D'Amore
3206b5e5868SGarrett D'Amore /*
3216b5e5868SGarrett D'Amore * Most multibyte locales just convert the wide character to the multibyte
3226b5e5868SGarrett D'Amore * form by stripping leading null bytes, and writing the 32-bit quantity
3236b5e5868SGarrett D'Amore * in big-endian order.
3246b5e5868SGarrett D'Amore */
3256b5e5868SGarrett D'Amore int
tomb_mbs(char * mb,wchar_t wc)3266b5e5868SGarrett D'Amore tomb_mbs(char *mb, wchar_t wc)
3276b5e5868SGarrett D'Amore {
3286b5e5868SGarrett D'Amore uint8_t *s = (uint8_t *)mb;
3296b5e5868SGarrett D'Amore int n = 0, c;
3306b5e5868SGarrett D'Amore
3316b5e5868SGarrett D'Amore if ((wc & 0xff000000U) != 0) {
3326b5e5868SGarrett D'Amore n = 4;
3336b5e5868SGarrett D'Amore } else if ((wc & 0x00ff0000U) != 0) {
3346b5e5868SGarrett D'Amore n = 3;
3356b5e5868SGarrett D'Amore } else if ((wc & 0x0000ff00U) != 0) {
3366b5e5868SGarrett D'Amore n = 2;
3376b5e5868SGarrett D'Amore } else {
3386b5e5868SGarrett D'Amore n = 1;
3396b5e5868SGarrett D'Amore }
3406b5e5868SGarrett D'Amore c = n;
3416b5e5868SGarrett D'Amore while (n) {
3426b5e5868SGarrett D'Amore n--;
3436b5e5868SGarrett D'Amore s[n] = wc & 0xff;
3446b5e5868SGarrett D'Amore wc >>= 8;
3456b5e5868SGarrett D'Amore }
3466b5e5868SGarrett D'Amore /* ensure null termination */
3476b5e5868SGarrett D'Amore s[c] = 0;
3486b5e5868SGarrett D'Amore return (c);
3496b5e5868SGarrett D'Amore }
3506b5e5868SGarrett D'Amore
3516b5e5868SGarrett D'Amore
3526b5e5868SGarrett D'Amore /*
3536b5e5868SGarrett D'Amore * big5 is a simple dual byte character set.
3546b5e5868SGarrett D'Amore */
3556b5e5868SGarrett D'Amore int
towide_big5(wchar_t * wc,const char * mb,unsigned n)356*5080145bSGarrett D'Amore towide_big5(wchar_t *wc, const char *mb, unsigned n)
3576b5e5868SGarrett D'Amore {
3586b5e5868SGarrett D'Amore return (towide_dbcs(wc, mb, n));
3596b5e5868SGarrett D'Amore }
3606b5e5868SGarrett D'Amore
3616b5e5868SGarrett D'Amore /*
3626b5e5868SGarrett D'Amore * GBK encodes wides in the same way that big5 does, the high order
3636b5e5868SGarrett D'Amore * bit of the first byte indicates a double byte character.
3646b5e5868SGarrett D'Amore */
3656b5e5868SGarrett D'Amore int
towide_gbk(wchar_t * wc,const char * mb,unsigned n)366*5080145bSGarrett D'Amore towide_gbk(wchar_t *wc, const char *mb, unsigned n)
3676b5e5868SGarrett D'Amore {
3686b5e5868SGarrett D'Amore return (towide_dbcs(wc, mb, n));
3696b5e5868SGarrett D'Amore }
3706b5e5868SGarrett D'Amore
3716b5e5868SGarrett D'Amore /*
3726b5e5868SGarrett D'Amore * GB2312 is another DBCS. Its cleaner than others in that the second
3736b5e5868SGarrett D'Amore * byte does not encode ASCII, but it supports characters.
3746b5e5868SGarrett D'Amore */
3756b5e5868SGarrett D'Amore int
towide_gb2312(wchar_t * wc,const char * mb,unsigned n)376*5080145bSGarrett D'Amore towide_gb2312(wchar_t *wc, const char *mb, unsigned n)
3776b5e5868SGarrett D'Amore {
3786b5e5868SGarrett D'Amore return (towide_dbcs(wc, mb, n));
3796b5e5868SGarrett D'Amore }
3806b5e5868SGarrett D'Amore
3816b5e5868SGarrett D'Amore /*
3826b5e5868SGarrett D'Amore * GB18030. This encodes as 8, 16, or 32-bits.
3836b5e5868SGarrett D'Amore * 7-bit values are in 1 byte, 4 byte sequences are used when
3846b5e5868SGarrett D'Amore * the second byte encodes 0x30-39 and all other sequences are 2 bytes.
3856b5e5868SGarrett D'Amore */
3866b5e5868SGarrett D'Amore int
towide_gb18030(wchar_t * wc,const char * mb,unsigned n)387*5080145bSGarrett D'Amore towide_gb18030(wchar_t *wc, const char *mb, unsigned n)
3886b5e5868SGarrett D'Amore {
3896b5e5868SGarrett D'Amore wchar_t c;
3906b5e5868SGarrett D'Amore
3916b5e5868SGarrett D'Amore c = *(uint8_t *)mb;
3926b5e5868SGarrett D'Amore
3936b5e5868SGarrett D'Amore if ((c & 0x80) == 0) {
3946b5e5868SGarrett D'Amore /* 7-bit */
3956b5e5868SGarrett D'Amore *wc = c;
3966b5e5868SGarrett D'Amore return (1);
3976b5e5868SGarrett D'Amore }
3986b5e5868SGarrett D'Amore if (n < 2) {
3996b5e5868SGarrett D'Amore werr("incomplete character sequence (%s)", show_mb(mb));
4006b5e5868SGarrett D'Amore return (-1);
4016b5e5868SGarrett D'Amore }
4026b5e5868SGarrett D'Amore
4036b5e5868SGarrett D'Amore /* pull in the second byte */
4046b5e5868SGarrett D'Amore c <<= 8;
4056b5e5868SGarrett D'Amore c |= (uint8_t)(mb[1]);
4066b5e5868SGarrett D'Amore
4076b5e5868SGarrett D'Amore if (((c & 0xff) >= 0x30) && ((c & 0xff) <= 0x39)) {
4086b5e5868SGarrett D'Amore if (n < 4) {
4096b5e5868SGarrett D'Amore werr("incomplete 4-byte character sequence (%s)",
4106b5e5868SGarrett D'Amore show_mb(mb));
4116b5e5868SGarrett D'Amore return (-1);
4126b5e5868SGarrett D'Amore }
4136b5e5868SGarrett D'Amore c <<= 8;
4146b5e5868SGarrett D'Amore c |= (uint8_t)(mb[2]);
4156b5e5868SGarrett D'Amore c <<= 8;
4166b5e5868SGarrett D'Amore c |= (uint8_t)(mb[3]);
4176b5e5868SGarrett D'Amore *wc = c;
4186b5e5868SGarrett D'Amore return (4);
4196b5e5868SGarrett D'Amore }
4206b5e5868SGarrett D'Amore
4216b5e5868SGarrett D'Amore *wc = c;
4226b5e5868SGarrett D'Amore return (2);
4236b5e5868SGarrett D'Amore }
4246b5e5868SGarrett D'Amore
4256b5e5868SGarrett D'Amore /*
4266b5e5868SGarrett D'Amore * MS-Kanji (aka SJIS) is almost a clean DBCS like the others, but it
4276b5e5868SGarrett D'Amore * also has a range of single byte characters above 0x80. (0xa1-0xdf).
4286b5e5868SGarrett D'Amore */
4296b5e5868SGarrett D'Amore int
towide_mskanji(wchar_t * wc,const char * mb,unsigned n)430*5080145bSGarrett D'Amore towide_mskanji(wchar_t *wc, const char *mb, unsigned n)
4316b5e5868SGarrett D'Amore {
4326b5e5868SGarrett D'Amore wchar_t c;
4336b5e5868SGarrett D'Amore
4346b5e5868SGarrett D'Amore c = *(uint8_t *)mb;
4356b5e5868SGarrett D'Amore
4366b5e5868SGarrett D'Amore if ((c < 0x80) || ((c > 0xa0) && (c < 0xe0))) {
4376b5e5868SGarrett D'Amore /* 7-bit */
4386b5e5868SGarrett D'Amore *wc = c;
439*5080145bSGarrett D'Amore return (1);
4406b5e5868SGarrett D'Amore }
4416b5e5868SGarrett D'Amore
4426b5e5868SGarrett D'Amore if (n < 2) {
4436b5e5868SGarrett D'Amore werr("incomplete character sequence (%s)", show_mb(mb));
4446b5e5868SGarrett D'Amore return (-1);
4456b5e5868SGarrett D'Amore }
4466b5e5868SGarrett D'Amore
4476b5e5868SGarrett D'Amore /* Store both bytes as a single 16-bit wide. */
4486b5e5868SGarrett D'Amore c <<= 8;
4496b5e5868SGarrett D'Amore c |= (uint8_t)(mb[1]);
4506b5e5868SGarrett D'Amore *wc = c;
4516b5e5868SGarrett D'Amore return (2);
4526b5e5868SGarrett D'Amore }
4536b5e5868SGarrett D'Amore
4546b5e5868SGarrett D'Amore /*
4556b5e5868SGarrett D'Amore * EUC forms. EUC encodings are "variable". FreeBSD carries some additional
4566b5e5868SGarrett D'Amore * variable data to encode these, but we're going to treat each as independent
4576b5e5868SGarrett D'Amore * instead. Its the only way we can sensibly move forward.
4586b5e5868SGarrett D'Amore *
4596b5e5868SGarrett D'Amore * Note that the way in which the different EUC forms vary is how wide
4606b5e5868SGarrett D'Amore * CS2 and CS3 are and what the first byte of them is.
4616b5e5868SGarrett D'Amore */
4626b5e5868SGarrett D'Amore static int
towide_euc_impl(wchar_t * wc,const char * mb,unsigned n,uint8_t cs2,uint8_t cs2width,uint8_t cs3,uint8_t cs3width)463*5080145bSGarrett D'Amore towide_euc_impl(wchar_t *wc, const char *mb, unsigned n,
4646b5e5868SGarrett D'Amore uint8_t cs2, uint8_t cs2width, uint8_t cs3, uint8_t cs3width)
4656b5e5868SGarrett D'Amore {
4666b5e5868SGarrett D'Amore int i;
4676b5e5868SGarrett D'Amore int width;
4686b5e5868SGarrett D'Amore wchar_t c;
4696b5e5868SGarrett D'Amore
4706b5e5868SGarrett D'Amore c = *(uint8_t *)mb;
4716b5e5868SGarrett D'Amore
4726b5e5868SGarrett D'Amore /*
4736b5e5868SGarrett D'Amore * All variations of EUC encode 7-bit ASCII as one byte, and use
4746b5e5868SGarrett D'Amore * additional bytes for more than that.
4756b5e5868SGarrett D'Amore */
4766b5e5868SGarrett D'Amore if ((c & 0x80) == 0) {
4776b5e5868SGarrett D'Amore /* 7-bit */
4786b5e5868SGarrett D'Amore *wc = c;
4796b5e5868SGarrett D'Amore return (1);
4806b5e5868SGarrett D'Amore }
4816b5e5868SGarrett D'Amore
4826b5e5868SGarrett D'Amore /*
4836b5e5868SGarrett D'Amore * All EUC variants reserve 0xa1-0xff to identify CS1, which
4846b5e5868SGarrett D'Amore * is always two bytes wide. Note that unused CS will be zero,
4856b5e5868SGarrett D'Amore * and that cannot be true because we know that the high order
4866b5e5868SGarrett D'Amore * bit must be set.
4876b5e5868SGarrett D'Amore */
4886b5e5868SGarrett D'Amore if (c >= 0xa1) {
4896b5e5868SGarrett D'Amore width = 2;
4906b5e5868SGarrett D'Amore } else if (c == cs2) {
4916b5e5868SGarrett D'Amore width = cs2width;
4926b5e5868SGarrett D'Amore } else if (c == cs3) {
4936b5e5868SGarrett D'Amore width = cs3width;
4946b5e5868SGarrett D'Amore }
4956b5e5868SGarrett D'Amore
4966b5e5868SGarrett D'Amore if (n < width) {
4976b5e5868SGarrett D'Amore werr("incomplete character sequence (%s)", show_mb(mb));
4986b5e5868SGarrett D'Amore return (-1);
4996b5e5868SGarrett D'Amore }
5006b5e5868SGarrett D'Amore
5016b5e5868SGarrett D'Amore for (i = 1; i < width; i++) {
5026b5e5868SGarrett D'Amore /* pull in the next byte */
5036b5e5868SGarrett D'Amore c <<= 8;
5046b5e5868SGarrett D'Amore c |= (uint8_t)(mb[i]);
5056b5e5868SGarrett D'Amore }
5066b5e5868SGarrett D'Amore
5076b5e5868SGarrett D'Amore *wc = c;
5086b5e5868SGarrett D'Amore return (width);
5096b5e5868SGarrett D'Amore }
5106b5e5868SGarrett D'Amore
5116b5e5868SGarrett D'Amore /*
5126b5e5868SGarrett D'Amore * EUC-CN encodes as follows:
5136b5e5868SGarrett D'Amore *
5146b5e5868SGarrett D'Amore * Code set 0 (ASCII): 0x21-0x7E
5156b5e5868SGarrett D'Amore * Code set 1 (CNS 11643-1992 Plane 1): 0xA1A1-0xFEFE
516723fee08SGarrett D'Amore * Code set 2: unused
5176b5e5868SGarrett D'Amore * Code set 3: unused
5186b5e5868SGarrett D'Amore */
5196b5e5868SGarrett D'Amore int
towide_euccn(wchar_t * wc,const char * mb,unsigned n)520*5080145bSGarrett D'Amore towide_euccn(wchar_t *wc, const char *mb, unsigned n)
5216b5e5868SGarrett D'Amore {
5226b5e5868SGarrett D'Amore return (towide_euc_impl(wc, mb, n, 0x8e, 4, 0, 0));
5236b5e5868SGarrett D'Amore }
5246b5e5868SGarrett D'Amore
5256b5e5868SGarrett D'Amore /*
5266b5e5868SGarrett D'Amore * EUC-JP encodes as follows:
5276b5e5868SGarrett D'Amore *
5286b5e5868SGarrett D'Amore * Code set 0 (ASCII or JIS X 0201-1976 Roman): 0x21-0x7E
5296b5e5868SGarrett D'Amore * Code set 1 (JIS X 0208): 0xA1A1-0xFEFE
5306b5e5868SGarrett D'Amore * Code set 2 (half-width katakana): 0x8EA1-0x8EDF
5316b5e5868SGarrett D'Amore * Code set 3 (JIS X 0212-1990): 0x8FA1A1-0x8FFEFE
5326b5e5868SGarrett D'Amore */
5336b5e5868SGarrett D'Amore int
towide_eucjp(wchar_t * wc,const char * mb,unsigned n)534*5080145bSGarrett D'Amore towide_eucjp(wchar_t *wc, const char *mb, unsigned n)
5356b5e5868SGarrett D'Amore {
5366b5e5868SGarrett D'Amore return (towide_euc_impl(wc, mb, n, 0x8e, 2, 0x8f, 3));
5376b5e5868SGarrett D'Amore }
5386b5e5868SGarrett D'Amore
5396b5e5868SGarrett D'Amore /*
5406b5e5868SGarrett D'Amore * EUC-KR encodes as follows:
5416b5e5868SGarrett D'Amore *
5426b5e5868SGarrett D'Amore * Code set 0 (ASCII or KS C 5636-1993): 0x21-0x7E
5436b5e5868SGarrett D'Amore * Code set 1 (KS C 5601-1992): 0xA1A1-0xFEFE
5446b5e5868SGarrett D'Amore * Code set 2: unused
5456b5e5868SGarrett D'Amore * Code set 3: unused
5466b5e5868SGarrett D'Amore */
5476b5e5868SGarrett D'Amore int
towide_euckr(wchar_t * wc,const char * mb,unsigned n)548*5080145bSGarrett D'Amore towide_euckr(wchar_t *wc, const char *mb, unsigned n)
5496b5e5868SGarrett D'Amore {
5506b5e5868SGarrett D'Amore return (towide_euc_impl(wc, mb, n, 0, 0, 0, 0));
5516b5e5868SGarrett D'Amore }
5526b5e5868SGarrett D'Amore
5536b5e5868SGarrett D'Amore /*
5546b5e5868SGarrett D'Amore * EUC-TW encodes as follows:
5556b5e5868SGarrett D'Amore *
5566b5e5868SGarrett D'Amore * Code set 0 (ASCII): 0x21-0x7E
5576b5e5868SGarrett D'Amore * Code set 1 (CNS 11643-1992 Plane 1): 0xA1A1-0xFEFE
5586b5e5868SGarrett D'Amore * Code set 2 (CNS 11643-1992 Planes 1-16): 0x8EA1A1A1-0x8EB0FEFE
5596b5e5868SGarrett D'Amore * Code set 3: unused
5606b5e5868SGarrett D'Amore */
5616b5e5868SGarrett D'Amore int
towide_euctw(wchar_t * wc,const char * mb,unsigned n)562*5080145bSGarrett D'Amore towide_euctw(wchar_t *wc, const char *mb, unsigned n)
5636b5e5868SGarrett D'Amore {
5646b5e5868SGarrett D'Amore return (towide_euc_impl(wc, mb, n, 0x8e, 4, 0, 0));
5656b5e5868SGarrett D'Amore }
5666b5e5868SGarrett D'Amore
5676b5e5868SGarrett D'Amore /*
5686b5e5868SGarrett D'Amore * Public entry points.
5696b5e5868SGarrett D'Amore */
5706b5e5868SGarrett D'Amore
5716b5e5868SGarrett D'Amore int
to_wide(wchar_t * wc,const char * mb)5726b5e5868SGarrett D'Amore to_wide(wchar_t *wc, const char *mb)
5736b5e5868SGarrett D'Amore {
5746b5e5868SGarrett D'Amore /* this won't fail hard */
575*5080145bSGarrett D'Amore return (_towide(wc, mb, strlen(mb)));
5766b5e5868SGarrett D'Amore }
5776b5e5868SGarrett D'Amore
5786b5e5868SGarrett D'Amore int
to_mb(char * mb,wchar_t wc)5796b5e5868SGarrett D'Amore to_mb(char *mb, wchar_t wc)
5806b5e5868SGarrett D'Amore {
5816b5e5868SGarrett D'Amore int rv;
5826b5e5868SGarrett D'Amore
5836b5e5868SGarrett D'Amore if ((rv = _tomb(mb, wc)) < 0) {
5846b5e5868SGarrett D'Amore errf(widemsg);
5856b5e5868SGarrett D'Amore free(widemsg);
5866b5e5868SGarrett D'Amore widemsg = NULL;
5876b5e5868SGarrett D'Amore }
5886b5e5868SGarrett D'Amore return (rv);
5896b5e5868SGarrett D'Amore }
5906b5e5868SGarrett D'Amore
5916b5e5868SGarrett D'Amore char *
to_mb_string(const wchar_t * wcs)5926b5e5868SGarrett D'Amore to_mb_string(const wchar_t *wcs)
5936b5e5868SGarrett D'Amore {
5946b5e5868SGarrett D'Amore char *mbs;
5956b5e5868SGarrett D'Amore char *ptr;
5966b5e5868SGarrett D'Amore int len;
5976b5e5868SGarrett D'Amore
5986b5e5868SGarrett D'Amore mbs = malloc((wcslen(wcs) * mb_cur_max) + 1);
5996b5e5868SGarrett D'Amore if (mbs == NULL) {
6006b5e5868SGarrett D'Amore errf("out of memory");
6016b5e5868SGarrett D'Amore return (NULL);
6026b5e5868SGarrett D'Amore }
6036b5e5868SGarrett D'Amore ptr = mbs;
6046b5e5868SGarrett D'Amore while (*wcs) {
6056b5e5868SGarrett D'Amore if ((len = to_mb(ptr, *wcs)) < 0) {
6066b5e5868SGarrett D'Amore INTERR;
6076b5e5868SGarrett D'Amore free(mbs);
6086b5e5868SGarrett D'Amore return (NULL);
6096b5e5868SGarrett D'Amore }
6106b5e5868SGarrett D'Amore wcs++;
6116b5e5868SGarrett D'Amore ptr += len;
6126b5e5868SGarrett D'Amore }
6136b5e5868SGarrett D'Amore *ptr = 0;
6146b5e5868SGarrett D'Amore return (mbs);
6156b5e5868SGarrett D'Amore }
6166b5e5868SGarrett D'Amore
6176b5e5868SGarrett D'Amore void
set_wide_encoding(const char * encoding)6186b5e5868SGarrett D'Amore set_wide_encoding(const char *encoding)
6196b5e5868SGarrett D'Amore {
6206b5e5868SGarrett D'Amore int i;
6216b5e5868SGarrett D'Amore
6226b5e5868SGarrett D'Amore _towide = towide_none;
6236b5e5868SGarrett D'Amore _tomb = tomb_none;
6246b5e5868SGarrett D'Amore _encoding = "NONE";
625723fee08SGarrett D'Amore _nbits = 8;
6266b5e5868SGarrett D'Amore
6276b5e5868SGarrett D'Amore for (i = 0; mb_encodings[i].name; i++) {
6286b5e5868SGarrett D'Amore if (strcasecmp(encoding, mb_encodings[i].name) == 0) {
6296b5e5868SGarrett D'Amore _towide = mb_encodings[i].towide;
6306b5e5868SGarrett D'Amore _tomb = mb_encodings[i].tomb;
6316b5e5868SGarrett D'Amore _encoding = mb_encodings[i].cname;
632723fee08SGarrett D'Amore _nbits = mb_encodings[i].nbits;
633723fee08SGarrett D'Amore break;
6346b5e5868SGarrett D'Amore }
6356b5e5868SGarrett D'Amore }
6366b5e5868SGarrett D'Amore }
6376b5e5868SGarrett D'Amore
6386b5e5868SGarrett D'Amore const char *
get_wide_encoding(void)6396b5e5868SGarrett D'Amore get_wide_encoding(void)
6406b5e5868SGarrett D'Amore {
6416b5e5868SGarrett D'Amore return (_encoding);
6426b5e5868SGarrett D'Amore }
643723fee08SGarrett D'Amore
644723fee08SGarrett D'Amore int
max_wide(void)645723fee08SGarrett D'Amore max_wide(void)
646723fee08SGarrett D'Amore {
647723fee08SGarrett D'Amore return ((int)((1U << _nbits) - 1));
648723fee08SGarrett D'Amore }
649