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
26 * $FreeBSD: src/sys/libkern/iconv_xlat16.c,v 1.3.20.1 2009/04/15 03:14:26 kensmith Exp $
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/iconv.h>
35 #include "iconv_converter_if.h"
42 MODULE_DEPEND(iconv_xlat16
, libiconv
, 2, 2, 2);
46 * XLAT16 converter instance
50 uint32_t * d_table
[0x200];
51 struct iconv_cspair
* d_csp
;
55 iconv_xlat16_open(struct iconv_converter_class
*dcp
,
56 struct iconv_cspair
*csp
, struct iconv_cspair
*cspf
, void **dpp
)
58 struct iconv_xlat16
*dp
;
59 uint32_t *headp
, **idxp
;
62 dp
= (struct iconv_xlat16
*)kobj_create((struct kobj_class
*)dcp
, M_ICONV
, M_WAITOK
);
63 headp
= (uint32_t *)((caddr_t
)csp
->cp_data
+ sizeof(dp
->d_table
));
64 idxp
= (uint32_t **)csp
->cp_data
;
65 for (i
= 0 ; i
< 0x200 ; i
++) {
67 dp
->d_table
[i
] = headp
;
70 dp
->d_table
[i
] = NULL
;
81 iconv_xlat16_close(void *data
)
83 struct iconv_xlat16
*dp
= data
;
85 dp
->d_csp
->cp_refcount
--;
86 kobj_delete((struct kobj
*)data
, M_ICONV
);
91 iconv_xlat16_conv(void *d2p
, const char **inbuf
,
92 size_t *inbytesleft
, char **outbuf
, size_t *outbytesleft
,
93 int convchar
, int casetype
)
95 struct iconv_xlat16
*dp
= (struct iconv_xlat16
*)d2p
;
99 size_t in
, on
, ir
, or, inlen
;
104 if (inbuf
== NULL
|| *inbuf
== NULL
|| outbuf
== NULL
|| *outbuf
== NULL
)
106 ir
= in
= *inbytesleft
;
107 or = on
= *outbytesleft
;
111 while(ir
> 0 && or > 0) {
116 c1
= ir
> 1 ? *(src
+1) & 0xff : 0;
119 c1
= c2
& 0x80 ? c1
| 0x100 : c1
;
120 c2
= c2
& 0x80 ? c2
& 0x7f : c2
;
122 if (ir
> 1 && dp
->d_table
[c1
]) {
124 * inbuf char is a double byte char
126 code
= dp
->d_table
[c1
][c2
];
133 if (!dp
->d_table
[c1
]) {
138 * inbuf char is a single byte char
141 code
= dp
->d_table
[c1
][c2
];
148 nullin
= (code
& XLAT16_ACCEPT_NULL_IN
) ? 1 : 0;
149 if (inlen
== 1 && nullin
) {
151 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte
158 * now start translation
160 if ((casetype
== KICONV_FROM_LOWER
&& code
& XLAT16_HAS_FROM_LOWER_CASE
) ||
161 (casetype
== KICONV_FROM_UPPER
&& code
& XLAT16_HAS_FROM_UPPER_CASE
)) {
162 c2
= (u_char
)(code
>> 16);
163 c1
= c2
& 0x80 ? 0x100 : 0;
164 c2
= c2
& 0x80 ? c2
& 0x7f : c2
;
165 code
= dp
->d_table
[c1
][c2
];
168 u
= (u_char
)(code
>> 8);
171 #ifdef XLAT16_ACCEPT_3BYTE_CHR
172 if (code
& XLAT16_IS_3BYTE_CHR
) {
179 *dst
++ = (u_char
)(code
>> 16);
183 if (u
|| code
& XLAT16_ACCEPT_NULL_OUT
) {
192 if ((casetype
== KICONV_LOWER
&& code
& XLAT16_HAS_LOWER_CASE
) ||
193 (casetype
== KICONV_UPPER
&& code
& XLAT16_HAS_UPPER_CASE
))
194 *dst
++ = (u_char
)(code
>> 16);
202 * there is a case that inbuf char is a single
203 * byte char while inlen == 2
205 if ((u_char
)*(src
+1) == 0 && !nullin
) {
223 *inbytesleft
-= in
- ir
;
224 *outbytesleft
-= on
- or;
229 iconv_xlat16_name(struct iconv_converter_class
*dcp
)
234 static kobj_method_t iconv_xlat16_methods
[] = {
235 KOBJMETHOD(iconv_converter_open
, iconv_xlat16_open
),
236 KOBJMETHOD(iconv_converter_close
, iconv_xlat16_close
),
237 KOBJMETHOD(iconv_converter_conv
, iconv_xlat16_conv
),
239 KOBJMETHOD(iconv_converter_init
, iconv_xlat16_init
),
240 KOBJMETHOD(iconv_converter_done
, iconv_xlat16_done
),
242 KOBJMETHOD(iconv_converter_name
, iconv_xlat16_name
),
246 KICONV_CONVERTER(xlat16
, sizeof(struct iconv_xlat16
));