Merge from vendor branch GCC:
[dragonfly.git] / sys / libiconv / iconv.c
1 /*
2  * Copyright (c) 2000-2001, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/libkern/iconv.c,v 1.1.2.1 2001/05/21 08:28:07 bp Exp $
33  * $DragonFly: src/sys/libiconv/iconv.c,v 1.8 2008/01/05 14:02:38 swildner Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/iconv.h>
39 #include <sys/malloc.h>
40
41 #include "iconv_converter_if.h"
42
43 SYSCTL_DECL(_kern_iconv);
44
45 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
46
47 MALLOC_DEFINE(M_ICONV, "ICONV", "ICONV structures");
48 MALLOC_DEFINE(M_ICONVDATA, "ICONV data", "ICONV data");
49
50 MODULE_VERSION(libiconv, 1);
51
52 #ifdef notnow
53 /*
54  * iconv converter instance
55  */
56 struct iconv_converter {
57         KOBJ_FIELDS;
58         void *                  c_data;
59 };
60 #endif
61
62 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
63
64 /*
65  * List of loaded converters
66  */
67 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
68     iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
69
70 /*
71  * List of supported/loaded charsets pairs
72  */
73 static TAILQ_HEAD(, iconv_cspair)
74     iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
75 static int iconv_csid = 1;
76
77 static char iconv_unicode_string[] = "unicode"; /* save eight bytes when possible */
78
79 static void iconv_unregister_cspair(struct iconv_cspair *csp);
80
81 static int
82 iconv_mod_unload(void)
83 {
84         struct iconv_cspair *csp;
85
86         while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL) {
87                 if (csp->cp_refcount)
88                         return EBUSY;
89                 iconv_unregister_cspair(csp);
90         }
91         return 0;
92 }
93
94 static int
95 iconv_mod_handler(module_t mod, int type, void *data)
96 {
97         int error;
98
99         switch (type) {
100             case MOD_LOAD:
101                 error = 0;
102                 break;
103             case MOD_UNLOAD:
104                 error = iconv_mod_unload();
105                 break;
106             default:
107                 error = EINVAL;
108         }
109         return error;
110 }
111
112 static moduledata_t iconv_mod = {
113         "iconv", iconv_mod_handler, NULL
114 };
115
116 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
117
118 static int
119 iconv_register_converter(struct iconv_converter_class *dcp)
120 {
121         kobj_class_instantiate((kobj_class_t)dcp);
122         TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
123         return 0;
124 }
125
126 static int
127 iconv_unregister_converter(struct iconv_converter_class *dcp)
128 {
129         if (dcp->refs != 1) {
130                 ICDEBUG("converter has %d references left\n", dcp->refs);
131                 return EBUSY;
132         }
133         TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
134         kobj_class_uninstantiate((kobj_class_t)dcp);
135         return 0;
136 }
137
138 static int
139 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
140 {
141         struct iconv_converter_class *dcp;
142
143         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
144                 if (name == NULL)
145                         continue;
146                 if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
147                         if (dcpp)
148                                 *dcpp = dcp;
149                         return 0;
150                 }
151         }
152         return ENOENT;
153 }
154
155 static int
156 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
157 {
158         struct iconv_cspair *csp;
159
160         TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
161                 if (strcmp(csp->cp_to, to) == 0 &&
162                     strcmp(csp->cp_from, from) == 0) {
163                         if (cspp)
164                                 *cspp = csp;
165                         return 0;
166                 }
167         }
168         return ENOENT;
169 }
170
171 static int
172 iconv_register_cspair(const char *to, const char *from,
173         struct iconv_converter_class *dcp, void *data,
174         struct iconv_cspair **cspp)
175 {
176         struct iconv_cspair *csp;
177         char *cp;
178         int csize, ucsto, ucsfrom;
179
180         if (iconv_lookupcs(to, from, NULL) == 0)
181                 return EEXIST;
182         csize = sizeof(*csp);
183         ucsto = strcmp(to, iconv_unicode_string) == 0;
184         if (!ucsto)
185                 csize += strlen(to) + 1;
186         ucsfrom = strcmp(from, iconv_unicode_string) == 0;
187         if (!ucsfrom)
188                 csize += strlen(from) + 1;
189         csp = kmalloc(csize, M_ICONV, M_WAITOK | M_ZERO);
190         csp->cp_id = iconv_csid++;
191         csp->cp_dcp = dcp;
192         cp = (char*)(csp + 1);
193         if (!ucsto) {
194                 strcpy(cp, to);
195                 csp->cp_to = cp;
196                 cp += strlen(cp) + 1;
197         } else
198                 csp->cp_to = iconv_unicode_string;
199         if (!ucsfrom) {
200                 strcpy(cp, from);
201                 csp->cp_from = cp;
202         } else
203                 csp->cp_from = iconv_unicode_string;
204         csp->cp_data = data;
205
206         TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
207         *cspp = csp;
208         return 0;
209 }
210
211 static void
212 iconv_unregister_cspair(struct iconv_cspair *csp)
213 {
214         TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
215         if (csp->cp_data)
216                 kfree(csp->cp_data, M_ICONVDATA);
217         kfree(csp, M_ICONV);
218 }
219
220 /*
221  * Lookup and create an instance of converter.
222  * Currently this layer didn't have associated 'instance' structure
223  * to avoid unnesessary memory allocation.
224  */
225 int
226 iconv_open(const char *to, const char *from, void **handle)
227 {
228         struct iconv_cspair *csp, *cspfrom, *cspto;
229         struct iconv_converter_class *dcp;
230         const char *cnvname;
231         int error;
232
233         /*
234          * First, lookup fully qualified cspairs
235          */
236         error = iconv_lookupcs(to, from, &csp);
237         if (error == 0)
238                 return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
239
240         /*
241          * Well, nothing found. Now try to construct a composite conversion
242          * ToDo: add a 'capability' field to converter
243          */
244         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
245                 cnvname = ICONV_CONVERTER_NAME(dcp);
246                 if (cnvname == NULL)
247                         continue;
248                 error = iconv_lookupcs(cnvname, from, &cspfrom);
249                 if (error)
250                         continue;
251                 error = iconv_lookupcs(to, cnvname, &cspto);
252                 if (error)
253                         continue;
254                 /*
255                  * Fine, we're found a pair which can be combined together
256                  */
257                 return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
258         }
259         return ENOENT;
260 }
261
262 int
263 iconv_close(void *handle)
264 {
265         return ICONV_CONVERTER_CLOSE(handle);
266 }
267
268 int
269 iconv_conv(void *handle, const char **inbuf,
270         size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
271 {
272         return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft);
273 }
274
275 /*
276  * Give a list of loaded converters. Each name terminated with 0.
277  * An empty string terminates the list.
278  */
279 static int
280 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
281 {
282         struct iconv_converter_class *dcp;
283         const char *name;
284         char spc;
285         int error;
286
287         error = 0;
288
289         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
290                 name = ICONV_CONVERTER_NAME(dcp);
291                 if (name == NULL)
292                         continue;
293                 error = SYSCTL_OUT(req, name, strlen(name) + 1);
294                 if (error)
295                         break;
296         }
297         if (error)
298                 return error;
299         spc = 0;
300         error = SYSCTL_OUT(req, &spc, sizeof(spc));
301         return error;
302 }
303
304 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
305             NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
306
307 /*
308  * List all available charset pairs.
309  */
310 static int
311 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
312 {
313         struct iconv_cspair *csp;
314         struct iconv_cspair_info csi;
315         int error;
316
317         error = 0;
318         bzero(&csi, sizeof(csi));
319         csi.cs_version = ICONV_CSPAIR_INFO_VER;
320
321         TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
322                 csi.cs_id = csp->cp_id;
323                 csi.cs_refcount = csp->cp_refcount;
324                 csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
325                 strcpy(csi.cs_to, csp->cp_to);
326                 strcpy(csi.cs_from, csp->cp_from);
327                 error = SYSCTL_OUT(req, &csi, sizeof(csi));
328                 if (error)
329                         break;
330         }
331         return error;
332 }
333
334 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
335             NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
336
337 /*
338  * Add new charset pair
339  */
340 static int
341 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
342 {
343         struct iconv_converter_class *dcp;
344         struct iconv_cspair *csp;
345         struct iconv_add_in din;
346         struct iconv_add_out dout;
347         int error;
348
349         error = SYSCTL_IN(req, &din, sizeof(din));
350         if (error)
351                 return error;
352         if (din.ia_version != ICONV_ADD_VER)
353                 return EINVAL;
354         if (din.ia_datalen > ICONV_CSMAXDATALEN)
355                 return EINVAL;
356
357         /*
358          * Make sure all user-supplied strings are terminated before
359          * proceeding.
360          */
361         din.ia_converter[ICONV_CNVNMAXLEN-1] = 0;
362         din.ia_to[ICONV_CSNMAXLEN-1] = 0;
363         din.ia_from[ICONV_CSNMAXLEN-1] = 0;
364
365         if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
366                 return EINVAL;
367         error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
368         if (error)
369                 return error;
370         if (din.ia_datalen) {
371                 csp->cp_data = kmalloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
372                 error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
373                 if (error)
374                         goto bad;
375         }
376         dout.ia_csid = csp->cp_id;
377         error = SYSCTL_OUT(req, &dout, sizeof(dout));
378         if (error)
379                 goto bad;
380         return 0;
381 bad:
382         iconv_unregister_cspair(csp);
383         return error;
384 }
385
386 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
387             NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
388
389 /*
390  * Default stubs for converters
391  */
392 int
393 iconv_converter_initstub(struct iconv_converter_class *dp)
394 {
395         return 0;
396 }
397
398 int
399 iconv_converter_donestub(struct iconv_converter_class *dp)
400 {
401         return 0;
402 }
403
404 int
405 iconv_converter_handler(module_t mod, int type, void *data)
406 {
407         struct iconv_converter_class *dcp = data;
408         int error;
409
410         switch (type) {
411             case MOD_LOAD:
412                 error = iconv_register_converter(dcp);
413                 if (error)
414                         break;
415                 error = ICONV_CONVERTER_INIT(dcp);
416                 if (error)
417                         iconv_unregister_converter(dcp);
418                 break;
419             case MOD_UNLOAD:
420                 ICONV_CONVERTER_DONE(dcp);
421                 error = iconv_unregister_converter(dcp);
422                 break;
423             default:
424                 error = EINVAL;
425         }
426         return error;
427 }
428
429 /*
430  * Common used functions
431  */
432 char *
433 iconv_convstr(void *handle, char *dst, const char *src)
434 {
435         char *p = dst;
436         int inlen, outlen, error;
437
438         if (handle == NULL) {
439                 strcpy(dst, src);
440                 return dst;
441         }
442         inlen = outlen = strlen(src);
443         error = iconv_conv(handle, NULL, NULL, &p, &outlen);
444         if (error)
445                 return NULL;
446         error = iconv_conv(handle, &src, &inlen, &p, &outlen);
447         if (error)
448                 return NULL;
449         *p = 0;
450         return dst;
451 }
452
453 void *
454 iconv_convmem(void *handle, void *dst, const void *src, int size)
455 {
456         const char *s = src;
457         char *d = dst;
458         int inlen, outlen, error;
459
460         if (size == 0)
461                 return dst;
462         if (handle == NULL) {
463                 memcpy(dst, src, size);
464                 return dst;
465         }
466         inlen = outlen = size;
467         error = iconv_conv(handle, NULL, NULL, &d, &outlen);
468         if (error)
469                 return NULL;
470         error = iconv_conv(handle, &s, &inlen, &d, &outlen);
471         if (error)
472                 return NULL;
473         return dst;
474 }
475
476 int
477 iconv_lookupcp(char **cpp, const char *s)
478 {
479         if (cpp == NULL) {
480                 ICDEBUG("warning a NULL list passed\n");
481                 return ENOENT;
482         }
483         for (; *cpp; cpp++)
484                 if (strcmp(*cpp, s) == 0)
485                         return 0;
486         return ENOENT;
487 }