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