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