xref: /freebsd/lib/libkiconv/xlat16_iconv.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*-
2  * Copyright (c) 2003, 2005 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  * $FreeBSD$
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, const 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, const 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, (const u_char **)&srcp,
225 			    &inbytesleft, &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, const u_char **ibuf, size_t * ilen, u_char **obuf,
342 	size_t * olen)
343 {
344 	const u_char *sp;
345 	u_char *dp, ilocal[3], olocal[3];
346 	u_char c1, c2;
347 	int ret;
348 	size_t ir, or;
349 
350 	sp = *ibuf;
351 	dp = *obuf;
352 	ir = *ilen;
353 
354 	bzero(*obuf, *olen);
355 	ret = my_iconv(cd, (const char **)&sp, ilen, (char **)&dp, olen);
356 	c1 = (*obuf)[0];
357 	c2 = (*obuf)[1];
358 
359 	if (ret == -1) {
360 		if (*ilen == ir - 1 && (*ibuf)[1] == '\0' && (c1 || c2))
361 			return (0);
362 		else
363 			return (-1);
364 	}
365 
366 	/*
367 	 * We must judge if inbuf is a single byte char or double byte char.
368 	 * Here, to judge, try first byte(*sp) conversion and compare.
369 	 */
370 	ir = 1;
371 	or = 3;
372 
373 	bzero(olocal, or);
374 	memcpy(ilocal, *ibuf, sizeof(ilocal));
375 	sp = ilocal;
376 	dp = olocal;
377 
378 	if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
379 		if (olocal[0] != c1)
380 			return (ret);
381 
382 		if (olocal[1] == c2 && (*ibuf)[1] == '\0') {
383 			/*
384 			 * inbuf is a single byte char
385 			 */
386 			*ilen = 1;
387 			*olen = or;
388 			return (ret);
389 		}
390 
391 		switch(or) {
392 		case 0:
393 		case 1:
394 			if (olocal[1] == c2) {
395 				/*
396 				 * inbuf is a single byte char,
397 				 * so return false here.
398 				 */
399 				return (-1);
400 			} else {
401 				/*
402 				 * inbuf is a double byte char
403 				 */
404 				return (ret);
405 			}
406 			break;
407 		case 2:
408 			/*
409 			 * should compare second byte of inbuf
410 			 */
411 			break;
412 		}
413 	} else {
414 		/*
415 		 * inbuf clould not be splitted, so inbuf is
416 		 * a double byte char.
417 		 */
418 		return (ret);
419 	}
420 
421 	/*
422 	 * try second byte(*(sp+1)) conversion, and compare
423 	 */
424 	ir = 1;
425 	or = 3;
426 
427 	bzero(olocal, or);
428 
429 	sp = ilocal + 1;
430 	dp = olocal;
431 
432 	if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
433 		if (olocal[0] == c2)
434 			/*
435 			 * inbuf is a single byte char
436 			 */
437 			return (-1);
438 	}
439 
440 	return (ret);
441 }
442 
443 #else /* statically linked */
444 
445 #include <sys/types.h>
446 #include <sys/iconv.h>
447 #include <errno.h>
448 
449 int
450 kiconv_add_xlat16_cspair(const char *tocode __unused, const char *fromcode __unused,
451     int flag __unused)
452 {
453 
454 	errno = EINVAL;
455 	return (-1);
456 }
457 
458 int
459 kiconv_add_xlat16_cspairs(const char *tocode __unused, const char *fromcode __unused)
460 {
461 	errno = EINVAL;
462 	return (-1);
463 }
464 
465 #endif /* PIC */
466