2 /* $NetBSD: citrus_iconv.c,v 1.10 2011/11/19 18:34:21 tnozaki Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/queue.h>
49 #include "citrus_namespace.h"
50 #include "citrus_bcs.h"
51 #include "citrus_esdb.h"
52 #include "citrus_region.h"
53 #include "citrus_memstream.h"
54 #include "citrus_mmap.h"
55 #include "citrus_module.h"
56 #include "citrus_lock.h"
57 #include "citrus_lookup.h"
58 #include "citrus_hash.h"
59 #include "citrus_iconv.h"
61 #define _CITRUS_ICONV_DIR "iconv.dir"
62 #define _CITRUS_ICONV_ALIAS "iconv.alias"
64 #define CI_HASH_SIZE 101
65 #define CI_INITIAL_MAX_REUSE 5
66 #define CI_ENV_MAX_REUSE "ICONV_MAX_REUSE"
68 static bool isinit = false;
69 static int shared_max_reuse, shared_num_unused;
70 static _CITRUS_HASH_HEAD(, _citrus_iconv_shared, CI_HASH_SIZE) shared_pool;
71 static TAILQ_HEAD(, _citrus_iconv_shared) shared_unused;
73 static pthread_rwlock_t ci_lock = PTHREAD_RWLOCK_INITIALIZER;
81 _CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE);
82 TAILQ_INIT(&shared_unused);
83 shared_max_reuse = -1;
84 if (!issetugid() && getenv(CI_ENV_MAX_REUSE))
85 shared_max_reuse = atoi(getenv(CI_ENV_MAX_REUSE));
86 if (shared_max_reuse < 0)
87 shared_max_reuse = CI_INITIAL_MAX_REUSE;
94 close_shared(struct _citrus_iconv_shared *ci)
101 (*ci->ci_ops->io_uninit_shared)(ci);
104 _citrus_unload_module(ci->ci_module);
111 open_shared(struct _citrus_iconv_shared * __restrict * __restrict rci,
112 const char * __restrict convname, const char * __restrict src,
113 const char * __restrict dst)
115 struct _citrus_iconv_shared *ci;
116 _citrus_iconv_getops_t getops;
121 #ifdef INCOMPATIBLE_WITH_GNU_ICONV
123 * Sadly, the gnu tools expect iconv to actually parse the
124 * byte stream and don't allow for a pass-through when
125 * the (src,dest) encodings are the same.
126 * See gettext-0.18.3+ NEWS:
127 * msgfmt now checks PO file headers more strictly with less
129 * NetBSD, also, doesn't do the below pass-through.
131 * Also note that this currently falls short if dst options have been
132 * specified. It may be the case that we want to ignore EILSEQ, in which
133 * case we should also select iconv_std anyways. This trick, while
134 * clever, may not be worth it.
136 module = (strcmp(src, dst) != 0) ? "iconv_std" : "iconv_none";
138 module = "iconv_std";
141 /* initialize iconv handle */
142 len_convname = strlen(convname);
143 ci = malloc(sizeof(*ci) + len_convname + 1);
148 ci->ci_module = NULL;
150 ci->ci_closure = NULL;
151 ci->ci_convname = (void *)&ci[1];
152 memcpy(ci->ci_convname, convname, len_convname + 1);
155 ret = _citrus_load_module(&ci->ci_module, module);
160 getops = (_citrus_iconv_getops_t)_citrus_find_getops(ci->ci_module,
166 ci->ci_ops = malloc(sizeof(*ci->ci_ops));
171 ret = (*getops)(ci->ci_ops);
175 if (ci->ci_ops->io_init_shared == NULL ||
176 ci->ci_ops->io_uninit_shared == NULL ||
177 ci->ci_ops->io_init_context == NULL ||
178 ci->ci_ops->io_uninit_context == NULL ||
179 ci->ci_ops->io_convert == NULL) {
184 /* initialize the converter */
185 ret = (*ci->ci_ops->io_init_shared)(ci, src, dst);
198 hash_func(const char *key)
201 return (_string_hash_func(key, CI_HASH_SIZE));
205 match_func(struct _citrus_iconv_shared * __restrict ci,
206 const char * __restrict key)
209 return (strcmp(ci->ci_convname, key));
213 get_shared(struct _citrus_iconv_shared * __restrict * __restrict rci,
214 const char *src, const char *dst)
216 struct _citrus_iconv_shared * ci;
217 char convname[PATH_MAX];
218 int hashval, ret = 0;
220 snprintf(convname, sizeof(convname), "%s/%s", src, dst);
224 /* lookup alread existing entry */
225 hashval = hash_func(convname);
226 _CITRUS_HASH_SEARCH(&shared_pool, ci, ci_hash_entry, match_func,
230 if (ci->ci_used_count == 0) {
231 TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry);
239 /* create new entry */
240 ret = open_shared(&ci, convname, src, dst);
244 _CITRUS_HASH_INSERT(&shared_pool, ci, ci_hash_entry, hashval);
245 ci->ci_used_count = 1;
255 release_shared(struct _citrus_iconv_shared * __restrict ci)
260 if (ci->ci_used_count == 0) {
261 /* put it into unused list */
263 TAILQ_INSERT_TAIL(&shared_unused, ci, ci_tailq_entry);
265 while (shared_num_unused > shared_max_reuse) {
266 ci = TAILQ_FIRST(&shared_unused);
267 TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry);
268 _CITRUS_HASH_REMOVE(ci, ci_hash_entry);
278 * _citrus_iconv_open:
279 * open a converter for the specified in/out codes.
282 _citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict rcv,
283 const char * __restrict src, const char * __restrict dst)
285 struct _citrus_iconv *cv = NULL;
286 struct _citrus_iconv_shared *ci = NULL;
287 char realdst[PATH_MAX], realsrc[PATH_MAX], *slashes;
289 char buf[PATH_MAX], path[PATH_MAX];
295 /* GNU behaviour, using locale encoding if "" or "char" is specified */
296 if ((strcmp(src, "") == 0) || (strcmp(src, "char") == 0))
297 src = nl_langinfo(CODESET);
298 if ((strcmp(dst, "") == 0) || (strcmp(dst, "char") == 0))
299 dst = nl_langinfo(CODESET);
301 strlcpy(realsrc, src, (size_t)PATH_MAX);
302 if ((slashes = strstr(realsrc, "//")) != NULL)
304 strlcpy(realdst, dst, (size_t)PATH_MAX);
305 if ((slashes = strstr(realdst, "//")) != NULL)
308 /* resolve codeset name aliases */
311 * Note that the below reads from realsrc and realdst while it's
312 * repopulating (writing to) realsrc and realdst, but it's done so with
313 * a trip through `buf`.
315 snprintf(path, sizeof(path), "%s/%s", _PATH_ICONV, _CITRUS_ICONV_ALIAS);
316 strlcpy(realsrc, _lookup_alias(path, realsrc, buf, (size_t)PATH_MAX,
317 _LOOKUP_CASE_IGNORE), (size_t)PATH_MAX);
318 strlcpy(realdst, _lookup_alias(path, realdst, buf, (size_t)PATH_MAX,
319 _LOOKUP_CASE_IGNORE), (size_t)PATH_MAX);
323 if (strchr(realsrc, '/') != NULL || strchr(realdst, '/'))
326 /* get shared record */
327 ret = get_shared(&ci, realsrc, realdst);
331 /* create/init context */
333 cv = malloc(sizeof(*cv));
341 (*rcv)->cv_shared = ci;
342 ret = (*ci->ci_ops->io_init_context)(*rcv);
352 * _citrus_iconv_close:
353 * close the specified converter.
356 _citrus_iconv_close(struct _citrus_iconv *cv)
360 (*cv->cv_shared->ci_ops->io_uninit_context)(cv);
361 release_shared(cv->cv_shared);
367 *_citrus_iconv_canonicalize(const char *name)
371 if ((buf = calloc((size_t)PATH_MAX, sizeof(*buf))) == NULL)
373 _citrus_esdb_alias(name, buf, (size_t)PATH_MAX);