1 /* $NetBSD: iconv.c,v 1.11 2009/03/03 16:22:33 explorer Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2003 Citrus Project,
7 * Copyright (c) 2009, 2010 Gabor Kovesdan <gabor@FreeBSD.org>,
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/queue.h>
34 #include <sys/types.h>
45 #include "citrus_types.h"
46 #include "citrus_module.h"
47 #include "citrus_esdb.h"
48 #include "citrus_hash.h"
49 #include "citrus_iconv.h"
51 #include "iconv-internal.h"
53 #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1)
56 __bsd___iconv_open(const char *out, const char *in, struct _citrus_iconv *handle)
61 * Remove anything following a //, as these are options (like
62 * //ignore, //translate, etc) and we just don't handle them.
63 * This is for compatibility with software that uses these
66 ret = _citrus_iconv_open(&handle, in, out);
68 errno = ret == ENOENT ? EINVAL : ret;
72 handle->cv_shared->ci_discard_ilseq = strcasestr(out, "//IGNORE");
74 return ((iconv_t)(void *)handle);
78 __bsd_iconv_open(const char *out, const char *in)
81 return (__bsd___iconv_open(out, in, NULL));
85 __bsd_iconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr)
87 struct _citrus_iconv *handle;
89 handle = (struct _citrus_iconv *)ptr;
90 return ((__bsd___iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0);
94 __bsd_iconv_close(iconv_t handle)
102 _citrus_iconv_close((struct _citrus_iconv *)(void *)handle);
108 __bsd_iconv(iconv_t handle, char **in, size_t *szin, char **out, size_t *szout)
113 if (ISBADF(handle)) {
118 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
119 in, szin, out, szout, 0, &ret);
129 __bsd___iconv(iconv_t handle, char **in, size_t *szin, char **out,
130 size_t *szout, uint32_t flags, size_t *invalids)
135 if (ISBADF(handle)) {
140 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
141 in, szin, out, szout, flags, &ret);
153 __bsd___iconv_get_list(char ***rlist, size_t *rsz, bool sorted)
157 ret = _citrus_esdb_get_list(rlist, rsz, sorted);
167 __bsd___iconv_free_list(char **list, size_t sz)
170 _citrus_esdb_free_list(list, sz);
174 * GNU-compatibile non-standard interfaces.
177 qsort_helper(const void *first, const void *second)
179 const char * const *s1;
180 const char * const *s2;
184 return (strcmp(*s1, *s2));
188 __bsd_iconvlist(int (*do_one) (unsigned int, const char * const *,
191 char **list, **names;
192 const char * const *np;
193 char *curitem, *curkey, *slashpos;
195 unsigned int i, j, n;
200 if (__bsd___iconv_get_list(&list, &sz, true)) {
204 qsort((void *)list, sz, sizeof(char *), qsort_helper);
207 slashpos = strchr(list[i], '/');
208 names = malloc(sz * sizeof(char *));
211 curkey = strndup(list[i], slashpos - list[i]);
215 for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) {
216 slashpos = strchr(list[i], '/');
217 if (strcmp(curkey, &slashpos[1]) == 0)
219 curitem = strdup(&slashpos[1]);
222 names[j++] = curitem;
224 np = (const char * const *)names;
226 for (n = 0; n < j; n++)
234 for (n = 0; n < j; n++)
239 __bsd___iconv_free_list(list, sz);
242 __inline const char *
243 __bsd_iconv_canonicalize(const char *name)
246 return (_citrus_iconv_canonicalize(name));
250 __bsd_iconvctl(iconv_t cd, int request, void *argument)
252 struct _citrus_iconv *cv;
253 struct iconv_hooks *hooks;
254 const char *convname;
259 cv = (struct _citrus_iconv *)(void *)cd;
260 hooks = (struct iconv_hooks *)argument;
270 convname = cv->cv_shared->ci_convname;
271 dst = strchr(convname, '/');
272 srclen = dst - convname;
274 *i = (srclen == strlen(dst)) && !memcmp(convname, dst, srclen);
276 case ICONV_GET_TRANSLITERATE:
279 case ICONV_SET_TRANSLITERATE:
280 return ((*i == 1) ? 0 : -1);
281 case ICONV_GET_DISCARD_ILSEQ:
282 *i = cv->cv_shared->ci_discard_ilseq ? 1 : 0;
284 case ICONV_SET_DISCARD_ILSEQ:
285 cv->cv_shared->ci_discard_ilseq = *i;
287 case ICONV_SET_HOOKS:
288 cv->cv_shared->ci_hooks = hooks;
290 case ICONV_SET_FALLBACKS:
293 case ICONV_GET_ILSEQ_INVALID:
294 *i = cv->cv_shared->ci_ilseq_invalid ? 1 : 0;
296 case ICONV_SET_ILSEQ_INVALID:
297 cv->cv_shared->ci_ilseq_invalid = *i;
306 __bsd_iconv_set_relocation_prefix(const char *orig_prefix __unused,
307 const char *curr_prefix __unused)