xref: /dragonfly/sys/libiconv/iconv.c (revision 6693db17)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/libkern/iconv.c,v 1.12.2.1.2.1 2009/04/15 03:14:26 kensmith Exp $
33  * $DragonFly: src/sys/libiconv/iconv.c,v 1.8 2008/01/05 14:02:38 swildner Exp $
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/iconv.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/syslog.h>
43 #include "iconv_converter_if.h"
44 
45 SYSCTL_DECL(_kern_iconv);
46 
47 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
48 
49 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
50 MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
51 
52 MODULE_VERSION(libiconv, 2);
53 
54 #ifdef notnow
55 /*
56  * iconv converter instance
57  */
58 struct iconv_converter {
59 	KOBJ_FIELDS;
60 	void *			c_data;
61 };
62 #endif
63 
64 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
65 
66 /*
67  * List of loaded converters
68  */
69 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
70     iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
71 
72 /*
73  * List of supported/loaded charsets pairs
74  */
75 static TAILQ_HEAD(, iconv_cspair)
76     iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
77 static int iconv_csid = 1;
78 
79 static char iconv_unicode_string[] = "unicode";	/* save eight bytes when possible */
80 
81 static void iconv_unregister_cspair(struct iconv_cspair *csp);
82 
83 static int
84 iconv_mod_unload(void)
85 {
86 	struct iconv_cspair *csp;
87 
88 	while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL) {
89 		if (csp->cp_refcount)
90 			return EBUSY;
91 	}
92 
93 	while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
94 		iconv_unregister_cspair(csp);
95 	return 0;
96 }
97 
98 static int
99 iconv_mod_handler(module_t mod, int type, void *data)
100 {
101 	int error;
102 
103 	switch (type) {
104 	    case MOD_LOAD:
105 		error = 0;
106 		break;
107 	    case MOD_UNLOAD:
108 		error = iconv_mod_unload();
109 		break;
110 	    default:
111 		error = EINVAL;
112 	}
113 	return error;
114 }
115 
116 static moduledata_t iconv_mod = {
117 	"iconv", iconv_mod_handler, NULL
118 };
119 
120 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
121 
122 static int
123 iconv_register_converter(struct iconv_converter_class *dcp)
124 {
125 	kobj_class_instantiate((kobj_class_t)dcp);
126 	TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
127 	return 0;
128 }
129 
130 static int
131 iconv_unregister_converter(struct iconv_converter_class *dcp)
132 {
133 	if (dcp->refs != 1) {
134 		ICDEBUG("converter has %d references left\n", dcp->refs);
135 		return EBUSY;
136 	}
137 	TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
138 	kobj_class_uninstantiate((kobj_class_t)dcp);
139 	return 0;
140 }
141 
142 static int
143 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
144 {
145 	struct iconv_converter_class *dcp;
146 
147 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
148 		if (name == NULL)
149 			continue;
150 		if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
151 			if (dcpp)
152 				*dcpp = dcp;
153 			return 0;
154 		}
155 	}
156 	return ENOENT;
157 }
158 
159 static int
160 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
161 {
162 	struct iconv_cspair *csp;
163 
164 	TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
165 		if (strcmp(csp->cp_to, to) == 0 &&
166 		    strcmp(csp->cp_from, from) == 0) {
167 			if (cspp)
168 				*cspp = csp;
169 			return 0;
170 		}
171 	}
172 	return ENOENT;
173 }
174 
175 static int
176 iconv_register_cspair(const char *to, const char *from,
177 	struct iconv_converter_class *dcp, void *data,
178 	struct iconv_cspair **cspp)
179 {
180 	struct iconv_cspair *csp;
181 	char *cp;
182 	int csize, ucsto, ucsfrom;
183 
184 	if (iconv_lookupcs(to, from, NULL) == 0)
185 		return EEXIST;
186 	csize = sizeof(*csp);
187 	ucsto = strcmp(to, iconv_unicode_string) == 0;
188 	if (!ucsto)
189 		csize += strlen(to) + 1;
190 	ucsfrom = strcmp(from, iconv_unicode_string) == 0;
191 	if (!ucsfrom)
192 		csize += strlen(from) + 1;
193 	csp = kmalloc(csize, M_ICONV, M_WAITOK | M_ZERO);
194 	csp->cp_id = iconv_csid++;
195 	csp->cp_dcp = dcp;
196 	cp = (char*)(csp + 1);
197 	if (!ucsto) {
198 		strcpy(cp, to);
199 		csp->cp_to = cp;
200 		cp += strlen(cp) + 1;
201 	} else
202 		csp->cp_to = iconv_unicode_string;
203 	if (!ucsfrom) {
204 		strcpy(cp, from);
205 		csp->cp_from = cp;
206 	} else
207 		csp->cp_from = iconv_unicode_string;
208 	csp->cp_data = data;
209 
210 	TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
211 	*cspp = csp;
212 	return 0;
213 }
214 
215 static void
216 iconv_unregister_cspair(struct iconv_cspair *csp)
217 {
218 	TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
219 	if (csp->cp_data)
220 		kfree(csp->cp_data, M_ICONVDATA);
221 	kfree(csp, M_ICONV);
222 }
223 
224 /*
225  * Lookup and create an instance of converter.
226  * Currently this layer didn't have associated 'instance' structure
227  * to avoid unnesessary memory allocation.
228  */
229 int
230 iconv_open(const char *to, const char *from, void **handle)
231 {
232 	struct iconv_cspair *csp, *cspfrom, *cspto;
233 	struct iconv_converter_class *dcp;
234 	const char *cnvname;
235 	int error;
236 
237 	/*
238 	 * First, lookup fully qualified cspairs
239 	 */
240 	error = iconv_lookupcs(to, from, &csp);
241 	if (error == 0)
242 		return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
243 
244 	/*
245 	 * Well, nothing found. Now try to construct a composite conversion
246 	 * ToDo: add a 'capability' field to converter
247 	 */
248 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
249 		cnvname = ICONV_CONVERTER_NAME(dcp);
250 		if (cnvname == NULL)
251 			continue;
252 		error = iconv_lookupcs(cnvname, from, &cspfrom);
253 		if (error)
254 			continue;
255 		error = iconv_lookupcs(to, cnvname, &cspto);
256 		if (error)
257 			continue;
258 		/*
259 		 * Fine, we're found a pair which can be combined together
260 		 */
261 		return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
262 	}
263 	return ENOENT;
264 }
265 
266 int
267 iconv_close(void *handle)
268 {
269 	return ICONV_CONVERTER_CLOSE(handle);
270 }
271 
272 int
273 iconv_conv(void *handle, const char **inbuf,
274 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
275 {
276 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, 0);
277 }
278 
279 int
280 iconv_conv_case(void *handle, const char **inbuf,
281 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
282 {
283 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, casetype);
284 }
285 
286 int
287 iconv_convchr(void *handle, const char **inbuf,
288 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
289 {
290 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, 0);
291 }
292 
293 int
294 iconv_convchr_case(void *handle, const char **inbuf,
295 	size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
296 {
297 	return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
298 }
299 
300 /*
301  * Give a list of loaded converters. Each name terminated with 0.
302  * An empty string terminates the list.
303  */
304 static int
305 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
306 {
307 	struct iconv_converter_class *dcp;
308 	const char *name;
309 	char spc;
310 	int error;
311 
312 	error = 0;
313 
314 	TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
315 		name = ICONV_CONVERTER_NAME(dcp);
316 		if (name == NULL)
317 			continue;
318 		error = SYSCTL_OUT(req, name, strlen(name) + 1);
319 		if (error)
320 			break;
321 	}
322 	if (error)
323 		return error;
324 	spc = 0;
325 	error = SYSCTL_OUT(req, &spc, sizeof(spc));
326 	return error;
327 }
328 
329 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
330 	    NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
331 
332 /*
333  * List all available charset pairs.
334  */
335 static int
336 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
337 {
338 	struct iconv_cspair *csp;
339 	struct iconv_cspair_info csi;
340 	int error;
341 
342 	error = 0;
343 	bzero(&csi, sizeof(csi));
344 	csi.cs_version = ICONV_CSPAIR_INFO_VER;
345 
346 	TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
347 		csi.cs_id = csp->cp_id;
348 		csi.cs_refcount = csp->cp_refcount;
349 		csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
350 		strcpy(csi.cs_to, csp->cp_to);
351 		strcpy(csi.cs_from, csp->cp_from);
352 		error = SYSCTL_OUT(req, &csi, sizeof(csi));
353 		if (error)
354 			break;
355 	}
356 	return error;
357 }
358 
359 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
360 	    NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
361 
362 /*
363  * Add new charset pair
364  */
365 static int
366 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
367 {
368 	struct iconv_converter_class *dcp;
369 	struct iconv_cspair *csp;
370 	struct iconv_add_in din;
371 	struct iconv_add_out dout;
372 	int error;
373 
374 	error = SYSCTL_IN(req, &din, sizeof(din));
375 	if (error)
376 		return error;
377 	if (din.ia_version != ICONV_ADD_VER)
378 		return EINVAL;
379 	if (din.ia_datalen > ICONV_CSMAXDATALEN)
380 		return EINVAL;
381 
382 	/*
383 	 * Make sure all user-supplied strings are terminated before
384 	 * proceeding.
385 	 *
386 	 * XXX return EINVAL if strings are not properly terminated
387 	 */
388 	din.ia_converter[ICONV_CNVNMAXLEN-1] = 0;
389 	din.ia_to[ICONV_CSNMAXLEN-1] = 0;
390 	din.ia_from[ICONV_CSNMAXLEN-1] = 0;
391 
392 	if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
393 		return EINVAL;
394 	error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
395 	if (error)
396 		return error;
397 	if (din.ia_datalen) {
398 		csp->cp_data = kmalloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
399 		error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
400 		if (error)
401 			goto bad;
402 	}
403 	dout.ia_csid = csp->cp_id;
404 	error = SYSCTL_OUT(req, &dout, sizeof(dout));
405 	if (error)
406 		goto bad;
407 	ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
408 	return 0;
409 bad:
410 	iconv_unregister_cspair(csp);
411 	return error;
412 }
413 
414 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
415 	    NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
416 
417 /*
418  * Default stubs for converters
419  */
420 int
421 iconv_converter_initstub(struct iconv_converter_class *dp)
422 {
423 	return 0;
424 }
425 
426 int
427 iconv_converter_donestub(struct iconv_converter_class *dp)
428 {
429 	return 0;
430 }
431 
432 int
433 iconv_converter_handler(module_t mod, int type, void *data)
434 {
435 	struct iconv_converter_class *dcp = data;
436 	int error;
437 
438 	switch (type) {
439 	    case MOD_LOAD:
440 		error = iconv_register_converter(dcp);
441 		if (error)
442 			break;
443 		error = ICONV_CONVERTER_INIT(dcp);
444 		if (error)
445 			iconv_unregister_converter(dcp);
446 		break;
447 	    case MOD_UNLOAD:
448 		ICONV_CONVERTER_DONE(dcp);
449 		error = iconv_unregister_converter(dcp);
450 		break;
451 	    default:
452 		error = EINVAL;
453 	}
454 	return error;
455 }
456 
457 /*
458  * Common used functions (don't use with unicode)
459  */
460 char *
461 iconv_convstr(void *handle, char *dst, const char *src)
462 {
463 	char *p = dst;
464 	int error;
465 	size_t inlen, outlen;
466 
467 	if (handle == NULL) {
468 		strcpy(dst, src);
469 		return dst;
470 	}
471 	inlen = outlen = strlen(src);
472 	error = iconv_conv(handle, NULL, NULL, &p, &outlen);
473 	if (error)
474 		return NULL;
475 	error = iconv_conv(handle, &src, &inlen, &p, &outlen);
476 	if (error)
477 		return NULL;
478 	*p = 0;
479 	return dst;
480 }
481 
482 void *
483 iconv_convmem(void *handle, void *dst, const void *src, int size)
484 {
485 	const char *s = src;
486 	char *d = dst;
487 	size_t inlen, outlen;
488 	int error;
489 
490 	if (size == 0)
491 		return dst;
492 	if (handle == NULL) {
493 		memcpy(dst, src, size);
494 		return dst;
495 	}
496 	inlen = outlen = size;
497 	error = iconv_conv(handle, NULL, NULL, &d, &outlen);
498 	if (error)
499 		return NULL;
500 	error = iconv_conv(handle, &s, &inlen, &d, &outlen);
501 	if (error)
502 		return NULL;
503 	return dst;
504 }
505 
506 int
507 iconv_lookupcp(char **cpp, const char *s)
508 {
509 	if (cpp == NULL) {
510 		ICDEBUG("warning a NULL list passed\n", ""); /* XXX ISO variadic								macros cannot
511 								leave out the
512 								variadic args */
513 		return ENOENT;
514 	}
515 	for (; *cpp; cpp++)
516 		if (strcmp(*cpp, s) == 0)
517 			return 0;
518 	return ENOENT;
519 }
520 
521 /*
522  * Return if fsname is in use of not
523  */
524 int
525 iconv_vfs_refcount(const char *fsname)
526 {
527 	struct vfsconf *vfsp;
528 
529 	vfsp = vfsconf_find_by_name(fsname);
530 	if (vfsp != NULL && vfsp->vfc_refcount > 0)
531 		return (EBUSY);
532 	return (0);
533 }
534