2 /* $NetBSD: citrus_iconv.c,v 1.7 2008/07/25 14:05:25 christos Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/queue.h>
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"
59 #define _CITRUS_ICONV_DIR "iconv.dir"
60 #define _CITRUS_ICONV_ALIAS "iconv.alias"
62 #define CI_HASH_SIZE 101
63 #define CI_INITIAL_MAX_REUSE 5
64 #define CI_ENV_MAX_REUSE "ICONV_MAX_REUSE"
66 static bool isinit = false;
67 static int shared_max_reuse, shared_num_unused;
68 static _CITRUS_HASH_HEAD(, _citrus_iconv_shared, CI_HASH_SIZE) shared_pool;
69 static TAILQ_HEAD(, _citrus_iconv_shared) shared_unused;
71 static pthread_rwlock_t ci_lock = PTHREAD_RWLOCK_INITIALIZER;
79 _CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE);
80 TAILQ_INIT(&shared_unused);
81 shared_max_reuse = -1;
82 if (!issetugid() && getenv(CI_ENV_MAX_REUSE))
83 shared_max_reuse = atoi(getenv(CI_ENV_MAX_REUSE));
84 if (shared_max_reuse < 0)
85 shared_max_reuse = CI_INITIAL_MAX_REUSE;
92 close_shared(struct _citrus_iconv_shared *ci)
99 (*ci->ci_ops->io_uninit_shared)(ci);
102 _citrus_unload_module(ci->ci_module);
109 open_shared(struct _citrus_iconv_shared * __restrict * __restrict rci,
110 const char * __restrict convname, const char * __restrict src,
111 const char * __restrict dst)
113 struct _citrus_iconv_shared *ci;
114 _citrus_iconv_getops_t getops;
119 #ifdef INCOMPATIBLE_WITH_GNU_ICONV
121 * Sadly, the gnu tools expect iconv to actually parse the
122 * byte stream and don't allow for a pass-through when
123 * the (src,dest) encodings are the same.
124 * See gettext-0.18.3+ NEWS:
125 * msgfmt now checks PO file headers more strictly with less
127 * NetBSD don't do this either.
129 module = (strcmp(src, dst) != 0) ? "iconv_std" : "iconv_none";
131 module = "iconv_std";
134 /* initialize iconv handle */
135 len_convname = strlen(convname);
136 ci = malloc(sizeof(*ci) + len_convname + 1);
141 ci->ci_module = NULL;
143 ci->ci_closure = NULL;
144 ci->ci_convname = (void *)&ci[1];
145 memcpy(ci->ci_convname, convname, len_convname + 1);
148 ret = _citrus_load_module(&ci->ci_module, module);
153 getops = (_citrus_iconv_getops_t)_citrus_find_getops(ci->ci_module,
159 ci->ci_ops = malloc(sizeof(*ci->ci_ops));
164 ret = (*getops)(ci->ci_ops);
168 if (ci->ci_ops->io_init_shared == NULL ||
169 ci->ci_ops->io_uninit_shared == NULL ||
170 ci->ci_ops->io_init_context == NULL ||
171 ci->ci_ops->io_uninit_context == NULL ||
172 ci->ci_ops->io_convert == NULL)
175 /* initialize the converter */
176 ret = (*ci->ci_ops->io_init_shared)(ci, src, dst);
189 hash_func(const char *key)
192 return (_string_hash_func(key, CI_HASH_SIZE));
196 match_func(struct _citrus_iconv_shared * __restrict ci,
197 const char * __restrict key)
200 return (strcmp(ci->ci_convname, key));
204 get_shared(struct _citrus_iconv_shared * __restrict * __restrict rci,
205 const char *src, const char *dst)
207 struct _citrus_iconv_shared * ci;
208 char convname[PATH_MAX];
209 int hashval, ret = 0;
211 snprintf(convname, sizeof(convname), "%s/%s", src, dst);
215 /* lookup alread existing entry */
216 hashval = hash_func(convname);
217 _CITRUS_HASH_SEARCH(&shared_pool, ci, ci_hash_entry, match_func,
221 if (ci->ci_used_count == 0) {
222 TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry);
230 /* create new entry */
231 ret = open_shared(&ci, convname, src, dst);
235 _CITRUS_HASH_INSERT(&shared_pool, ci, ci_hash_entry, hashval);
236 ci->ci_used_count = 1;
246 release_shared(struct _citrus_iconv_shared * __restrict ci)
251 if (ci->ci_used_count == 0) {
252 /* put it into unused list */
254 TAILQ_INSERT_TAIL(&shared_unused, ci, ci_tailq_entry);
256 while (shared_num_unused > shared_max_reuse) {
257 ci = TAILQ_FIRST(&shared_unused);
258 TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry);
259 _CITRUS_HASH_REMOVE(ci, ci_hash_entry);
269 * _citrus_iconv_open:
270 * open a converter for the specified in/out codes.
273 _citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict rcv,
274 const char * __restrict src, const char * __restrict dst)
276 struct _citrus_iconv *cv = NULL;
277 struct _citrus_iconv_shared *ci = NULL;
278 char realdst[PATH_MAX], realsrc[PATH_MAX];
279 char buf[PATH_MAX], path[PATH_MAX];
284 /* GNU behaviour, using locale encoding if "" or "char" is specified */
285 if ((strcmp(src, "") == 0) || (strcmp(src, "char") == 0))
286 src = nl_langinfo(CODESET);
287 if ((strcmp(dst, "") == 0) || (strcmp(dst, "char") == 0))
288 dst = nl_langinfo(CODESET);
290 /* resolve codeset name aliases */
291 strlcpy(realsrc, _lookup_alias(path, src, buf, (size_t)PATH_MAX,
292 _LOOKUP_CASE_IGNORE), (size_t)PATH_MAX);
293 strlcpy(realdst, _lookup_alias(path, dst, buf, (size_t)PATH_MAX,
294 _LOOKUP_CASE_IGNORE), (size_t)PATH_MAX);
297 if (strchr(realsrc, '/') != NULL || strchr(realdst, '/'))
300 /* get shared record */
301 ret = get_shared(&ci, realsrc, realdst);
305 /* create/init context */
307 cv = malloc(sizeof(*cv));
315 (*rcv)->cv_shared = ci;
316 ret = (*ci->ci_ops->io_init_context)(*rcv);
326 * _citrus_iconv_close:
327 * close the specified converter.
330 _citrus_iconv_close(struct _citrus_iconv *cv)
334 (*cv->cv_shared->ci_ops->io_uninit_context)(cv);
335 release_shared(cv->cv_shared);
341 *_citrus_iconv_canonicalize(const char *name)
345 if ((buf = malloc((size_t)PATH_MAX)) == NULL)
347 memset((void *)buf, 0, (size_t)PATH_MAX);
348 _citrus_esdb_alias(name, buf, (size_t)PATH_MAX);