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