2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003, 2005 Ryuichiro Imura
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/iconv.h>
38 #include "iconv_converter_if.h"
44 #define KICONV_UCS_COMBINE 0x1
45 #define KICONV_UCS_FROM_UTF8 0x2
46 #define KICONV_UCS_TO_UTF8 0x4
47 #define KICONV_UCS_FROM_LE 0x8
48 #define KICONV_UCS_TO_LE 0x10
49 #define KICONV_UCS_FROM_UTF16 0x20
50 #define KICONV_UCS_TO_UTF16 0x40
51 #define KICONV_UCS_UCS4 0x80
53 #define ENCODING_UTF16 "UTF-16BE"
54 #define ENCODING_UTF8 "UTF-8"
58 int from_flag, to_flag;
59 } unicode_family[] = {
60 { "UTF-8", KICONV_UCS_FROM_UTF8, KICONV_UCS_TO_UTF8 },
61 { "UCS-2LE", KICONV_UCS_FROM_LE, KICONV_UCS_TO_LE },
62 { "UTF-16BE", KICONV_UCS_FROM_UTF16, KICONV_UCS_TO_UTF16 },
63 { "UTF-16LE", KICONV_UCS_FROM_UTF16|KICONV_UCS_FROM_LE,
64 KICONV_UCS_TO_UTF16|KICONV_UCS_TO_LE },
68 static uint32_t utf8_to_ucs4(const char *src, size_t *utf8width, size_t srclen);
69 static u_char *ucs4_to_utf8(uint32_t ucs4, char * dst, size_t *utf8width, size_t dstlen);
70 static uint32_t encode_surrogate(uint32_t code);
71 static uint32_t decode_surrogate(const u_char *ucs);
74 MODULE_DEPEND(iconv_ucs, libiconv, 2, 2, 2);
78 * UCS converter instance
83 struct iconv_cspair * d_csp;
84 struct iconv_cspair * d_cspf;
91 iconv_ucs_open(struct iconv_converter_class *dcp,
92 struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
96 const char *from, *to;
98 dp = (struct iconv_ucs *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK);
100 from = cspf ? cspf->cp_from : csp->cp_from;
105 dp->convtype |= KICONV_UCS_COMBINE;
106 for (i = 0; unicode_family[i].name; i++) {
107 if (strcasecmp(from, unicode_family[i].name) == 0)
108 dp->convtype |= unicode_family[i].from_flag;
109 if (strcasecmp(to, unicode_family[i].name) == 0)
110 dp->convtype |= unicode_family[i].to_flag;
112 if (strcmp(ENCODING_UNICODE, ENCODING_UTF16) == 0)
113 dp->convtype |= KICONV_UCS_UCS4;
115 dp->convtype &= ~KICONV_UCS_UCS4;
117 dp->f_ctp = dp->t_ctp = NULL;
118 if (dp->convtype & KICONV_UCS_COMBINE) {
119 if ((dp->convtype & KICONV_UCS_FROM_UTF8) == 0 &&
120 (dp->convtype & KICONV_UCS_FROM_LE) == 0) {
121 iconv_open(ENCODING_UNICODE, from, &dp->f_ctp);
123 if ((dp->convtype & KICONV_UCS_TO_UTF8) == 0 &&
124 (dp->convtype & KICONV_UCS_TO_LE) == 0) {
125 iconv_open(to, ENCODING_UNICODE, &dp->t_ctp);
130 if (dp->convtype & (KICONV_UCS_FROM_UTF8 | KICONV_UCS_TO_UTF8))
131 iconv_open(KICONV_WCTYPE_NAME, ENCODING_UTF8, &dp->ctype);
134 if (dp->convtype & (KICONV_UCS_FROM_UTF8 | KICONV_UCS_FROM_LE)) {
141 if (dp->convtype & (KICONV_UCS_TO_UTF8 | KICONV_UCS_TO_LE))
148 iconv_ucs_close(void *data)
150 struct iconv_ucs *dp = data;
153 iconv_close(dp->f_ctp);
155 iconv_close(dp->t_ctp);
157 iconv_close(dp->ctype);
159 dp->d_cspf->cp_refcount--;
160 else if (dp->convtype & (KICONV_UCS_FROM_UTF8 | KICONV_UCS_FROM_LE))
161 dp->d_csp->cp_refcount--;
162 if (dp->convtype & (KICONV_UCS_TO_UTF8 | KICONV_UCS_TO_LE))
163 dp->d_csp->cp_refcount--;
164 kobj_delete((struct kobj*)data, M_ICONV);
169 iconv_ucs_conv(void *d2p, const char **inbuf,
170 size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
171 int convchar, int casetype)
173 struct iconv_ucs *dp = (struct iconv_ucs*)d2p;
175 size_t in, on, ir, or, inlen, outlen, ucslen;
181 if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
183 ir = in = *inbytesleft;
184 or = on = *outbytesleft;
188 while (ir > 0 && or > 0) {
191 * The first half of conversion.
192 * (convert any code into ENCODING_UNICODE)
196 if (dp->convtype & KICONV_UCS_FROM_UTF8) {
197 /* convert UTF-8 to ENCODING_UNICODE */
199 code = utf8_to_ucs4(p, &inlen, ir);
205 if (casetype == KICONV_FROM_LOWER && dp->ctype) {
206 code = towlower(code, dp->ctype);
207 } else if (casetype == KICONV_FROM_UPPER && dp->ctype) {
208 code = towupper(code, dp->ctype);
211 if ((code >= 0xd800 && code < 0xe000) || code >= 0x110000 ) {
212 /* reserved for utf-16 surrogate pair */
213 /* invalid unicode */
219 if (dp->convtype & KICONV_UCS_UCS4) {
221 code = encode_surrogate(code);
223 /* can't handle with ucs-2 */
231 /* save UCS-4 into ucs[] */
232 for (q = ucs, i = ucslen - 1 ; i >= 0 ; i--)
233 *q++ = (code >> (i << 3)) & 0xff;
235 } else if (dp->convtype & KICONV_UCS_COMBINE && dp->f_ctp) {
236 /* convert local code to ENCODING_UNICODE */
240 ret = iconv_convchr_case(dp->f_ctp, &p, &inlen, (char **)&q,
241 &ucslen, casetype & (KICONV_FROM_LOWER | KICONV_FROM_UPPER));
248 /* src code is a proper subset of ENCODING_UNICODE */
250 if (dp->convtype & KICONV_UCS_FROM_LE) {
258 if ((*q & 0xfc) == 0xd8) {
259 if (dp->convtype & KICONV_UCS_UCS4 &&
260 dp->convtype & KICONV_UCS_FROM_UTF16) {
263 /* invalid unicode */
276 if (dp->convtype & KICONV_UCS_FROM_LE) {
283 if ((*q & 0xfc) != 0xdc) {
284 /* invalid unicode */
292 * The second half of conversion.
293 * (convert ENCODING_UNICODE into any code)
296 if (dp->convtype & KICONV_UCS_TO_UTF8) {
298 if (ucslen == 4 && dp->convtype & KICONV_UCS_UCS4) {
299 /* decode surrogate pair */
300 code = decode_surrogate(p);
302 code = (ucs[0] << 8) | ucs[1];
305 if (casetype == KICONV_LOWER && dp->ctype) {
306 code = towlower(code, dp->ctype);
307 } else if (casetype == KICONV_UPPER && dp->ctype) {
308 code = towupper(code, dp->ctype);
312 if (ucs4_to_utf8(code, q, &outlen, or) == NULL) {
322 } else if (dp->convtype & KICONV_UCS_COMBINE && dp->t_ctp) {
323 ret = iconv_convchr_case(dp->t_ctp, &p, &ucslen, &dst,
324 &or, casetype & (KICONV_LOWER | KICONV_UPPER));
332 /* dst code is a proper subset of ENCODING_UNICODE */
340 if (dp->convtype & KICONV_UCS_TO_LE) {
349 if ((dp->convtype & KICONV_UCS_UCS4) == 0 ||
350 (dp->convtype & KICONV_UCS_TO_UTF16) == 0) {
354 if (dp->convtype & KICONV_UCS_TO_LE) {
370 *inbytesleft -= in - ir;
371 *outbytesleft -= on - or;
376 iconv_ucs_init(struct iconv_converter_class *dcp)
380 error = iconv_add(ENCODING_UNICODE, ENCODING_UNICODE, ENCODING_UTF8);
383 error = iconv_add(ENCODING_UNICODE, ENCODING_UTF8, ENCODING_UNICODE);
390 iconv_ucs_done(struct iconv_converter_class *dcp)
396 iconv_ucs_name(struct iconv_converter_class *dcp)
398 return (ENCODING_UNICODE);
401 static kobj_method_t iconv_ucs_methods[] = {
402 KOBJMETHOD(iconv_converter_open, iconv_ucs_open),
403 KOBJMETHOD(iconv_converter_close, iconv_ucs_close),
404 KOBJMETHOD(iconv_converter_conv, iconv_ucs_conv),
405 KOBJMETHOD(iconv_converter_init, iconv_ucs_init),
406 KOBJMETHOD(iconv_converter_done, iconv_ucs_done),
407 KOBJMETHOD(iconv_converter_name, iconv_ucs_name),
411 KICONV_CONVERTER(ucs, sizeof(struct iconv_ucs));
414 utf8_to_ucs4(const char *src, size_t *utf8width, size_t srclen)
420 * get leading 1 byte from utf-8
422 if ((*src & 0x80) == 0) {
424 * leading 1 bit is "0"
426 * ucs-4: 00000000 00000000 00000000 0xxxxxxx
429 /* get trailing 7 bits */
431 } else if ((*src & 0xe0) == 0xc0) {
433 * leading 3 bits are "110"
434 * utf-8: 110xxxxx 10yyyyyy
435 * ucs-4: 00000000 00000000 00000xxx xxyyyyyy
438 /* get trailing 5 bits */
440 } else if ((*src & 0xf0) == 0xe0) {
442 * leading 4 bits are "1110"
443 * utf-8: 1110xxxx 10yyyyyy 10zzzzzz
444 * ucs-4: 00000000 00000000 xxxxyyyy yyzzzzzz
447 /* get trailing 4 bits */
449 } else if ((*src & 0xf8) == 0xf0) {
451 * leading 5 bits are "11110"
452 * utf-8: 11110www 10xxxxxx 10yyyyyy 10zzzzzz
453 * ucs-4: 00000000 000wwwxx xxxxyyyy yyzzzzzz
456 /* get trailing 3 bits */
459 /* out of utf-16 range or having illegal bits */
467 * get left parts from utf-8
469 for (i = 1 ; i < w ; i++) {
470 if ((*(src + i) & 0xc0) != 0x80) {
471 /* invalid: leading 2 bits are not "10" */
474 /* concatenate trailing 6 bits into ucs4 */
476 ucs4 |= *(src + i) & 0x3f;
484 ucs4_to_utf8(uint32_t ucs4, char *dst, size_t *utf8width, size_t dstlen)
490 * determine utf-8 width and leading bits
495 } else if (ucs4 < 0x800) {
497 lead = 0xc0; /* "11" */
498 } else if (ucs4 < 0x10000) {
500 lead = 0xe0; /* "111" */
501 } else if (ucs4 < 0x200000) {
503 lead = 0xf0; /* "1111" */
515 for (i = w - 1 ; i >= 1 ; i--) {
516 /* get trailing 6 bits and put it with leading bit as "1" */
517 *(p + i) = (ucs4 & 0x3f) | 0x80;
528 encode_surrogate(uint32_t code)
530 return ((((code - 0x10000) << 6) & 0x3ff0000) |
531 ((code - 0x10000) & 0x3ff) | 0xd800dc00);
535 decode_surrogate(const u_char *ucs)
537 return ((((ucs[0] & 0x3) << 18) | (ucs[1] << 10) |
538 ((ucs[2] & 0x3) << 8) | ucs[3]) + 0x10000);