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"
45 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2);
48 #define C2I1(c) ((c) & 0x8000 ? ((c) & 0xff) | 0x100 : (c) & 0xff)
49 #define C2I2(c) ((c) & 0x8000 ? ((c) >> 8) & 0x7f : ((c) >> 8) & 0xff)
52 * XLAT16 converter instance
56 uint32_t * d_table[0x200];
59 struct iconv_cspair * d_csp;
63 iconv_xlat16_open(struct iconv_converter_class *dcp,
64 struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
66 struct iconv_xlat16 *dp;
67 uint32_t *headp, **idxp;
70 dp = (struct iconv_xlat16 *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK);
71 headp = (uint32_t *)((caddr_t)csp->cp_data + sizeof(dp->d_table));
72 idxp = (uint32_t **)csp->cp_data;
73 for (i = 0 ; i < 0x200 ; i++) {
75 dp->d_table[i] = headp;
78 dp->d_table[i] = NULL;
83 if (strcmp(csp->cp_to, KICONV_WCTYPE_NAME) != 0) {
84 if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_from, &dp->f_ctp) != 0)
86 if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_to, &dp->t_ctp) != 0)
89 dp->f_ctp = dp->t_ctp = dp;
99 iconv_xlat16_close(void *data)
101 struct iconv_xlat16 *dp = data;
103 if (dp->f_ctp && dp->f_ctp != data)
104 iconv_close(dp->f_ctp);
105 if (dp->t_ctp && dp->t_ctp != data)
106 iconv_close(dp->t_ctp);
107 dp->d_csp->cp_refcount--;
108 kobj_delete((struct kobj*)data, M_ICONV);
113 iconv_xlat16_conv(void *d2p, const char **inbuf,
114 size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
115 int convchar, int casetype)
117 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
121 size_t in, on, ir, or, inlen;
124 uint16_t c1, c2, ctmp;
126 if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
128 ir = in = *inbytesleft;
129 or = on = *outbytesleft;
133 while(ir > 0 && or > 0) {
138 c1 = ir > 1 ? *(src+1) & 0xff : 0;
142 c1 = c2 & 0x80 ? c1 | 0x100 : c1;
143 c2 = c2 & 0x80 ? c2 & 0x7f : c2;
145 if (ir > 1 && dp->d_table[c1] && dp->d_table[c1][c2]) {
147 * inbuf char is a double byte char
151 /* toupper,tolower */
152 if (casetype == KICONV_FROM_LOWER && dp->f_ctp)
153 ctmp = towlower(((u_char)*src << 8) | (u_char)*(src + 1),
155 else if (casetype == KICONV_FROM_UPPER && dp->f_ctp)
156 ctmp = towupper(((u_char)*src << 8) | (u_char)*(src + 1),
166 if (!dp->d_table[c1]) {
171 * inbuf char is a single byte char
175 if (casetype & (KICONV_FROM_LOWER|KICONV_FROM_UPPER))
176 code = dp->d_table[c1][c2];
178 if (casetype == KICONV_FROM_LOWER) {
180 ctmp = towlower((u_char)*src, dp->f_ctp);
181 else if (code & XLAT16_HAS_FROM_LOWER_CASE)
182 ctmp = (u_char)(code >> 16);
183 } else if (casetype == KICONV_FROM_UPPER) {
185 ctmp = towupper((u_char)*src, dp->f_ctp);
186 else if (code & XLAT16_HAS_FROM_UPPER_CASE)
187 ctmp = (u_char)(code >> 16);
190 c1 = C2I1(ctmp << 8);
191 c2 = C2I2(ctmp << 8);
195 code = dp->d_table[c1][c2];
201 nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0;
202 if (inlen == 1 && nullin) {
204 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte
211 * now start translation
213 u = (u_char)(code >> 8);
216 #ifdef XLAT16_ACCEPT_3BYTE_CHR
217 if (code & XLAT16_IS_3BYTE_CHR) {
224 *dst++ = (u_char)(code >> 16);
228 if (u || code & XLAT16_ACCEPT_NULL_OUT) {
234 /* toupper,tolower */
235 if (casetype == KICONV_LOWER && dp->t_ctp) {
236 code = towlower((uint16_t)code, dp->t_ctp);
237 u = (u_char)(code >> 8);
240 if (casetype == KICONV_UPPER && dp->t_ctp) {
241 code = towupper((uint16_t)code, dp->t_ctp);
242 u = (u_char)(code >> 8);
250 /* toupper,tolower */
251 if (casetype == KICONV_LOWER) {
253 l = (u_char)towlower(l, dp->t_ctp);
254 else if (code & XLAT16_HAS_LOWER_CASE)
255 l = (u_char)(code >> 16);
257 if (casetype == KICONV_UPPER) {
259 l = (u_char)towupper(l, dp->t_ctp);
260 else if (code & XLAT16_HAS_UPPER_CASE)
261 l = (u_char)(code >> 16);
270 * there is a case that inbuf char is a single
271 * byte char while inlen == 2
273 if ((u_char)*(src+1) == '\0' && !nullin ) {
291 *inbytesleft -= in - ir;
292 *outbytesleft -= on - or;
297 iconv_xlat16_name(struct iconv_converter_class *dcp)
303 iconv_xlat16_tolower(void *d2p, int c)
305 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
311 } else if (c < 0x10000) {
317 if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_LOWER_CASE) {
318 /*return (int)(dp->d_table[c1][c2] & 0xffff);*/
319 out = dp->d_table[c1][c2] & 0xffff;
320 if ((out & 0xff) == 0)
321 out = (out >> 8) & 0xff;
328 iconv_xlat16_toupper(void *d2p, int c)
330 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
336 } else if (c < 0x10000) {
342 if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_UPPER_CASE) {
343 out = dp->d_table[c1][c2] & 0xffff;
344 if ((out & 0xff) == 0)
345 out = (out >> 8) & 0xff;
351 static kobj_method_t iconv_xlat16_methods[] = {
352 KOBJMETHOD(iconv_converter_open, iconv_xlat16_open),
353 KOBJMETHOD(iconv_converter_close, iconv_xlat16_close),
354 KOBJMETHOD(iconv_converter_conv, iconv_xlat16_conv),
356 KOBJMETHOD(iconv_converter_init, iconv_xlat16_init),
357 KOBJMETHOD(iconv_converter_done, iconv_xlat16_done),
359 KOBJMETHOD(iconv_converter_name, iconv_xlat16_name),
360 KOBJMETHOD(iconv_converter_tolower, iconv_xlat16_tolower),
361 KOBJMETHOD(iconv_converter_toupper, iconv_xlat16_toupper),
365 KICONV_CONVERTER(xlat16, sizeof(struct iconv_xlat16));