1 /* $NetBSD: citrus_iconv.c,v 1.8 2009/09/05 06:44:27 dholland 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.8 2009/09/05 06:44:27 dholland Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
35 #include "reentrant.h"
45 #include <sys/types.h>
46 #include <sys/queue.h>
48 #include "citrus_namespace.h"
49 #include "citrus_bcs.h"
50 #include "citrus_region.h"
51 #include "citrus_memstream.h"
52 #include "citrus_mmap.h"
53 #include "citrus_module.h"
54 #include "citrus_lookup.h"
55 #include "citrus_hash.h"
56 #include "citrus_iconv.h"
58 #define _CITRUS_ICONV_DIR "iconv.dir"
59 #define _CITRUS_ICONV_ALIAS "iconv.alias"
61 #define CI_HASH_SIZE 101
62 #define CI_INITIAL_MAX_REUSE 5
63 #define CI_ENV_MAX_REUSE "ICONV_MAX_REUSE"
66 static rwlock_t lock
= RWLOCK_INITIALIZER
;
68 static int isinit
= 0;
69 static _CITRUS_HASH_HEAD(, _citrus_iconv_shared
, CI_HASH_SIZE
) shared_pool
;
70 static TAILQ_HEAD(, _citrus_iconv_shared
) shared_unused
;
71 static int shared_num_unused
, shared_max_reuse
;
78 _CITRUS_HASH_INIT(&shared_pool
, CI_HASH_SIZE
);
79 TAILQ_INIT(&shared_unused
);
80 shared_max_reuse
= -1;
81 if (!issetugid() && getenv(CI_ENV_MAX_REUSE
))
82 shared_max_reuse
= atoi(getenv(CI_ENV_MAX_REUSE
));
83 if (shared_max_reuse
< 0)
84 shared_max_reuse
= CI_INITIAL_MAX_REUSE
;
92 * lookup iconv.dir entry in the specified directory.
94 * line format of iconv.dir file:
97 * module : iconv module name.
98 * arg : argument for the module (generally, description file name)
102 lookup_iconv_entry(const char *curdir
, const char *key
,
103 char *linebuf
, size_t linebufsize
,
104 const char **module
, const char **variable
)
107 char *p
, path
[PATH_MAX
];
110 snprintf(path
, (size_t)PATH_MAX
, ("%s/" _CITRUS_ICONV_DIR
), curdir
);
113 cp
= p
= _lookup_simple(path
, key
, linebuf
, linebufsize
,
114 _LOOKUP_CASE_IGNORE
);
118 /* get module name */
120 cq
= _bcs_skip_nonws(cp
);
126 cp
= _bcs_skip_ws(cq
);
127 *variable
= p
+= cp
- cq
;
128 cq
= _bcs_skip_nonws(cp
);
135 close_shared(struct _citrus_iconv_shared
*ci
)
141 (*ci
->ci_ops
->io_uninit_shared
)(ci
);
144 _citrus_unload_module(ci
->ci_module
);
151 open_shared(struct _citrus_iconv_shared
* __restrict
* __restrict rci
,
152 const char * __restrict basedir
, const char * __restrict convname
,
153 const char * __restrict src
, const char * __restrict dst
)
156 struct _citrus_iconv_shared
*ci
;
157 _citrus_iconv_getops_t getops
;
158 char linebuf
[LINE_MAX
];
159 const char *module
, *variable
;
162 /* search converter entry */
163 ret
= lookup_iconv_entry(basedir
, convname
, linebuf
, sizeof(linebuf
),
168 ret
= lookup_iconv_entry(basedir
, "*",
169 linebuf
, sizeof(linebuf
),
175 /* initialize iconv handle */
176 len_convname
= strlen(convname
);
177 ci
= malloc(sizeof(*ci
)+len_convname
+1);
182 ci
->ci_module
= NULL
;
184 ci
->ci_closure
= NULL
;
185 ci
->ci_convname
= (void *)&ci
[1];
186 memcpy(ci
->ci_convname
, convname
, len_convname
+1);
189 ret
= _citrus_load_module(&ci
->ci_module
, module
);
194 getops
= (_citrus_iconv_getops_t
)
195 _citrus_find_getops(ci
->ci_module
, module
, "iconv");
200 ci
->ci_ops
= malloc(sizeof(*ci
->ci_ops
));
205 ret
= (*getops
)(ci
->ci_ops
, sizeof(*ci
->ci_ops
),
206 _CITRUS_ICONV_ABI_VERSION
);
211 if (ci
->ci_ops
->io_abi_version
== 1) {
212 /* binary compatibility broken at ver.2 */
217 if (ci
->ci_ops
->io_init_shared
== NULL
||
218 ci
->ci_ops
->io_uninit_shared
== NULL
||
219 ci
->ci_ops
->io_init_context
== NULL
||
220 ci
->ci_ops
->io_uninit_context
== NULL
||
221 ci
->ci_ops
->io_convert
== NULL
)
224 /* initialize the converter */
225 ret
= (*ci
->ci_ops
->io_init_shared
)(ci
, basedir
, src
, dst
,
226 (const void *)variable
,
240 hash_func(const char *key
)
242 return _string_hash_func(key
, CI_HASH_SIZE
);
246 match_func(struct _citrus_iconv_shared
* __restrict ci
,
247 const char * __restrict key
)
249 return strcmp(ci
->ci_convname
, key
);
253 get_shared(struct _citrus_iconv_shared
* __restrict
* __restrict rci
,
254 const char *basedir
, const char *src
, const char *dst
)
258 struct _citrus_iconv_shared
* ci
;
259 char convname
[PATH_MAX
];
261 snprintf(convname
, sizeof(convname
), "%s/%s", src
, dst
);
263 rwlock_wrlock(&lock
);
265 /* lookup alread existing entry */
266 hashval
= hash_func(convname
);
267 _CITRUS_HASH_SEARCH(&shared_pool
, ci
, ci_hash_entry
, match_func
,
271 if (ci
->ci_used_count
== 0) {
272 TAILQ_REMOVE(&shared_unused
, ci
, ci_tailq_entry
);
280 /* create new entry */
281 ret
= open_shared(&ci
, basedir
, convname
, src
, dst
);
285 _CITRUS_HASH_INSERT(&shared_pool
, ci
, ci_hash_entry
, hashval
);
286 ci
->ci_used_count
= 1;
290 rwlock_unlock(&lock
);
296 release_shared(struct _citrus_iconv_shared
* __restrict ci
)
298 rwlock_wrlock(&lock
);
301 if (ci
->ci_used_count
== 0) {
302 /* put it into unused list */
304 TAILQ_INSERT_TAIL(&shared_unused
, ci
, ci_tailq_entry
);
306 while (shared_num_unused
> shared_max_reuse
) {
307 ci
= TAILQ_FIRST(&shared_unused
);
308 _DIAGASSERT(ci
!= NULL
);
309 TAILQ_REMOVE(&shared_unused
, ci
, ci_tailq_entry
);
310 _CITRUS_HASH_REMOVE(ci
, ci_hash_entry
);
316 rwlock_unlock(&lock
);
320 * _citrus_iconv_open:
321 * open a converter for the specified in/out codes.
324 _citrus_iconv_open(struct _citrus_iconv
* __restrict
* __restrict rcv
,
325 const char * __restrict basedir
,
326 const char * __restrict src
, const char * __restrict dst
)
329 struct _citrus_iconv_shared
*ci
= NULL
;
330 struct _citrus_iconv
*cv
;
331 char realsrc
[PATH_MAX
], realdst
[PATH_MAX
];
332 char buf
[PATH_MAX
], path
[PATH_MAX
];
336 /* resolve codeset name aliases */
337 snprintf(path
, sizeof(path
), "%s/%s", basedir
, _CITRUS_ICONV_ALIAS
);
339 _lookup_alias(path
, src
, buf
, (size_t)PATH_MAX
,
340 _LOOKUP_CASE_IGNORE
),
343 _lookup_alias(path
, dst
, buf
, (size_t)PATH_MAX
,
344 _LOOKUP_CASE_IGNORE
),
348 if (strchr(realsrc
, '/') != NULL
|| strchr(realdst
, '/'))
351 /* get shared record */
352 ret
= get_shared(&ci
, basedir
, realsrc
, realdst
);
356 /* create/init context */
357 cv
= malloc(sizeof(*cv
));
364 ret
= (*ci
->ci_ops
->io_init_context
)(cv
);
376 * _citrus_iconv_close:
377 * close the specified converter.
380 _citrus_iconv_close(struct _citrus_iconv
*cv
)
383 (*cv
->cv_shared
->ci_ops
->io_uninit_context
)(cv
);
384 release_shared(cv
->cv_shared
);