2 /* $NetBSD: iconv.c,v 1.11 2009/03/03 16:22:33 explorer Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c) 2003 Citrus Project,
8 * Copyright (c) 2009, 2010 Gabor Kovesdan <gabor@FreeBSD.org>,
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
46 #include "citrus_types.h"
47 #include "citrus_module.h"
48 #include "citrus_esdb.h"
49 #include "citrus_hash.h"
50 #include "citrus_iconv.h"
52 #include "iconv-internal.h"
54 #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1)
57 __bsd___iconv_open(const char *out, const char *in, struct _citrus_iconv *handle)
62 * Remove anything following a //, as these are options (like
63 * //ignore, //translate, etc) and we just don't handle them.
64 * This is for compatibility with software that uses these
67 ret = _citrus_iconv_open(&handle, in, out);
69 errno = ret == ENOENT ? EINVAL : ret;
73 handle->cv_shared->ci_discard_ilseq = strcasestr(out, "//IGNORE");
74 handle->cv_shared->ci_ilseq_invalid = false;
75 handle->cv_shared->ci_hooks = NULL;
77 return ((iconv_t)(void *)handle);
81 __bsd_iconv_open(const char *out, const char *in)
84 return (__bsd___iconv_open(out, in, NULL));
88 __bsd_iconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr)
90 struct _citrus_iconv *handle;
92 handle = (struct _citrus_iconv *)ptr;
93 return ((__bsd___iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0);
97 __bsd_iconv_close(iconv_t handle)
100 if (ISBADF(handle)) {
105 _citrus_iconv_close((struct _citrus_iconv *)(void *)handle);
111 __bsd_iconv(iconv_t handle, char **in, size_t *szin, char **out, size_t *szout)
116 if (ISBADF(handle)) {
121 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
122 in, szin, out, szout, 0, &ret);
132 __bsd___iconv(iconv_t handle, char **in, size_t *szin, char **out,
133 size_t *szout, uint32_t flags, size_t *invalids)
138 if (ISBADF(handle)) {
143 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
144 in, szin, out, szout, flags, &ret);
156 __bsd___iconv_get_list(char ***rlist, size_t *rsz, bool sorted)
160 ret = _citrus_esdb_get_list(rlist, rsz, sorted);
170 __bsd___iconv_free_list(char **list, size_t sz)
173 _citrus_esdb_free_list(list, sz);
177 * GNU-compatibile non-standard interfaces.
180 qsort_helper(const void *first, const void *second)
182 const char * const *s1;
183 const char * const *s2;
187 return (strcmp(*s1, *s2));
191 __bsd_iconvlist(int (*do_one) (unsigned int, const char * const *,
194 char **list, **names;
195 const char * const *np;
196 char *curitem, *curkey, *slashpos;
198 unsigned int i, j, n;
203 if (__bsd___iconv_get_list(&list, &sz, true)) {
207 qsort((void *)list, sz, sizeof(char *), qsort_helper);
210 slashpos = strchr(list[i], '/');
211 names = malloc(sz * sizeof(char *));
214 curkey = strndup(list[i], slashpos - list[i]);
218 for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) {
219 slashpos = strchr(list[i], '/');
220 if (strcmp(curkey, &slashpos[1]) == 0)
222 curitem = strdup(&slashpos[1]);
225 names[j++] = curitem;
227 np = (const char * const *)names;
229 for (n = 0; n < j; n++)
237 for (n = 0; n < j; n++)
242 __bsd___iconv_free_list(list, sz);
245 __inline const char *
246 __bsd_iconv_canonicalize(const char *name)
249 return (_citrus_iconv_canonicalize(name));
253 __bsd_iconvctl(iconv_t cd, int request, void *argument)
255 struct _citrus_iconv *cv;
256 struct iconv_hooks *hooks;
257 const char *convname;
262 cv = (struct _citrus_iconv *)(void *)cd;
263 hooks = (struct iconv_hooks *)argument;
273 convname = cv->cv_shared->ci_convname;
274 dst = strchr(convname, '/');
275 srclen = dst - convname;
277 *i = (srclen == strlen(dst)) && !memcmp(convname, dst, srclen);
279 case ICONV_GET_TRANSLITERATE:
282 case ICONV_SET_TRANSLITERATE:
283 return ((*i == 1) ? 0 : -1);
284 case ICONV_GET_DISCARD_ILSEQ:
285 *i = cv->cv_shared->ci_discard_ilseq ? 1 : 0;
287 case ICONV_SET_DISCARD_ILSEQ:
288 cv->cv_shared->ci_discard_ilseq = *i;
290 case ICONV_SET_HOOKS:
291 cv->cv_shared->ci_hooks = hooks;
293 case ICONV_SET_FALLBACKS:
296 case ICONV_GET_ILSEQ_INVALID:
297 *i = cv->cv_shared->ci_ilseq_invalid ? 1 : 0;
299 case ICONV_SET_ILSEQ_INVALID:
300 cv->cv_shared->ci_ilseq_invalid = *i;
309 __bsd_iconv_set_relocation_prefix(const char *orig_prefix __unused,
310 const char *curr_prefix __unused)