1 /* $NetBSD: citrus_iconv.c,v 1.10 2011/11/19 18:34:21 tnozaki Exp $ */
4 * Copyright (c)2003 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_iconv.c,v 1.10 2011/11/19 18:34:21 tnozaki Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
35 #include "reentrant.h"
37 #include <sys/types.h>
38 #include <sys/queue.h>
51 #include "citrus_namespace.h"
52 #include "citrus_bcs.h"
53 #include "citrus_region.h"
54 #include "citrus_memstream.h"
55 #include "citrus_mmap.h"
56 #include "citrus_module.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"
69 static rwlock_t lock
= RWLOCK_INITIALIZER
;
72 static bool isinit
= false;
73 static _CITRUS_HASH_HEAD(, _citrus_iconv_shared
, CI_HASH_SIZE
) shared_pool
;
74 static TAILQ_HEAD(, _citrus_iconv_shared
) shared_unused
;
75 static int shared_num_unused
, shared_max_reuse
;
82 _CITRUS_HASH_INIT(&shared_pool
, CI_HASH_SIZE
);
83 TAILQ_INIT(&shared_unused
);
84 shared_max_reuse
= -1;
85 if (!issetugid() && getenv(CI_ENV_MAX_REUSE
))
86 shared_max_reuse
= atoi(getenv(CI_ENV_MAX_REUSE
));
87 if (shared_max_reuse
< 0)
88 shared_max_reuse
= CI_INITIAL_MAX_REUSE
;
96 * lookup iconv.dir entry in the specified directory.
98 * line format of iconv.dir file:
101 * module : iconv module name.
102 * arg : argument for the module (generally, description file name)
106 lookup_iconv_entry(const char *curdir
, const char *key
,
107 char *linebuf
, size_t linebufsize
,
108 const char **module
, const char **variable
)
111 char *p
, path
[PATH_MAX
];
114 snprintf(path
, (size_t)PATH_MAX
, ("%s/" _CITRUS_ICONV_DIR
), curdir
);
117 cp
= p
= _lookup_simple(path
, key
, linebuf
, linebufsize
,
118 _LOOKUP_CASE_IGNORE
);
122 /* get module name */
124 cq
= _bcs_skip_nonws(cp
);
130 cp
= _bcs_skip_ws(cq
);
131 *variable
= p
+= cp
- cq
;
132 cq
= _bcs_skip_nonws(cp
);
139 close_shared(struct _citrus_iconv_shared
*ci
)
145 (*ci
->ci_ops
->io_uninit_shared
)(ci
);
148 _citrus_unload_module(ci
->ci_module
);
155 open_shared(struct _citrus_iconv_shared
* __restrict
* __restrict rci
,
156 const char * __restrict basedir
, const char * __restrict convname
,
157 const char * __restrict src
, const char * __restrict dst
)
160 struct _citrus_iconv_shared
*ci
;
161 _citrus_iconv_getops_t getops
;
162 char linebuf
[LINE_MAX
];
163 const char *module
, *variable
;
166 /* search converter entry */
167 ret
= lookup_iconv_entry(basedir
, convname
, linebuf
, sizeof(linebuf
),
172 ret
= lookup_iconv_entry(basedir
, "*",
173 linebuf
, sizeof(linebuf
),
179 /* initialize iconv handle */
180 len_convname
= strlen(convname
);
181 ci
= malloc(sizeof(*ci
)+len_convname
+1);
186 ci
->ci_module
= NULL
;
188 ci
->ci_closure
= NULL
;
189 ci
->ci_convname
= (void *)&ci
[1];
190 memcpy(ci
->ci_convname
, convname
, len_convname
+1);
193 ret
= _citrus_load_module(&ci
->ci_module
, module
);
198 getops
= (_citrus_iconv_getops_t
)
199 _citrus_find_getops(ci
->ci_module
, module
, "iconv");
204 ci
->ci_ops
= malloc(sizeof(*ci
->ci_ops
));
209 ret
= (*getops
)(ci
->ci_ops
, sizeof(*ci
->ci_ops
),
210 _CITRUS_ICONV_ABI_VERSION
);
215 if (ci
->ci_ops
->io_abi_version
== 1) {
216 /* binary compatibility broken at ver.2 */
221 if (ci
->ci_ops
->io_init_shared
== NULL
||
222 ci
->ci_ops
->io_uninit_shared
== NULL
||
223 ci
->ci_ops
->io_init_context
== NULL
||
224 ci
->ci_ops
->io_uninit_context
== NULL
||
225 ci
->ci_ops
->io_convert
== NULL
) {
230 /* initialize the converter */
231 ret
= (*ci
->ci_ops
->io_init_shared
)(ci
, basedir
, src
, dst
,
232 (const void *)variable
,
246 hash_func(const char *key
)
248 return _string_hash_func(key
, CI_HASH_SIZE
);
252 match_func(struct _citrus_iconv_shared
* __restrict ci
,
253 const char * __restrict key
)
255 return strcmp(ci
->ci_convname
, key
);
259 get_shared(struct _citrus_iconv_shared
* __restrict
* __restrict rci
,
260 const char *basedir
, const char *src
, const char *dst
)
264 struct _citrus_iconv_shared
* ci
;
265 char convname
[PATH_MAX
];
267 snprintf(convname
, sizeof(convname
), "%s/%s", src
, dst
);
269 rwlock_wrlock(&lock
);
271 /* lookup alread existing entry */
272 hashval
= hash_func(convname
);
273 _CITRUS_HASH_SEARCH(&shared_pool
, ci
, ci_hash_entry
, match_func
,
277 if (ci
->ci_used_count
== 0) {
278 TAILQ_REMOVE(&shared_unused
, ci
, ci_tailq_entry
);
286 /* create new entry */
287 ret
= open_shared(&ci
, basedir
, convname
, src
, dst
);
291 _CITRUS_HASH_INSERT(&shared_pool
, ci
, ci_hash_entry
, hashval
);
292 ci
->ci_used_count
= 1;
296 rwlock_unlock(&lock
);
302 release_shared(struct _citrus_iconv_shared
* __restrict ci
)
304 rwlock_wrlock(&lock
);
307 if (ci
->ci_used_count
== 0) {
308 /* put it into unused list */
310 TAILQ_INSERT_TAIL(&shared_unused
, ci
, ci_tailq_entry
);
312 while (shared_num_unused
> shared_max_reuse
) {
313 ci
= TAILQ_FIRST(&shared_unused
);
314 _DIAGASSERT(ci
!= NULL
);
315 TAILQ_REMOVE(&shared_unused
, ci
, ci_tailq_entry
);
316 _CITRUS_HASH_REMOVE(ci
, ci_hash_entry
);
322 rwlock_unlock(&lock
);
326 * _citrus_iconv_open:
327 * open a converter for the specified in/out codes.
330 _citrus_iconv_open(struct _citrus_iconv
* __restrict
* __restrict rcv
,
331 const char * __restrict basedir
,
332 const char * __restrict src
, const char * __restrict dst
)
335 struct _citrus_iconv_shared
*ci
= NULL
;
336 struct _citrus_iconv
*cv
;
337 char realsrc
[PATH_MAX
], realdst
[PATH_MAX
];
338 char buf
[PATH_MAX
], path
[PATH_MAX
];
342 /* resolve codeset name aliases */
343 snprintf(path
, sizeof(path
), "%s/%s", basedir
, _CITRUS_ICONV_ALIAS
);
345 _lookup_alias(path
, src
, buf
, (size_t)PATH_MAX
,
346 _LOOKUP_CASE_IGNORE
),
349 _lookup_alias(path
, dst
, buf
, (size_t)PATH_MAX
,
350 _LOOKUP_CASE_IGNORE
),
354 if (strchr(realsrc
, '/') != NULL
|| strchr(realdst
, '/'))
357 /* get shared record */
358 ret
= get_shared(&ci
, basedir
, realsrc
, realdst
);
362 /* create/init context */
363 cv
= malloc(sizeof(*cv
));
370 ret
= (*ci
->ci_ops
->io_init_context
)(cv
);
382 * _citrus_iconv_close:
383 * close the specified converter.
386 _citrus_iconv_close(struct _citrus_iconv
*cv
)
389 (*cv
->cv_shared
->ci_ops
->io_uninit_context
)(cv
);
390 release_shared(cv
->cv_shared
);