xref: /freebsd/sys/libkern/iconv_xlat16.c (revision 81ad6265)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003, 2005 Ryuichiro Imura
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
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>
37 
38 #include "iconv_converter_if.h"
39 
40 /*
41  * "XLAT16" converter
42  */
43 
44 #ifdef MODULE_DEPEND
45 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2);
46 #endif
47 
48 #define C2I1(c)	((c) & 0x8000 ? ((c) & 0xff) | 0x100 : (c) & 0xff)
49 #define C2I2(c)	((c) & 0x8000 ? ((c) >> 8) & 0x7f : ((c) >> 8) & 0xff)
50 
51 /*
52  * XLAT16 converter instance
53  */
54 struct iconv_xlat16 {
55 	KOBJ_FIELDS;
56 	uint32_t *		d_table[0x200];
57 	void *			f_ctp;
58 	void *			t_ctp;
59 	struct iconv_cspair *	d_csp;
60 };
61 
62 static int
63 iconv_xlat16_open(struct iconv_converter_class *dcp,
64 	struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
65 {
66 	struct iconv_xlat16 *dp;
67 	uint32_t *headp, **idxp;
68 	int i;
69 
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++) {
74 		if (*idxp) {
75 			dp->d_table[i] = headp;
76 			headp += 0x80;
77 		} else {
78 			dp->d_table[i] = NULL;
79 		}
80 		idxp++;
81 	}
82 
83 	if (strcmp(csp->cp_to, KICONV_WCTYPE_NAME) != 0) {
84 		if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_from, &dp->f_ctp) != 0)
85 			dp->f_ctp = NULL;
86 		if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_to, &dp->t_ctp) != 0)
87 			dp->t_ctp = NULL;
88 	} else {
89 		dp->f_ctp = dp->t_ctp = dp;
90 	}
91 
92 	dp->d_csp = csp;
93 	csp->cp_refcount++;
94 	*dpp = (void*)dp;
95 	return (0);
96 }
97 
98 static int
99 iconv_xlat16_close(void *data)
100 {
101 	struct iconv_xlat16 *dp = data;
102 
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);
109 	return (0);
110 }
111 
112 static int
113 iconv_xlat16_conv(void *d2p, const char **inbuf,
114 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
115 	int convchar, int casetype)
116 {
117 	struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
118 	const char *src;
119 	char *dst;
120 	int nullin, ret = 0;
121 	size_t in, on, ir, or, inlen;
122 	uint32_t code;
123 	u_char u, l;
124 	uint16_t c1, c2, ctmp;
125 
126 	if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
127 		return (0);
128 	ir = in = *inbytesleft;
129 	or = on = *outbytesleft;
130 	src = *inbuf;
131 	dst = *outbuf;
132 
133 	while(ir > 0 && or > 0) {
134 		inlen = 0;
135 		code = 0;
136 
137 		c1 = ir > 1 ? *(src+1) & 0xff : 0;
138 		c2 = *src & 0xff;
139 		ctmp = 0;
140 
141 		c1 = c2 & 0x80 ? c1 | 0x100 : c1;
142 		c2 = c2 & 0x80 ? c2 & 0x7f : c2;
143 
144 		if (ir > 1 && dp->d_table[c1] && dp->d_table[c1][c2]) {
145 			/*
146 			 * inbuf char is a double byte char
147 			 */
148 			inlen = 2;
149 
150 			/* toupper,tolower */
151 			if (casetype == KICONV_FROM_LOWER && dp->f_ctp)
152 				ctmp = towlower(((u_char)*src << 8) | (u_char)*(src + 1),
153 				    dp->f_ctp);
154 			else if (casetype == KICONV_FROM_UPPER && dp->f_ctp)
155 				ctmp = towupper(((u_char)*src << 8) | (u_char)*(src + 1),
156 				    dp->f_ctp);
157 			if (ctmp) {
158 				c1 = C2I1(ctmp);
159 				c2 = C2I2(ctmp);
160 			}
161 		}
162 
163 		if (inlen == 0) {
164 			c1 &= 0xff00;
165 			if (!dp->d_table[c1]) {
166 				ret = -1;
167 				break;
168 			}
169 			/*
170 			 * inbuf char is a single byte char
171 			 */
172 			inlen = 1;
173 
174 			if (casetype & (KICONV_FROM_LOWER|KICONV_FROM_UPPER))
175 				code = dp->d_table[c1][c2];
176 
177 			if (casetype == KICONV_FROM_LOWER) {
178 				if (dp->f_ctp)
179 					ctmp = towlower((u_char)*src, dp->f_ctp);
180 				else if (code & XLAT16_HAS_FROM_LOWER_CASE)
181 					ctmp = (u_char)(code >> 16);
182 			} else if (casetype == KICONV_FROM_UPPER) {
183 				if (dp->f_ctp)
184 					ctmp = towupper((u_char)*src, dp->f_ctp);
185 				else if (code & XLAT16_HAS_FROM_UPPER_CASE)
186 					ctmp = (u_char)(code >> 16);
187 			}
188 			if (ctmp) {
189 				c1 = C2I1(ctmp << 8);
190 				c2 = C2I2(ctmp << 8);
191 			}
192 		}
193 
194 		code = dp->d_table[c1][c2];
195 		if (!code) {
196 			ret = -1;
197 			break;
198 		}
199 
200 		nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0;
201 		if (inlen == 1 && nullin) {
202 			/*
203 			 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte
204 			 */
205 			ret = -1;
206 			break;
207 		}
208 
209 		/*
210 		 * now start translation
211 		 */
212 		u = (u_char)(code >> 8);
213 		l = (u_char)code;
214 
215 #ifdef XLAT16_ACCEPT_3BYTE_CHR
216 		if (code & XLAT16_IS_3BYTE_CHR) {
217 			if (or < 3) {
218 				ret = -1;
219 				break;
220 			}
221 			*dst++ = u;
222 			*dst++ = l;
223 			*dst++ = (u_char)(code >> 16);
224 			or -= 3;
225 		} else
226 #endif
227 		if (u || code & XLAT16_ACCEPT_NULL_OUT) {
228 			if (or < 2) {
229 				ret = -1;
230 				break;
231 			}
232 
233 			/* toupper,tolower */
234 			if (casetype == KICONV_LOWER && dp->t_ctp) {
235 				code = towlower((uint16_t)code, dp->t_ctp);
236 				u = (u_char)(code >> 8);
237 				l = (u_char)code;
238 			}
239 			if (casetype == KICONV_UPPER && dp->t_ctp) {
240 				code = towupper((uint16_t)code, dp->t_ctp);
241 				u = (u_char)(code >> 8);
242 				l = (u_char)code;
243 			}
244 
245 			*dst++ = u;
246 			*dst++ = l;
247 			or -= 2;
248 		} else {
249 			/* toupper,tolower */
250 			if (casetype == KICONV_LOWER) {
251 				if (dp->t_ctp)
252 					l = (u_char)towlower(l, dp->t_ctp);
253 				else if (code & XLAT16_HAS_LOWER_CASE)
254 					l = (u_char)(code >> 16);
255 			}
256 			if (casetype == KICONV_UPPER) {
257 				if (dp->t_ctp)
258 					l = (u_char)towupper(l, dp->t_ctp);
259 				else if (code & XLAT16_HAS_UPPER_CASE)
260 					l = (u_char)(code >> 16);
261 			}
262 
263 			*dst++ = l;
264 			or--;
265 		}
266 
267 		if (inlen == 2) {
268 			/*
269 			 * there is a case that inbuf char is a single
270 			 * byte char while inlen == 2
271 			 */
272 			if ((u_char)*(src+1) == '\0' && !nullin ) {
273 				src++;
274 				ir--;
275 			} else {
276 				src += 2;
277 				ir -= 2;
278 			}
279 		} else {
280 			src++;
281 			ir--;
282 		}
283 
284 		if (convchar == 1)
285 			break;
286 	}
287 
288 	*inbuf += in - ir;
289 	*outbuf += on - or;
290 	*inbytesleft -= in - ir;
291 	*outbytesleft -= on - or;
292 	return (ret);
293 }
294 
295 static const char *
296 iconv_xlat16_name(struct iconv_converter_class *dcp)
297 {
298 	return ("xlat16");
299 }
300 
301 static int
302 iconv_xlat16_tolower(void *d2p, int c)
303 {
304         struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
305 	int c1, c2, out;
306 
307 	if (c < 0x100) {
308 		c1 = C2I1(c << 8);
309 		c2 = C2I2(c << 8);
310 	} else if (c < 0x10000) {
311                 c1 = C2I1(c);
312                 c2 = C2I2(c);
313 	} else
314 		return (c);
315 
316 	if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_LOWER_CASE) {
317 		/*return (int)(dp->d_table[c1][c2] & 0xffff);*/
318 		out = dp->d_table[c1][c2] & 0xffff;
319 		if ((out & 0xff) == 0)
320 			out = (out >> 8) & 0xff;
321 		return (out);
322 	} else
323 		return (c);
324 }
325 
326 static int
327 iconv_xlat16_toupper(void *d2p, int c)
328 {
329         struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
330 	int c1, c2, out;
331 
332 	if (c < 0x100) {
333 		c1 = C2I1(c << 8);
334 		c2 = C2I2(c << 8);
335 	} else if (c < 0x10000) {
336                 c1 = C2I1(c);
337                 c2 = C2I2(c);
338 	} else
339 		return (c);
340 
341 	if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_UPPER_CASE) {
342 		out = dp->d_table[c1][c2] & 0xffff;
343 		if ((out & 0xff) == 0)
344 			out = (out >> 8) & 0xff;
345 		return (out);
346 	} else
347 		return (c);
348 }
349 
350 static kobj_method_t iconv_xlat16_methods[] = {
351 	KOBJMETHOD(iconv_converter_open,	iconv_xlat16_open),
352 	KOBJMETHOD(iconv_converter_close,	iconv_xlat16_close),
353 	KOBJMETHOD(iconv_converter_conv,	iconv_xlat16_conv),
354 #if 0
355 	KOBJMETHOD(iconv_converter_init,	iconv_xlat16_init),
356 	KOBJMETHOD(iconv_converter_done,	iconv_xlat16_done),
357 #endif
358 	KOBJMETHOD(iconv_converter_name,	iconv_xlat16_name),
359 	KOBJMETHOD(iconv_converter_tolower,	iconv_xlat16_tolower),
360 	KOBJMETHOD(iconv_converter_toupper,	iconv_xlat16_toupper),
361 	{0, 0}
362 };
363 
364 KICONV_CONVERTER(xlat16, sizeof(struct iconv_xlat16));
365