xref: /freebsd/sys/libkern/iconv.c (revision 3e0efd2ec4fcb4cd68fb8ccf8aea6fc6151c454b)
1 /*-
2  * Copyright (c) 2000-2001 Boris Popov
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/iconv.h>
34 #include <sys/malloc.h>
35 #include <sys/mount.h>
36 #include <sys/sx.h>
37 #include <sys/syslog.h>
38 
39 #include "iconv_converter_if.h"
40 
41 SYSCTL_DECL(_kern_iconv);
42 
43 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
44 
45 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
46 static MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
47 
48 MODULE_VERSION(libiconv, 2);
49 
50 static struct sx iconv_lock;
51 
52 #ifdef notnow
53 /*
54  * iconv converter instance
55  */
56 struct iconv_converter {
57 	KOBJ_FIELDS;
58 	void *			c_data;
59 };
60 #endif
61 
62 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
63 
64 /*
65  * List of loaded converters
66  */
67 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
68     iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
69 
70 /*
71  * List of supported/loaded charsets pairs
72  */
73 static TAILQ_HEAD(, iconv_cspair)
74     iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
75 static int iconv_csid = 1;
76 
77 static char iconv_unicode_string[] = "unicode";	/* save eight bytes when possible */
78 
79 static void iconv_unregister_cspair(struct iconv_cspair *csp);
80 
81 static int
82 iconv_mod_unload(void)
83 {
84 	struct iconv_cspair *csp;
85 
86 	sx_xlock(&iconv_lock);
87 	while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL) {
88 		if (csp->cp_refcount)
89 			return EBUSY;
90 	}
91 
92 	while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
93 		iconv_unregister_cspair(csp);
94 	sx_xunlock(&iconv_lock);
95 	sx_destroy(&iconv_lock);
96 	return 0;
97 }
98 
99 static int
100 iconv_mod_handler(module_t mod, int type, void *data)
101 {
102 	int error;
103 
104 	switch (type) {
105 	    case MOD_LOAD:
106 		error = 0;
107 		sx_init(&iconv_lock, "iconv");
108 		break;
109 	    case MOD_UNLOAD:
110 		error = iconv_mod_unload();
111 		break;
112 	    default:
113 		error = EINVAL;
114 	}
115 	return error;
116 }
117 
118 static moduledata_t iconv_mod = {
119 	"iconv", iconv_mod_handler, NULL
120 };
121 
122 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
123 
124 static int
125 iconv_register_converter(struct iconv_converter_class *dcp)
126 {
127 	kobj_class_compile((struct kobj_class*)dcp);
128 	dcp->refs++;
129 	TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
130 	return 0;
131 }
132 
133 static int
134 iconv_unregister_converter(struct iconv_converter_class *dcp)
135 {
136 	dcp->refs--;
137 	if (dcp->refs > 1) {
138 		ICDEBUG("converter have %d referenses left\n", dcp->refs);
139 		return EBUSY;
140 	}
141 	TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
142 	kobj_class_free((struct kobj_class*)dcp);
143 	return 0;
144 }
145 
146 static int
147 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
148 {
149 	struct iconv_converter_class *dcp;
150 
151 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
152 		if (name == NULL)
153 			continue;
154 		if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
155 			if (dcpp)
156 				*dcpp = dcp;
157 			return 0;
158 		}
159 	}
160 	return ENOENT;
161 }
162 
163 static int
164 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
165 {
166 	struct iconv_cspair *csp;
167 
168 	TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
169 		if (strcmp(csp->cp_to, to) == 0 &&
170 		    strcmp(csp->cp_from, from) == 0) {
171 			if (cspp)
172 				*cspp = csp;
173 			return 0;
174 		}
175 	}
176 	return ENOENT;
177 }
178 
179 static int
180 iconv_register_cspair(const char *to, const char *from,
181 	struct iconv_converter_class *dcp, void *data,
182 	struct iconv_cspair **cspp)
183 {
184 	struct iconv_cspair *csp;
185 	char *cp;
186 	int csize, ucsto, ucsfrom;
187 
188 	if (iconv_lookupcs(to, from, NULL) == 0)
189 		return EEXIST;
190 	csize = sizeof(*csp);
191 	ucsto = strcmp(to, iconv_unicode_string) == 0;
192 	if (!ucsto)
193 		csize += strlen(to) + 1;
194 	ucsfrom = strcmp(from, iconv_unicode_string) == 0;
195 	if (!ucsfrom)
196 		csize += strlen(from) + 1;
197 	csp = malloc(csize, M_ICONV, M_WAITOK);
198 	bzero(csp, csize);
199 	csp->cp_id = iconv_csid++;
200 	csp->cp_dcp = dcp;
201 	cp = (char*)(csp + 1);
202 	if (!ucsto) {
203 		strcpy(cp, to);
204 		csp->cp_to = cp;
205 		cp += strlen(cp) + 1;
206 	} else
207 		csp->cp_to = iconv_unicode_string;
208 	if (!ucsfrom) {
209 		strcpy(cp, from);
210 		csp->cp_from = cp;
211 	} else
212 		csp->cp_from = iconv_unicode_string;
213 	csp->cp_data = data;
214 
215 	TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
216 	*cspp = csp;
217 	return 0;
218 }
219 
220 static void
221 iconv_unregister_cspair(struct iconv_cspair *csp)
222 {
223 	TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
224 	if (csp->cp_data)
225 		free(csp->cp_data, M_ICONVDATA);
226 	free(csp, M_ICONV);
227 }
228 
229 /*
230  * Lookup and create an instance of converter.
231  * Currently this layer didn't have associated 'instance' structure
232  * to avoid unnesessary memory allocation.
233  */
234 int
235 iconv_open(const char *to, const char *from, void **handle)
236 {
237 	struct iconv_cspair *csp, *cspfrom, *cspto;
238 	struct iconv_converter_class *dcp;
239 	const char *cnvname;
240 	int error;
241 
242 	/*
243 	 * First, lookup fully qualified cspairs
244 	 */
245 	error = iconv_lookupcs(to, from, &csp);
246 	if (error == 0)
247 		return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
248 
249 	/*
250 	 * Well, nothing found. Now try to construct a composite conversion
251 	 * ToDo: add a 'capability' field to converter
252 	 */
253 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
254 		cnvname = ICONV_CONVERTER_NAME(dcp);
255 		if (cnvname == NULL)
256 			continue;
257 		error = iconv_lookupcs(cnvname, from, &cspfrom);
258 		if (error)
259 			continue;
260 		error = iconv_lookupcs(to, cnvname, &cspto);
261 		if (error)
262 			continue;
263 		/*
264 		 * Fine, we're found a pair which can be combined together
265 		 */
266 		return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
267 	}
268 	return ENOENT;
269 }
270 
271 int
272 iconv_close(void *handle)
273 {
274 	return ICONV_CONVERTER_CLOSE(handle);
275 }
276 
277 int
278 iconv_conv(void *handle, const char **inbuf,
279 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
280 {
281 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, 0);
282 }
283 
284 int
285 iconv_conv_case(void *handle, const char **inbuf,
286 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
287 {
288 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, casetype);
289 }
290 
291 int
292 iconv_convchr(void *handle, const char **inbuf,
293 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
294 {
295 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, 0);
296 }
297 
298 int
299 iconv_convchr_case(void *handle, const char **inbuf,
300 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
301 {
302 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
303 }
304 
305 int
306 towlower(int c, void *handle)
307 {
308 	return ICONV_CONVERTER_TOLOWER(handle, c);
309 }
310 
311 int
312 towupper(int c, void *handle)
313 {
314 	return ICONV_CONVERTER_TOUPPER(handle, c);
315 }
316 
317 /*
318  * Give a list of loaded converters. Each name terminated with 0.
319  * An empty string terminates the list.
320  */
321 static int
322 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
323 {
324 	struct iconv_converter_class *dcp;
325 	const char *name;
326 	char spc;
327 	int error;
328 
329 	error = 0;
330 	sx_slock(&iconv_lock);
331 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
332 		name = ICONV_CONVERTER_NAME(dcp);
333 		if (name == NULL)
334 			continue;
335 		error = SYSCTL_OUT(req, name, strlen(name) + 1);
336 		if (error)
337 			break;
338 	}
339 	sx_sunlock(&iconv_lock);
340 	if (error)
341 		return error;
342 	spc = 0;
343 	error = SYSCTL_OUT(req, &spc, sizeof(spc));
344 	return error;
345 }
346 
347 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
348 	    NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
349 
350 /*
351  * List all available charset pairs.
352  */
353 static int
354 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
355 {
356 	struct iconv_cspair *csp;
357 	struct iconv_cspair_info csi;
358 	int error;
359 
360 	error = 0;
361 	bzero(&csi, sizeof(csi));
362 	csi.cs_version = ICONV_CSPAIR_INFO_VER;
363 	sx_slock(&iconv_lock);
364 	TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
365 		csi.cs_id = csp->cp_id;
366 		csi.cs_refcount = csp->cp_refcount;
367 		csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
368 		strcpy(csi.cs_to, csp->cp_to);
369 		strcpy(csi.cs_from, csp->cp_from);
370 		error = SYSCTL_OUT(req, &csi, sizeof(csi));
371 		if (error)
372 			break;
373 	}
374 	sx_sunlock(&iconv_lock);
375 	return error;
376 }
377 
378 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
379 	    NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
380 
381 int
382 iconv_add(const char *converter, const char *to, const char *from)
383 {
384 	struct iconv_converter_class *dcp;
385 	struct iconv_cspair *csp;
386 
387 	if (iconv_lookupconv(converter, &dcp) != 0)
388 		return EINVAL;
389 
390 	return iconv_register_cspair(to, from, dcp, NULL, &csp);
391 }
392 
393 /*
394  * Add new charset pair
395  */
396 static int
397 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
398 {
399 	struct iconv_converter_class *dcp;
400 	struct iconv_cspair *csp;
401 	struct iconv_add_in din;
402 	struct iconv_add_out dout;
403 	int error;
404 
405 	error = SYSCTL_IN(req, &din, sizeof(din));
406 	if (error)
407 		return error;
408 	if (din.ia_version != ICONV_ADD_VER)
409 		return EINVAL;
410 	if (din.ia_datalen > ICONV_CSMAXDATALEN)
411 		return EINVAL;
412 	if (strlen(din.ia_from) >= ICONV_CSNMAXLEN)
413 		return EINVAL;
414 	if (strlen(din.ia_to) >= ICONV_CSNMAXLEN)
415 		return EINVAL;
416 	if (strlen(din.ia_converter) >= ICONV_CNVNMAXLEN)
417 		return EINVAL;
418 	if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
419 		return EINVAL;
420 	sx_xlock(&iconv_lock);
421 	error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
422 	if (error) {
423 		sx_xunlock(&iconv_lock);
424 		return error;
425 	}
426 	if (din.ia_datalen) {
427 		csp->cp_data = malloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
428 		error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
429 		if (error)
430 			goto bad;
431 	}
432 	dout.ia_csid = csp->cp_id;
433 	error = SYSCTL_OUT(req, &dout, sizeof(dout));
434 	if (error)
435 		goto bad;
436 	sx_xunlock(&iconv_lock);
437 	ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
438 	return 0;
439 bad:
440 	iconv_unregister_cspair(csp);
441 	sx_xunlock(&iconv_lock);
442 	return error;
443 }
444 
445 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
446 	    NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
447 
448 /*
449  * Default stubs for converters
450  */
451 int
452 iconv_converter_initstub(struct iconv_converter_class *dp)
453 {
454 	return 0;
455 }
456 
457 int
458 iconv_converter_donestub(struct iconv_converter_class *dp)
459 {
460 	return 0;
461 }
462 
463 int
464 iconv_converter_tolowerstub(int c, void *handle)
465 {
466 	return (c);
467 }
468 
469 int
470 iconv_converter_handler(module_t mod, int type, void *data)
471 {
472 	struct iconv_converter_class *dcp = data;
473 	int error;
474 
475 	switch (type) {
476 	    case MOD_LOAD:
477 		sx_xlock(&iconv_lock);
478 		error = iconv_register_converter(dcp);
479 		if (error) {
480 			sx_xunlock(&iconv_lock);
481 			break;
482 		}
483 		error = ICONV_CONVERTER_INIT(dcp);
484 		if (error)
485 			iconv_unregister_converter(dcp);
486 		sx_xunlock(&iconv_lock);
487 		break;
488 	    case MOD_UNLOAD:
489 		sx_xlock(&iconv_lock);
490 		ICONV_CONVERTER_DONE(dcp);
491 		error = iconv_unregister_converter(dcp);
492 		sx_xunlock(&iconv_lock);
493 		break;
494 	    default:
495 		error = EINVAL;
496 	}
497 	return error;
498 }
499 
500 /*
501  * Common used functions (don't use with unicode)
502  */
503 char *
504 iconv_convstr(void *handle, char *dst, const char *src)
505 {
506 	char *p = dst;
507 	size_t inlen, outlen;
508 	int error;
509 
510 	if (handle == NULL) {
511 		strcpy(dst, src);
512 		return dst;
513 	}
514 	inlen = outlen = strlen(src);
515 	error = iconv_conv(handle, NULL, NULL, &p, &outlen);
516 	if (error)
517 		return NULL;
518 	error = iconv_conv(handle, &src, &inlen, &p, &outlen);
519 	if (error)
520 		return NULL;
521 	*p = 0;
522 	return dst;
523 }
524 
525 void *
526 iconv_convmem(void *handle, void *dst, const void *src, int size)
527 {
528 	const char *s = src;
529 	char *d = dst;
530 	size_t inlen, outlen;
531 	int error;
532 
533 	if (size == 0)
534 		return dst;
535 	if (handle == NULL) {
536 		memcpy(dst, src, size);
537 		return dst;
538 	}
539 	inlen = outlen = size;
540 	error = iconv_conv(handle, NULL, NULL, &d, &outlen);
541 	if (error)
542 		return NULL;
543 	error = iconv_conv(handle, &s, &inlen, &d, &outlen);
544 	if (error)
545 		return NULL;
546 	return dst;
547 }
548 
549 int
550 iconv_lookupcp(char **cpp, const char *s)
551 {
552 	if (cpp == NULL) {
553 		ICDEBUG("warning a NULL list passed\n", "");
554 		return ENOENT;
555 	}
556 	for (; *cpp; cpp++)
557 		if (strcmp(*cpp, s) == 0)
558 			return 0;
559 	return ENOENT;
560 }
561 
562 /*
563  * Return if fsname is in use of not
564  */
565 int
566 iconv_vfs_refcount(const char *fsname)
567 {
568 	struct vfsconf *vfsp;
569 
570 	vfsp = vfs_byname(fsname);
571 	if (vfsp != NULL && vfsp->vfc_refcount > 0)
572 		return (EBUSY);
573 	return (0);
574 }
575