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