Home | History | Annotate | Line # | Download | only in citrus
citrus_iconv.c revision 1.9.4.1
      1  1.9.4.1      yamt /*	$NetBSD: citrus_iconv.c,v 1.9.4.1 2012/04/17 00:05:16 yamt Exp $	*/
      2      1.1  tshiozak 
      3      1.1  tshiozak /*-
      4      1.1  tshiozak  * Copyright (c)2003 Citrus Project,
      5      1.1  tshiozak  * All rights reserved.
      6      1.1  tshiozak  *
      7      1.1  tshiozak  * Redistribution and use in source and binary forms, with or without
      8      1.1  tshiozak  * modification, are permitted provided that the following conditions
      9      1.1  tshiozak  * are met:
     10      1.1  tshiozak  * 1. Redistributions of source code must retain the above copyright
     11      1.1  tshiozak  *    notice, this list of conditions and the following disclaimer.
     12      1.1  tshiozak  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  tshiozak  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  tshiozak  *    documentation and/or other materials provided with the distribution.
     15      1.1  tshiozak  *
     16      1.1  tshiozak  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17      1.1  tshiozak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.1  tshiozak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.1  tshiozak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20      1.1  tshiozak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.1  tshiozak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.1  tshiozak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.1  tshiozak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.1  tshiozak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  tshiozak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  tshiozak  * SUCH DAMAGE.
     27      1.1  tshiozak  */
     28      1.1  tshiozak 
     29      1.1  tshiozak #include <sys/cdefs.h>
     30      1.1  tshiozak #if defined(LIBC_SCCS) && !defined(lint)
     31  1.9.4.1      yamt __RCSID("$NetBSD: citrus_iconv.c,v 1.9.4.1 2012/04/17 00:05:16 yamt Exp $");
     32      1.1  tshiozak #endif /* LIBC_SCCS and not lint */
     33      1.1  tshiozak 
     34      1.1  tshiozak #include "namespace.h"
     35      1.2  tshiozak #include "reentrant.h"
     36      1.9    jruoho 
     37      1.9    jruoho #include <sys/types.h>
     38      1.9    jruoho #include <sys/queue.h>
     39      1.9    jruoho 
     40      1.1  tshiozak #include <assert.h>
     41      1.9    jruoho #include <dirent.h>
     42      1.9    jruoho #include <errno.h>
     43      1.9    jruoho #include <limits.h>
     44      1.9    jruoho #include <paths.h>
     45      1.9    jruoho #include <stdbool.h>
     46      1.1  tshiozak #include <stdio.h>
     47      1.1  tshiozak #include <stdlib.h>
     48      1.1  tshiozak #include <string.h>
     49      1.1  tshiozak #include <unistd.h>
     50      1.1  tshiozak 
     51      1.1  tshiozak #include "citrus_namespace.h"
     52      1.1  tshiozak #include "citrus_bcs.h"
     53      1.1  tshiozak #include "citrus_region.h"
     54      1.1  tshiozak #include "citrus_memstream.h"
     55      1.1  tshiozak #include "citrus_mmap.h"
     56      1.1  tshiozak #include "citrus_module.h"
     57      1.1  tshiozak #include "citrus_lookup.h"
     58      1.2  tshiozak #include "citrus_hash.h"
     59      1.1  tshiozak #include "citrus_iconv.h"
     60      1.1  tshiozak 
     61      1.1  tshiozak #define _CITRUS_ICONV_DIR	"iconv.dir"
     62      1.1  tshiozak #define _CITRUS_ICONV_ALIAS	"iconv.alias"
     63      1.1  tshiozak 
     64      1.2  tshiozak #define CI_HASH_SIZE 101
     65      1.2  tshiozak #define CI_INITIAL_MAX_REUSE	5
     66      1.2  tshiozak #define CI_ENV_MAX_REUSE	"ICONV_MAX_REUSE"
     67      1.2  tshiozak 
     68      1.2  tshiozak #ifdef _REENTRANT
     69      1.2  tshiozak static rwlock_t lock = RWLOCK_INITIALIZER;
     70      1.2  tshiozak #endif
     71      1.9    jruoho 
     72      1.9    jruoho static bool isinit = false;
     73      1.2  tshiozak static _CITRUS_HASH_HEAD(, _citrus_iconv_shared, CI_HASH_SIZE) shared_pool;
     74      1.2  tshiozak static TAILQ_HEAD(, _citrus_iconv_shared) shared_unused;
     75      1.2  tshiozak static int shared_num_unused, shared_max_reuse;
     76      1.2  tshiozak 
     77      1.2  tshiozak static __inline void
     78      1.2  tshiozak init_cache(void)
     79      1.2  tshiozak {
     80      1.2  tshiozak 	rwlock_wrlock(&lock);
     81      1.2  tshiozak 	if (!isinit) {
     82      1.2  tshiozak 		_CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE);
     83      1.2  tshiozak 		TAILQ_INIT(&shared_unused);
     84      1.2  tshiozak 		shared_max_reuse = -1;
     85      1.2  tshiozak 		if (!issetugid() && getenv(CI_ENV_MAX_REUSE))
     86      1.2  tshiozak 			shared_max_reuse = atoi(getenv(CI_ENV_MAX_REUSE));
     87      1.2  tshiozak 		if (shared_max_reuse < 0)
     88      1.2  tshiozak 			shared_max_reuse = CI_INITIAL_MAX_REUSE;
     89      1.9    jruoho 		isinit = true;
     90      1.2  tshiozak 	}
     91      1.2  tshiozak 	rwlock_unlock(&lock);
     92      1.2  tshiozak }
     93      1.2  tshiozak 
     94      1.1  tshiozak /*
     95      1.1  tshiozak  * lookup_iconv_entry:
     96      1.1  tshiozak  *	lookup iconv.dir entry in the specified directory.
     97      1.1  tshiozak  *
     98      1.1  tshiozak  * line format of iconv.dir file:
     99      1.1  tshiozak  *	key  module  arg
    100      1.1  tshiozak  * key    : lookup key.
    101      1.1  tshiozak  * module : iconv module name.
    102      1.1  tshiozak  * arg    : argument for the module (generally, description file name)
    103      1.1  tshiozak  *
    104      1.1  tshiozak  */
    105      1.1  tshiozak static __inline int
    106      1.1  tshiozak lookup_iconv_entry(const char *curdir, const char *key,
    107      1.1  tshiozak 		   char *linebuf, size_t linebufsize,
    108      1.1  tshiozak 		   const char **module, const char **variable)
    109      1.1  tshiozak {
    110      1.1  tshiozak 	const char *cp, *cq;
    111      1.1  tshiozak 	char *p, path[PATH_MAX];
    112      1.1  tshiozak 
    113      1.1  tshiozak 	/* iconv.dir path */
    114      1.8  dholland 	snprintf(path, (size_t)PATH_MAX, ("%s/" _CITRUS_ICONV_DIR), curdir);
    115      1.1  tshiozak 
    116      1.1  tshiozak 	/* lookup db */
    117      1.4  tshiozak 	cp = p = _lookup_simple(path, key, linebuf, linebufsize,
    118      1.4  tshiozak 				_LOOKUP_CASE_IGNORE);
    119      1.1  tshiozak 	if (p == NULL)
    120      1.1  tshiozak 		return ENOENT;
    121      1.1  tshiozak 
    122      1.1  tshiozak 	/* get module name */
    123      1.1  tshiozak 	*module = p;
    124      1.1  tshiozak 	cq = _bcs_skip_nonws(cp);
    125      1.1  tshiozak 	p[cq-cp] = '\0';
    126      1.1  tshiozak 	p += cq-cp+1;
    127      1.1  tshiozak 	cq++;
    128      1.1  tshiozak 
    129      1.1  tshiozak 	/* get variable */
    130      1.1  tshiozak 	cp = _bcs_skip_ws(cq);
    131      1.1  tshiozak 	*variable = p += cp - cq;
    132      1.1  tshiozak 	cq = _bcs_skip_nonws(cp);
    133      1.1  tshiozak 	p[cq-cp] = '\0';
    134      1.1  tshiozak 
    135      1.1  tshiozak 	return 0;
    136      1.1  tshiozak }
    137      1.1  tshiozak 
    138      1.2  tshiozak static __inline void
    139      1.2  tshiozak close_shared(struct _citrus_iconv_shared *ci)
    140      1.2  tshiozak {
    141      1.2  tshiozak 	if (ci) {
    142      1.2  tshiozak 		if (ci->ci_module) {
    143      1.2  tshiozak 			if (ci->ci_ops) {
    144      1.2  tshiozak 				if (ci->ci_closure)
    145      1.2  tshiozak 					(*ci->ci_ops->io_uninit_shared)(ci);
    146      1.2  tshiozak 				free(ci->ci_ops);
    147      1.2  tshiozak 			}
    148      1.2  tshiozak 			_citrus_unload_module(ci->ci_module);
    149      1.2  tshiozak 		}
    150      1.2  tshiozak 		free(ci);
    151      1.2  tshiozak 	}
    152      1.2  tshiozak }
    153      1.2  tshiozak 
    154      1.2  tshiozak static __inline int
    155      1.2  tshiozak open_shared(struct _citrus_iconv_shared * __restrict * __restrict rci,
    156      1.2  tshiozak 	    const char * __restrict basedir, const char * __restrict convname,
    157      1.2  tshiozak 	    const char * __restrict src, const char * __restrict dst)
    158      1.1  tshiozak {
    159      1.1  tshiozak 	int ret;
    160      1.2  tshiozak 	struct _citrus_iconv_shared *ci;
    161      1.1  tshiozak 	_citrus_iconv_getops_t getops;
    162      1.2  tshiozak 	char linebuf[LINE_MAX];
    163      1.1  tshiozak 	const char *module, *variable;
    164      1.2  tshiozak 	size_t len_convname;
    165      1.1  tshiozak 
    166      1.1  tshiozak 	/* search converter entry */
    167      1.2  tshiozak 	ret = lookup_iconv_entry(basedir, convname, linebuf, sizeof(linebuf),
    168      1.1  tshiozak 				 &module, &variable);
    169      1.1  tshiozak 	if (ret) {
    170      1.1  tshiozak 		if (ret == ENOENT)
    171      1.1  tshiozak 			/* fallback */
    172      1.1  tshiozak 			ret = lookup_iconv_entry(basedir, "*",
    173      1.5      yamt 						 linebuf, sizeof(linebuf),
    174      1.1  tshiozak 						 &module, &variable);
    175      1.1  tshiozak 		if (ret)
    176      1.1  tshiozak 			return ret;
    177      1.1  tshiozak 	}
    178      1.1  tshiozak 
    179      1.1  tshiozak 	/* initialize iconv handle */
    180      1.2  tshiozak 	len_convname = strlen(convname);
    181      1.2  tshiozak 	ci = malloc(sizeof(*ci)+len_convname+1);
    182      1.1  tshiozak 	if (!ci) {
    183      1.1  tshiozak 		ret = errno;
    184      1.1  tshiozak 		goto err;
    185      1.1  tshiozak 	}
    186      1.1  tshiozak 	ci->ci_module = NULL;
    187      1.1  tshiozak 	ci->ci_ops = NULL;
    188      1.1  tshiozak 	ci->ci_closure = NULL;
    189      1.2  tshiozak 	ci->ci_convname = (void *)&ci[1];
    190      1.2  tshiozak 	memcpy(ci->ci_convname, convname, len_convname+1);
    191      1.1  tshiozak 
    192      1.1  tshiozak 	/* load module */
    193      1.1  tshiozak 	ret = _citrus_load_module(&ci->ci_module, module);
    194      1.1  tshiozak 	if (ret)
    195      1.1  tshiozak 		goto err;
    196      1.1  tshiozak 
    197      1.1  tshiozak 	/* get operators */
    198      1.1  tshiozak 	getops = (_citrus_iconv_getops_t)
    199      1.1  tshiozak 	    _citrus_find_getops(ci->ci_module, module, "iconv");
    200      1.1  tshiozak 	if (!getops) {
    201      1.1  tshiozak 		ret = EOPNOTSUPP;
    202      1.1  tshiozak 		goto err;
    203      1.1  tshiozak 	}
    204      1.1  tshiozak 	ci->ci_ops = malloc(sizeof(*ci->ci_ops));
    205      1.1  tshiozak 	if (!ci->ci_ops) {
    206      1.1  tshiozak 		ret = errno;
    207      1.1  tshiozak 		goto err;
    208      1.1  tshiozak 	}
    209      1.1  tshiozak 	ret = (*getops)(ci->ci_ops, sizeof(*ci->ci_ops),
    210      1.1  tshiozak 			_CITRUS_ICONV_ABI_VERSION);
    211      1.1  tshiozak 	if (ret)
    212      1.1  tshiozak 		goto err;
    213      1.1  tshiozak 
    214      1.2  tshiozak 	/* version check */
    215      1.2  tshiozak 	if (ci->ci_ops->io_abi_version == 1) {
    216      1.2  tshiozak 		/* binary compatibility broken at ver.2 */
    217      1.2  tshiozak 		ret = EINVAL;
    218      1.2  tshiozak 		goto err;
    219      1.2  tshiozak 	}
    220      1.2  tshiozak 
    221      1.2  tshiozak 	if (ci->ci_ops->io_init_shared == NULL ||
    222      1.2  tshiozak 	    ci->ci_ops->io_uninit_shared == NULL ||
    223      1.2  tshiozak 	    ci->ci_ops->io_init_context == NULL ||
    224      1.2  tshiozak 	    ci->ci_ops->io_uninit_context == NULL ||
    225  1.9.4.1      yamt 	    ci->ci_ops->io_convert == NULL) {
    226  1.9.4.1      yamt 		ret = EINVAL;
    227      1.1  tshiozak 		goto err;
    228  1.9.4.1      yamt 	}
    229      1.1  tshiozak 
    230      1.1  tshiozak 	/* initialize the converter */
    231      1.2  tshiozak 	ret = (*ci->ci_ops->io_init_shared)(ci, basedir, src, dst,
    232      1.2  tshiozak 					    (const void *)variable,
    233      1.2  tshiozak 					    strlen(variable)+1);
    234      1.1  tshiozak 	if (ret)
    235      1.1  tshiozak 		goto err;
    236      1.1  tshiozak 
    237      1.1  tshiozak 	*rci = ci;
    238      1.1  tshiozak 
    239      1.1  tshiozak 	return 0;
    240      1.1  tshiozak err:
    241      1.2  tshiozak 	close_shared(ci);
    242      1.2  tshiozak 	return ret;
    243      1.2  tshiozak }
    244      1.2  tshiozak 
    245      1.2  tshiozak static __inline int
    246      1.2  tshiozak hash_func(const char *key)
    247      1.2  tshiozak {
    248      1.2  tshiozak 	return _string_hash_func(key, CI_HASH_SIZE);
    249      1.2  tshiozak }
    250      1.2  tshiozak 
    251      1.2  tshiozak static __inline int
    252      1.2  tshiozak match_func(struct _citrus_iconv_shared * __restrict ci,
    253      1.2  tshiozak 	   const char * __restrict key)
    254      1.2  tshiozak {
    255      1.2  tshiozak 	return strcmp(ci->ci_convname, key);
    256      1.2  tshiozak }
    257      1.2  tshiozak 
    258      1.2  tshiozak static int
    259      1.2  tshiozak get_shared(struct _citrus_iconv_shared * __restrict * __restrict rci,
    260      1.2  tshiozak 	   const char *basedir, const char *src, const char *dst)
    261      1.2  tshiozak {
    262      1.2  tshiozak 	int ret = 0;
    263      1.2  tshiozak 	int hashval;
    264      1.2  tshiozak 	struct _citrus_iconv_shared * ci;
    265      1.2  tshiozak 	char convname[PATH_MAX];
    266      1.2  tshiozak 
    267      1.2  tshiozak 	snprintf(convname, sizeof(convname), "%s/%s", src, dst);
    268      1.2  tshiozak 
    269      1.2  tshiozak 	rwlock_wrlock(&lock);
    270      1.2  tshiozak 
    271      1.2  tshiozak 	/* lookup alread existing entry */
    272      1.2  tshiozak 	hashval = hash_func(convname);
    273      1.2  tshiozak 	_CITRUS_HASH_SEARCH(&shared_pool, ci, ci_hash_entry, match_func,
    274      1.2  tshiozak 			    convname, hashval);
    275      1.2  tshiozak 	if (ci != NULL) {
    276      1.2  tshiozak 		/* found */
    277      1.2  tshiozak 		if (ci->ci_used_count == 0) {
    278      1.2  tshiozak 			TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry);
    279      1.2  tshiozak 			shared_num_unused--;
    280      1.2  tshiozak 		}
    281      1.2  tshiozak 		ci->ci_used_count++;
    282      1.2  tshiozak 		*rci = ci;
    283      1.2  tshiozak 		goto quit;
    284      1.2  tshiozak 	}
    285      1.2  tshiozak 
    286      1.2  tshiozak 	/* create new entry */
    287      1.2  tshiozak 	ret = open_shared(&ci, basedir, convname, src, dst);
    288      1.2  tshiozak 	if (ret)
    289      1.2  tshiozak 		goto quit;
    290      1.2  tshiozak 
    291      1.2  tshiozak 	_CITRUS_HASH_INSERT(&shared_pool, ci, ci_hash_entry, hashval);
    292      1.2  tshiozak 	ci->ci_used_count = 1;
    293      1.2  tshiozak 	*rci = ci;
    294      1.2  tshiozak 
    295      1.2  tshiozak quit:
    296      1.2  tshiozak 	rwlock_unlock(&lock);
    297      1.2  tshiozak 
    298      1.1  tshiozak 	return ret;
    299      1.1  tshiozak }
    300      1.1  tshiozak 
    301      1.2  tshiozak static void
    302      1.2  tshiozak release_shared(struct _citrus_iconv_shared * __restrict ci)
    303      1.2  tshiozak {
    304      1.2  tshiozak 	rwlock_wrlock(&lock);
    305      1.2  tshiozak 
    306      1.2  tshiozak 	ci->ci_used_count--;
    307      1.2  tshiozak 	if (ci->ci_used_count == 0) {
    308      1.2  tshiozak 		/* put it into unused list */
    309      1.2  tshiozak 		shared_num_unused++;
    310      1.2  tshiozak 		TAILQ_INSERT_TAIL(&shared_unused, ci, ci_tailq_entry);
    311      1.2  tshiozak 		/* flood out */
    312      1.2  tshiozak 		while (shared_num_unused > shared_max_reuse) {
    313      1.2  tshiozak 			ci = TAILQ_FIRST(&shared_unused);
    314      1.2  tshiozak 			_DIAGASSERT(ci != NULL);
    315      1.2  tshiozak 			TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry);
    316      1.3  tshiozak 			_CITRUS_HASH_REMOVE(ci, ci_hash_entry);
    317      1.2  tshiozak 			shared_num_unused--;
    318      1.2  tshiozak 			close_shared(ci);
    319      1.2  tshiozak 		}
    320      1.2  tshiozak 	}
    321      1.2  tshiozak 
    322      1.2  tshiozak 	rwlock_unlock(&lock);
    323      1.2  tshiozak }
    324      1.2  tshiozak 
    325      1.2  tshiozak /*
    326      1.2  tshiozak  * _citrus_iconv_open:
    327      1.2  tshiozak  *	open a converter for the specified in/out codes.
    328      1.2  tshiozak  */
    329      1.2  tshiozak int
    330      1.2  tshiozak _citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict rcv,
    331      1.2  tshiozak 		   const char * __restrict basedir,
    332      1.2  tshiozak 		   const char * __restrict src, const char * __restrict dst)
    333      1.2  tshiozak {
    334      1.2  tshiozak 	int ret;
    335      1.7  christos 	struct _citrus_iconv_shared *ci = NULL;
    336      1.2  tshiozak 	struct _citrus_iconv *cv;
    337      1.2  tshiozak 	char realsrc[PATH_MAX], realdst[PATH_MAX];
    338      1.2  tshiozak 	char buf[PATH_MAX], path[PATH_MAX];
    339      1.2  tshiozak 
    340      1.2  tshiozak 	init_cache();
    341      1.2  tshiozak 
    342      1.2  tshiozak 	/* resolve codeset name aliases */
    343      1.2  tshiozak 	snprintf(path, sizeof(path), "%s/%s", basedir, _CITRUS_ICONV_ALIAS);
    344      1.4  tshiozak 	strlcpy(realsrc,
    345      1.6  christos 		_lookup_alias(path, src, buf, (size_t)PATH_MAX,
    346      1.6  christos 		_LOOKUP_CASE_IGNORE),
    347      1.6  christos 		(size_t)PATH_MAX);
    348      1.4  tshiozak 	strlcpy(realdst,
    349      1.6  christos 		_lookup_alias(path, dst, buf, (size_t)PATH_MAX,
    350      1.6  christos 		_LOOKUP_CASE_IGNORE),
    351      1.6  christos 		(size_t)PATH_MAX);
    352      1.2  tshiozak 
    353      1.2  tshiozak 	/* sanity check */
    354      1.2  tshiozak 	if (strchr(realsrc, '/') != NULL || strchr(realdst, '/'))
    355      1.2  tshiozak 		return EINVAL;
    356      1.2  tshiozak 
    357      1.2  tshiozak 	/* get shared record */
    358      1.2  tshiozak 	ret = get_shared(&ci, basedir, realsrc, realdst);
    359      1.2  tshiozak 	if (ret)
    360      1.2  tshiozak 		return ret;
    361      1.2  tshiozak 
    362      1.2  tshiozak 	/* create/init context */
    363      1.2  tshiozak 	cv = malloc(sizeof(*cv));
    364      1.2  tshiozak 	if (cv == NULL) {
    365      1.2  tshiozak 		ret = errno;
    366      1.2  tshiozak 		release_shared(ci);
    367      1.2  tshiozak 		return ret;
    368      1.2  tshiozak 	}
    369      1.2  tshiozak 	cv->cv_shared = ci;
    370      1.2  tshiozak 	ret = (*ci->ci_ops->io_init_context)(cv);
    371      1.2  tshiozak 	if (ret) {
    372      1.2  tshiozak 		release_shared(ci);
    373      1.2  tshiozak 		free(cv);
    374      1.2  tshiozak 		return ret;
    375      1.2  tshiozak 	}
    376      1.2  tshiozak 	*rcv = cv;
    377      1.2  tshiozak 
    378      1.2  tshiozak 	return 0;
    379      1.2  tshiozak }
    380      1.2  tshiozak 
    381      1.1  tshiozak /*
    382      1.1  tshiozak  * _citrus_iconv_close:
    383      1.1  tshiozak  *	close the specified converter.
    384      1.1  tshiozak  */
    385      1.1  tshiozak void
    386      1.2  tshiozak _citrus_iconv_close(struct _citrus_iconv *cv)
    387      1.1  tshiozak {
    388      1.2  tshiozak 	if (cv) {
    389      1.2  tshiozak 		(*cv->cv_shared->ci_ops->io_uninit_context)(cv);
    390      1.2  tshiozak 		release_shared(cv->cv_shared);
    391      1.2  tshiozak 		free(cv);
    392      1.1  tshiozak 	}
    393      1.1  tshiozak }
    394