binutils214 stage 2/4.
[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.2 2003/06/17 04:28:42 dillon 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_compile((struct kobj_class*)dcp);
122         dcp->refs++;
123         TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
124         return 0;
125 }
126
127 static int
128 iconv_unregister_converter(struct iconv_converter_class *dcp)
129 {
130         if (dcp->refs > 1) {
131                 ICDEBUG("converter have %d referenses left\n", dcp->refs);
132                 return EBUSY;
133         }
134         TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
135         kobj_class_free((struct kobj_class*)dcp);
136         return 0;
137 }
138
139 static int
140 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
141 {
142         struct iconv_converter_class *dcp;
143
144         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
145                 if (name == NULL)
146                         continue;
147                 if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
148                         if (dcpp)
149                                 *dcpp = dcp;
150                         return 0;
151                 }
152         }
153         return ENOENT;
154 }
155
156 static int
157 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
158 {
159         struct iconv_cspair *csp;
160
161         TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
162                 if (strcmp(csp->cp_to, to) == 0 &&
163                     strcmp(csp->cp_from, from) == 0) {
164                         if (cspp)
165                                 *cspp = csp;
166                         return 0;
167                 }
168         }
169         return ENOENT;
170 }
171
172 static int
173 iconv_register_cspair(const char *to, const char *from,
174         struct iconv_converter_class *dcp, void *data,
175         struct iconv_cspair **cspp)
176 {
177         struct iconv_cspair *csp;
178         char *cp;
179         int csize, ucsto, ucsfrom;
180
181         if (iconv_lookupcs(to, from, NULL) == 0)
182                 return EEXIST;
183         csize = sizeof(*csp);
184         ucsto = strcmp(to, iconv_unicode_string) == 0;
185         if (!ucsto)
186                 csize += strlen(to) + 1;
187         ucsfrom = strcmp(from, iconv_unicode_string) == 0;
188         if (!ucsfrom)
189                 csize += strlen(from) + 1;
190         csp = malloc(csize, M_ICONV, M_WAITOK);
191         bzero(csp, csize);
192         csp->cp_id = iconv_csid++;
193         csp->cp_dcp = dcp;
194         cp = (char*)(csp + 1);
195         if (!ucsto) {
196                 strcpy(cp, to);
197                 csp->cp_to = cp;
198                 cp += strlen(cp) + 1;
199         } else
200                 csp->cp_to = iconv_unicode_string;
201         if (!ucsfrom) {
202                 strcpy(cp, from);
203                 csp->cp_from = cp;
204         } else
205                 csp->cp_from = iconv_unicode_string;
206         csp->cp_data = data;
207
208         TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
209         *cspp = csp;
210         return 0;
211 }
212
213 static void
214 iconv_unregister_cspair(struct iconv_cspair *csp)
215 {
216         TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
217         if (csp->cp_data)
218                 free(csp->cp_data, M_ICONVDATA);
219         free(csp, M_ICONV);
220 }
221
222 /*
223  * Lookup and create an instance of converter.
224  * Currently this layer didn't have associated 'instance' structure
225  * to avoid unnesessary memory allocation.
226  */
227 int
228 iconv_open(const char *to, const char *from, void **handle)
229 {
230         struct iconv_cspair *csp, *cspfrom, *cspto;
231         struct iconv_converter_class *dcp;
232         const char *cnvname;
233         int error;
234
235         /*
236          * First, lookup fully qualified cspairs
237          */
238         error = iconv_lookupcs(to, from, &csp);
239         if (error == 0)
240                 return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
241
242         /*
243          * Well, nothing found. Now try to construct a composite conversion
244          * ToDo: add a 'capability' field to converter
245          */
246         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
247                 cnvname = ICONV_CONVERTER_NAME(dcp);
248                 if (cnvname == NULL)
249                         continue;
250                 error = iconv_lookupcs(cnvname, from, &cspfrom);
251                 if (error)
252                         continue;
253                 error = iconv_lookupcs(to, cnvname, &cspto);
254                 if (error)
255                         continue;
256                 /*
257                  * Fine, we're found a pair which can be combined together
258                  */
259                 return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
260         }
261         return ENOENT;
262 }
263
264 int
265 iconv_close(void *handle)
266 {
267         return ICONV_CONVERTER_CLOSE(handle);
268 }
269
270 int
271 iconv_conv(void *handle, const char **inbuf,
272         size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
273 {
274         return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft);
275 }
276
277 /*
278  * Give a list of loaded converters. Each name terminated with 0.
279  * An empty string terminates the list.
280  */
281 static int
282 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
283 {
284         struct iconv_converter_class *dcp;
285         const char *name;
286         char spc;
287         int error;
288
289         error = 0;
290
291         TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
292                 name = ICONV_CONVERTER_NAME(dcp);
293                 if (name == NULL)
294                         continue;
295                 error = SYSCTL_OUT(req, name, strlen(name) + 1);
296                 if (error)
297                         break;
298         }
299         if (error)
300                 return error;
301         spc = 0;
302         error = SYSCTL_OUT(req, &spc, sizeof(spc));
303         return error;
304 }
305
306 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
307             NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
308
309 /*
310  * List all available charset pairs.
311  */
312 static int
313 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
314 {
315         struct iconv_cspair *csp;
316         struct iconv_cspair_info csi;
317         int error;
318
319         error = 0;
320         bzero(&csi, sizeof(csi));
321         csi.cs_version = ICONV_CSPAIR_INFO_VER;
322
323         TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
324                 csi.cs_id = csp->cp_id;
325                 csi.cs_refcount = csp->cp_refcount;
326                 csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
327                 strcpy(csi.cs_to, csp->cp_to);
328                 strcpy(csi.cs_from, csp->cp_from);
329                 error = SYSCTL_OUT(req, &csi, sizeof(csi));
330                 if (error)
331                         break;
332         }
333         return error;
334 }
335
336 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
337             NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
338
339 /*
340  * Add new charset pair
341  */
342 static int
343 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
344 {
345         struct iconv_converter_class *dcp;
346         struct iconv_cspair *csp;
347         struct iconv_add_in din;
348         struct iconv_add_out dout;
349         int error;
350
351         error = SYSCTL_IN(req, &din, sizeof(din));
352         if (error)
353                 return error;
354         if (din.ia_version != ICONV_ADD_VER)
355                 return EINVAL;
356         if (din.ia_datalen > ICONV_CSMAXDATALEN)
357                 return EINVAL;
358         if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
359                 return EINVAL;
360         error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
361         if (error)
362                 return error;
363         if (din.ia_datalen) {
364                 csp->cp_data = malloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
365                 error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
366                 if (error)
367                         goto bad;
368         }
369         dout.ia_csid = csp->cp_id;
370         error = SYSCTL_OUT(req, &dout, sizeof(dout));
371         if (error)
372                 goto bad;
373         return 0;
374 bad:
375         iconv_unregister_cspair(csp);
376         return error;
377 }
378
379 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
380             NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
381
382 /*
383  * Default stubs for converters
384  */
385 int
386 iconv_converter_initstub(struct iconv_converter_class *dp)
387 {
388         return 0;
389 }
390
391 int
392 iconv_converter_donestub(struct iconv_converter_class *dp)
393 {
394         return 0;
395 }
396
397 int
398 iconv_converter_handler(module_t mod, int type, void *data)
399 {
400         struct iconv_converter_class *dcp = data;
401         int error;
402
403         switch (type) {
404             case MOD_LOAD:
405                 error = iconv_register_converter(dcp);
406                 if (error)
407                         break;
408                 error = ICONV_CONVERTER_INIT(dcp);
409                 if (error)
410                         iconv_unregister_converter(dcp);
411                 break;
412             case MOD_UNLOAD:
413                 ICONV_CONVERTER_DONE(dcp);
414                 error = iconv_unregister_converter(dcp);
415                 break;
416             default:
417                 error = EINVAL;
418         }
419         return error;
420 }
421
422 /*
423  * Common used functions
424  */
425 char *
426 iconv_convstr(void *handle, char *dst, const char *src)
427 {
428         char *p = dst;
429         int inlen, outlen, error;
430
431         if (handle == NULL) {
432                 strcpy(dst, src);
433                 return dst;
434         }
435         inlen = outlen = strlen(src);
436         error = iconv_conv(handle, NULL, NULL, &p, &outlen);
437         if (error)
438                 return NULL;
439         error = iconv_conv(handle, &src, &inlen, &p, &outlen);
440         if (error)
441                 return NULL;
442         *p = 0;
443         return dst;
444 }
445
446 void *
447 iconv_convmem(void *handle, void *dst, const void *src, int size)
448 {
449         const char *s = src;
450         char *d = dst;
451         int inlen, outlen, error;
452
453         if (size == 0)
454                 return dst;
455         if (handle == NULL) {
456                 memcpy(dst, src, size);
457                 return dst;
458         }
459         inlen = outlen = size;
460         error = iconv_conv(handle, NULL, NULL, &d, &outlen);
461         if (error)
462                 return NULL;
463         error = iconv_conv(handle, &s, &inlen, &d, &outlen);
464         if (error)
465                 return NULL;
466         return dst;
467 }
468
469 int
470 iconv_lookupcp(char **cpp, const char *s)
471 {
472         if (cpp == NULL) {
473                 ICDEBUG("warning a NULL list passed\n");
474                 return ENOENT;
475         }
476         for (; *cpp; cpp++)
477                 if (strcmp(*cpp, s) == 0)
478                         return 0;
479         return ENOENT;
480 }