1 /* $FreeBSD: head/lib/libc/iconv/iconv.c 254273 2013-08-13 07:15:01Z peter $ */
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/citrus_types.h"
45 #include "../citrus/citrus_module.h"
46 #include "../citrus/citrus_esdb.h"
47 #include "../citrus/citrus_hash.h"
48 #include "../citrus/citrus_iconv.h"
50 #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1)
52 int _iconv_version = _ICONV_VERSION;
54 iconv_t _iconv_open(const char *out, const char *in,
55 struct _citrus_iconv *prealloc);
58 _iconv_open(const char *out, const char *in, struct _citrus_iconv *handle)
60 const char *out_slashes;
65 * Remove anything following a //, as these are options (like
66 * //ignore, //translate, etc) and we just don't handle them.
67 * This is for compatibility with software that uses these
70 out_slashes = strstr(out, "//");
71 if (out_slashes != NULL) {
72 out_noslashes = strndup(out, out_slashes - out);
73 if (out_noslashes == NULL) {
77 ret = _citrus_iconv_open(&handle, in, out_noslashes);
80 ret = _citrus_iconv_open(&handle, in, out);
84 errno = ret == ENOENT ? EINVAL : ret;
88 handle->cv_shared->ci_discard_ilseq = strcasestr(out, "//IGNORE");
89 handle->cv_shared->ci_ilseq_invalid = false;
90 handle->cv_shared->ci_hooks = NULL;
92 return ((iconv_t)(void *)handle);
96 iconv_open(const char *out, const char *in)
99 return (_iconv_open(out, in, NULL));
103 iconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr)
105 struct _citrus_iconv *handle;
107 handle = (struct _citrus_iconv *)ptr;
108 return ((_iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0);
112 iconv_close(iconv_t handle)
115 if (ISBADF(handle)) {
120 _citrus_iconv_close((struct _citrus_iconv *)(void *)handle);
126 iconv(iconv_t handle, const char **in, size_t *szin, char **out, size_t *szout)
131 if (ISBADF(handle)) {
136 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
137 in, szin, out, szout, 0, &ret);
147 __iconv(iconv_t handle, const char **in, size_t *szin, char **out,
148 size_t *szout, uint32_t flags, size_t *invalids)
153 if (ISBADF(handle)) {
158 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
159 in, szin, out, szout, flags, &ret);
171 __iconv_get_list(char ***rlist, size_t *rsz, bool sorted)
175 ret = _citrus_esdb_get_list(rlist, rsz, sorted);
185 __iconv_free_list(char **list, size_t sz)
188 _citrus_esdb_free_list(list, sz);
192 * GNU-compatibile non-standard interfaces.
195 qsort_helper(const void *first, const void *second)
197 const char * const *s1;
198 const char * const *s2;
202 return (strcmp(*s1, *s2));
206 iconvlist(int (*do_one) (unsigned int, const char * const *,
209 char **list, **names;
210 const char * const *np;
211 char *curitem, *curkey, *slashpos;
217 if (__iconv_get_list(&list, &sz, true))
219 qsort((void *)list, sz, sizeof(char *), qsort_helper);
222 slashpos = strchr(list[i], '/');
223 curkey = (char *)malloc(slashpos - list[i] + 2);
224 names = (char **)malloc(sz * sizeof(char *));
225 if ((curkey == NULL) || (names == NULL)) {
226 __iconv_free_list(list, sz);
229 strlcpy(curkey, list[i], slashpos - list[i] + 1);
231 for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) {
232 slashpos = strchr(list[i], '/');
233 curitem = (char *)malloc(strlen(slashpos) + 1);
234 if (curitem == NULL) {
235 __iconv_free_list(list, sz);
238 strlcpy(curitem, &slashpos[1], strlen(slashpos) + 1);
239 if (strcmp(curkey, curitem) == 0) {
242 names[j++] = curitem;
244 np = (const char * const *)names;
249 __iconv_free_list(list, sz);
253 *iconv_canonicalize(const char *name)
256 return (_citrus_iconv_canonicalize(name));
260 iconvctl(iconv_t cd, int request, void *argument)
262 struct _citrus_iconv *cv;
263 struct iconv_hooks *hooks;
264 const char *convname;
265 char src[PATH_MAX], *dst;
268 cv = (struct _citrus_iconv *)(void *)cd;
269 hooks = (struct iconv_hooks *)argument;
279 convname = cv->cv_shared->ci_convname;
280 dst = strchr(convname, '/');
282 strlcpy(src, convname, dst - convname + 1);
284 if ((convname == NULL) || (src == NULL) || (dst == NULL))
286 *i = strcmp(src, dst) == 0 ? 1 : 0;
288 case ICONV_GET_TRANSLITERATE:
291 case ICONV_SET_TRANSLITERATE:
292 return ((*i == 1) ? 0 : -1);
293 case ICONV_GET_DISCARD_ILSEQ:
294 *i = cv->cv_shared->ci_discard_ilseq ? 1 : 0;
296 case ICONV_SET_DISCARD_ILSEQ:
297 cv->cv_shared->ci_discard_ilseq = *i;
299 case ICONV_SET_HOOKS:
300 cv->cv_shared->ci_hooks = hooks;
302 case ICONV_SET_FALLBACKS:
305 case ICONV_GET_ILSEQ_INVALID:
306 *i = cv->cv_shared->ci_ilseq_invalid ? 1 : 0;
308 case ICONV_SET_ILSEQ_INVALID:
309 cv->cv_shared->ci_ilseq_invalid = *i;
318 iconv_set_relocation_prefix(const char *orig_prefix __unused,
319 const char *curr_prefix __unused)