xref: /dragonfly/sys/libiconv/iconv.c (revision 4eb35779)
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  * $FreeBSD: head/sys/libkern/iconv.c 267291 2014-06-09 19:27:47Z jhb $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/iconv.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mount.h>
36 #include <sys/syslog.h>
37 
38 #include "iconv_converter_if.h"
39 
40 SYSCTL_DECL(_kern_iconv);
41 
42 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
43 
44 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
45 static MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
46 
47 MODULE_VERSION(libiconv, 2);
48 
49 static struct lock iconv_lock;
50 
51 #ifdef notnow
52 /*
53  * iconv converter instance
54  */
55 struct iconv_converter {
56 	KOBJ_FIELDS;
57 	void *			c_data;
58 };
59 #endif
60 
61 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
62 
63 /*
64  * List of loaded converters
65  */
66 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
67     iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
68 
69 /*
70  * List of supported/loaded charsets pairs
71  */
72 static TAILQ_HEAD(, iconv_cspair)
73     iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
74 static int iconv_csid = 1;
75 
76 static char iconv_unicode_string[] = "unicode";	/* save eight bytes when possible */
77 
78 static void iconv_unregister_cspair(struct iconv_cspair *csp);
79 
80 static int
iconv_mod_unload(void)81 iconv_mod_unload(void)
82 {
83 	struct iconv_cspair *csp;
84 
85 	lockmgr(&iconv_lock, LK_EXCLUSIVE);
86 	TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
87 		if (csp->cp_refcount) {
88 			lockmgr(&iconv_lock, LK_RELEASE);
89 			return EBUSY;
90 		}
91 	}
92 
93 	while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
94 		iconv_unregister_cspair(csp);
95 	lockmgr(&iconv_lock, LK_RELEASE);
96 	lockuninit(&iconv_lock);
97 	return 0;
98 }
99 
100 static int
iconv_mod_handler(module_t mod,int type,void * data)101 iconv_mod_handler(module_t mod, int type, void *data)
102 {
103 	int error;
104 
105 	switch (type) {
106 	    case MOD_LOAD:
107 		error = 0;
108 		lockinit(&iconv_lock, "iconv", 0, LK_CANRECURSE);
109 		break;
110 	    case MOD_UNLOAD:
111 		error = iconv_mod_unload();
112 		break;
113 	    default:
114 		error = EINVAL;
115 	}
116 	return error;
117 }
118 
119 static moduledata_t iconv_mod = {
120 	"iconv", iconv_mod_handler, NULL
121 };
122 
123 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
124 
125 static int
iconv_register_converter(struct iconv_converter_class * dcp)126 iconv_register_converter(struct iconv_converter_class *dcp)
127 {
128 	kobj_class_instantiate((kobj_class_t)dcp);
129 	TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
130 	return 0;
131 }
132 
133 static int
iconv_unregister_converter(struct iconv_converter_class * dcp)134 iconv_unregister_converter(struct iconv_converter_class *dcp)
135 {
136 	if (dcp->refs != 1) {
137 		ICDEBUG("converter has %d references left\n", dcp->refs);
138 		return EBUSY;
139 	}
140 	TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
141 	kobj_class_uninstantiate((kobj_class_t)dcp);
142 	return 0;
143 }
144 
145 static int
iconv_lookupconv(const char * name,struct iconv_converter_class ** dcpp)146 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
147 {
148 	struct iconv_converter_class *dcp;
149 
150 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
151 		if (name == NULL)
152 			continue;
153 		if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
154 			if (dcpp)
155 				*dcpp = dcp;
156 			return 0;
157 		}
158 	}
159 	return ENOENT;
160 }
161 
162 static int
iconv_lookupcs(const char * to,const char * from,struct iconv_cspair ** cspp)163 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
164 {
165 	struct iconv_cspair *csp;
166 
167 	TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
168 		if (strcasecmp(csp->cp_to, to) == 0 &&
169 		    strcasecmp(csp->cp_from, from) == 0) {
170 			if (cspp)
171 				*cspp = csp;
172 			return 0;
173 		}
174 	}
175 	return ENOENT;
176 }
177 
178 static int
iconv_register_cspair(const char * to,const char * from,struct iconv_converter_class * dcp,void * data,struct iconv_cspair ** cspp)179 iconv_register_cspair(const char *to, const char *from,
180 	struct iconv_converter_class *dcp, void *data,
181 	struct iconv_cspair **cspp)
182 {
183 	struct iconv_cspair *csp;
184 	char *cp;
185 	int csize, ucsto, ucsfrom;
186 
187 	if (iconv_lookupcs(to, from, NULL) == 0)
188 		return EEXIST;
189 	csize = sizeof(*csp);
190 	ucsto = strcmp(to, iconv_unicode_string) == 0;
191 	if (!ucsto)
192 		csize += strlen(to) + 1;
193 	ucsfrom = strcmp(from, iconv_unicode_string) == 0;
194 	if (!ucsfrom)
195 		csize += strlen(from) + 1;
196 	csp = kmalloc(csize, M_ICONV, M_WAITOK | M_ZERO);
197 	csp->cp_id = iconv_csid++;
198 	csp->cp_dcp = dcp;
199 	cp = (char*)(csp + 1);
200 	if (!ucsto) {
201 		strcpy(cp, to);
202 		csp->cp_to = cp;
203 		cp += strlen(cp) + 1;
204 	} else
205 		csp->cp_to = iconv_unicode_string;
206 	if (!ucsfrom) {
207 		strcpy(cp, from);
208 		csp->cp_from = cp;
209 	} else
210 		csp->cp_from = iconv_unicode_string;
211 	csp->cp_data = data;
212 
213 	TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
214 	*cspp = csp;
215 	return 0;
216 }
217 
218 static void
iconv_unregister_cspair(struct iconv_cspair * csp)219 iconv_unregister_cspair(struct iconv_cspair *csp)
220 {
221 	TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
222 	if (csp->cp_data)
223 		kfree(csp->cp_data, M_ICONVDATA);
224 	kfree(csp, M_ICONV);
225 }
226 
227 /*
228  * Lookup and create an instance of converter.
229  * Currently this layer didn't have associated 'instance' structure
230  * to avoid unnesessary memory allocation.
231  */
232 int
iconv_open(const char * to,const char * from,void ** handle)233 iconv_open(const char *to, const char *from, void **handle)
234 {
235 	struct iconv_cspair *csp, *cspfrom, *cspto;
236 	struct iconv_converter_class *dcp;
237 	const char *cnvname;
238 	int error;
239 
240 	/*
241 	 * First, lookup fully qualified cspairs
242 	 */
243 	error = iconv_lookupcs(to, from, &csp);
244 	if (error == 0)
245 		return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
246 
247 	/*
248 	 * Well, nothing found. Now try to construct a composite conversion
249 	 * ToDo: add a 'capability' field to converter
250 	 */
251 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
252 		cnvname = ICONV_CONVERTER_NAME(dcp);
253 		if (cnvname == NULL)
254 			continue;
255 		error = iconv_lookupcs(cnvname, from, &cspfrom);
256 		if (error)
257 			continue;
258 		error = iconv_lookupcs(to, cnvname, &cspto);
259 		if (error)
260 			continue;
261 		/*
262 		 * Fine, we're found a pair which can be combined together
263 		 */
264 		return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
265 	}
266 	return ENOENT;
267 }
268 
269 int
iconv_close(void * handle)270 iconv_close(void *handle)
271 {
272 	return ICONV_CONVERTER_CLOSE(handle);
273 }
274 
275 int
iconv_conv(void * handle,const char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)276 iconv_conv(void *handle, const char **inbuf,
277 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
278 {
279 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, 0);
280 }
281 
282 int
iconv_conv_case(void * handle,const char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft,int casetype)283 iconv_conv_case(void *handle, const char **inbuf,
284 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
285 {
286 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, casetype);
287 }
288 
289 int
iconv_convchr(void * handle,const char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft)290 iconv_convchr(void *handle, const char **inbuf,
291 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
292 {
293 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, 0);
294 }
295 
296 int
iconv_convchr_case(void * handle,const char ** inbuf,size_t * inbytesleft,char ** outbuf,size_t * outbytesleft,int casetype)297 iconv_convchr_case(void *handle, const char **inbuf,
298 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
299 {
300 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
301 }
302 
303 int
towlower(int c,void * handle)304 towlower(int c, void *handle)
305 {
306 	return ICONV_CONVERTER_TOLOWER(handle, c);
307 }
308 
309 int
towupper(int c,void * handle)310 towupper(int c, void *handle)
311 {
312 	return ICONV_CONVERTER_TOUPPER(handle, c);
313 }
314 
315 /*
316  * Give a list of loaded converters. Each name terminated with 0.
317  * An empty string terminates the list.
318  */
319 static int
iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)320 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
321 {
322 	struct iconv_converter_class *dcp;
323 	const char *name;
324 	char spc;
325 	int error;
326 
327 	error = 0;
328 
329 	lockmgr(&iconv_lock, LK_SHARED);
330 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
331 		name = ICONV_CONVERTER_NAME(dcp);
332 		if (name == NULL)
333 			continue;
334 		error = SYSCTL_OUT(req, name, strlen(name) + 1);
335 		if (error)
336 			break;
337 	}
338 	lockmgr(&iconv_lock, LK_RELEASE);
339 	if (error)
340 		return error;
341 	spc = 0;
342 	error = SYSCTL_OUT(req, &spc, sizeof(spc));
343 	return error;
344 }
345 
346 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
347 	    NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
348 
349 /*
350  * List all available charset pairs.
351  */
352 static int
iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)353 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
354 {
355 	struct iconv_cspair *csp;
356 	struct iconv_cspair_info csi;
357 	int error;
358 
359 	error = 0;
360 	bzero(&csi, sizeof(csi));
361 	csi.cs_version = ICONV_CSPAIR_INFO_VER;
362 
363 	lockmgr(&iconv_lock, LK_SHARED);
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 	lockmgr(&iconv_lock, LK_RELEASE);
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
iconv_add(const char * converter,const char * to,const char * from)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
iconv_sysctl_add(SYSCTL_HANDLER_ARGS)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 	lockmgr(&iconv_lock, LK_EXCLUSIVE);
421 	error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
422 	if (error) {
423 		lockmgr(&iconv_lock, LK_RELEASE);
424 		return error;
425 	}
426 	if (din.ia_datalen) {
427 		csp->cp_data = kmalloc(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 	lockmgr(&iconv_lock, LK_RELEASE);
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 	lockmgr(&iconv_lock, LK_RELEASE);
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
iconv_converter_initstub(struct iconv_converter_class * dp)452 iconv_converter_initstub(struct iconv_converter_class *dp)
453 {
454 	return 0;
455 }
456 
457 int
iconv_converter_donestub(struct iconv_converter_class * dp)458 iconv_converter_donestub(struct iconv_converter_class *dp)
459 {
460 	return 0;
461 }
462 
463 int
iconv_converter_tolowerstub(int c,void * handle)464 iconv_converter_tolowerstub(int c, void *handle)
465 {
466 	return (c);
467 }
468 
469 int
iconv_converter_handler(module_t mod,int type,void * data)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 		lockmgr(&iconv_lock, LK_EXCLUSIVE);
478 		error = iconv_register_converter(dcp);
479 		if (error) {
480 			lockmgr(&iconv_lock, LK_RELEASE);
481 			break;
482 		}
483 		error = ICONV_CONVERTER_INIT(dcp);
484 		if (error)
485 			iconv_unregister_converter(dcp);
486 		lockmgr(&iconv_lock, LK_RELEASE);
487 		break;
488 	    case MOD_UNLOAD:
489 		lockmgr(&iconv_lock, LK_EXCLUSIVE);
490 		ICONV_CONVERTER_DONE(dcp);
491 		error = iconv_unregister_converter(dcp);
492 		lockmgr(&iconv_lock, LK_RELEASE);
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 *
iconv_convstr(void * handle,char * dst,const char * src)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 *
iconv_convmem(void * handle,void * dst,const void * src,int size)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
iconv_lookupcp(char ** cpp,const char * s)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
iconv_vfs_refcount(const char * fsname)566 iconv_vfs_refcount(const char *fsname)
567 {
568 	struct vfsconf *vfsp;
569 
570 	vfsp = vfsconf_find_by_name(fsname);
571 	if (vfsp != NULL && vfsp->vfc_refcount > 0)
572 		return (EBUSY);
573 	return (0);
574 }
575