2 /* $NetBSD: iconv.c,v 1.11 2009/03/03 16:22:33 explorer Exp $ */
5 * Copyright (c) 2003 Citrus Project,
6 * Copyright (c) 2009, 2010 Gabor Kovesdan <gabor@FreeBSD.org>,
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
44 #include "citrus_types.h"
45 #include "citrus_module.h"
46 #include "citrus_esdb.h"
47 #include "citrus_hash.h"
48 #include "citrus_iconv.h"
50 #include <_libiconv_compat.h>
51 #ifdef __LIBICONV_COMPAT
52 __weak_reference(iconv, libiconv);
53 __weak_reference(iconv_open, libiconv_open);
54 __weak_reference(iconv_open_into, libiconv_open_into);
55 __weak_reference(iconv_close, libiconv_close);
56 __weak_reference(iconvlist, libiconvlist);
57 __weak_reference(iconvctl, libiconvctl);
58 __weak_reference(iconv_set_relocation_prefix, libiconv_set_relocation_prefix);
59 __weak_reference(_iconv_version, _libiconv_version);
62 #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1)
64 int _iconv_version = _ICONV_VERSION;
66 iconv_t _iconv_open(const char *out, const char *in,
67 struct _citrus_iconv *prealloc);
70 _iconv_open(const char *out, const char *in, struct _citrus_iconv *handle)
72 const char *out_slashes;
77 * Remove anything following a //, as these are options (like
78 * //ignore, //translate, etc) and we just don't handle them.
79 * This is for compatibility with software that uses these
82 out_slashes = strstr(out, "//");
83 if (out_slashes != NULL) {
84 out_noslashes = strndup(out, out_slashes - out);
85 if (out_noslashes == NULL) {
89 ret = _citrus_iconv_open(&handle, in, out_noslashes);
92 ret = _citrus_iconv_open(&handle, in, out);
96 errno = ret == ENOENT ? EINVAL : ret;
100 handle->cv_shared->ci_discard_ilseq = strcasestr(out, "//IGNORE");
101 handle->cv_shared->ci_hooks = NULL;
103 return ((iconv_t)(void *)handle);
107 iconv_open(const char *out, const char *in)
110 return (_iconv_open(out, in, NULL));
114 iconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr)
116 struct _citrus_iconv *handle;
118 handle = (struct _citrus_iconv *)ptr;
119 return ((_iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0);
123 iconv_close(iconv_t handle)
126 if (ISBADF(handle)) {
131 _citrus_iconv_close((struct _citrus_iconv *)(void *)handle);
137 iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout)
142 if (ISBADF(handle)) {
147 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
148 in, szin, out, szout, 0, &ret);
158 __iconv(iconv_t handle, const char **in, size_t *szin, char **out,
159 size_t *szout, uint32_t flags, size_t *invalids)
164 if (ISBADF(handle)) {
169 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
170 in, szin, out, szout, flags, &ret);
182 __iconv_get_list(char ***rlist, size_t *rsz, bool sorted)
186 ret = _citrus_esdb_get_list(rlist, rsz, sorted);
196 __iconv_free_list(char **list, size_t sz)
199 _citrus_esdb_free_list(list, sz);
203 * GNU-compatibile non-standard interfaces.
206 qsort_helper(const void *first, const void *second)
208 const char * const *s1;
209 const char * const *s2;
213 return (strcmp(*s1, *s2));
217 iconvlist(int (*do_one) (unsigned int, const char * const *,
220 char **list, **names;
221 const char * const *np;
222 char *curitem, *curkey, *slashpos;
228 if (__iconv_get_list(&list, &sz, true))
230 qsort((void *)list, sz, sizeof(char *), qsort_helper);
233 slashpos = strchr(list[i], '/');
234 curkey = (char *)malloc(slashpos - list[i] + 2);
235 names = (char **)malloc(sz * sizeof(char *));
236 if ((curkey == NULL) || (names == NULL)) {
237 __iconv_free_list(list, sz);
240 strlcpy(curkey, list[i], slashpos - list[i] + 1);
241 names[j++] = strdup(curkey);
242 for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) {
243 slashpos = strchr(list[i], '/');
244 curitem = (char *)malloc(strlen(slashpos) + 1);
245 if (curitem == NULL) {
246 __iconv_free_list(list, sz);
249 strlcpy(curitem, &slashpos[1], strlen(slashpos) + 1);
250 if (strcmp(curkey, curitem) == 0) {
253 names[j++] = strdup(curitem);
255 np = (const char * const *)names;
260 __iconv_free_list(list, sz);
264 *iconv_canonicalize(const char *name)
267 return (_citrus_iconv_canonicalize(name));
271 iconvctl(iconv_t cd, int request, void *argument)
273 struct _citrus_iconv *cv;
274 struct iconv_hooks *hooks;
275 const char *convname;
276 char src[PATH_MAX], *dst;
279 cv = (struct _citrus_iconv *)(void *)cd;
280 hooks = (struct iconv_hooks *)argument;
290 convname = cv->cv_shared->ci_convname;
291 dst = strchr(convname, '/');
293 strlcpy(src, convname, dst - convname + 1);
295 if ((convname == NULL) || (src == NULL) || (dst == NULL))
297 *i = strcmp(src, dst) == 0 ? 1 : 0;
299 case ICONV_GET_TRANSLITERATE:
302 case ICONV_SET_TRANSLITERATE:
303 return ((*i == 1) ? 0 : -1);
304 case ICONV_GET_DISCARD_ILSEQ:
305 *i = cv->cv_shared->ci_discard_ilseq ? 1 : 0;
307 case ICONV_SET_DISCARD_ILSEQ:
308 cv->cv_shared->ci_discard_ilseq = *i;
310 case ICONV_SET_HOOKS:
311 cv->cv_shared->ci_hooks = hooks;
313 case ICONV_SET_FALLBACKS:
323 iconv_set_relocation_prefix(const char *orig_prefix __unused,
324 const char *curr_prefix __unused)