1/* $FreeBSD$ */
2/* $NetBSD: iconv.c,v 1.11 2009/03/03 16:22:33 explorer Exp $ */
3
4/*-
5 * Copyright (c) 2003 Citrus Project,
6 * Copyright (c) 2009, 2010 Gabor Kovesdan <gabor@FreeBSD.org>,
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32#include <sys/queue.h>
33#include <sys/types.h>
34
35#include <assert.h>
36#include <errno.h>
37#include <iconv.h>
38#include <limits.h>
39#include <paths.h>
40#include <stdbool.h>
41#include <stdlib.h>
42#include <string.h>
43
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"
49
50#include "iconv-internal.h"
51
52#define ISBADF(_h_)	(!(_h_) || (_h_) == (iconv_t)-1)
53
54static iconv_t
55__bsd___iconv_open(const char *out, const char *in, struct _citrus_iconv *handle)
56{
57	const char *out_slashes;
58	char *out_noslashes;
59	int ret;
60
61	/*
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
65	 * blindly.
66	 */
67	out_slashes = strstr(out, "//");
68	if (out_slashes != NULL) {
69		out_noslashes = strndup(out, out_slashes - out);
70		if (out_noslashes == NULL) {
71			errno = ENOMEM;
72			return ((iconv_t)-1);
73		}
74		ret = _citrus_iconv_open(&handle, in, out_noslashes);
75		free(out_noslashes);
76	} else {
77		ret = _citrus_iconv_open(&handle, in, out);
78	}
79
80	if (ret) {
81		errno = ret == ENOENT ? EINVAL : ret;
82		return ((iconv_t)-1);
83	}
84
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;
88
89	return ((iconv_t)(void *)handle);
90}
91
92iconv_t
93__bsd_iconv_open(const char *out, const char *in)
94{
95
96	return (__bsd___iconv_open(out, in, NULL));
97}
98
99int
100__bsd_iconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr)
101{
102	struct _citrus_iconv *handle;
103
104	handle = (struct _citrus_iconv *)ptr;
105	return ((__bsd___iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0);
106}
107
108int
109__bsd_iconv_close(iconv_t handle)
110{
111
112	if (ISBADF(handle)) {
113		errno = EBADF;
114		return (-1);
115	}
116
117	_citrus_iconv_close((struct _citrus_iconv *)(void *)handle);
118
119	return (0);
120}
121
122size_t
123__bsd_iconv(iconv_t handle, char **in, size_t *szin, char **out, size_t *szout)
124{
125	size_t ret;
126	int err;
127
128	if (ISBADF(handle)) {
129		errno = EBADF;
130		return ((size_t)-1);
131	}
132
133	err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
134	    in, szin, out, szout, 0, &ret);
135	if (err) {
136		errno = err;
137		ret = (size_t)-1;
138	}
139
140	return (ret);
141}
142
143size_t
144__bsd___iconv(iconv_t handle, char **in, size_t *szin, char **out,
145    size_t *szout, uint32_t flags, size_t *invalids)
146{
147	size_t ret;
148	int err;
149
150	if (ISBADF(handle)) {
151		errno = EBADF;
152		return ((size_t)-1);
153	}
154
155	err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle,
156	    in, szin, out, szout, flags, &ret);
157	if (invalids)
158		*invalids = ret;
159	if (err) {
160		errno = err;
161		ret = (size_t)-1;
162	}
163
164	return (ret);
165}
166
167int
168__bsd___iconv_get_list(char ***rlist, size_t *rsz, bool sorted)
169{
170	int ret;
171
172	ret = _citrus_esdb_get_list(rlist, rsz, sorted);
173	if (ret) {
174		errno = ret;
175		return (-1);
176	}
177
178	return (0);
179}
180
181void
182__bsd___iconv_free_list(char **list, size_t sz)
183{
184
185	_citrus_esdb_free_list(list, sz);
186}
187
188/*
189 * GNU-compatibile non-standard interfaces.
190 */
191static int
192qsort_helper(const void *first, const void *second)
193{
194	const char * const *s1;
195	const char * const *s2;
196
197	s1 = first;
198	s2 = second;
199	return (strcmp(*s1, *s2));
200}
201
202void
203__bsd_iconvlist(int (*do_one) (unsigned int, const char * const *,
204    void *), void *data)
205{
206	char **list, **names;
207	const char * const *np;
208	char *curitem, *curkey, *slashpos;
209	size_t sz;
210	unsigned int i, j, n;
211
212	i = 0;
213	names = NULL;
214
215	if (__bsd___iconv_get_list(&list, &sz, true)) {
216		list = NULL;
217		goto out;
218	}
219	qsort((void *)list, sz, sizeof(char *), qsort_helper);
220	while (i < sz) {
221		j = 0;
222		slashpos = strchr(list[i], '/');
223		names = malloc(sz * sizeof(char *));
224		if (names == NULL)
225			goto out;
226		curkey = strndup(list[i], slashpos - list[i]);
227		if (curkey == NULL)
228			goto out;
229		names[j++] = curkey;
230		for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) {
231			slashpos = strchr(list[i], '/');
232			if (strcmp(curkey, &slashpos[1]) == 0)
233				continue;
234			curitem = strdup(&slashpos[1]);
235			if (curitem == NULL)
236				goto out;
237			names[j++] = curitem;
238		}
239		np = (const char * const *)names;
240		do_one(j, np, data);
241		for (n = 0; n < j; n++)
242			free(names[n]);
243		free(names);
244		names = NULL;
245	}
246
247out:
248	if (names != NULL) {
249		for (n = 0; n < j; n++)
250			free(names[n]);
251		free(names);
252	}
253	if (list != NULL)
254		__bsd___iconv_free_list(list, sz);
255}
256
257__inline const char *
258__bsd_iconv_canonicalize(const char *name)
259{
260
261	return (_citrus_iconv_canonicalize(name));
262}
263
264int
265__bsd_iconvctl(iconv_t cd, int request, void *argument)
266{
267	struct _citrus_iconv *cv;
268	struct iconv_hooks *hooks;
269	const char *convname;
270	char *dst;
271	int *i;
272	size_t srclen;
273
274	cv = (struct _citrus_iconv *)(void *)cd;
275	hooks = (struct iconv_hooks *)argument;
276	i = (int *)argument;
277
278	if (ISBADF(cd)) {
279		errno = EBADF;
280		return (-1);
281	}
282
283	switch (request) {
284	case ICONV_TRIVIALP:
285		convname = cv->cv_shared->ci_convname;
286		dst = strchr(convname, '/');
287		srclen = dst - convname;
288		dst++;
289		*i = (srclen == strlen(dst)) && !memcmp(convname, dst, srclen);
290		return (0);
291	case ICONV_GET_TRANSLITERATE:
292		*i = 1;
293		return (0);
294	case ICONV_SET_TRANSLITERATE:
295		return  ((*i == 1) ? 0 : -1);
296	case ICONV_GET_DISCARD_ILSEQ:
297		*i = cv->cv_shared->ci_discard_ilseq ? 1 : 0;
298		return (0);
299	case ICONV_SET_DISCARD_ILSEQ:
300		cv->cv_shared->ci_discard_ilseq = *i;
301		return (0);
302	case ICONV_SET_HOOKS:
303		cv->cv_shared->ci_hooks = hooks;
304		return (0);
305	case ICONV_SET_FALLBACKS:
306		errno = EOPNOTSUPP;
307		return (-1);
308	case ICONV_GET_ILSEQ_INVALID:
309		*i = cv->cv_shared->ci_ilseq_invalid ? 1 : 0;
310		return (0);
311	case ICONV_SET_ILSEQ_INVALID:
312		cv->cv_shared->ci_ilseq_invalid = *i;
313		return (0);
314	default:
315		errno = EINVAL;
316		return (-1);
317	}
318}
319
320void
321__bsd_iconv_set_relocation_prefix(const char *orig_prefix __unused,
322    const char *curr_prefix __unused)
323{
324
325}
326