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 "iconv-internal.h"
52 #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1)
55 __bsd___iconv_open(const char *out, const char *in, struct _citrus_iconv *handle)
57 const char *out_slashes;
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 out_slashes = strstr(out, "//");
68 if (out_slashes != NULL) {
69 out_noslashes = strndup(out, out_slashes - out);
70 if (out_noslashes == NULL) {
74 ret = _citrus_iconv_open(&handle, in, out_noslashes);
77 ret = _citrus_iconv_open(&handle, in, out);
81 errno = ret == ENOENT ? EINVAL : ret;
85 handle->cv_shared->ci_discard_ilseq = strcasestr(out, "//IGNORE");
86 handle->cv_shared->ci_ilseq_invalid = false;
87 handle->cv_shared->ci_hooks = NULL;
89 return ((iconv_t)(void *)handle);
93 __bsd_iconv_open(const char *out, const char *in)
96 return (__bsd___iconv_open(out, in, NULL));
100 __bsd_iconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr)
102 struct _citrus_iconv *handle;
104 handle = (struct _citrus_iconv *)ptr;
105 return ((__bsd___iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0);
109 __bsd_iconv_close(iconv_t handle)
112 if (ISBADF(handle)) {
117 _citrus_iconv_close((struct _citrus_iconv *)(void *)handle);
123 __bsd_iconv(iconv_t handle, char **in, size_t *szin, char **out, size_t *szout)
128 if (ISBADF(handle)) {
133 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
134 in, szin, out, szout, 0, &ret);
144 __bsd___iconv(iconv_t handle, char **in, size_t *szin, char **out,
145 size_t *szout, uint32_t flags, size_t *invalids)
150 if (ISBADF(handle)) {
155 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
156 in, szin, out, szout, flags, &ret);
168 __bsd___iconv_get_list(char ***rlist, size_t *rsz, bool sorted)
172 ret = _citrus_esdb_get_list(rlist, rsz, sorted);
182 __bsd___iconv_free_list(char **list, size_t sz)
185 _citrus_esdb_free_list(list, sz);
189 * GNU-compatibile non-standard interfaces.
192 qsort_helper(const void *first, const void *second)
194 const char * const *s1;
195 const char * const *s2;
199 return (strcmp(*s1, *s2));
203 __bsd_iconvlist(int (*do_one) (unsigned int, const char * const *,
206 char **list, **names;
207 const char * const *np;
208 char *curitem, *curkey, *slashpos;
214 if (__bsd___iconv_get_list(&list, &sz, true))
216 qsort((void *)list, sz, sizeof(char *), qsort_helper);
219 slashpos = strchr(list[i], '/');
220 curkey = (char *)malloc(slashpos - list[i] + 2);
221 names = (char **)malloc(sz * sizeof(char *));
222 if ((curkey == NULL) || (names == NULL)) {
223 __bsd___iconv_free_list(list, sz);
226 strlcpy(curkey, list[i], slashpos - list[i] + 1);
228 for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) {
229 slashpos = strchr(list[i], '/');
230 curitem = (char *)malloc(strlen(slashpos) + 1);
231 if (curitem == NULL) {
232 __bsd___iconv_free_list(list, sz);
235 strlcpy(curitem, &slashpos[1], strlen(slashpos) + 1);
236 if (strcmp(curkey, curitem) == 0) {
239 names[j++] = curitem;
241 np = (const char * const *)names;
246 __bsd___iconv_free_list(list, sz);
249 __inline const char *
250 __bsd_iconv_canonicalize(const char *name)
253 return (_citrus_iconv_canonicalize(name));
257 __bsd_iconvctl(iconv_t cd, int request, void *argument)
259 struct _citrus_iconv *cv;
260 struct iconv_hooks *hooks;
261 const char *convname;
262 char src[PATH_MAX], *dst;
265 cv = (struct _citrus_iconv *)(void *)cd;
266 hooks = (struct iconv_hooks *)argument;
276 convname = cv->cv_shared->ci_convname;
277 dst = strchr(convname, '/');
279 strlcpy(src, convname, dst - convname + 1);
281 if ((convname == NULL) || (src == NULL) || (dst == NULL))
283 *i = strcmp(src, dst) == 0 ? 1 : 0;
285 case ICONV_GET_TRANSLITERATE:
288 case ICONV_SET_TRANSLITERATE:
289 return ((*i == 1) ? 0 : -1);
290 case ICONV_GET_DISCARD_ILSEQ:
291 *i = cv->cv_shared->ci_discard_ilseq ? 1 : 0;
293 case ICONV_SET_DISCARD_ILSEQ:
294 cv->cv_shared->ci_discard_ilseq = *i;
296 case ICONV_SET_HOOKS:
297 cv->cv_shared->ci_hooks = hooks;
299 case ICONV_SET_FALLBACKS:
302 case ICONV_GET_ILSEQ_INVALID:
303 *i = cv->cv_shared->ci_ilseq_invalid ? 1 : 0;
305 case ICONV_SET_ILSEQ_INVALID:
306 cv->cv_shared->ci_ilseq_invalid = *i;
315 __bsd_iconv_set_relocation_prefix(const char *orig_prefix __unused,
316 const char *curr_prefix __unused)