xref: /titanic_53/usr/src/cmd/localedef/wide.c (revision 723fee089c0a7e1bd9527b9a4b0f0abf5970336c)
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.
46b5e5868SGarrett D'Amore  * You may only use this file in accordance with the terms version 1.0
56b5e5868SGarrett D'Amore  * 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 /*
136b5e5868SGarrett D'Amore  * Copyright 2010 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";
47*723fee08SGarrett 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;
57*723fee08SGarrett D'Amore 	/* the maximum number of bits required for priorities */
58*723fee08SGarrett 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[] = {
62*723fee08SGarrett D'Amore 	/*
63*723fee08SGarrett D'Amore 	 * UTF8 values max out at 0x1fffff (although in theory there could
64*723fee08SGarrett D'Amore 	 * be later extensions, but it won't happen.)  This means we only need
65*723fee08SGarrett D'Amore 	 * 21 bits to be able to encode the entire range of priorities.
66*723fee08SGarrett D'Amore 	 */
67*723fee08SGarrett D'Amore 	{ "UTF-8",	"UTF-8",	21, towide_utf8, tomb_utf8 },
68*723fee08SGarrett D'Amore 	{ "UTF8",	"UTF-8",	21, towide_utf8, tomb_utf8 },
69*723fee08SGarrett D'Amore 	{ "utf8",	"UTF-8",	21, towide_utf8, tomb_utf8 },
70*723fee08SGarrett D'Amore 	{ "utf-8",	"UTF-8",	21, towide_utf8, tomb_utf8 },
716b5e5868SGarrett D'Amore 
72*723fee08SGarrett D'Amore 	{ "EUC-CN",	"EUC-CN",	16, towide_euccn, tomb_mbs },
73*723fee08SGarrett D'Amore 	{ "eucCN",	"EUC-CN",	16, towide_euccn, tomb_mbs },
74*723fee08SGarrett D'Amore 	/*
75*723fee08SGarrett D'Amore 	 * Becuase the 3-byte form of EUC-JP use the same leading byte,
76*723fee08SGarrett D'Amore 	 * only 17 bits required to provide unique priorities.  (The low
77*723fee08SGarrett D'Amore 	 * bit of that first byte is set.)  By setting this value low,
78*723fee08SGarrett D'Amore 	 * we can get by with only 3 bytes in the strxfrm expansion.
79*723fee08SGarrett D'Amore 	 */
80*723fee08SGarrett D'Amore 	{ "EUC-JP",	"EUC-JP",	17, towide_eucjp, tomb_mbs },
81*723fee08SGarrett D'Amore 	{ "eucJP",	"EUC-JP",	17, towide_eucjp, tomb_mbs },
826b5e5868SGarrett D'Amore 
83*723fee08SGarrett D'Amore 	{ "EUC-KR",	"EUC-KR",	16, towide_euckr, tomb_mbs },
84*723fee08SGarrett D'Amore 	{ "eucKR",	"EUC-KR",	16, towide_euckr, tomb_mbs },
85*723fee08SGarrett D'Amore 	/*
86*723fee08SGarrett D'Amore 	 * EUC-TW uses 2 bytes most of the time, but 4 bytes if the
87*723fee08SGarrett D'Amore 	 * high order byte is 0x8E.  However, with 4 byte encodings,
88*723fee08SGarrett D'Amore 	 * the third byte will be A0-B0.  So we only need to consider
89*723fee08SGarrett D'Amore 	 * the lower order 24 bits for collation.
90*723fee08SGarrett D'Amore 	 */
91*723fee08SGarrett D'Amore 	{ "EUC-TW",	"EUC-TW",	24, towide_euctw, tomb_mbs },
92*723fee08SGarrett D'Amore 	{ "eucTW",	"EUC-TW",	24, towide_euctw, tomb_mbs },
936b5e5868SGarrett D'Amore 
94*723fee08SGarrett D'Amore 	{ "MS_Kanji",	"MSKanji",	16, towide_mskanji, tomb_mbs },
95*723fee08SGarrett D'Amore 	{ "MSKanji",	"MSKanji",	16, towide_mskanji, tomb_mbs },
96*723fee08SGarrett D'Amore 	{ "PCK",	"MSKanji",	16, towide_mskanji, tomb_mbs },
97*723fee08SGarrett D'Amore 	{ "SJIS",	"MSKanji",	16, towide_mskanji, tomb_mbs },
98*723fee08SGarrett D'Amore 	{ "Shift_JIS",	"MSKanji",	16, towide_mskanji, tomb_mbs },
996b5e5868SGarrett D'Amore 
100*723fee08SGarrett D'Amore 	{ "BIG5",	"BIG5",		16, towide_big5, tomb_mbs },
101*723fee08SGarrett D'Amore 	{ "big5",	"BIG5",		16, towide_big5, tomb_mbs },
102*723fee08SGarrett D'Amore 	{ "Big5",	"BIG5",		16, towide_big5, tomb_mbs },
1036b5e5868SGarrett D'Amore 
104*723fee08SGarrett D'Amore 	{ "GBK",	"GBK",		16, towide_gbk,	tomb_mbs },
1056b5e5868SGarrett D'Amore 
106*723fee08SGarrett D'Amore 	/*
107*723fee08SGarrett D'Amore 	 * GB18030 can get away with just 31 bits.  This is because the
108*723fee08SGarrett D'Amore 	 * high order bit is always set for 4 byte values, and the
109*723fee08SGarrett D'Amore 	 * at least one of the other bits in that 4 byte value will
110*723fee08SGarrett D'Amore 	 * be non-zero.
111*723fee08SGarrett D'Amore 	 */
112*723fee08SGarrett D'Amore 	{ "GB18030",	"GB18030",	31, towide_gb18030, tomb_mbs },
1136b5e5868SGarrett D'Amore 
114*723fee08SGarrett D'Amore 	/*
115*723fee08SGarrett D'Amore 	 * This should probably be an aliase for euc-cn, or vice versa.
116*723fee08SGarrett D'Amore 	 */
117*723fee08SGarrett D'Amore 	{ "GB2312",	"GB2312",	16, towide_gb2312, tomb_mbs },
1186b5e5868SGarrett D'Amore 
119*723fee08SGarrett D'Amore 	{ "ASCII",	"ASCII",	7, towide_none,	tomb_none },
120*723fee08SGarrett D'Amore 	{ "US-ASCII",	"ASCII",	7, towide_none,	tomb_none },
121*723fee08SGarrett D'Amore 	{ "646",	"ASCII",	7, towide_none,	tomb_none },
1226b5e5868SGarrett D'Amore 
1236b5e5868SGarrett D'Amore 	{ NULL, NULL },
1246b5e5868SGarrett D'Amore };
1256b5e5868SGarrett D'Amore 
1266b5e5868SGarrett D'Amore static char *
1276b5e5868SGarrett D'Amore show_mb(const char *mb)
1286b5e5868SGarrett D'Amore {
1296b5e5868SGarrett D'Amore 	static char buf[64];
1306b5e5868SGarrett D'Amore 
1316b5e5868SGarrett D'Amore 	/* ASCII stuff we just print */
1326b5e5868SGarrett D'Amore 	if (isascii(*mb) && isgraph(*mb)) {
1336b5e5868SGarrett D'Amore 		buf[0] = *mb;
1346b5e5868SGarrett D'Amore 		buf[1] = 0;
1356b5e5868SGarrett D'Amore 		return (buf);
1366b5e5868SGarrett D'Amore 	}
1376b5e5868SGarrett D'Amore 	buf[0] = 0;
1386b5e5868SGarrett D'Amore 	while (*mb != 0) {
1396b5e5868SGarrett D'Amore 		char scr[8];
1406b5e5868SGarrett D'Amore 		(void) snprintf(scr, sizeof (scr), "\\x%02x", *mb);
1416b5e5868SGarrett D'Amore 		(void) strlcat(buf, scr, sizeof (buf));
1426b5e5868SGarrett D'Amore 		mb++;
1436b5e5868SGarrett D'Amore 	}
1446b5e5868SGarrett D'Amore 	return (buf);
1456b5e5868SGarrett D'Amore }
1466b5e5868SGarrett D'Amore 
1476b5e5868SGarrett D'Amore static char	*widemsg;
1486b5e5868SGarrett D'Amore 
1496b5e5868SGarrett D'Amore void
1506b5e5868SGarrett D'Amore werr(const char *fmt, ...)
1516b5e5868SGarrett D'Amore {
1526b5e5868SGarrett D'Amore 	char	*msg;
1536b5e5868SGarrett D'Amore 
1546b5e5868SGarrett D'Amore 	va_list	va;
1556b5e5868SGarrett D'Amore 	va_start(va, fmt);
1566b5e5868SGarrett D'Amore 	(void) vasprintf(&msg, fmt, va);
1576b5e5868SGarrett D'Amore 	va_end(va);
1586b5e5868SGarrett D'Amore 
1596b5e5868SGarrett D'Amore 	free(widemsg);
1606b5e5868SGarrett D'Amore 	widemsg = msg;
1616b5e5868SGarrett D'Amore }
1626b5e5868SGarrett D'Amore 
1636b5e5868SGarrett D'Amore /*
1646b5e5868SGarrett D'Amore  * This is used for 8-bit encodings.
1656b5e5868SGarrett D'Amore  */
1666b5e5868SGarrett D'Amore int
1676b5e5868SGarrett D'Amore towide_none(wchar_t *c, const char *mb, int n)
1686b5e5868SGarrett 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 	if (n < 1) {
1746b5e5868SGarrett D'Amore 		werr("no character data");
1756b5e5868SGarrett D'Amore 		return (-1);
1766b5e5868SGarrett D'Amore 	}
1776b5e5868SGarrett D'Amore 	*c = (uint8_t)*mb;
1786b5e5868SGarrett D'Amore 	return (1);
1796b5e5868SGarrett D'Amore }
1806b5e5868SGarrett D'Amore 
1816b5e5868SGarrett D'Amore int
1826b5e5868SGarrett D'Amore tomb_none(char *mb, wchar_t wc)
1836b5e5868SGarrett D'Amore {
1846b5e5868SGarrett D'Amore 	if (mb_cur_max != 1) {
1856b5e5868SGarrett D'Amore 		werr("invalid or unsupported multibyte locale");
1866b5e5868SGarrett D'Amore 		return (-1);
1876b5e5868SGarrett D'Amore 	}
1886b5e5868SGarrett D'Amore 	*(uint8_t *)mb = (wc & 0xff);
1896b5e5868SGarrett D'Amore 	mb[1] = 0;
1906b5e5868SGarrett D'Amore 	return (1);
1916b5e5868SGarrett D'Amore }
1926b5e5868SGarrett D'Amore 
1936b5e5868SGarrett D'Amore /*
1946b5e5868SGarrett D'Amore  * UTF-8 stores wide characters in UTF-32 form.
1956b5e5868SGarrett D'Amore  */
1966b5e5868SGarrett D'Amore int
1976b5e5868SGarrett D'Amore towide_utf8(wchar_t *wc, const char *mb, int n)
1986b5e5868SGarrett D'Amore {
1996b5e5868SGarrett D'Amore 	wchar_t	c;
2006b5e5868SGarrett D'Amore 	int	nb;
2016b5e5868SGarrett D'Amore 	int	lv;	/* lowest legal value */
2026b5e5868SGarrett D'Amore 	int	i;
2036b5e5868SGarrett D'Amore 	const uint8_t *s = (const uint8_t *)mb;
2046b5e5868SGarrett D'Amore 
2056b5e5868SGarrett D'Amore 	if (n < 1) {
2066b5e5868SGarrett D'Amore 		werr("no utf8 data");
2076b5e5868SGarrett D'Amore 		return (-1);
2086b5e5868SGarrett D'Amore 	}
2096b5e5868SGarrett D'Amore 	c = *s;
2106b5e5868SGarrett D'Amore 
2116b5e5868SGarrett D'Amore 	if ((c & 0x80) == 0) {
2126b5e5868SGarrett D'Amore 		/* 7-bit ASCII */
2136b5e5868SGarrett D'Amore 		*wc = c;
2146b5e5868SGarrett D'Amore 		return (1);
2156b5e5868SGarrett D'Amore 	} else if ((c & 0xe0) == 0xc0) {
2166b5e5868SGarrett D'Amore 		/* u80-u7ff - two bytes encoded */
2176b5e5868SGarrett D'Amore 		nb = 2;
2186b5e5868SGarrett D'Amore 		lv = 0x80;
2196b5e5868SGarrett D'Amore 		c &= ~0xe0;
2206b5e5868SGarrett D'Amore 	} else if ((c & 0xf0) == 0xe0) {
2216b5e5868SGarrett D'Amore 		/* u800-uffff - three bytes encoded */
2226b5e5868SGarrett D'Amore 		nb = 3;
2236b5e5868SGarrett D'Amore 		lv = 0x800;
2246b5e5868SGarrett D'Amore 		c &= ~0xf0;
2256b5e5868SGarrett D'Amore 	} else if ((c & 0xf8) == 0xf0) {
2266b5e5868SGarrett D'Amore 		/* u1000-u1fffff - four bytes encoded */
2276b5e5868SGarrett D'Amore 		nb = 4;
2286b5e5868SGarrett D'Amore 		lv = 0x1000;
2296b5e5868SGarrett D'Amore 		c &= ~0xf8;
2306b5e5868SGarrett D'Amore 	} else {
2316b5e5868SGarrett D'Amore 		/* 5 and 6 byte encodings are not legal unicode */
2326b5e5868SGarrett D'Amore 		werr("utf8 encoding too large (%s)", show_mb(mb));
2336b5e5868SGarrett D'Amore 		return (-1);
2346b5e5868SGarrett D'Amore 	}
2356b5e5868SGarrett D'Amore 	if (nb > n) {
2366b5e5868SGarrett D'Amore 		werr("incomplete utf8 sequence (%s)", show_mb(mb));
2376b5e5868SGarrett D'Amore 		return (-1);
2386b5e5868SGarrett D'Amore 	}
2396b5e5868SGarrett D'Amore 
2406b5e5868SGarrett D'Amore 	for (i = 1; i < nb; i++) {
2416b5e5868SGarrett D'Amore 		if (((s[i]) & 0xc0) != 0x80) {
2426b5e5868SGarrett D'Amore 			werr("illegal utf8 byte (%x)", s[i]);
2436b5e5868SGarrett D'Amore 			return (-1);
2446b5e5868SGarrett D'Amore 		}
2456b5e5868SGarrett D'Amore 		c <<= 6;
2466b5e5868SGarrett D'Amore 		c |= (s[i] & 0x3f);
2476b5e5868SGarrett D'Amore 	}
2486b5e5868SGarrett D'Amore 
2496b5e5868SGarrett D'Amore 	if (c < lv) {
2506b5e5868SGarrett D'Amore 		werr("illegal redundant utf8 encoding (%s)", show_mb(mb));
2516b5e5868SGarrett D'Amore 		return (-1);
2526b5e5868SGarrett D'Amore 	}
2536b5e5868SGarrett D'Amore 	*wc = c;
2546b5e5868SGarrett D'Amore 	return (nb);
2556b5e5868SGarrett D'Amore }
2566b5e5868SGarrett D'Amore 
2576b5e5868SGarrett D'Amore int
2586b5e5868SGarrett D'Amore tomb_utf8(char *mb, wchar_t wc)
2596b5e5868SGarrett D'Amore {
2606b5e5868SGarrett D'Amore 	uint8_t *s = (uint8_t *)mb;
2616b5e5868SGarrett D'Amore 	uint8_t msk;
2626b5e5868SGarrett D'Amore 	int cnt;
2636b5e5868SGarrett D'Amore 	int i;
2646b5e5868SGarrett D'Amore 
2656b5e5868SGarrett D'Amore 	if (wc <= 0x7f) {
2666b5e5868SGarrett D'Amore 		s[0] = wc & 0x7f;
2676b5e5868SGarrett D'Amore 		s[1] = 0;
2686b5e5868SGarrett D'Amore 		return (1);
2696b5e5868SGarrett D'Amore 	}
2706b5e5868SGarrett D'Amore 	if (wc <= 0x7ff) {
2716b5e5868SGarrett D'Amore 		cnt = 2;
2726b5e5868SGarrett D'Amore 		msk = 0xc0;
2736b5e5868SGarrett D'Amore 	} else if (wc <= 0xffff) {
2746b5e5868SGarrett D'Amore 		cnt = 3;
2756b5e5868SGarrett D'Amore 		msk = 0xe0;
2766b5e5868SGarrett D'Amore 	} else if (wc <= 0x1fffff) {
2776b5e5868SGarrett D'Amore 		cnt = 4;
2786b5e5868SGarrett D'Amore 		msk = 0xf0;
2796b5e5868SGarrett D'Amore 	} else {
2806b5e5868SGarrett D'Amore 		werr("illegal uf8 char (%x)", wc);
2816b5e5868SGarrett D'Amore 		return (-1);
2826b5e5868SGarrett D'Amore 	}
2836b5e5868SGarrett D'Amore 	for (i = cnt - 1; i; i--) {
2846b5e5868SGarrett D'Amore 		s[i] = (wc & 0x3f) | 0x80;
2856b5e5868SGarrett D'Amore 		wc >>= 6;
2866b5e5868SGarrett D'Amore 	}
2876b5e5868SGarrett D'Amore 	s[0] = (msk) | wc;
2886b5e5868SGarrett D'Amore 	s[cnt] = 0;
2896b5e5868SGarrett D'Amore 	return (cnt);
2906b5e5868SGarrett D'Amore }
2916b5e5868SGarrett D'Amore 
2926b5e5868SGarrett D'Amore /*
2936b5e5868SGarrett D'Amore  * Several encodings share a simplistic dual byte encoding.  In these
2946b5e5868SGarrett D'Amore  * forms, they all indicate that a two byte sequence is to be used if
2956b5e5868SGarrett D'Amore  * the first byte has its high bit set.  They all store this simple
2966b5e5868SGarrett D'Amore  * encoding as a 16-bit value, although a great many of the possible
2976b5e5868SGarrett D'Amore  * code points are not used in most character sets.  This gives a possible
2986b5e5868SGarrett D'Amore  * set of just over 32,000 valid code points.
2996b5e5868SGarrett D'Amore  *
3006b5e5868SGarrett D'Amore  * 0x00 - 0x7f		- 1 byte encoding
3016b5e5868SGarrett D'Amore  * 0x80 - 0x7fff	- illegal
3026b5e5868SGarrett D'Amore  * 0x8000 - 0xffff	- 2 byte encoding
3036b5e5868SGarrett D'Amore  */
3046b5e5868SGarrett D'Amore static int
3056b5e5868SGarrett D'Amore towide_dbcs(wchar_t *wc, const char *mb, int n)
3066b5e5868SGarrett D'Amore {
3076b5e5868SGarrett D'Amore 	wchar_t	c;
3086b5e5868SGarrett D'Amore 
3096b5e5868SGarrett D'Amore 	c = *(uint8_t *)mb;
3106b5e5868SGarrett D'Amore 
3116b5e5868SGarrett D'Amore 	if (n < 1) {
3126b5e5868SGarrett D'Amore 		werr("no character data");
3136b5e5868SGarrett D'Amore 		return (-1);
3146b5e5868SGarrett D'Amore 	}
3156b5e5868SGarrett D'Amore 	if ((c & 0x80) == 0) {
3166b5e5868SGarrett D'Amore 		/* 7-bit */
3176b5e5868SGarrett D'Amore 		*wc = c;
3186b5e5868SGarrett D'Amore 		return (1);
3196b5e5868SGarrett D'Amore 	}
3206b5e5868SGarrett D'Amore 	if (n < 2) {
3216b5e5868SGarrett D'Amore 		werr("incomplete character sequence (%s)", show_mb(mb));
3226b5e5868SGarrett D'Amore 		return (-1);
3236b5e5868SGarrett D'Amore 	}
3246b5e5868SGarrett D'Amore 
3256b5e5868SGarrett D'Amore 	/* Store both bytes as a single 16-bit wide. */
3266b5e5868SGarrett D'Amore 	c <<= 8;
3276b5e5868SGarrett D'Amore 	c |= (uint8_t)(mb[1]);
3286b5e5868SGarrett D'Amore 	*wc = c;
3296b5e5868SGarrett D'Amore 	return (2);
3306b5e5868SGarrett D'Amore }
3316b5e5868SGarrett D'Amore 
3326b5e5868SGarrett D'Amore /*
3336b5e5868SGarrett D'Amore  * Most multibyte locales just convert the wide character to the multibyte
3346b5e5868SGarrett D'Amore  * form by stripping leading null bytes, and writing the 32-bit quantity
3356b5e5868SGarrett D'Amore  * in big-endian order.
3366b5e5868SGarrett D'Amore  */
3376b5e5868SGarrett D'Amore int
3386b5e5868SGarrett D'Amore tomb_mbs(char *mb, wchar_t wc)
3396b5e5868SGarrett D'Amore {
3406b5e5868SGarrett D'Amore 	uint8_t *s = (uint8_t *)mb;
3416b5e5868SGarrett D'Amore 	int 	n = 0, c;
3426b5e5868SGarrett D'Amore 
3436b5e5868SGarrett D'Amore 	if ((wc & 0xff000000U) != 0) {
3446b5e5868SGarrett D'Amore 		n = 4;
3456b5e5868SGarrett D'Amore 	} else if ((wc & 0x00ff0000U) != 0) {
3466b5e5868SGarrett D'Amore 		n = 3;
3476b5e5868SGarrett D'Amore 	} else if ((wc & 0x0000ff00U) != 0) {
3486b5e5868SGarrett D'Amore 		n = 2;
3496b5e5868SGarrett D'Amore 	} else {
3506b5e5868SGarrett D'Amore 		n = 1;
3516b5e5868SGarrett D'Amore 	}
3526b5e5868SGarrett D'Amore 	c = n;
3536b5e5868SGarrett D'Amore 	while (n) {
3546b5e5868SGarrett D'Amore 		n--;
3556b5e5868SGarrett D'Amore 		s[n] = wc & 0xff;
3566b5e5868SGarrett D'Amore 		wc >>= 8;
3576b5e5868SGarrett D'Amore 	}
3586b5e5868SGarrett D'Amore 	/* ensure null termination */
3596b5e5868SGarrett D'Amore 	s[c] = 0;
3606b5e5868SGarrett D'Amore 	return (c);
3616b5e5868SGarrett D'Amore }
3626b5e5868SGarrett D'Amore 
3636b5e5868SGarrett D'Amore 
3646b5e5868SGarrett D'Amore /*
3656b5e5868SGarrett D'Amore  * big5 is a simple dual byte character set.
3666b5e5868SGarrett D'Amore  */
3676b5e5868SGarrett D'Amore int
3686b5e5868SGarrett D'Amore towide_big5(wchar_t *wc, const char *mb, int n)
3696b5e5868SGarrett D'Amore {
3706b5e5868SGarrett D'Amore 	return (towide_dbcs(wc, mb, n));
3716b5e5868SGarrett D'Amore }
3726b5e5868SGarrett D'Amore 
3736b5e5868SGarrett D'Amore /*
3746b5e5868SGarrett D'Amore  * GBK encodes wides in the same way that big5 does, the high order
3756b5e5868SGarrett D'Amore  * bit of the first byte indicates a double byte character.
3766b5e5868SGarrett D'Amore  */
3776b5e5868SGarrett D'Amore int
3786b5e5868SGarrett D'Amore towide_gbk(wchar_t *wc, const char *mb, int n)
3796b5e5868SGarrett D'Amore {
3806b5e5868SGarrett D'Amore 	return (towide_dbcs(wc, mb, n));
3816b5e5868SGarrett D'Amore }
3826b5e5868SGarrett D'Amore 
3836b5e5868SGarrett D'Amore /*
3846b5e5868SGarrett D'Amore  * GB2312 is another DBCS.  Its cleaner than others in that the second
3856b5e5868SGarrett D'Amore  * byte does not encode ASCII, but it supports characters.
3866b5e5868SGarrett D'Amore  */
3876b5e5868SGarrett D'Amore int
3886b5e5868SGarrett D'Amore towide_gb2312(wchar_t *wc, const char *mb, int n)
3896b5e5868SGarrett D'Amore {
3906b5e5868SGarrett D'Amore 	return (towide_dbcs(wc, mb, n));
3916b5e5868SGarrett D'Amore }
3926b5e5868SGarrett D'Amore 
3936b5e5868SGarrett D'Amore /*
3946b5e5868SGarrett D'Amore  * GB18030.  This encodes as 8, 16, or 32-bits.
3956b5e5868SGarrett D'Amore  * 7-bit values are in 1 byte,  4 byte sequences are used when
3966b5e5868SGarrett D'Amore  * the second byte encodes 0x30-39 and all other sequences are 2 bytes.
3976b5e5868SGarrett D'Amore  */
3986b5e5868SGarrett D'Amore int
3996b5e5868SGarrett D'Amore towide_gb18030(wchar_t *wc, const char *mb, int n)
4006b5e5868SGarrett D'Amore {
4016b5e5868SGarrett D'Amore 	wchar_t	c;
4026b5e5868SGarrett D'Amore 
4036b5e5868SGarrett D'Amore 	c = *(uint8_t *)mb;
4046b5e5868SGarrett D'Amore 
4056b5e5868SGarrett D'Amore 	if (n < 1) {
4066b5e5868SGarrett D'Amore 		werr("no character data");
4076b5e5868SGarrett D'Amore 		return (-1);
4086b5e5868SGarrett D'Amore 	}
4096b5e5868SGarrett D'Amore 	if ((c & 0x80) == 0) {
4106b5e5868SGarrett D'Amore 		/* 7-bit */
4116b5e5868SGarrett D'Amore 		*wc = c;
4126b5e5868SGarrett D'Amore 		return (1);
4136b5e5868SGarrett D'Amore 	}
4146b5e5868SGarrett D'Amore 	if (n < 2) {
4156b5e5868SGarrett D'Amore 		werr("incomplete character sequence (%s)", show_mb(mb));
4166b5e5868SGarrett D'Amore 		return (-1);
4176b5e5868SGarrett D'Amore 	}
4186b5e5868SGarrett D'Amore 
4196b5e5868SGarrett D'Amore 	/* pull in the second byte */
4206b5e5868SGarrett D'Amore 	c <<= 8;
4216b5e5868SGarrett D'Amore 	c |= (uint8_t)(mb[1]);
4226b5e5868SGarrett D'Amore 
4236b5e5868SGarrett D'Amore 	if (((c & 0xff) >= 0x30) && ((c & 0xff) <= 0x39)) {
4246b5e5868SGarrett D'Amore 		if (n < 4) {
4256b5e5868SGarrett D'Amore 			werr("incomplete 4-byte character sequence (%s)",
4266b5e5868SGarrett D'Amore 			    show_mb(mb));
4276b5e5868SGarrett D'Amore 			return (-1);
4286b5e5868SGarrett D'Amore 		}
4296b5e5868SGarrett D'Amore 		c <<= 8;
4306b5e5868SGarrett D'Amore 		c |= (uint8_t)(mb[2]);
4316b5e5868SGarrett D'Amore 		c <<= 8;
4326b5e5868SGarrett D'Amore 		c |= (uint8_t)(mb[3]);
4336b5e5868SGarrett D'Amore 		*wc = c;
4346b5e5868SGarrett D'Amore 		return (4);
4356b5e5868SGarrett D'Amore 	}
4366b5e5868SGarrett D'Amore 
4376b5e5868SGarrett D'Amore 	*wc = c;
4386b5e5868SGarrett D'Amore 	return (2);
4396b5e5868SGarrett D'Amore }
4406b5e5868SGarrett D'Amore 
4416b5e5868SGarrett D'Amore /*
4426b5e5868SGarrett D'Amore  * MS-Kanji (aka SJIS) is almost a clean DBCS like the others, but it
4436b5e5868SGarrett D'Amore  * also has a range of single byte characters above 0x80.  (0xa1-0xdf).
4446b5e5868SGarrett D'Amore  */
4456b5e5868SGarrett D'Amore int
4466b5e5868SGarrett D'Amore towide_mskanji(wchar_t *wc, const char *mb, int n)
4476b5e5868SGarrett D'Amore {
4486b5e5868SGarrett D'Amore 	wchar_t	c;
4496b5e5868SGarrett D'Amore 
4506b5e5868SGarrett D'Amore 	c = *(uint8_t *)mb;
4516b5e5868SGarrett D'Amore 
4526b5e5868SGarrett D'Amore 	if (n < 1) {
4536b5e5868SGarrett D'Amore 		werr("no character data");
4546b5e5868SGarrett D'Amore 		return (-1);
4556b5e5868SGarrett D'Amore 	}
4566b5e5868SGarrett D'Amore 	if ((c < 0x80) || ((c > 0xa0) && (c < 0xe0))) {
4576b5e5868SGarrett D'Amore 		/* 7-bit */
4586b5e5868SGarrett D'Amore 		*wc = c;
4596b5e5868SGarrett D'Amore 		return (-1);
4606b5e5868SGarrett D'Amore 	}
4616b5e5868SGarrett D'Amore 
4626b5e5868SGarrett D'Amore 	if (n < 2) {
4636b5e5868SGarrett D'Amore 		werr("incomplete character sequence (%s)", show_mb(mb));
4646b5e5868SGarrett D'Amore 		return (-1);
4656b5e5868SGarrett D'Amore 	}
4666b5e5868SGarrett D'Amore 
4676b5e5868SGarrett D'Amore 	/* Store both bytes as a single 16-bit wide. */
4686b5e5868SGarrett D'Amore 	c <<= 8;
4696b5e5868SGarrett D'Amore 	c |= (uint8_t)(mb[1]);
4706b5e5868SGarrett D'Amore 	*wc = c;
4716b5e5868SGarrett D'Amore 	return (2);
4726b5e5868SGarrett D'Amore }
4736b5e5868SGarrett D'Amore 
4746b5e5868SGarrett D'Amore /*
4756b5e5868SGarrett D'Amore  * EUC forms.  EUC encodings are "variable".  FreeBSD carries some additional
4766b5e5868SGarrett D'Amore  * variable data to encode these, but we're going to treat each as independent
4776b5e5868SGarrett D'Amore  * instead.  Its the only way we can sensibly move forward.
4786b5e5868SGarrett D'Amore  *
4796b5e5868SGarrett D'Amore  * Note that the way in which the different EUC forms vary is how wide
4806b5e5868SGarrett D'Amore  * CS2 and CS3 are and what the first byte of them is.
4816b5e5868SGarrett D'Amore  */
4826b5e5868SGarrett D'Amore static int
4836b5e5868SGarrett D'Amore towide_euc_impl(wchar_t *wc, const char *mb, int n,
4846b5e5868SGarrett D'Amore     uint8_t cs2, uint8_t cs2width, uint8_t cs3, uint8_t cs3width)
4856b5e5868SGarrett D'Amore {
4866b5e5868SGarrett D'Amore 	int i;
4876b5e5868SGarrett D'Amore 	int width;
4886b5e5868SGarrett D'Amore 	wchar_t	c;
4896b5e5868SGarrett D'Amore 
4906b5e5868SGarrett D'Amore 	c = *(uint8_t *)mb;
4916b5e5868SGarrett D'Amore 
4926b5e5868SGarrett D'Amore 	if (n < 1) {
4936b5e5868SGarrett D'Amore 		werr("no character data");
4946b5e5868SGarrett D'Amore 		return (-1);
4956b5e5868SGarrett D'Amore 	}
4966b5e5868SGarrett D'Amore 
4976b5e5868SGarrett D'Amore 	/*
4986b5e5868SGarrett D'Amore 	 * All variations of EUC encode 7-bit ASCII as one byte, and use
4996b5e5868SGarrett D'Amore 	 * additional bytes for more than that.
5006b5e5868SGarrett D'Amore 	 */
5016b5e5868SGarrett D'Amore 	if ((c & 0x80) == 0) {
5026b5e5868SGarrett D'Amore 		/* 7-bit */
5036b5e5868SGarrett D'Amore 		*wc = c;
5046b5e5868SGarrett D'Amore 		return (1);
5056b5e5868SGarrett D'Amore 	}
5066b5e5868SGarrett D'Amore 
5076b5e5868SGarrett D'Amore 	/*
5086b5e5868SGarrett D'Amore 	 * All EUC variants reserve 0xa1-0xff to identify CS1, which
5096b5e5868SGarrett D'Amore 	 * is always two bytes wide.  Note that unused CS will be zero,
5106b5e5868SGarrett D'Amore 	 * and that cannot be true because we know that the high order
5116b5e5868SGarrett D'Amore 	 * bit must be set.
5126b5e5868SGarrett D'Amore 	 */
5136b5e5868SGarrett D'Amore 	if (c >= 0xa1) {
5146b5e5868SGarrett D'Amore 		width = 2;
5156b5e5868SGarrett D'Amore 	} else if (c == cs2) {
5166b5e5868SGarrett D'Amore 		width = cs2width;
5176b5e5868SGarrett D'Amore 	} else if (c == cs3) {
5186b5e5868SGarrett D'Amore 		width = cs3width;
5196b5e5868SGarrett D'Amore 	}
5206b5e5868SGarrett D'Amore 
5216b5e5868SGarrett D'Amore 	if (n < width) {
5226b5e5868SGarrett D'Amore 		werr("incomplete character sequence (%s)", show_mb(mb));
5236b5e5868SGarrett D'Amore 		return (-1);
5246b5e5868SGarrett D'Amore 	}
5256b5e5868SGarrett D'Amore 
5266b5e5868SGarrett D'Amore 	for (i = 1; i < width; i++) {
5276b5e5868SGarrett D'Amore 		/* pull in the next byte */
5286b5e5868SGarrett D'Amore 		c <<= 8;
5296b5e5868SGarrett D'Amore 		c |= (uint8_t)(mb[i]);
5306b5e5868SGarrett D'Amore 	}
5316b5e5868SGarrett D'Amore 
5326b5e5868SGarrett D'Amore 	*wc = c;
5336b5e5868SGarrett D'Amore 	return (width);
5346b5e5868SGarrett D'Amore }
5356b5e5868SGarrett D'Amore 
5366b5e5868SGarrett D'Amore /*
5376b5e5868SGarrett D'Amore  * EUC-CN encodes as follows:
5386b5e5868SGarrett D'Amore  *
5396b5e5868SGarrett D'Amore  * Code set 0 (ASCII):				0x21-0x7E
5406b5e5868SGarrett D'Amore  * Code set 1 (CNS 11643-1992 Plane 1):		0xA1A1-0xFEFE
541*723fee08SGarrett D'Amore  * Code set 2:					unused
5426b5e5868SGarrett D'Amore  * Code set 3:					unused
5436b5e5868SGarrett D'Amore  */
5446b5e5868SGarrett D'Amore int
5456b5e5868SGarrett D'Amore towide_euccn(wchar_t *wc, const char *mb, int n)
5466b5e5868SGarrett D'Amore {
5476b5e5868SGarrett D'Amore 	return (towide_euc_impl(wc, mb, n, 0x8e, 4, 0, 0));
5486b5e5868SGarrett D'Amore }
5496b5e5868SGarrett D'Amore 
5506b5e5868SGarrett D'Amore /*
5516b5e5868SGarrett D'Amore  * EUC-JP encodes as follows:
5526b5e5868SGarrett D'Amore  *
5536b5e5868SGarrett D'Amore  * Code set 0 (ASCII or JIS X 0201-1976 Roman):	0x21-0x7E
5546b5e5868SGarrett D'Amore  * Code set 1 (JIS X 0208):			0xA1A1-0xFEFE
5556b5e5868SGarrett D'Amore  * Code set 2 (half-width katakana):		0x8EA1-0x8EDF
5566b5e5868SGarrett D'Amore  * Code set 3 (JIS X 0212-1990):		0x8FA1A1-0x8FFEFE
5576b5e5868SGarrett D'Amore  */
5586b5e5868SGarrett D'Amore int
5596b5e5868SGarrett D'Amore towide_eucjp(wchar_t *wc, const char *mb, int n)
5606b5e5868SGarrett D'Amore {
5616b5e5868SGarrett D'Amore 	return (towide_euc_impl(wc, mb, n, 0x8e, 2, 0x8f, 3));
5626b5e5868SGarrett D'Amore }
5636b5e5868SGarrett D'Amore 
5646b5e5868SGarrett D'Amore /*
5656b5e5868SGarrett D'Amore  * EUC-KR encodes as follows:
5666b5e5868SGarrett D'Amore  *
5676b5e5868SGarrett D'Amore  * Code set 0 (ASCII or KS C 5636-1993):	0x21-0x7E
5686b5e5868SGarrett D'Amore  * Code set 1 (KS C 5601-1992):			0xA1A1-0xFEFE
5696b5e5868SGarrett D'Amore  * Code set 2:					unused
5706b5e5868SGarrett D'Amore  * Code set 3:					unused
5716b5e5868SGarrett D'Amore  */
5726b5e5868SGarrett D'Amore int
5736b5e5868SGarrett D'Amore towide_euckr(wchar_t *wc, const char *mb, int n)
5746b5e5868SGarrett D'Amore {
5756b5e5868SGarrett D'Amore 	return (towide_euc_impl(wc, mb, n, 0, 0, 0, 0));
5766b5e5868SGarrett D'Amore }
5776b5e5868SGarrett D'Amore 
5786b5e5868SGarrett D'Amore /*
5796b5e5868SGarrett D'Amore  * EUC-TW encodes as follows:
5806b5e5868SGarrett D'Amore  *
5816b5e5868SGarrett D'Amore  * Code set 0 (ASCII):				0x21-0x7E
5826b5e5868SGarrett D'Amore  * Code set 1 (CNS 11643-1992 Plane 1):		0xA1A1-0xFEFE
5836b5e5868SGarrett D'Amore  * Code set 2 (CNS 11643-1992 Planes 1-16):	0x8EA1A1A1-0x8EB0FEFE
5846b5e5868SGarrett D'Amore  * Code set 3:					unused
5856b5e5868SGarrett D'Amore  */
5866b5e5868SGarrett D'Amore int
5876b5e5868SGarrett D'Amore towide_euctw(wchar_t *wc, const char *mb, int n)
5886b5e5868SGarrett D'Amore {
5896b5e5868SGarrett D'Amore 	return (towide_euc_impl(wc, mb, n, 0x8e, 4, 0, 0));
5906b5e5868SGarrett D'Amore }
5916b5e5868SGarrett D'Amore 
5926b5e5868SGarrett D'Amore /*
5936b5e5868SGarrett D'Amore  * Public entry points.
5946b5e5868SGarrett D'Amore  */
5956b5e5868SGarrett D'Amore 
5966b5e5868SGarrett D'Amore int
5976b5e5868SGarrett D'Amore to_wide(wchar_t *wc, const char *mb)
5986b5e5868SGarrett D'Amore {
5996b5e5868SGarrett D'Amore 	/* this won't fail hard */
6006b5e5868SGarrett D'Amore 	return (_towide(wc, mb, strlen(mb) + 1));
6016b5e5868SGarrett D'Amore }
6026b5e5868SGarrett D'Amore 
6036b5e5868SGarrett D'Amore int
6046b5e5868SGarrett D'Amore to_mb(char *mb, wchar_t wc)
6056b5e5868SGarrett D'Amore {
6066b5e5868SGarrett D'Amore 	int	rv;
6076b5e5868SGarrett D'Amore 
6086b5e5868SGarrett D'Amore 	if ((rv = _tomb(mb, wc)) < 0) {
6096b5e5868SGarrett D'Amore 		errf(widemsg);
6106b5e5868SGarrett D'Amore 		free(widemsg);
6116b5e5868SGarrett D'Amore 		widemsg = NULL;
6126b5e5868SGarrett D'Amore 	}
6136b5e5868SGarrett D'Amore 	return (rv);
6146b5e5868SGarrett D'Amore }
6156b5e5868SGarrett D'Amore 
6166b5e5868SGarrett D'Amore char *
6176b5e5868SGarrett D'Amore to_mb_string(const wchar_t *wcs)
6186b5e5868SGarrett D'Amore {
6196b5e5868SGarrett D'Amore 	char	*mbs;
6206b5e5868SGarrett D'Amore 	char	*ptr;
6216b5e5868SGarrett D'Amore 	int	len;
6226b5e5868SGarrett D'Amore 
6236b5e5868SGarrett D'Amore 	mbs = malloc((wcslen(wcs) * mb_cur_max) + 1);
6246b5e5868SGarrett D'Amore 	if (mbs == NULL) {
6256b5e5868SGarrett D'Amore 		errf("out of memory");
6266b5e5868SGarrett D'Amore 		return (NULL);
6276b5e5868SGarrett D'Amore 	}
6286b5e5868SGarrett D'Amore 	ptr = mbs;
6296b5e5868SGarrett D'Amore 	while (*wcs) {
6306b5e5868SGarrett D'Amore 		if ((len = to_mb(ptr, *wcs)) < 0) {
6316b5e5868SGarrett D'Amore 			INTERR;
6326b5e5868SGarrett D'Amore 			free(mbs);
6336b5e5868SGarrett D'Amore 			return (NULL);
6346b5e5868SGarrett D'Amore 		}
6356b5e5868SGarrett D'Amore 		wcs++;
6366b5e5868SGarrett D'Amore 		ptr += len;
6376b5e5868SGarrett D'Amore 	}
6386b5e5868SGarrett D'Amore 	*ptr = 0;
6396b5e5868SGarrett D'Amore 	return (mbs);
6406b5e5868SGarrett D'Amore }
6416b5e5868SGarrett D'Amore 
6426b5e5868SGarrett D'Amore void
6436b5e5868SGarrett D'Amore set_wide_encoding(const char *encoding)
6446b5e5868SGarrett D'Amore {
6456b5e5868SGarrett D'Amore 	int i;
6466b5e5868SGarrett D'Amore 
6476b5e5868SGarrett D'Amore 	_towide = towide_none;
6486b5e5868SGarrett D'Amore 	_tomb = tomb_none;
6496b5e5868SGarrett D'Amore 	_encoding = "NONE";
650*723fee08SGarrett D'Amore 	_nbits = 8;
6516b5e5868SGarrett D'Amore 
6526b5e5868SGarrett D'Amore 	for (i = 0; mb_encodings[i].name; i++) {
6536b5e5868SGarrett D'Amore 		if (strcasecmp(encoding, mb_encodings[i].name) == 0) {
6546b5e5868SGarrett D'Amore 			_towide = mb_encodings[i].towide;
6556b5e5868SGarrett D'Amore 			_tomb = mb_encodings[i].tomb;
6566b5e5868SGarrett D'Amore 			_encoding = mb_encodings[i].cname;
657*723fee08SGarrett D'Amore 			_nbits = mb_encodings[i].nbits;
658*723fee08SGarrett D'Amore 			break;
6596b5e5868SGarrett D'Amore 		}
6606b5e5868SGarrett D'Amore 	}
6616b5e5868SGarrett D'Amore }
6626b5e5868SGarrett D'Amore 
6636b5e5868SGarrett D'Amore const char *
6646b5e5868SGarrett D'Amore get_wide_encoding(void)
6656b5e5868SGarrett D'Amore {
6666b5e5868SGarrett D'Amore 	return (_encoding);
6676b5e5868SGarrett D'Amore }
668*723fee08SGarrett D'Amore 
669*723fee08SGarrett D'Amore int
670*723fee08SGarrett D'Amore max_wide(void)
671*723fee08SGarrett D'Amore {
672*723fee08SGarrett D'Amore 	return ((int)((1U << _nbits) - 1));
673*723fee08SGarrett D'Amore }
674