The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/iconv.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/8.3/sys/libkern/iconv.c 230205 2012-01-16 08:29:52Z kevlo $");
   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/sx.h>
   43 #include <sys/syslog.h>
   44 
   45 #include "iconv_converter_if.h"
   46 
   47 SYSCTL_DECL(_kern_iconv);
   48 
   49 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
   50 
   51 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
   52 MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
   53 
   54 MODULE_VERSION(libiconv, 2);
   55 
   56 static struct sx iconv_lock;
   57 
   58 #ifdef notnow
   59 /*
   60  * iconv converter instance
   61  */
   62 struct iconv_converter {
   63         KOBJ_FIELDS;
   64         void *                  c_data;
   65 };
   66 #endif
   67 
   68 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
   69 
   70 /*
   71  * List of loaded converters
   72  */
   73 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
   74     iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
   75 
   76 /*
   77  * List of supported/loaded charsets pairs
   78  */
   79 static TAILQ_HEAD(, iconv_cspair)
   80     iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
   81 static int iconv_csid = 1;
   82 
   83 static char iconv_unicode_string[] = "unicode"; /* save eight bytes when possible */
   84 
   85 static void iconv_unregister_cspair(struct iconv_cspair *csp);
   86 
   87 static int
   88 iconv_mod_unload(void)
   89 {
   90         struct iconv_cspair *csp;
   91 
   92         sx_xlock(&iconv_lock);
   93         while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL) {
   94                 if (csp->cp_refcount)
   95                         return EBUSY;
   96         }
   97 
   98         while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
   99                 iconv_unregister_cspair(csp);
  100         sx_xunlock(&iconv_lock);
  101         sx_destroy(&iconv_lock);
  102         return 0;
  103 }
  104 
  105 static int
  106 iconv_mod_handler(module_t mod, int type, void *data)
  107 {
  108         int error;
  109 
  110         switch (type) {
  111             case MOD_LOAD:
  112                 error = 0;
  113                 sx_init(&iconv_lock, "iconv");
  114                 break;
  115             case MOD_UNLOAD:
  116                 error = iconv_mod_unload();
  117                 break;
  118             default:
  119                 error = EINVAL;
  120         }
  121         return error;
  122 }
  123 
  124 static moduledata_t iconv_mod = {
  125         "iconv", iconv_mod_handler, NULL
  126 };
  127 
  128 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
  129 
  130 static int
  131 iconv_register_converter(struct iconv_converter_class *dcp)
  132 {
  133         kobj_class_compile((struct kobj_class*)dcp);
  134         dcp->refs++;
  135         TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
  136         return 0;
  137 }
  138 
  139 static int
  140 iconv_unregister_converter(struct iconv_converter_class *dcp)
  141 {
  142         if (dcp->refs > 1) {
  143                 ICDEBUG("converter have %d referenses 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 (strcmp(csp->cp_to, to) == 0 &&
  175                     strcmp(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, CTLFLAG_RD | CTLTYPE_OPAQUE,
  353             NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
  354 
  355 /*
  356  * List all available charset pairs.
  357  */
  358 static int
  359 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
  360 {
  361         struct iconv_cspair *csp;
  362         struct iconv_cspair_info csi;
  363         int error;
  364 
  365         error = 0;
  366         bzero(&csi, sizeof(csi));
  367         csi.cs_version = ICONV_CSPAIR_INFO_VER;
  368         sx_slock(&iconv_lock);
  369         TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
  370                 csi.cs_id = csp->cp_id;
  371                 csi.cs_refcount = csp->cp_refcount;
  372                 csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
  373                 strcpy(csi.cs_to, csp->cp_to);
  374                 strcpy(csi.cs_from, csp->cp_from);
  375                 error = SYSCTL_OUT(req, &csi, sizeof(csi));
  376                 if (error)
  377                         break;
  378         }
  379         sx_sunlock(&iconv_lock);
  380         return error;
  381 }
  382 
  383 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
  384             NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
  385 
  386 int
  387 iconv_add(const char *converter, const char *to, const char *from)
  388 {
  389         struct iconv_converter_class *dcp;
  390         struct iconv_cspair *csp;
  391 
  392         if (iconv_lookupconv(converter, &dcp) != 0)
  393                 return EINVAL;
  394 
  395         return iconv_register_cspair(to, from, dcp, NULL, &csp);
  396 }
  397 
  398 /*
  399  * Add new charset pair
  400  */
  401 static int
  402 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
  403 {
  404         struct iconv_converter_class *dcp;
  405         struct iconv_cspair *csp;
  406         struct iconv_add_in din;
  407         struct iconv_add_out dout;
  408         int error;
  409 
  410         error = SYSCTL_IN(req, &din, sizeof(din));
  411         if (error)
  412                 return error;
  413         if (din.ia_version != ICONV_ADD_VER)
  414                 return EINVAL;
  415         if (din.ia_datalen > ICONV_CSMAXDATALEN)
  416                 return EINVAL;
  417         if (strlen(din.ia_from) >= ICONV_CSNMAXLEN)
  418                 return EINVAL;
  419         if (strlen(din.ia_to) >= ICONV_CSNMAXLEN)
  420                 return EINVAL;
  421         if (strlen(din.ia_converter) >= ICONV_CNVNMAXLEN)
  422                 return EINVAL;
  423         if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
  424                 return EINVAL;
  425         sx_xlock(&iconv_lock);
  426         error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
  427         if (error) {
  428                 sx_xunlock(&iconv_lock);
  429                 return error;
  430         }
  431         if (din.ia_datalen) {
  432                 csp->cp_data = malloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
  433                 error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
  434                 if (error)
  435                         goto bad;
  436         }
  437         dout.ia_csid = csp->cp_id;
  438         error = SYSCTL_OUT(req, &dout, sizeof(dout));
  439         if (error)
  440                 goto bad;
  441         sx_xunlock(&iconv_lock);
  442         ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
  443         return 0;
  444 bad:
  445         iconv_unregister_cspair(csp);
  446         sx_xunlock(&iconv_lock);
  447         return error;
  448 }
  449 
  450 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
  451             NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
  452 
  453 /*
  454  * Default stubs for converters
  455  */
  456 int
  457 iconv_converter_initstub(struct iconv_converter_class *dp)
  458 {
  459         return 0;
  460 }
  461 
  462 int
  463 iconv_converter_donestub(struct iconv_converter_class *dp)
  464 {
  465         return 0;
  466 }
  467 
  468 int
  469 iconv_converter_tolowerstub(int c, void *handle)
  470 {
  471         return (c);
  472 }
  473 
  474 int
  475 iconv_converter_handler(module_t mod, int type, void *data)
  476 {
  477         struct iconv_converter_class *dcp = data;
  478         int error;
  479 
  480         switch (type) {
  481             case MOD_LOAD:
  482                 sx_xlock(&iconv_lock);
  483                 error = iconv_register_converter(dcp);
  484                 if (error) {
  485                         sx_xunlock(&iconv_lock);
  486                         break;
  487                 }
  488                 error = ICONV_CONVERTER_INIT(dcp);
  489                 if (error)
  490                         iconv_unregister_converter(dcp);
  491                 sx_xunlock(&iconv_lock);
  492                 break;
  493             case MOD_UNLOAD:
  494                 sx_xlock(&iconv_lock);
  495                 ICONV_CONVERTER_DONE(dcp);
  496                 error = iconv_unregister_converter(dcp);
  497                 sx_xunlock(&iconv_lock);
  498                 break;
  499             default:
  500                 error = EINVAL;
  501         }
  502         return error;
  503 }
  504 
  505 /*
  506  * Common used functions (don't use with unicode)
  507  */
  508 char *
  509 iconv_convstr(void *handle, char *dst, const char *src)
  510 {
  511         char *p = dst;
  512         size_t inlen, outlen;
  513         int error;
  514 
  515         if (handle == NULL) {
  516                 strcpy(dst, src);
  517                 return dst;
  518         }
  519         inlen = outlen = strlen(src);
  520         error = iconv_conv(handle, NULL, NULL, &p, &outlen);
  521         if (error)
  522                 return NULL;
  523         error = iconv_conv(handle, &src, &inlen, &p, &outlen);
  524         if (error)
  525                 return NULL;
  526         *p = 0;
  527         return dst;
  528 }
  529 
  530 void *
  531 iconv_convmem(void *handle, void *dst, const void *src, int size)
  532 {
  533         const char *s = src;
  534         char *d = dst;
  535         size_t inlen, outlen;
  536         int error;
  537 
  538         if (size == 0)
  539                 return dst;
  540         if (handle == NULL) {
  541                 memcpy(dst, src, size);
  542                 return dst;
  543         }
  544         inlen = outlen = size;
  545         error = iconv_conv(handle, NULL, NULL, &d, &outlen);
  546         if (error)
  547                 return NULL;
  548         error = iconv_conv(handle, &s, &inlen, &d, &outlen);
  549         if (error)
  550                 return NULL;
  551         return dst;
  552 }
  553 
  554 int
  555 iconv_lookupcp(char **cpp, const char *s)
  556 {
  557         if (cpp == NULL) {
  558                 ICDEBUG("warning a NULL list passed\n", ""); /* XXX ISO variadic                                                                macros cannot
  559                                                                 leave out the
  560                                                                 variadic args */
  561                 return ENOENT;
  562         }
  563         for (; *cpp; cpp++)
  564                 if (strcmp(*cpp, s) == 0)
  565                         return 0;
  566         return ENOENT;
  567 }
  568 
  569 /*
  570  * Return if fsname is in use of not
  571  */
  572 int
  573 iconv_vfs_refcount(const char *fsname)
  574 {
  575         struct vfsconf *vfsp;
  576 
  577         vfsp = vfs_byname(fsname);
  578         if (vfsp != NULL && vfsp->vfc_refcount > 0)
  579                 return (EBUSY);
  580         return (0);
  581 }

Cache object: ada4d90fdaccd2413a5c0058933d5c81


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.