2 * Copyright (c) 2003, 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"
43 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2);
47 * XLAT16 converter instance
51 uint32_t * d_table[0x200];
52 struct iconv_cspair * d_csp;
56 iconv_xlat16_open(struct iconv_converter_class *dcp,
57 struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
59 struct iconv_xlat16 *dp;
60 uint32_t *headp, **idxp;
63 dp = (struct iconv_xlat16 *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK);
64 headp = (uint32_t *)((caddr_t)csp->cp_data + sizeof(dp->d_table));
65 idxp = (uint32_t **)csp->cp_data;
66 for (i = 0 ; i < 0x200 ; i++) {
68 dp->d_table[i] = headp;
71 dp->d_table[i] = NULL;
82 iconv_xlat16_close(void *data)
84 struct iconv_xlat16 *dp = data;
86 dp->d_csp->cp_refcount--;
87 kobj_delete((struct kobj*)data, M_ICONV);
92 iconv_xlat16_conv(void *d2p, const char **inbuf,
93 size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
94 int convchar, int casetype)
96 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
100 size_t in, on, ir, or, inlen;
105 if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
107 ir = in = *inbytesleft;
108 or = on = *outbytesleft;
112 while(ir > 0 && or > 0) {
117 c1 = ir > 1 ? *(src+1) & 0xff : 0;
120 c1 = c2 & 0x80 ? c1 | 0x100 : c1;
121 c2 = c2 & 0x80 ? c2 & 0x7f : c2;
123 if (ir > 1 && dp->d_table[c1]) {
125 * inbuf char is a double byte char
127 code = dp->d_table[c1][c2];
134 if (!dp->d_table[c1]) {
139 * inbuf char is a single byte char
142 code = dp->d_table[c1][c2];
149 nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0;
150 if (inlen == 1 && nullin) {
152 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte
159 * now start translation
161 if ((casetype == KICONV_FROM_LOWER && code & XLAT16_HAS_FROM_LOWER_CASE) ||
162 (casetype == KICONV_FROM_UPPER && code & XLAT16_HAS_FROM_UPPER_CASE)) {
163 c2 = (u_char)(code >> 16);
164 c1 = c2 & 0x80 ? 0x100 : 0;
165 c2 = c2 & 0x80 ? c2 & 0x7f : c2;
166 code = dp->d_table[c1][c2];
169 u = (u_char)(code >> 8);
172 #ifdef XLAT16_ACCEPT_3BYTE_CHR
173 if (code & XLAT16_IS_3BYTE_CHR) {
180 *dst++ = (u_char)(code >> 16);
184 if (u || code & XLAT16_ACCEPT_NULL_OUT) {
193 if ((casetype == KICONV_LOWER && code & XLAT16_HAS_LOWER_CASE) ||
194 (casetype == KICONV_UPPER && code & XLAT16_HAS_UPPER_CASE))
195 *dst++ = (u_char)(code >> 16);
203 * there is a case that inbuf char is a single
204 * byte char while inlen == 2
206 if ((u_char)*(src+1) == 0 && !nullin ) {
224 *inbytesleft -= in - ir;
225 *outbytesleft -= on - or;
230 iconv_xlat16_name(struct iconv_converter_class *dcp)
235 static kobj_method_t iconv_xlat16_methods[] = {
236 KOBJMETHOD(iconv_converter_open, iconv_xlat16_open),
237 KOBJMETHOD(iconv_converter_close, iconv_xlat16_close),
238 KOBJMETHOD(iconv_converter_conv, iconv_xlat16_conv),
240 KOBJMETHOD(iconv_converter_init, iconv_xlat16_init),
241 KOBJMETHOD(iconv_converter_done, iconv_xlat16_done),
243 KOBJMETHOD(iconv_converter_name, iconv_xlat16_name),
247 KICONV_CONVERTER(xlat16, sizeof(struct iconv_xlat16));