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: stable/8/sys/libkern/iconv.c 245460 2013-01-15 15:29:28Z 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         TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
   94                 if (csp->cp_refcount) {
   95                         sx_xunlock(&iconv_lock);
   96                         return EBUSY;
   97                 }
   98         }
   99 
  100         while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
  101                 iconv_unregister_cspair(csp);
  102         sx_xunlock(&iconv_lock);
  103         sx_destroy(&iconv_lock);
  104         return 0;
  105 }
  106 
  107 static int
  108 iconv_mod_handler(module_t mod, int type, void *data)
  109 {
  110         int error;
  111 
  112         switch (type) {
  113             case MOD_LOAD:
  114                 error = 0;
  115                 sx_init(&iconv_lock, "iconv");
  116                 break;
  117             case MOD_UNLOAD:
  118                 error = iconv_mod_unload();
  119                 break;
  120             default:
  121                 error = EINVAL;
  122         }
  123         return error;
  124 }
  125 
  126 static moduledata_t iconv_mod = {
  127         "iconv", iconv_mod_handler, NULL
  128 };
  129 
  130 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
  131 
  132 static int
  133 iconv_register_converter(struct iconv_converter_class *dcp)
  134 {
  135         kobj_class_compile((struct kobj_class*)dcp);
  136         dcp->refs++;
  137         TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
  138         return 0;
  139 }
  140 
  141 static int
  142 iconv_unregister_converter(struct iconv_converter_class *dcp)
  143 {
  144         if (dcp->refs > 1) {
  145                 ICDEBUG("converter have %d referenses left\n", dcp->refs);
  146                 return EBUSY;
  147         }
  148         TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
  149         kobj_class_free((struct kobj_class*)dcp);
  150         return 0;
  151 }
  152 
  153 static int
  154 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
  155 {
  156         struct iconv_converter_class *dcp;
  157 
  158         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
  159                 if (name == NULL)
  160                         continue;
  161                 if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
  162                         if (dcpp)
  163                                 *dcpp = dcp;
  164                         return 0;
  165                 }
  166         }
  167         return ENOENT;
  168 }
  169 
  170 static int
  171 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
  172 {
  173         struct iconv_cspair *csp;
  174 
  175         TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
  176                 if (strcmp(csp->cp_to, to) == 0 &&
  177                     strcmp(csp->cp_from, from) == 0) {
  178                         if (cspp)
  179                                 *cspp = csp;
  180                         return 0;
  181                 }
  182         }
  183         return ENOENT;
  184 }
  185 
  186 static int
  187 iconv_register_cspair(const char *to, const char *from,
  188         struct iconv_converter_class *dcp, void *data,
  189         struct iconv_cspair **cspp)
  190 {
  191         struct iconv_cspair *csp;
  192         char *cp;
  193         int csize, ucsto, ucsfrom;
  194 
  195         if (iconv_lookupcs(to, from, NULL) == 0)
  196                 return EEXIST;
  197         csize = sizeof(*csp);
  198         ucsto = strcmp(to, iconv_unicode_string) == 0;
  199         if (!ucsto)
  200                 csize += strlen(to) + 1;
  201         ucsfrom = strcmp(from, iconv_unicode_string) == 0;
  202         if (!ucsfrom)
  203                 csize += strlen(from) + 1;
  204         csp = malloc(csize, M_ICONV, M_WAITOK);
  205         bzero(csp, csize);
  206         csp->cp_id = iconv_csid++;
  207         csp->cp_dcp = dcp;
  208         cp = (char*)(csp + 1);
  209         if (!ucsto) {
  210                 strcpy(cp, to);
  211                 csp->cp_to = cp;
  212                 cp += strlen(cp) + 1;
  213         } else
  214                 csp->cp_to = iconv_unicode_string;
  215         if (!ucsfrom) {
  216                 strcpy(cp, from);
  217                 csp->cp_from = cp;
  218         } else
  219                 csp->cp_from = iconv_unicode_string;
  220         csp->cp_data = data;
  221 
  222         TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
  223         *cspp = csp;
  224         return 0;
  225 }
  226 
  227 static void
  228 iconv_unregister_cspair(struct iconv_cspair *csp)
  229 {
  230         TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
  231         if (csp->cp_data)
  232                 free(csp->cp_data, M_ICONVDATA);
  233         free(csp, M_ICONV);
  234 }
  235 
  236 /*
  237  * Lookup and create an instance of converter.
  238  * Currently this layer didn't have associated 'instance' structure
  239  * to avoid unnesessary memory allocation.
  240  */
  241 int
  242 iconv_open(const char *to, const char *from, void **handle)
  243 {
  244         struct iconv_cspair *csp, *cspfrom, *cspto;
  245         struct iconv_converter_class *dcp;
  246         const char *cnvname;
  247         int error;
  248 
  249         /*
  250          * First, lookup fully qualified cspairs
  251          */
  252         error = iconv_lookupcs(to, from, &csp);
  253         if (error == 0)
  254                 return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
  255 
  256         /*
  257          * Well, nothing found. Now try to construct a composite conversion
  258          * ToDo: add a 'capability' field to converter
  259          */
  260         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
  261                 cnvname = ICONV_CONVERTER_NAME(dcp);
  262                 if (cnvname == NULL)
  263                         continue;
  264                 error = iconv_lookupcs(cnvname, from, &cspfrom);
  265                 if (error)
  266                         continue;
  267                 error = iconv_lookupcs(to, cnvname, &cspto);
  268                 if (error)
  269                         continue;
  270                 /*
  271                  * Fine, we're found a pair which can be combined together
  272                  */
  273                 return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
  274         }
  275         return ENOENT;
  276 }
  277 
  278 int
  279 iconv_close(void *handle)
  280 {
  281         return ICONV_CONVERTER_CLOSE(handle);
  282 }
  283 
  284 int
  285 iconv_conv(void *handle, const char **inbuf,
  286         size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
  287 {
  288         return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, 0);
  289 }
  290 
  291 int
  292 iconv_conv_case(void *handle, const char **inbuf,
  293         size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
  294 {
  295         return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, casetype);
  296 }
  297 
  298 int
  299 iconv_convchr(void *handle, const char **inbuf,
  300         size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
  301 {
  302         return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, 0);
  303 }
  304 
  305 int
  306 iconv_convchr_case(void *handle, const char **inbuf,
  307         size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
  308 {
  309         return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
  310 }
  311 
  312 int
  313 towlower(int c, void *handle)
  314 {
  315         return ICONV_CONVERTER_TOLOWER(handle, c);
  316 }
  317 
  318 int
  319 towupper(int c, void *handle)
  320 {
  321         return ICONV_CONVERTER_TOUPPER(handle, c);
  322 }
  323 
  324 /*
  325  * Give a list of loaded converters. Each name terminated with 0.
  326  * An empty string terminates the list.
  327  */
  328 static int
  329 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
  330 {
  331         struct iconv_converter_class *dcp;
  332         const char *name;
  333         char spc;
  334         int error;
  335 
  336         error = 0;
  337         sx_slock(&iconv_lock);
  338         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
  339                 name = ICONV_CONVERTER_NAME(dcp);
  340                 if (name == NULL)
  341                         continue;
  342                 error = SYSCTL_OUT(req, name, strlen(name) + 1);
  343                 if (error)
  344                         break;
  345         }
  346         sx_sunlock(&iconv_lock);
  347         if (error)
  348                 return error;
  349         spc = 0;
  350         error = SYSCTL_OUT(req, &spc, sizeof(spc));
  351         return error;
  352 }
  353 
  354 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
  355             NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
  356 
  357 /*
  358  * List all available charset pairs.
  359  */
  360 static int
  361 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
  362 {
  363         struct iconv_cspair *csp;
  364         struct iconv_cspair_info csi;
  365         int error;
  366 
  367         error = 0;
  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));
  378                 if (error)
  379                         break;
  380         }
  381         sx_sunlock(&iconv_lock);
  382         return error;
  383 }
  384 
  385 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
  386             NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
  387 
  388 int
  389 iconv_add(const char *converter, const char *to, const char *from)
  390 {
  391         struct iconv_converter_class *dcp;
  392         struct iconv_cspair *csp;
  393 
  394         if (iconv_lookupconv(converter, &dcp) != 0)
  395                 return EINVAL;
  396 
  397         return iconv_register_cspair(to, from, dcp, NULL, &csp);
  398 }
  399 
  400 /*
  401  * Add new charset pair
  402  */
  403 static int
  404 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
  405 {
  406         struct iconv_converter_class *dcp;
  407         struct iconv_cspair *csp;
  408         struct iconv_add_in din;
  409         struct iconv_add_out dout;
  410         int error;
  411 
  412         error = SYSCTL_IN(req, &din, sizeof(din));
  413         if (error)
  414                 return error;
  415         if (din.ia_version != ICONV_ADD_VER)
  416                 return EINVAL;
  417         if (din.ia_datalen > ICONV_CSMAXDATALEN)
  418                 return EINVAL;
  419         if (strlen(din.ia_from) >= ICONV_CSNMAXLEN)
  420                 return EINVAL;
  421         if (strlen(din.ia_to) >= ICONV_CSNMAXLEN)
  422                 return EINVAL;
  423         if (strlen(din.ia_converter) >= ICONV_CNVNMAXLEN)
  424                 return EINVAL;
  425         if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
  426                 return EINVAL;
  427         sx_xlock(&iconv_lock);
  428         error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
  429         if (error) {
  430                 sx_xunlock(&iconv_lock);
  431                 return error;
  432         }
  433         if (din.ia_datalen) {
  434                 csp->cp_data = malloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
  435                 error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
  436                 if (error)
  437                         goto bad;
  438         }
  439         dout.ia_csid = csp->cp_id;
  440         error = SYSCTL_OUT(req, &dout, sizeof(dout));
  441         if (error)
  442                 goto bad;
  443         sx_xunlock(&iconv_lock);
  444         ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
  445         return 0;
  446 bad:
  447         iconv_unregister_cspair(csp);
  448         sx_xunlock(&iconv_lock);
  449         return error;
  450 }
  451 
  452 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
  453             NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
  454 
  455 /*
  456  * Default stubs for converters
  457  */
  458 int
  459 iconv_converter_initstub(struct iconv_converter_class *dp)
  460 {
  461         return 0;
  462 }
  463 
  464 int
  465 iconv_converter_donestub(struct iconv_converter_class *dp)
  466 {
  467         return 0;
  468 }
  469 
  470 int
  471 iconv_converter_tolowerstub(int c, void *handle)
  472 {
  473         return (c);
  474 }
  475 
  476 int
  477 iconv_converter_handler(module_t mod, int type, void *data)
  478 {
  479         struct iconv_converter_class *dcp = data;
  480         int error;
  481 
  482         switch (type) {
  483             case MOD_LOAD:
  484                 sx_xlock(&iconv_lock);
  485                 error = iconv_register_converter(dcp);
  486                 if (error) {
  487                         sx_xunlock(&iconv_lock);
  488                         break;
  489                 }
  490                 error = ICONV_CONVERTER_INIT(dcp);
  491                 if (error)
  492                         iconv_unregister_converter(dcp);
  493                 sx_xunlock(&iconv_lock);
  494                 break;
  495             case MOD_UNLOAD:
  496                 sx_xlock(&iconv_lock);
  497                 ICONV_CONVERTER_DONE(dcp);
  498                 error = iconv_unregister_converter(dcp);
  499                 sx_xunlock(&iconv_lock);
  500                 break;
  501             default:
  502                 error = EINVAL;
  503         }
  504         return error;
  505 }
  506 
  507 /*
  508  * Common used functions (don't use with unicode)
  509  */
  510 char *
  511 iconv_convstr(void *handle, char *dst, const char *src)
  512 {
  513         char *p = dst;
  514         size_t inlen, outlen;
  515         int error;
  516 
  517         if (handle == NULL) {
  518                 strcpy(dst, src);
  519                 return dst;
  520         }
  521         inlen = outlen = strlen(src);
  522         error = iconv_conv(handle, NULL, NULL, &p, &outlen);
  523         if (error)
  524                 return NULL;
  525         error = iconv_conv(handle, &src, &inlen, &p, &outlen);
  526         if (error)
  527                 return NULL;
  528         *p = 0;
  529         return dst;
  530 }
  531 
  532 void *
  533 iconv_convmem(void *handle, void *dst, const void *src, int size)
  534 {
  535         const char *s = src;
  536         char *d = dst;
  537         size_t inlen, outlen;
  538         int error;
  539 
  540         if (size == 0)
  541                 return dst;
  542         if (handle == NULL) {
  543                 memcpy(dst, src, size);
  544                 return dst;
  545         }
  546         inlen = outlen = size;
  547         error = iconv_conv(handle, NULL, NULL, &d, &outlen);
  548         if (error)
  549                 return NULL;
  550         error = iconv_conv(handle, &s, &inlen, &d, &outlen);
  551         if (error)
  552                 return NULL;
  553         return dst;
  554 }
  555 
  556 int
  557 iconv_lookupcp(char **cpp, const char *s)
  558 {
  559         if (cpp == NULL) {
  560                 ICDEBUG("warning a NULL list passed\n", ""); /* XXX ISO variadic                                                                macros cannot
  561                                                                 leave out the
  562                                                                 variadic args */
  563                 return ENOENT;
  564         }
  565         for (; *cpp; cpp++)
  566                 if (strcmp(*cpp, s) == 0)
  567                         return 0;
  568         return ENOENT;
  569 }
  570 
  571 /*
  572  * Return if fsname is in use of not
  573  */
  574 int
  575 iconv_vfs_refcount(const char *fsname)
  576 {
  577         struct vfsconf *vfsp;
  578 
  579         vfsp = vfs_byname(fsname);
  580         if (vfsp != NULL && vfsp->vfc_refcount > 0)
  581                 return (EBUSY);
  582         return (0);
  583 }

Cache object: ea7cb0ac0cc56cc2f741b26f233cc4f0


[ 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.