xref: /freebsd/sys/libkern/iconv_xlat16.c (revision 271c3a9060f2ee55607ebe146523f888e1db2654)
1 /*-
2  * Copyright (c) 2003, Ryuichiro Imura
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
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>
35 
36 #include "iconv_converter_if.h"
37 
38 /*
39  * "XLAT16" converter
40  */
41 
42 #ifdef MODULE_DEPEND
43 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2);
44 #endif
45 
46 /*
47  * XLAT16 converter instance
48  */
49 struct iconv_xlat16 {
50 	KOBJ_FIELDS;
51 	uint32_t *		d_table[0x200];
52 	struct iconv_cspair *	d_csp;
53 };
54 
55 static int
56 iconv_xlat16_open(struct iconv_converter_class *dcp,
57 	struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
58 {
59 	struct iconv_xlat16 *dp;
60 	uint32_t *headp, **idxp;
61 	int i;
62 
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++) {
67 		if (*idxp) {
68 			dp->d_table[i] = headp;
69 			headp += 0x80;
70 		} else {
71 			dp->d_table[i] = NULL;
72 		}
73 		idxp++;
74 	}
75 	dp->d_csp = csp;
76 	csp->cp_refcount++;
77 	*dpp = (void*)dp;
78 	return (0);
79 }
80 
81 static int
82 iconv_xlat16_close(void *data)
83 {
84 	struct iconv_xlat16 *dp = data;
85 
86 	dp->d_csp->cp_refcount--;
87 	kobj_delete((struct kobj*)data, M_ICONV);
88 	return (0);
89 }
90 
91 static int
92 iconv_xlat16_conv(void *d2p, const char **inbuf,
93 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
94 	int convchar, int casetype)
95 {
96 	struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
97 	const char *src;
98 	char *dst;
99 	int nullin, ret = 0;
100 	size_t in, on, ir, or, inlen;
101 	uint32_t code;
102 	u_char u, l;
103 	uint16_t c1, c2;
104 
105 	if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
106 		return (0);
107 	ir = in = *inbytesleft;
108 	or = on = *outbytesleft;
109 	src = *inbuf;
110 	dst = *outbuf;
111 
112 	while(ir > 0 && or > 0) {
113 
114 		inlen = 0;
115 		code = '\0';
116 
117 		c1 = ir > 1 ? *(src+1) & 0xff : 0;
118 		c2 = *src & 0xff;
119 
120 		c1 = c2 & 0x80 ? c1 | 0x100 : c1;
121 		c2 = c2 & 0x80 ? c2 & 0x7f : c2;
122 
123 		if (ir > 1 && dp->d_table[c1]) {
124 			/*
125 			 * inbuf char is a double byte char
126 			 */
127 			code = dp->d_table[c1][c2];
128 			if (code)
129 				inlen = 2;
130 		}
131 
132 		if (inlen == 0) {
133 			c1 &= 0xff00;
134 			if (!dp->d_table[c1]) {
135 				ret = -1;
136 				break;
137 			}
138 			/*
139 			 * inbuf char is a single byte char
140 			 */
141 			inlen = 1;
142 			code = dp->d_table[c1][c2];
143 			if (!code) {
144 				ret = -1;
145 				break;
146 			}
147 		}
148 
149 		nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0;
150 		if (inlen == 1 && nullin) {
151 			/*
152 			 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte
153 			 */
154 			ret = -1;
155 			break;
156 		}
157 
158 		/*
159 		 * now start translation
160 		 */
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];
167 		}
168 
169 		u = (u_char)(code >> 8);
170 		l = (u_char)code;
171 
172 #ifdef XLAT16_ACCEPT_3BYTE_CHR
173 		if (code & XLAT16_IS_3BYTE_CHR) {
174 			if (or < 3) {
175 				ret = -1;
176 				break;
177 			}
178 			*dst++ = u;
179 			*dst++ = l;
180 			*dst++ = (u_char)(code >> 16);
181 			or -= 3;
182 		} else
183 #endif
184 		if (u || code & XLAT16_ACCEPT_NULL_OUT) {
185 			if (or < 2) {
186 				ret = -1;
187 				break;
188 			}
189 			*dst++ = u;
190 			*dst++ = l;
191 			or -= 2;
192 		} else {
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);
196 			else
197 				*dst++ = l;
198 			or--;
199 		}
200 
201 		if (inlen == 2) {
202 			/*
203 			 * there is a case that inbuf char is a single
204 			 * byte char while inlen == 2
205 			 */
206 			if ((u_char)*(src+1) == 0 && !nullin ) {
207 				src++;
208 				ir--;
209 			} else {
210 				src += 2;
211 				ir -= 2;
212 			}
213 		} else {
214 			src++;
215 			ir--;
216 		}
217 
218 		if (convchar == 1)
219 			break;
220 	}
221 
222 	*inbuf += in - ir;
223 	*outbuf += on - or;
224 	*inbytesleft -= in - ir;
225 	*outbytesleft -= on - or;
226 	return (ret);
227 }
228 
229 static const char *
230 iconv_xlat16_name(struct iconv_converter_class *dcp)
231 {
232 	return ("xlat16");
233 }
234 
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),
239 #if 0
240 	KOBJMETHOD(iconv_converter_init,	iconv_xlat16_init),
241 	KOBJMETHOD(iconv_converter_done,	iconv_xlat16_done),
242 #endif
243 	KOBJMETHOD(iconv_converter_name,	iconv_xlat16_name),
244 	{0, 0}
245 };
246 
247 KICONV_CONVERTER(xlat16, sizeof(struct iconv_xlat16));
248