Merge commit 'cc543d0f9e35a75cc302a4cb152756d233299564'
[unleashed.git] / bin / localedef / wide.c
blobdde5636b3a8b852fc01fe2fb3614b493adb9fa30
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
14 * Copyright 2012 Garrett D'Amore <garrett@damore.org> All rights reserved.
18 * The functions in this file convert from the standard multibyte forms
19 * to the wide character forms used internally by libc. Unfortunately,
20 * this approach means that we need a method for each and every encoding.
23 #include <stdlib.h>
24 #include <wchar.h>
25 #include <string.h>
26 #include <note.h>
27 #include <sys/types.h>
28 #include "localedef.h"
30 static int towide_none(wchar_t *, const char *, unsigned);
31 static int towide_utf8(wchar_t *, const char *, unsigned);
32 static int towide_big5(wchar_t *, const char *, unsigned);
33 static int towide_gbk(wchar_t *, const char *, unsigned);
34 static int towide_gb2312(wchar_t *, const char *, unsigned);
35 static int towide_gb18030(wchar_t *, const char *, unsigned);
36 static int towide_mskanji(wchar_t *, const char *, unsigned);
37 static int towide_euccn(wchar_t *, const char *, unsigned);
38 static int towide_eucjp(wchar_t *, const char *, unsigned);
39 static int towide_euckr(wchar_t *, const char *, unsigned);
40 static int towide_euctw(wchar_t *, const char *, unsigned);
42 static int tomb_none(char *, wchar_t);
43 static int tomb_utf8(char *, wchar_t);
44 static int tomb_mbs(char *, wchar_t);
46 static int (*_towide)(wchar_t *, const char *, unsigned) = towide_none;
47 static int (*_tomb)(char *, wchar_t) = tomb_none;
48 static char _encoding_buffer[20] = "NONE";
49 static const char *_encoding = _encoding_buffer;
50 static int _nbits = 7;
53 * Table of supported encodings. We only bother to list the multibyte
54 * encodings here, because single byte locales are handed by "NONE".
56 static struct {
57 const char *name;
58 /* the name that the underlying libc implemenation uses */
59 const char *cname;
60 /* the maximum number of bits required for priorities */
61 int nbits;
62 int (*towide)(wchar_t *, const char *, unsigned);
63 int (*tomb)(char *, wchar_t);
64 } mb_encodings[] = {
66 * UTF8 values max out at 0x1fffff (although in theory there could
67 * be later extensions, but it won't happen.) This means we only need
68 * 21 bits to be able to encode the entire range of priorities.
70 { "UTF-8", "UTF-8", 21, towide_utf8, tomb_utf8 },
71 { "UTF8", "UTF-8", 21, towide_utf8, tomb_utf8 },
72 { "utf8", "UTF-8", 21, towide_utf8, tomb_utf8 },
73 { "utf-8", "UTF-8", 21, towide_utf8, tomb_utf8 },
75 { "EUC-CN", "EUC-CN", 16, towide_euccn, tomb_mbs },
76 { "eucCN", "EUC-CN", 16, towide_euccn, tomb_mbs },
78 * Becuase the 3-byte form of EUC-JP use the same leading byte,
79 * only 17 bits required to provide unique priorities. (The low
80 * bit of that first byte is set.) By setting this value low,
81 * we can get by with only 3 bytes in the strxfrm expansion.
83 { "EUC-JP", "EUC-JP", 17, towide_eucjp, tomb_mbs },
84 { "eucJP", "EUC-JP", 17, towide_eucjp, tomb_mbs },
86 { "EUC-KR", "EUC-KR", 16, towide_euckr, tomb_mbs },
87 { "eucKR", "EUC-KR", 16, towide_euckr, tomb_mbs },
89 * EUC-TW uses 2 bytes most of the time, but 4 bytes if the
90 * high order byte is 0x8E. However, with 4 byte encodings,
91 * the third byte will be A0-B0. So we only need to consider
92 * the lower order 24 bits for collation.
94 { "EUC-TW", "EUC-TW", 24, towide_euctw, tomb_mbs },
95 { "eucTW", "EUC-TW", 24, towide_euctw, tomb_mbs },
97 { "MS_Kanji", "MSKanji", 16, towide_mskanji, tomb_mbs },
98 { "MSKanji", "MSKanji", 16, towide_mskanji, tomb_mbs },
99 { "PCK", "MSKanji", 16, towide_mskanji, tomb_mbs },
100 { "SJIS", "MSKanji", 16, towide_mskanji, tomb_mbs },
101 { "Shift_JIS", "MSKanji", 16, towide_mskanji, tomb_mbs },
103 { "BIG5", "BIG5", 16, towide_big5, tomb_mbs },
104 { "big5", "BIG5", 16, towide_big5, tomb_mbs },
105 { "Big5", "BIG5", 16, towide_big5, tomb_mbs },
107 { "GBK", "GBK", 16, towide_gbk, tomb_mbs },
110 * GB18030 can get away with just 31 bits. This is because the
111 * high order bit is always set for 4 byte values, and the
112 * at least one of the other bits in that 4 byte value will
113 * be non-zero.
115 { "GB18030", "GB18030", 31, towide_gb18030, tomb_mbs },
118 * This should probably be an aliase for euc-cn, or vice versa.
120 { "GB2312", "GB2312", 16, towide_gb2312, tomb_mbs },
122 { NULL, NULL },
125 static char *
126 show_mb(const char *mb)
128 static char buf[64];
130 /* ASCII stuff we just print */
131 if (isascii(*mb) && isgraph(*mb)) {
132 buf[0] = *mb;
133 buf[1] = 0;
134 return (buf);
136 buf[0] = 0;
137 while (*mb != 0) {
138 char scr[8];
139 (void) snprintf(scr, sizeof (scr), "\\x%02x", *mb);
140 (void) strlcat(buf, scr, sizeof (buf));
141 mb++;
143 return (buf);
146 static char *widemsg;
148 void
149 werr(const char *fmt, ...)
151 char *msg;
153 va_list va;
154 va_start(va, fmt);
155 (void) vasprintf(&msg, fmt, va);
156 va_end(va);
158 free(widemsg);
159 widemsg = msg;
163 * This is used for 8-bit encodings.
166 towide_none(wchar_t *c, const char *mb, unsigned n)
168 _NOTE(ARGUNUSED(n));
170 if (mb_cur_max != 1) {
171 werr("invalid or unsupported multibyte locale");
172 return (-1);
174 *c = (uint8_t)*mb;
175 return (1);
179 tomb_none(char *mb, wchar_t wc)
181 if (mb_cur_max != 1) {
182 werr("invalid or unsupported multibyte locale");
183 return (-1);
185 *(uint8_t *)mb = (wc & 0xff);
186 mb[1] = 0;
187 return (1);
191 * UTF-8 stores wide characters in UTF-32 form.
194 towide_utf8(wchar_t *wc, const char *mb, unsigned n)
196 wchar_t c;
197 int nb;
198 int lv; /* lowest legal value */
199 int i;
200 const uint8_t *s = (const uint8_t *)mb;
202 c = *s;
204 if ((c & 0x80) == 0) {
205 /* 7-bit ASCII */
206 *wc = c;
207 return (1);
208 } else if ((c & 0xe0) == 0xc0) {
209 /* u80-u7ff - two bytes encoded */
210 nb = 2;
211 lv = 0x80;
212 c &= ~0xe0;
213 } else if ((c & 0xf0) == 0xe0) {
214 /* u800-uffff - three bytes encoded */
215 nb = 3;
216 lv = 0x800;
217 c &= ~0xf0;
218 } else if ((c & 0xf8) == 0xf0) {
219 /* u1000-u1fffff - four bytes encoded */
220 nb = 4;
221 lv = 0x1000;
222 c &= ~0xf8;
223 } else {
224 /* 5 and 6 byte encodings are not legal unicode */
225 werr("utf8 encoding too large (%s)", show_mb(mb));
226 return (-1);
228 if (nb > n) {
229 werr("incomplete utf8 sequence (%s)", show_mb(mb));
230 return (-1);
233 for (i = 1; i < nb; i++) {
234 if (((s[i]) & 0xc0) != 0x80) {
235 werr("illegal utf8 byte (%x)", s[i]);
236 return (-1);
238 c <<= 6;
239 c |= (s[i] & 0x3f);
242 if (c < lv) {
243 werr("illegal redundant utf8 encoding (%s)", show_mb(mb));
244 return (-1);
246 *wc = c;
247 return (nb);
251 tomb_utf8(char *mb, wchar_t wc)
253 uint8_t *s = (uint8_t *)mb;
254 uint8_t msk;
255 int cnt;
256 int i;
258 if (wc <= 0x7f) {
259 s[0] = wc & 0x7f;
260 s[1] = 0;
261 return (1);
263 if (wc <= 0x7ff) {
264 cnt = 2;
265 msk = 0xc0;
266 } else if (wc <= 0xffff) {
267 cnt = 3;
268 msk = 0xe0;
269 } else if (wc <= 0x1fffff) {
270 cnt = 4;
271 msk = 0xf0;
272 } else {
273 werr("illegal uf8 char (%x)", wc);
274 return (-1);
276 for (i = cnt - 1; i; i--) {
277 s[i] = (wc & 0x3f) | 0x80;
278 wc >>= 6;
280 s[0] = (msk) | wc;
281 s[cnt] = 0;
282 return (cnt);
286 * Several encodings share a simplistic dual byte encoding. In these
287 * forms, they all indicate that a two byte sequence is to be used if
288 * the first byte has its high bit set. They all store this simple
289 * encoding as a 16-bit value, although a great many of the possible
290 * code points are not used in most character sets. This gives a possible
291 * set of just over 32,000 valid code points.
293 * 0x00 - 0x7f - 1 byte encoding
294 * 0x80 - 0x7fff - illegal
295 * 0x8000 - 0xffff - 2 byte encoding
297 static int
298 towide_dbcs(wchar_t *wc, const char *mb, unsigned n)
300 wchar_t c;
302 c = *(uint8_t *)mb;
304 if ((c & 0x80) == 0) {
305 /* 7-bit */
306 *wc = c;
307 return (1);
309 if (n < 2) {
310 werr("incomplete character sequence (%s)", show_mb(mb));
311 return (-1);
314 /* Store both bytes as a single 16-bit wide. */
315 c <<= 8;
316 c |= (uint8_t)(mb[1]);
317 *wc = c;
318 return (2);
322 * Most multibyte locales just convert the wide character to the multibyte
323 * form by stripping leading null bytes, and writing the 32-bit quantity
324 * in big-endian order.
327 tomb_mbs(char *mb, wchar_t wc)
329 uint8_t *s = (uint8_t *)mb;
330 int n = 0, c;
332 if ((wc & 0xff000000U) != 0) {
333 n = 4;
334 } else if ((wc & 0x00ff0000U) != 0) {
335 n = 3;
336 } else if ((wc & 0x0000ff00U) != 0) {
337 n = 2;
338 } else {
339 n = 1;
341 c = n;
342 while (n) {
343 n--;
344 s[n] = wc & 0xff;
345 wc >>= 8;
347 /* ensure null termination */
348 s[c] = 0;
349 return (c);
354 * big5 is a simple dual byte character set.
357 towide_big5(wchar_t *wc, const char *mb, unsigned n)
359 return (towide_dbcs(wc, mb, n));
363 * GBK encodes wides in the same way that big5 does, the high order
364 * bit of the first byte indicates a double byte character.
367 towide_gbk(wchar_t *wc, const char *mb, unsigned n)
369 return (towide_dbcs(wc, mb, n));
373 * GB2312 is another DBCS. Its cleaner than others in that the second
374 * byte does not encode ASCII, but it supports characters.
377 towide_gb2312(wchar_t *wc, const char *mb, unsigned n)
379 return (towide_dbcs(wc, mb, n));
383 * GB18030. This encodes as 8, 16, or 32-bits.
384 * 7-bit values are in 1 byte, 4 byte sequences are used when
385 * the second byte encodes 0x30-39 and all other sequences are 2 bytes.
388 towide_gb18030(wchar_t *wc, const char *mb, unsigned n)
390 wchar_t c;
392 c = *(uint8_t *)mb;
394 if ((c & 0x80) == 0) {
395 /* 7-bit */
396 *wc = c;
397 return (1);
399 if (n < 2) {
400 werr("incomplete character sequence (%s)", show_mb(mb));
401 return (-1);
404 /* pull in the second byte */
405 c <<= 8;
406 c |= (uint8_t)(mb[1]);
408 if (((c & 0xff) >= 0x30) && ((c & 0xff) <= 0x39)) {
409 if (n < 4) {
410 werr("incomplete 4-byte character sequence (%s)",
411 show_mb(mb));
412 return (-1);
414 c <<= 8;
415 c |= (uint8_t)(mb[2]);
416 c <<= 8;
417 c |= (uint8_t)(mb[3]);
418 *wc = c;
419 return (4);
422 *wc = c;
423 return (2);
427 * MS-Kanji (aka SJIS) is almost a clean DBCS like the others, but it
428 * also has a range of single byte characters above 0x80. (0xa1-0xdf).
431 towide_mskanji(wchar_t *wc, const char *mb, unsigned n)
433 wchar_t c;
435 c = *(uint8_t *)mb;
437 if ((c < 0x80) || ((c > 0xa0) && (c < 0xe0))) {
438 /* 7-bit */
439 *wc = c;
440 return (1);
443 if (n < 2) {
444 werr("incomplete character sequence (%s)", show_mb(mb));
445 return (-1);
448 /* Store both bytes as a single 16-bit wide. */
449 c <<= 8;
450 c |= (uint8_t)(mb[1]);
451 *wc = c;
452 return (2);
456 * EUC forms. EUC encodings are "variable". FreeBSD carries some additional
457 * variable data to encode these, but we're going to treat each as independent
458 * instead. Its the only way we can sensibly move forward.
460 * Note that the way in which the different EUC forms vary is how wide
461 * CS2 and CS3 are and what the first byte of them is.
463 static int
464 towide_euc_impl(wchar_t *wc, const char *mb, unsigned n,
465 uint8_t cs2, uint8_t cs2width, uint8_t cs3, uint8_t cs3width)
467 int i;
468 int width;
469 wchar_t c;
471 c = *(uint8_t *)mb;
474 * All variations of EUC encode 7-bit ASCII as one byte, and use
475 * additional bytes for more than that.
477 if ((c & 0x80) == 0) {
478 /* 7-bit */
479 *wc = c;
480 return (1);
484 * All EUC variants reserve 0xa1-0xff to identify CS1, which
485 * is always two bytes wide. Note that unused CS will be zero,
486 * and that cannot be true because we know that the high order
487 * bit must be set.
489 if (c >= 0xa1) {
490 width = 2;
491 } else if (c == cs2) {
492 width = cs2width;
493 } else if (c == cs3) {
494 width = cs3width;
497 if (n < width) {
498 werr("incomplete character sequence (%s)", show_mb(mb));
499 return (-1);
502 for (i = 1; i < width; i++) {
503 /* pull in the next byte */
504 c <<= 8;
505 c |= (uint8_t)(mb[i]);
508 *wc = c;
509 return (width);
513 * EUC-CN encodes as follows:
515 * Code set 0 (ASCII): 0x21-0x7E
516 * Code set 1 (CNS 11643-1992 Plane 1): 0xA1A1-0xFEFE
517 * Code set 2: unused
518 * Code set 3: unused
521 towide_euccn(wchar_t *wc, const char *mb, unsigned n)
523 return (towide_euc_impl(wc, mb, n, 0x8e, 4, 0, 0));
527 * EUC-JP encodes as follows:
529 * Code set 0 (ASCII or JIS X 0201-1976 Roman): 0x21-0x7E
530 * Code set 1 (JIS X 0208): 0xA1A1-0xFEFE
531 * Code set 2 (half-width katakana): 0x8EA1-0x8EDF
532 * Code set 3 (JIS X 0212-1990): 0x8FA1A1-0x8FFEFE
535 towide_eucjp(wchar_t *wc, const char *mb, unsigned n)
537 return (towide_euc_impl(wc, mb, n, 0x8e, 2, 0x8f, 3));
541 * EUC-KR encodes as follows:
543 * Code set 0 (ASCII or KS C 5636-1993): 0x21-0x7E
544 * Code set 1 (KS C 5601-1992): 0xA1A1-0xFEFE
545 * Code set 2: unused
546 * Code set 3: unused
549 towide_euckr(wchar_t *wc, const char *mb, unsigned n)
551 return (towide_euc_impl(wc, mb, n, 0, 0, 0, 0));
555 * EUC-TW encodes as follows:
557 * Code set 0 (ASCII): 0x21-0x7E
558 * Code set 1 (CNS 11643-1992 Plane 1): 0xA1A1-0xFEFE
559 * Code set 2 (CNS 11643-1992 Planes 1-16): 0x8EA1A1A1-0x8EB0FEFE
560 * Code set 3: unused
563 towide_euctw(wchar_t *wc, const char *mb, unsigned n)
565 return (towide_euc_impl(wc, mb, n, 0x8e, 4, 0, 0));
569 * Public entry points.
573 to_wide(wchar_t *wc, const char *mb)
575 /* this won't fail hard */
576 return (_towide(wc, mb, strlen(mb)));
580 to_mb(char *mb, wchar_t wc)
582 int rv;
584 if ((rv = _tomb(mb, wc)) < 0) {
585 errf(widemsg);
586 free(widemsg);
587 widemsg = NULL;
589 return (rv);
592 char *
593 to_mb_string(const wchar_t *wcs)
595 char *mbs;
596 char *ptr;
597 int len;
599 mbs = malloc((wcslen(wcs) * mb_cur_max) + 1);
600 if (mbs == NULL) {
601 errf("out of memory");
602 return (NULL);
604 ptr = mbs;
605 while (*wcs) {
606 if ((len = to_mb(ptr, *wcs)) < 0) {
607 INTERR;
608 free(mbs);
609 return (NULL);
611 wcs++;
612 ptr += len;
614 *ptr = 0;
615 return (mbs);
618 void
619 set_wide_encoding(const char *encoding)
621 int i;
623 _towide = towide_none;
624 _tomb = tomb_none;
625 _nbits = 8;
627 (void) snprintf(_encoding_buffer, sizeof (_encoding_buffer), "NONE:%s",
628 encoding);
629 for (i = 0; mb_encodings[i].name; i++) {
630 if (strcasecmp(encoding, mb_encodings[i].name) == 0) {
631 _towide = mb_encodings[i].towide;
632 _tomb = mb_encodings[i].tomb;
633 _encoding = mb_encodings[i].cname;
634 _nbits = mb_encodings[i].nbits;
635 break;
640 const char *
641 get_wide_encoding(void)
643 return (_encoding);
647 max_wide(void)
649 return ((int)((1U << _nbits) - 1));