2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
37 #include <sys/syslog.h>
39 #include "iconv_converter_if.h"
41 SYSCTL_DECL(_kern_iconv);
43 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
45 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
46 static MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
48 MODULE_VERSION(libiconv, 2);
50 static struct sx iconv_lock;
54 * iconv converter instance
56 struct iconv_converter {
62 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
65 * List of loaded converters
67 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
68 iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
71 * List of supported/loaded charsets pairs
73 static TAILQ_HEAD(, iconv_cspair)
74 iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
75 static int iconv_csid = 1;
77 static char iconv_unicode_string[] = "unicode"; /* save eight bytes when possible */
79 static void iconv_unregister_cspair(struct iconv_cspair *csp);
82 iconv_mod_unload(void)
84 struct iconv_cspair *csp;
86 sx_xlock(&iconv_lock);
87 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
88 if (csp->cp_refcount) {
89 sx_xunlock(&iconv_lock);
94 while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
95 iconv_unregister_cspair(csp);
96 sx_xunlock(&iconv_lock);
97 sx_destroy(&iconv_lock);
102 iconv_mod_handler(module_t mod, int type, void *data)
109 sx_init(&iconv_lock, "iconv");
112 error = iconv_mod_unload();
120 static moduledata_t iconv_mod = {
121 "iconv", iconv_mod_handler, NULL
124 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
127 iconv_register_converter(struct iconv_converter_class *dcp)
129 kobj_class_compile((struct kobj_class*)dcp);
131 TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
136 iconv_unregister_converter(struct iconv_converter_class *dcp)
140 ICDEBUG("converter has %d references left\n", dcp->refs);
143 TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
144 kobj_class_free((struct kobj_class*)dcp);
149 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
151 struct iconv_converter_class *dcp;
153 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
156 if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
166 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
168 struct iconv_cspair *csp;
170 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
171 if (strcmp(csp->cp_to, to) == 0 &&
172 strcmp(csp->cp_from, from) == 0) {
182 iconv_register_cspair(const char *to, const char *from,
183 struct iconv_converter_class *dcp, void *data,
184 struct iconv_cspair **cspp)
186 struct iconv_cspair *csp;
188 int csize, ucsto, ucsfrom;
190 if (iconv_lookupcs(to, from, NULL) == 0)
192 csize = sizeof(*csp);
193 ucsto = strcmp(to, iconv_unicode_string) == 0;
195 csize += strlen(to) + 1;
196 ucsfrom = strcmp(from, iconv_unicode_string) == 0;
198 csize += strlen(from) + 1;
199 csp = malloc(csize, M_ICONV, M_WAITOK);
201 csp->cp_id = iconv_csid++;
203 cp = (char*)(csp + 1);
207 cp += strlen(cp) + 1;
209 csp->cp_to = iconv_unicode_string;
214 csp->cp_from = iconv_unicode_string;
217 TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
223 iconv_unregister_cspair(struct iconv_cspair *csp)
225 TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
227 free(csp->cp_data, M_ICONVDATA);
232 * Lookup and create an instance of converter.
233 * Currently this layer didn't have associated 'instance' structure
234 * to avoid unnesessary memory allocation.
237 iconv_open(const char *to, const char *from, void **handle)
239 struct iconv_cspair *csp, *cspfrom, *cspto;
240 struct iconv_converter_class *dcp;
245 * First, lookup fully qualified cspairs
247 error = iconv_lookupcs(to, from, &csp);
249 return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
252 * Well, nothing found. Now try to construct a composite conversion
253 * ToDo: add a 'capability' field to converter
255 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
256 cnvname = ICONV_CONVERTER_NAME(dcp);
259 error = iconv_lookupcs(cnvname, from, &cspfrom);
262 error = iconv_lookupcs(to, cnvname, &cspto);
266 * Fine, we're found a pair which can be combined together
268 return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
274 iconv_close(void *handle)
276 return ICONV_CONVERTER_CLOSE(handle);
280 iconv_conv(void *handle, const char **inbuf,
281 size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
283 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, 0);
287 iconv_conv_case(void *handle, const char **inbuf,
288 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
290 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, casetype);
294 iconv_convchr(void *handle, const char **inbuf,
295 size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
297 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, 0);
301 iconv_convchr_case(void *handle, const char **inbuf,
302 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
304 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
308 towlower(int c, void *handle)
310 return ICONV_CONVERTER_TOLOWER(handle, c);
314 towupper(int c, void *handle)
316 return ICONV_CONVERTER_TOUPPER(handle, c);
320 * Give a list of loaded converters. Each name terminated with 0.
321 * An empty string terminates the list.
324 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
326 struct iconv_converter_class *dcp;
332 sx_slock(&iconv_lock);
333 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
334 name = ICONV_CONVERTER_NAME(dcp);
337 error = SYSCTL_OUT(req, name, strlen(name) + 1);
341 sx_sunlock(&iconv_lock);
345 error = SYSCTL_OUT(req, &spc, sizeof(spc));
349 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
350 NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
353 * List all available charset pairs.
356 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
358 struct iconv_cspair *csp;
359 struct iconv_cspair_info csi;
363 bzero(&csi, sizeof(csi));
364 csi.cs_version = ICONV_CSPAIR_INFO_VER;
365 sx_slock(&iconv_lock);
366 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
367 csi.cs_id = csp->cp_id;
368 csi.cs_refcount = csp->cp_refcount;
369 csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
370 strcpy(csi.cs_to, csp->cp_to);
371 strcpy(csi.cs_from, csp->cp_from);
372 error = SYSCTL_OUT(req, &csi, sizeof(csi));
376 sx_sunlock(&iconv_lock);
380 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
381 NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
384 iconv_add(const char *converter, const char *to, const char *from)
386 struct iconv_converter_class *dcp;
387 struct iconv_cspair *csp;
389 if (iconv_lookupconv(converter, &dcp) != 0)
392 return iconv_register_cspair(to, from, dcp, NULL, &csp);
396 * Add new charset pair
399 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
401 struct iconv_converter_class *dcp;
402 struct iconv_cspair *csp;
403 struct iconv_add_in din;
404 struct iconv_add_out dout;
407 error = SYSCTL_IN(req, &din, sizeof(din));
410 if (din.ia_version != ICONV_ADD_VER)
412 if (din.ia_datalen > ICONV_CSMAXDATALEN)
414 if (strlen(din.ia_from) >= ICONV_CSNMAXLEN)
416 if (strlen(din.ia_to) >= ICONV_CSNMAXLEN)
418 if (strlen(din.ia_converter) >= ICONV_CNVNMAXLEN)
420 if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
422 sx_xlock(&iconv_lock);
423 error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
425 sx_xunlock(&iconv_lock);
428 if (din.ia_datalen) {
429 csp->cp_data = malloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
430 error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
434 dout.ia_csid = csp->cp_id;
435 error = SYSCTL_OUT(req, &dout, sizeof(dout));
438 sx_xunlock(&iconv_lock);
439 ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
442 iconv_unregister_cspair(csp);
443 sx_xunlock(&iconv_lock);
447 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
448 NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
451 * Default stubs for converters
454 iconv_converter_initstub(struct iconv_converter_class *dp)
460 iconv_converter_donestub(struct iconv_converter_class *dp)
466 iconv_converter_tolowerstub(int c, void *handle)
472 iconv_converter_handler(module_t mod, int type, void *data)
474 struct iconv_converter_class *dcp = data;
479 sx_xlock(&iconv_lock);
480 error = iconv_register_converter(dcp);
482 sx_xunlock(&iconv_lock);
485 error = ICONV_CONVERTER_INIT(dcp);
487 iconv_unregister_converter(dcp);
488 sx_xunlock(&iconv_lock);
491 sx_xlock(&iconv_lock);
492 ICONV_CONVERTER_DONE(dcp);
493 error = iconv_unregister_converter(dcp);
494 sx_xunlock(&iconv_lock);
503 * Common used functions (don't use with unicode)
506 iconv_convstr(void *handle, char *dst, const char *src)
509 size_t inlen, outlen;
512 if (handle == NULL) {
516 inlen = outlen = strlen(src);
517 error = iconv_conv(handle, NULL, NULL, &p, &outlen);
520 error = iconv_conv(handle, &src, &inlen, &p, &outlen);
528 iconv_convmem(void *handle, void *dst, const void *src, int size)
532 size_t inlen, outlen;
537 if (handle == NULL) {
538 memcpy(dst, src, size);
541 inlen = outlen = size;
542 error = iconv_conv(handle, NULL, NULL, &d, &outlen);
545 error = iconv_conv(handle, &s, &inlen, &d, &outlen);
552 iconv_lookupcp(char **cpp, const char *s)
555 ICDEBUG("warning a NULL list passed\n", "");
559 if (strcmp(*cpp, s) == 0)
565 * Return if fsname is in use of not
568 iconv_vfs_refcount(const char *fsname)
570 struct vfsconf *vfsp;
572 vfsp = vfs_byname(fsname);
573 if (vfsp != NULL && vfsp->vfc_refcount > 0)