2 * Copyright (c) 2003, 2005 Ryuichiro Imura
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/iconv.h>
36 #include "iconv_converter_if.h"
42 #define KICONV_UCS_COMBINE 0x1
43 #define KICONV_UCS_FROM_UTF8 0x2
44 #define KICONV_UCS_TO_UTF8 0x4
45 #define KICONV_UCS_FROM_LE 0x8
46 #define KICONV_UCS_TO_LE 0x10
47 #define KICONV_UCS_FROM_UTF16 0x20
48 #define KICONV_UCS_TO_UTF16 0x40
49 #define KICONV_UCS_UCS4 0x80
51 #define ENCODING_UTF16 "UTF-16BE"
52 #define ENCODING_UTF8 "UTF-8"
56 int from_flag, to_flag;
57 } unicode_family[] = {
58 { "UTF-8", KICONV_UCS_FROM_UTF8, KICONV_UCS_TO_UTF8 },
59 { "UCS-2LE", KICONV_UCS_FROM_LE, KICONV_UCS_TO_LE },
60 { "UTF-16BE", KICONV_UCS_FROM_UTF16, KICONV_UCS_TO_UTF16 },
61 { "UTF-16LE", KICONV_UCS_FROM_UTF16|KICONV_UCS_FROM_LE,
62 KICONV_UCS_TO_UTF16|KICONV_UCS_TO_LE },
66 static uint32_t utf8_to_ucs4(const char *src, size_t *utf8width, size_t srclen);
67 static u_char *ucs4_to_utf8(uint32_t ucs4, char * dst, size_t *utf8width, size_t dstlen);
68 static uint32_t encode_surrogate(uint32_t code);
69 static uint32_t decode_surrogate(const u_char *ucs);
72 MODULE_DEPEND(iconv_ucs, libiconv, 2, 2, 2);
76 * UCS converter instance
81 struct iconv_cspair * d_csp;
82 struct iconv_cspair * d_cspf;
89 iconv_ucs_open(struct iconv_converter_class *dcp,
90 struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
94 const char *from, *to;
96 dp = (struct iconv_ucs *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK);
98 from = cspf ? cspf->cp_from : csp->cp_from;
103 dp->convtype |= KICONV_UCS_COMBINE;
104 for (i = 0; unicode_family[i].name; i++) {
105 if (strcasecmp(from, unicode_family[i].name) == 0)
106 dp->convtype |= unicode_family[i].from_flag;
107 if (strcasecmp(to, unicode_family[i].name) == 0)
108 dp->convtype |= unicode_family[i].to_flag;
110 if (strcmp(ENCODING_UNICODE, ENCODING_UTF16) == 0)
111 dp->convtype |= KICONV_UCS_UCS4;
113 dp->convtype &= ~KICONV_UCS_UCS4;
115 dp->f_ctp = dp->t_ctp = NULL;
116 if (dp->convtype & KICONV_UCS_COMBINE) {
117 if ((dp->convtype & KICONV_UCS_FROM_UTF8) == 0 &&
118 (dp->convtype & KICONV_UCS_FROM_LE) == 0) {
119 iconv_open(ENCODING_UNICODE, from, &dp->f_ctp);
121 if ((dp->convtype & KICONV_UCS_TO_UTF8) == 0 &&
122 (dp->convtype & KICONV_UCS_TO_LE) == 0) {
123 iconv_open(to, ENCODING_UNICODE, &dp->t_ctp);
128 if (dp->convtype & (KICONV_UCS_FROM_UTF8 | KICONV_UCS_TO_UTF8))
129 iconv_open(KICONV_WCTYPE_NAME, ENCODING_UTF8, &dp->ctype);
132 if (dp->convtype & (KICONV_UCS_FROM_UTF8 | KICONV_UCS_FROM_LE)) {
139 if (dp->convtype & (KICONV_UCS_TO_UTF8 | KICONV_UCS_TO_LE))
146 iconv_ucs_close(void *data)
148 struct iconv_ucs *dp = data;
151 iconv_close(dp->f_ctp);
153 iconv_close(dp->t_ctp);
155 iconv_close(dp->ctype);
157 dp->d_cspf->cp_refcount--;
158 else if (dp->convtype & (KICONV_UCS_FROM_UTF8 | KICONV_UCS_FROM_LE))
159 dp->d_csp->cp_refcount--;
160 if (dp->convtype & (KICONV_UCS_TO_UTF8 | KICONV_UCS_TO_LE))
161 dp->d_csp->cp_refcount--;
162 kobj_delete((struct kobj*)data, M_ICONV);
167 iconv_ucs_conv(void *d2p, const char **inbuf,
168 size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
169 int convchar, int casetype)
171 struct iconv_ucs *dp = (struct iconv_ucs*)d2p;
173 size_t in, on, ir, or, inlen, outlen, ucslen;
179 if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
181 ir = in = *inbytesleft;
182 or = on = *outbytesleft;
186 while (ir > 0 && or > 0) {
189 * The first half of conversion.
190 * (convert any code into ENCODING_UNICODE)
194 if (dp->convtype & KICONV_UCS_FROM_UTF8) {
195 /* convert UTF-8 to ENCODING_UNICODE */
197 code = utf8_to_ucs4(p, &inlen, ir);
203 if (casetype == KICONV_FROM_LOWER && dp->ctype) {
204 code = towlower(code, dp->ctype);
205 } else if (casetype == KICONV_FROM_UPPER && dp->ctype) {
206 code = towupper(code, dp->ctype);
209 if ((code >= 0xd800 && code < 0xe000) || code >= 0x110000 ) {
210 /* reserved for utf-16 surrogate pair */
211 /* invalid unicode */
217 if (dp->convtype & KICONV_UCS_UCS4) {
219 code = encode_surrogate(code);
221 /* can't handle with ucs-2 */
229 /* save UCS-4 into ucs[] */
230 for (q = ucs, i = ucslen - 1 ; i >= 0 ; i--)
231 *q++ = (code >> (i << 3)) & 0xff;
233 } else if (dp->convtype & KICONV_UCS_COMBINE && dp->f_ctp) {
234 /* convert local code to ENCODING_UNICODE */
238 ret = iconv_convchr_case(dp->f_ctp, &p, &inlen, (char **)&q,
239 &ucslen, casetype & (KICONV_FROM_LOWER | KICONV_FROM_UPPER));
246 /* src code is a proper subset of ENCODING_UNICODE */
248 if (dp->convtype & KICONV_UCS_FROM_LE) {
256 if ((*q & 0xfc) == 0xd8) {
257 if (dp->convtype & KICONV_UCS_UCS4 &&
258 dp->convtype & KICONV_UCS_FROM_UTF16) {
261 /* invalid unicode */
274 if (dp->convtype & KICONV_UCS_FROM_LE) {
281 if ((*q & 0xfc) != 0xdc) {
282 /* invalid unicode */
290 * The second half of conversion.
291 * (convert ENCODING_UNICODE into any code)
294 if (dp->convtype & KICONV_UCS_TO_UTF8) {
296 if (ucslen == 4 && dp->convtype & KICONV_UCS_UCS4) {
297 /* decode surrogate pair */
298 code = decode_surrogate(p);
300 code = (ucs[0] << 8) | ucs[1];
303 if (casetype == KICONV_LOWER && dp->ctype) {
304 code = towlower(code, dp->ctype);
305 } else if (casetype == KICONV_UPPER && dp->ctype) {
306 code = towupper(code, dp->ctype);
310 if (ucs4_to_utf8(code, q, &outlen, or) == NULL) {
320 } else if (dp->convtype & KICONV_UCS_COMBINE && dp->t_ctp) {
321 ret = iconv_convchr_case(dp->t_ctp, &p, &ucslen, &dst,
322 &or, casetype & (KICONV_LOWER | KICONV_UPPER));
330 /* dst code is a proper subset of ENCODING_UNICODE */
338 if (dp->convtype & KICONV_UCS_TO_LE) {
347 if ((dp->convtype & KICONV_UCS_UCS4) == 0 ||
348 (dp->convtype & KICONV_UCS_TO_UTF16) == 0) {
352 if (dp->convtype & KICONV_UCS_TO_LE) {
368 *inbytesleft -= in - ir;
369 *outbytesleft -= on - or;
374 iconv_ucs_init(struct iconv_converter_class *dcp)
378 error = iconv_add(ENCODING_UNICODE, ENCODING_UNICODE, ENCODING_UTF8);
381 error = iconv_add(ENCODING_UNICODE, ENCODING_UTF8, ENCODING_UNICODE);
388 iconv_ucs_done(struct iconv_converter_class *dcp)
394 iconv_ucs_name(struct iconv_converter_class *dcp)
396 return (ENCODING_UNICODE);
399 static kobj_method_t iconv_ucs_methods[] = {
400 KOBJMETHOD(iconv_converter_open, iconv_ucs_open),
401 KOBJMETHOD(iconv_converter_close, iconv_ucs_close),
402 KOBJMETHOD(iconv_converter_conv, iconv_ucs_conv),
403 KOBJMETHOD(iconv_converter_init, iconv_ucs_init),
404 KOBJMETHOD(iconv_converter_done, iconv_ucs_done),
405 KOBJMETHOD(iconv_converter_name, iconv_ucs_name),
409 KICONV_CONVERTER(ucs, sizeof(struct iconv_ucs));
412 utf8_to_ucs4(const char *src, size_t *utf8width, size_t srclen)
418 * get leading 1 byte from utf-8
420 if ((*src & 0x80) == 0) {
422 * leading 1 bit is "0"
424 * ucs-4: 00000000 00000000 00000000 0xxxxxxx
427 /* get trailing 7 bits */
429 } else if ((*src & 0xe0) == 0xc0) {
431 * leading 3 bits are "110"
432 * utf-8: 110xxxxx 10yyyyyy
433 * ucs-4: 00000000 00000000 00000xxx xxyyyyyy
436 /* get trailing 5 bits */
438 } else if ((*src & 0xf0) == 0xe0) {
440 * leading 4 bits are "1110"
441 * utf-8: 1110xxxx 10yyyyyy 10zzzzzz
442 * ucs-4: 00000000 00000000 xxxxyyyy yyzzzzzz
445 /* get trailing 4 bits */
447 } else if ((*src & 0xf8) == 0xf0) {
449 * leading 5 bits are "11110"
450 * utf-8: 11110www 10xxxxxx 10yyyyyy 10zzzzzz
451 * ucs-4: 00000000 000wwwxx xxxxyyyy yyzzzzzz
454 /* get trailing 3 bits */
457 /* out of utf-16 range or having illegal bits */
465 * get left parts from utf-8
467 for (i = 1 ; i < w ; i++) {
468 if ((*(src + i) & 0xc0) != 0x80) {
469 /* invalid: leading 2 bits are not "10" */
472 /* concatenate trailing 6 bits into ucs4 */
474 ucs4 |= *(src + i) & 0x3f;
482 ucs4_to_utf8(uint32_t ucs4, char *dst, size_t *utf8width, size_t dstlen)
488 * determine utf-8 width and leading bits
493 } else if (ucs4 < 0x800) {
495 lead = 0xc0; /* "11" */
496 } else if (ucs4 < 0x10000) {
498 lead = 0xe0; /* "111" */
499 } else if (ucs4 < 0x200000) {
501 lead = 0xf0; /* "1111" */
513 for (i = w - 1 ; i >= 1 ; i--) {
514 /* get trailing 6 bits and put it with leading bit as "1" */
515 *(p + i) = (ucs4 & 0x3f) | 0x80;
526 encode_surrogate(uint32_t code)
528 return ((((code - 0x10000) << 6) & 0x3ff0000) |
529 ((code - 0x10000) & 0x3ff) | 0xd800dc00);
533 decode_surrogate(const u_char *ucs)
535 return ((((ucs[0] & 0x3) << 18) | (ucs[1] << 10) |
536 ((ucs[2] & 0x3) << 8) | ucs[3]) + 0x10000);