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