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