xref: /freebsd/lib/libkiconv/xlat16_iconv.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 /*
30  * kiconv(3) requires shared linked, and reduce module size
31  * when statically linked.
32  */
33 
34 #ifdef PIC
35 
36 #include <sys/types.h>
37 #include <sys/iconv.h>
38 #include <sys/sysctl.h>
39 
40 #include <ctype.h>
41 #include <dlfcn.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <locale.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <wctype.h>
49 
50 #include "quirks.h"
51 
52 struct xlat16_table {
53 	uint32_t *	idx[0x200];
54 	void *		data;
55 	size_t		size;
56 };
57 
58 static struct xlat16_table kiconv_xlat16_open(const char *, const char *, int);
59 static int chklocale(int, const char *);
60 
61 #ifdef ICONV_DLOPEN
62 typedef void *iconv_t;
63 static int my_iconv_init(void);
64 static iconv_t (*my_iconv_open)(const char *, const char *);
65 static size_t (*my_iconv)(iconv_t, char **, size_t *, char **, size_t *);
66 static int (*my_iconv_close)(iconv_t);
67 #else
68 #include <iconv.h>
69 #define my_iconv_init() 0
70 #define my_iconv_open iconv_open
71 #define my_iconv iconv
72 #define my_iconv_close iconv_close
73 #endif
74 static size_t my_iconv_char(iconv_t, u_char **, size_t *, u_char **, size_t *);
75 
76 int
77 kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
78 {
79 	int error;
80 	size_t idxsize;
81 	struct xlat16_table xt;
82 	void *data;
83 	char *p;
84 	const char unicode[] = ENCODING_UNICODE;
85 
86 	if ((flag & KICONV_WCTYPE) == 0 &&
87 	    strcmp(unicode, tocode) != 0 &&
88 	    strcmp(unicode, fromcode) != 0 &&
89 	    kiconv_lookupconv(unicode) == 0) {
90 		error = kiconv_add_xlat16_cspair(unicode, fromcode, flag);
91 		if (error)
92 			return (-1);
93 		error = kiconv_add_xlat16_cspair(tocode, unicode, flag);
94 		return (error);
95 	}
96 
97 	if (kiconv_lookupcs(tocode, fromcode) == 0)
98 		return (0);
99 
100 	if (flag & KICONV_WCTYPE)
101 		xt = kiconv_xlat16_open(fromcode, fromcode, flag);
102 	else
103 		xt = kiconv_xlat16_open(tocode, fromcode, flag);
104 	if (xt.size == 0)
105 		return (-1);
106 
107 	idxsize = sizeof(xt.idx);
108 
109 	if ((idxsize + xt.size) > ICONV_CSMAXDATALEN) {
110 		errno = E2BIG;
111 		return (-1);
112 	}
113 
114 	if ((data = malloc(idxsize + xt.size)) != NULL) {
115 		p = data;
116 		memcpy(p, xt.idx, idxsize);
117 		p += idxsize;
118 		memcpy(p, xt.data, xt.size);
119 		error = kiconv_add_xlat16_table(tocode, fromcode, data,
120 		    (int)(idxsize + xt.size));
121 		return (error);
122 	}
123 
124 	return (-1);
125 }
126 
127 int
128 kiconv_add_xlat16_cspairs(const char *foreigncode, const char *localcode)
129 {
130 	int error, locale;
131 
132 	error = kiconv_add_xlat16_cspair(foreigncode, localcode,
133 	    KICONV_FROM_LOWER | KICONV_FROM_UPPER);
134 	if (error)
135 		return (error);
136 	error = kiconv_add_xlat16_cspair(localcode, foreigncode,
137 	    KICONV_LOWER | KICONV_UPPER);
138 	if (error)
139 		return (error);
140 	locale = chklocale(LC_CTYPE, localcode);
141 	if (locale == 0) {
142 		error = kiconv_add_xlat16_cspair(KICONV_WCTYPE_NAME, localcode,
143 		    KICONV_WCTYPE);
144 		if (error)
145 			return (error);
146 	}
147 
148 	return (0);
149 }
150 
151 static struct xlat16_table
152 kiconv_xlat16_open(const char *tocode, const char *fromcode, int lcase)
153 {
154 	u_char src[3], dst[4], *srcp, *dstp, ud, ld;
155 	int us, ls, ret;
156 	uint16_t c;
157 	uint32_t table[0x80];
158 	size_t inbytesleft, outbytesleft, pre_q_size, post_q_size;
159 	struct xlat16_table xt;
160 	struct quirk_replace_list *pre_q_list, *post_q_list;
161 	iconv_t cd;
162 	char *p;
163 
164 	xt.data = NULL;
165 	xt.size = 0;
166 
167 	src[2] = '\0';
168 	dst[3] = '\0';
169 
170 	ret = my_iconv_init();
171 	if (ret)
172 		return (xt);
173 
174 	cd = my_iconv_open(search_quirk(tocode, fromcode, &pre_q_list, &pre_q_size),
175 	    search_quirk(fromcode, tocode, &post_q_list, &post_q_size));
176 	if (cd == (iconv_t) (-1))
177 		return (xt);
178 
179 	if ((xt.data = malloc(0x200 * 0x80 * sizeof(uint32_t))) == NULL)
180 		return (xt);
181 
182 	p = xt.data;
183 
184 	for (ls = 0 ; ls < 0x200 ; ls++) {
185 		xt.idx[ls] = NULL;
186 		for (us = 0 ; us < 0x80 ; us++) {
187 			srcp = src;
188 			dstp = dst;
189 
190 			inbytesleft = 2;
191 			outbytesleft = 3;
192 			bzero(dst, outbytesleft);
193 
194 			c = ((ls & 0x100 ? us | 0x80 : us) << 8) | (u_char)ls;
195 
196 			if (lcase & KICONV_WCTYPE) {
197 				if ((c & 0xff) == 0)
198 					c >>= 8;
199 				if (iswupper(c)) {
200 					c = towlower(c);
201 					if ((c & 0xff00) == 0)
202 						c <<= 8;
203 					table[us] = c | XLAT16_HAS_LOWER_CASE;
204 				} else if (iswlower(c)) {
205 					c = towupper(c);
206 					if ((c & 0xff00) == 0)
207 						c <<= 8;
208 					table[us] = c | XLAT16_HAS_UPPER_CASE;
209 				} else
210 					table[us] = 0;
211 				/*
212 				 * store not NULL
213 				 */
214 				if (table[us])
215 					xt.idx[ls] = table;
216 
217 				continue;
218 			}
219 
220 			c = quirk_vendor2unix(c, pre_q_list, pre_q_size);
221 			src[0] = (u_char)(c >> 8);
222 			src[1] = (u_char)c;
223 
224 			ret = my_iconv_char(cd, &srcp, &inbytesleft,
225 				&dstp, &outbytesleft);
226 			if (ret == -1) {
227 				table[us] = 0;
228 				continue;
229 			}
230 
231 			ud = (u_char)dst[0];
232 			ld = (u_char)dst[1];
233 
234 			switch(outbytesleft) {
235 			case 0:
236 #ifdef XLAT16_ACCEPT_3BYTE_CHR
237 				table[us] = (ud << 8) | ld;
238 				table[us] |= (u_char)dst[2] << 16;
239 				table[us] |= XLAT16_IS_3BYTE_CHR;
240 #else
241 				table[us] = 0;
242 				continue;
243 #endif
244 				break;
245 			case 1:
246 				table[us] = quirk_unix2vendor((ud << 8) | ld,
247 				    post_q_list, post_q_size);
248 				if ((table[us] >> 8) == 0)
249 					table[us] |= XLAT16_ACCEPT_NULL_OUT;
250 				break;
251 			case 2:
252 				table[us] = ud;
253 				if (lcase & KICONV_LOWER && ud != tolower(ud)) {
254 					table[us] |= (u_char)tolower(ud) << 16;
255 					table[us] |= XLAT16_HAS_LOWER_CASE;
256 				}
257 				if (lcase & KICONV_UPPER && ud != toupper(ud)) {
258 					table[us] |= (u_char)toupper(ud) << 16;
259 					table[us] |= XLAT16_HAS_UPPER_CASE;
260 				}
261 				break;
262 			}
263 
264 			switch(inbytesleft) {
265 			case 0:
266 				if ((ls & 0xff) == 0)
267 					table[us] |= XLAT16_ACCEPT_NULL_IN;
268 				break;
269 			case 1:
270 				c = ls > 0xff ? us | 0x80 : us;
271 				if (lcase & KICONV_FROM_LOWER && c != tolower(c)) {
272 					table[us] |= (u_char)tolower(c) << 16;
273 					table[us] |= XLAT16_HAS_FROM_LOWER_CASE;
274 				}
275 				if (lcase & KICONV_FROM_UPPER && c != toupper(c)) {
276 					table[us] |= (u_char)toupper(c) << 16;
277 					table[us] |= XLAT16_HAS_FROM_UPPER_CASE;
278 				}
279 				break;
280 			}
281 
282 			if (table[us] == 0)
283 				continue;
284 
285 			/*
286 			 * store not NULL
287 			 */
288 			xt.idx[ls] = table;
289 		}
290 		if (xt.idx[ls]) {
291 			memcpy(p, table, sizeof(table));
292 			p += sizeof(table);
293 		}
294 	}
295 	my_iconv_close(cd);
296 
297 	xt.size = p - (char *)xt.data;
298 	xt.data = realloc(xt.data, xt.size);
299 	return (xt);
300 }
301 
302 static int
303 chklocale(int category, const char *code)
304 {
305 	char *p;
306 	int error = -1;
307 
308 	p = strchr(setlocale(category, NULL), '.');
309 	if (p++) {
310 		error = strcasecmp(code, p);
311 		if (error) {
312 			/* XXX - can't avoid calling quirk here... */
313 			error = strcasecmp(code, kiconv_quirkcs(p,
314 			    KICONV_VENDOR_MICSFT));
315 		}
316 	}
317 	return (error);
318 }
319 
320 #ifdef ICONV_DLOPEN
321 static int
322 my_iconv_init(void)
323 {
324 	void *iconv_lib;
325 
326 	iconv_lib = dlopen("libiconv.so", RTLD_LAZY | RTLD_GLOBAL);
327 	if (iconv_lib == NULL) {
328 		warn("Unable to load iconv library: %s\n", dlerror());
329 		errno = ENOENT;
330 		return (-1);
331 	}
332 	my_iconv_open = dlsym(iconv_lib, "iconv_open");
333 	my_iconv = dlsym(iconv_lib, "iconv");
334 	my_iconv_close = dlsym(iconv_lib, "iconv_close");
335 
336 	return (0);
337 }
338 #endif
339 
340 static size_t
341 my_iconv_char(iconv_t cd, u_char **ibuf, size_t * ilen, u_char **obuf,
342 	size_t * olen)
343 {
344 	u_char *sp, *dp, ilocal[3], olocal[3];
345 	u_char c1, c2;
346 	int ret;
347 	size_t ir, or;
348 
349 	sp = *ibuf;
350 	dp = *obuf;
351 	ir = *ilen;
352 
353 	bzero(*obuf, *olen);
354 	ret = my_iconv(cd, (char **)&sp, ilen, (char **)&dp, olen);
355 	c1 = (*obuf)[0];
356 	c2 = (*obuf)[1];
357 
358 	if (ret == -1) {
359 		if (*ilen == ir - 1 && (*ibuf)[1] == '\0' && (c1 || c2))
360 			return (0);
361 		else
362 			return (-1);
363 	}
364 
365 	/*
366 	 * We must judge if inbuf is a single byte char or double byte char.
367 	 * Here, to judge, try first byte(*sp) conversion and compare.
368 	 */
369 	ir = 1;
370 	or = 3;
371 
372 	bzero(olocal, or);
373 	memcpy(ilocal, *ibuf, sizeof(ilocal));
374 	sp = ilocal;
375 	dp = olocal;
376 
377 	if ((my_iconv(cd,(char **)&sp, &ir, (char **)&dp, &or)) != -1) {
378 		if (olocal[0] != c1)
379 			return (ret);
380 
381 		if (olocal[1] == c2 && (*ibuf)[1] == '\0') {
382 			/*
383 			 * inbuf is a single byte char
384 			 */
385 			*ilen = 1;
386 			*olen = or;
387 			return (ret);
388 		}
389 
390 		switch(or) {
391 		case 0:
392 		case 1:
393 			if (olocal[1] == c2) {
394 				/*
395 				 * inbuf is a single byte char,
396 				 * so return false here.
397 				 */
398 				return (-1);
399 			} else {
400 				/*
401 				 * inbuf is a double byte char
402 				 */
403 				return (ret);
404 			}
405 			break;
406 		case 2:
407 			/*
408 			 * should compare second byte of inbuf
409 			 */
410 			break;
411 		}
412 	} else {
413 		/*
414 		 * inbuf clould not be splitted, so inbuf is
415 		 * a double byte char.
416 		 */
417 		return (ret);
418 	}
419 
420 	/*
421 	 * try second byte(*(sp+1)) conversion, and compare
422 	 */
423 	ir = 1;
424 	or = 3;
425 
426 	bzero(olocal, or);
427 
428 	sp = ilocal + 1;
429 	dp = olocal;
430 
431 	if ((my_iconv(cd,(char **)&sp, &ir, (char **)&dp, &or)) != -1) {
432 		if (olocal[0] == c2)
433 			/*
434 			 * inbuf is a single byte char
435 			 */
436 			return (-1);
437 	}
438 
439 	return (ret);
440 }
441 
442 #else /* statically linked */
443 
444 #include <sys/types.h>
445 #include <sys/iconv.h>
446 #include <errno.h>
447 
448 int
449 kiconv_add_xlat16_cspair(const char *tocode __unused, const char *fromcode __unused,
450     int flag __unused)
451 {
452 
453 	errno = EINVAL;
454 	return (-1);
455 }
456 
457 int
458 kiconv_add_xlat16_cspairs(const char *tocode __unused, const char *fromcode __unused)
459 {
460 	errno = EINVAL;
461 	return (-1);
462 }
463 
464 #endif /* PIC */
465