2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000-2001 Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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>
39 #include <sys/syslog.h>
41 #include "iconv_converter_if.h"
43 SYSCTL_DECL(_kern_iconv);
45 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
46 "kernel iconv interface");
48 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
49 static MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
51 MODULE_VERSION(libiconv, 2);
53 static struct sx iconv_lock;
57 * iconv converter instance
59 struct iconv_converter {
65 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
68 * List of loaded converters
70 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
71 iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
74 * List of supported/loaded charsets pairs
76 static TAILQ_HEAD(, iconv_cspair)
77 iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
78 static int iconv_csid = 1;
80 static char iconv_unicode_string[] = "unicode"; /* save eight bytes when possible */
82 static void iconv_unregister_cspair(struct iconv_cspair *csp);
85 iconv_mod_unload(void)
87 struct iconv_cspair *csp;
89 sx_xlock(&iconv_lock);
90 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
91 if (csp->cp_refcount) {
92 sx_xunlock(&iconv_lock);
97 while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
98 iconv_unregister_cspair(csp);
99 sx_xunlock(&iconv_lock);
100 sx_destroy(&iconv_lock);
105 iconv_mod_handler(module_t mod, int type, void *data)
112 sx_init(&iconv_lock, "iconv");
115 error = iconv_mod_unload();
123 static moduledata_t iconv_mod = {
124 "iconv", iconv_mod_handler, NULL
127 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
130 iconv_register_converter(struct iconv_converter_class *dcp)
132 kobj_class_compile((struct kobj_class*)dcp);
134 TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
139 iconv_unregister_converter(struct iconv_converter_class *dcp)
143 ICDEBUG("converter has %d references left\n", dcp->refs);
146 TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
147 kobj_class_free((struct kobj_class*)dcp);
152 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
154 struct iconv_converter_class *dcp;
156 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
159 if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
169 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
171 struct iconv_cspair *csp;
173 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
174 if (strcasecmp(csp->cp_to, to) == 0 &&
175 strcasecmp(csp->cp_from, from) == 0) {
185 iconv_register_cspair(const char *to, const char *from,
186 struct iconv_converter_class *dcp, void *data,
187 struct iconv_cspair **cspp)
189 struct iconv_cspair *csp;
191 int csize, ucsto, ucsfrom;
193 if (iconv_lookupcs(to, from, NULL) == 0)
195 csize = sizeof(*csp);
196 ucsto = strcmp(to, iconv_unicode_string) == 0;
198 csize += strlen(to) + 1;
199 ucsfrom = strcmp(from, iconv_unicode_string) == 0;
201 csize += strlen(from) + 1;
202 csp = malloc(csize, M_ICONV, M_WAITOK);
204 csp->cp_id = iconv_csid++;
206 cp = (char*)(csp + 1);
210 cp += strlen(cp) + 1;
212 csp->cp_to = iconv_unicode_string;
217 csp->cp_from = iconv_unicode_string;
220 TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
226 iconv_unregister_cspair(struct iconv_cspair *csp)
228 TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
230 free(csp->cp_data, M_ICONVDATA);
235 * Lookup and create an instance of converter.
236 * Currently this layer didn't have associated 'instance' structure
237 * to avoid unnesessary memory allocation.
240 iconv_open(const char *to, const char *from, void **handle)
242 struct iconv_cspair *csp, *cspfrom, *cspto;
243 struct iconv_converter_class *dcp;
248 * First, lookup fully qualified cspairs
250 error = iconv_lookupcs(to, from, &csp);
252 return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
255 * Well, nothing found. Now try to construct a composite conversion
256 * ToDo: add a 'capability' field to converter
258 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
259 cnvname = ICONV_CONVERTER_NAME(dcp);
262 error = iconv_lookupcs(cnvname, from, &cspfrom);
265 error = iconv_lookupcs(to, cnvname, &cspto);
269 * Fine, we're found a pair which can be combined together
271 return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
277 iconv_close(void *handle)
279 return ICONV_CONVERTER_CLOSE(handle);
283 iconv_conv(void *handle, const char **inbuf,
284 size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
286 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, 0);
290 iconv_conv_case(void *handle, const char **inbuf,
291 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
293 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, casetype);
297 iconv_convchr(void *handle, const char **inbuf,
298 size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
300 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, 0);
304 iconv_convchr_case(void *handle, const char **inbuf,
305 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
307 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
311 towlower(int c, void *handle)
313 return ICONV_CONVERTER_TOLOWER(handle, c);
317 towupper(int c, void *handle)
319 return ICONV_CONVERTER_TOUPPER(handle, c);
323 * Give a list of loaded converters. Each name terminated with 0.
324 * An empty string terminates the list.
327 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
329 struct iconv_converter_class *dcp;
335 sx_slock(&iconv_lock);
336 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
337 name = ICONV_CONVERTER_NAME(dcp);
340 error = SYSCTL_OUT(req, name, strlen(name) + 1);
344 sx_sunlock(&iconv_lock);
348 error = SYSCTL_OUT(req, &spc, sizeof(spc));
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");
358 * List all available charset pairs.
361 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
363 struct iconv_cspair *csp;
364 struct iconv_cspair_info csi;
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));
381 sx_sunlock(&iconv_lock);
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");
391 iconv_add(const char *converter, const char *to, const char *from)
393 struct iconv_converter_class *dcp;
394 struct iconv_cspair *csp;
396 if (iconv_lookupconv(converter, &dcp) != 0)
399 return iconv_register_cspair(to, from, dcp, NULL, &csp);
403 * Add new charset pair
406 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
408 struct iconv_converter_class *dcp;
409 struct iconv_cspair *csp;
410 struct iconv_add_in din;
411 struct iconv_add_out dout;
414 error = SYSCTL_IN(req, &din, sizeof(din));
417 if (din.ia_version != ICONV_ADD_VER)
419 if (din.ia_datalen > ICONV_CSMAXDATALEN)
421 if (strnlen(din.ia_from, sizeof(din.ia_from)) >= ICONV_CSNMAXLEN)
423 if (strnlen(din.ia_to, sizeof(din.ia_to)) >= ICONV_CSNMAXLEN)
425 if (strnlen(din.ia_converter, sizeof(din.ia_converter)) >= ICONV_CNVNMAXLEN)
427 if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
429 sx_xlock(&iconv_lock);
430 error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
432 sx_xunlock(&iconv_lock);
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);
441 dout.ia_csid = csp->cp_id;
442 error = SYSCTL_OUT(req, &dout, sizeof(dout));
445 sx_xunlock(&iconv_lock);
446 ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
449 iconv_unregister_cspair(csp);
450 sx_xunlock(&iconv_lock);
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");
460 * Default stubs for converters
463 iconv_converter_initstub(struct iconv_converter_class *dp)
469 iconv_converter_donestub(struct iconv_converter_class *dp)
475 iconv_converter_tolowerstub(int c, void *handle)
481 iconv_converter_handler(module_t mod, int type, void *data)
483 struct iconv_converter_class *dcp = data;
488 sx_xlock(&iconv_lock);
489 error = iconv_register_converter(dcp);
491 sx_xunlock(&iconv_lock);
494 error = ICONV_CONVERTER_INIT(dcp);
496 iconv_unregister_converter(dcp);
497 sx_xunlock(&iconv_lock);
500 sx_xlock(&iconv_lock);
501 ICONV_CONVERTER_DONE(dcp);
502 error = iconv_unregister_converter(dcp);
503 sx_xunlock(&iconv_lock);
512 * Common used functions (don't use with unicode)
515 iconv_convstr(void *handle, char *dst, const char *src)
518 size_t inlen, outlen;
521 if (handle == NULL) {
525 inlen = outlen = strlen(src);
526 error = iconv_conv(handle, NULL, NULL, &p, &outlen);
529 error = iconv_conv(handle, &src, &inlen, &p, &outlen);
537 iconv_convmem(void *handle, void *dst, const void *src, int size)
541 size_t inlen, outlen;
546 if (handle == NULL) {
547 memcpy(dst, src, size);
550 inlen = outlen = size;
551 error = iconv_conv(handle, NULL, NULL, &d, &outlen);
554 error = iconv_conv(handle, &s, &inlen, &d, &outlen);
561 iconv_lookupcp(char **cpp, const char *s)
564 ICDEBUG("warning a NULL list passed\n", "");
568 if (strcmp(*cpp, s) == 0)
574 * Return if fsname is in use of not
577 iconv_vfs_refcount(const char *fsname)
579 struct vfsconf *vfsp;
581 vfsp = vfs_byname(fsname);
582 if (vfsp != NULL && vfsp->vfc_refcount > 0)