FreeBSD/Linux Kernel Cross Reference
sys/libkern/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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/8.2/sys/libkern/iconv.c 199583 2009-11-20 15:27:52Z jhb $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/iconv.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/sx.h>
43 #include <sys/syslog.h>
44
45 #include "iconv_converter_if.h"
46
47 SYSCTL_DECL(_kern_iconv);
48
49 SYSCTL_NODE(_kern, OID_AUTO, iconv, CTLFLAG_RW, NULL, "kernel iconv interface");
50
51 MALLOC_DEFINE(M_ICONV, "iconv", "ICONV structures");
52 MALLOC_DEFINE(M_ICONVDATA, "iconv_data", "ICONV data");
53
54 MODULE_VERSION(libiconv, 2);
55
56 static struct sx iconv_lock;
57
58 #ifdef notnow
59 /*
60 * iconv converter instance
61 */
62 struct iconv_converter {
63 KOBJ_FIELDS;
64 void * c_data;
65 };
66 #endif
67
68 struct sysctl_oid *iconv_oid_hook = &sysctl___kern_iconv;
69
70 /*
71 * List of loaded converters
72 */
73 static TAILQ_HEAD(iconv_converter_list, iconv_converter_class)
74 iconv_converters = TAILQ_HEAD_INITIALIZER(iconv_converters);
75
76 /*
77 * List of supported/loaded charsets pairs
78 */
79 static TAILQ_HEAD(, iconv_cspair)
80 iconv_cslist = TAILQ_HEAD_INITIALIZER(iconv_cslist);
81 static int iconv_csid = 1;
82
83 static char iconv_unicode_string[] = "unicode"; /* save eight bytes when possible */
84
85 static void iconv_unregister_cspair(struct iconv_cspair *csp);
86
87 static int
88 iconv_mod_unload(void)
89 {
90 struct iconv_cspair *csp;
91
92 sx_xlock(&iconv_lock);
93 while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL) {
94 if (csp->cp_refcount)
95 return EBUSY;
96 }
97
98 while ((csp = TAILQ_FIRST(&iconv_cslist)) != NULL)
99 iconv_unregister_cspair(csp);
100 sx_xunlock(&iconv_lock);
101 sx_destroy(&iconv_lock);
102 return 0;
103 }
104
105 static int
106 iconv_mod_handler(module_t mod, int type, void *data)
107 {
108 int error;
109
110 switch (type) {
111 case MOD_LOAD:
112 error = 0;
113 sx_init(&iconv_lock, "iconv");
114 break;
115 case MOD_UNLOAD:
116 error = iconv_mod_unload();
117 break;
118 default:
119 error = EINVAL;
120 }
121 return error;
122 }
123
124 static moduledata_t iconv_mod = {
125 "iconv", iconv_mod_handler, NULL
126 };
127
128 DECLARE_MODULE(iconv, iconv_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
129
130 static int
131 iconv_register_converter(struct iconv_converter_class *dcp)
132 {
133 kobj_class_compile((struct kobj_class*)dcp);
134 dcp->refs++;
135 TAILQ_INSERT_TAIL(&iconv_converters, dcp, cc_link);
136 return 0;
137 }
138
139 static int
140 iconv_unregister_converter(struct iconv_converter_class *dcp)
141 {
142 if (dcp->refs > 1) {
143 ICDEBUG("converter have %d referenses left\n", dcp->refs);
144 return EBUSY;
145 }
146 TAILQ_REMOVE(&iconv_converters, dcp, cc_link);
147 kobj_class_free((struct kobj_class*)dcp);
148 return 0;
149 }
150
151 static int
152 iconv_lookupconv(const char *name, struct iconv_converter_class **dcpp)
153 {
154 struct iconv_converter_class *dcp;
155
156 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
157 if (name == NULL)
158 continue;
159 if (strcmp(name, ICONV_CONVERTER_NAME(dcp)) == 0) {
160 if (dcpp)
161 *dcpp = dcp;
162 return 0;
163 }
164 }
165 return ENOENT;
166 }
167
168 static int
169 iconv_lookupcs(const char *to, const char *from, struct iconv_cspair **cspp)
170 {
171 struct iconv_cspair *csp;
172
173 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
174 if (strcmp(csp->cp_to, to) == 0 &&
175 strcmp(csp->cp_from, from) == 0) {
176 if (cspp)
177 *cspp = csp;
178 return 0;
179 }
180 }
181 return ENOENT;
182 }
183
184 static int
185 iconv_register_cspair(const char *to, const char *from,
186 struct iconv_converter_class *dcp, void *data,
187 struct iconv_cspair **cspp)
188 {
189 struct iconv_cspair *csp;
190 char *cp;
191 int csize, ucsto, ucsfrom;
192
193 if (iconv_lookupcs(to, from, NULL) == 0)
194 return EEXIST;
195 csize = sizeof(*csp);
196 ucsto = strcmp(to, iconv_unicode_string) == 0;
197 if (!ucsto)
198 csize += strlen(to) + 1;
199 ucsfrom = strcmp(from, iconv_unicode_string) == 0;
200 if (!ucsfrom)
201 csize += strlen(from) + 1;
202 csp = malloc(csize, M_ICONV, M_WAITOK);
203 bzero(csp, csize);
204 csp->cp_id = iconv_csid++;
205 csp->cp_dcp = dcp;
206 cp = (char*)(csp + 1);
207 if (!ucsto) {
208 strcpy(cp, to);
209 csp->cp_to = cp;
210 cp += strlen(cp) + 1;
211 } else
212 csp->cp_to = iconv_unicode_string;
213 if (!ucsfrom) {
214 strcpy(cp, from);
215 csp->cp_from = cp;
216 } else
217 csp->cp_from = iconv_unicode_string;
218 csp->cp_data = data;
219
220 TAILQ_INSERT_TAIL(&iconv_cslist, csp, cp_link);
221 *cspp = csp;
222 return 0;
223 }
224
225 static void
226 iconv_unregister_cspair(struct iconv_cspair *csp)
227 {
228 TAILQ_REMOVE(&iconv_cslist, csp, cp_link);
229 if (csp->cp_data)
230 free(csp->cp_data, M_ICONVDATA);
231 free(csp, M_ICONV);
232 }
233
234 /*
235 * Lookup and create an instance of converter.
236 * Currently this layer didn't have associated 'instance' structure
237 * to avoid unnesessary memory allocation.
238 */
239 int
240 iconv_open(const char *to, const char *from, void **handle)
241 {
242 struct iconv_cspair *csp, *cspfrom, *cspto;
243 struct iconv_converter_class *dcp;
244 const char *cnvname;
245 int error;
246
247 /*
248 * First, lookup fully qualified cspairs
249 */
250 error = iconv_lookupcs(to, from, &csp);
251 if (error == 0)
252 return ICONV_CONVERTER_OPEN(csp->cp_dcp, csp, NULL, handle);
253
254 /*
255 * Well, nothing found. Now try to construct a composite conversion
256 * ToDo: add a 'capability' field to converter
257 */
258 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
259 cnvname = ICONV_CONVERTER_NAME(dcp);
260 if (cnvname == NULL)
261 continue;
262 error = iconv_lookupcs(cnvname, from, &cspfrom);
263 if (error)
264 continue;
265 error = iconv_lookupcs(to, cnvname, &cspto);
266 if (error)
267 continue;
268 /*
269 * Fine, we're found a pair which can be combined together
270 */
271 return ICONV_CONVERTER_OPEN(dcp, cspto, cspfrom, handle);
272 }
273 return ENOENT;
274 }
275
276 int
277 iconv_close(void *handle)
278 {
279 return ICONV_CONVERTER_CLOSE(handle);
280 }
281
282 int
283 iconv_conv(void *handle, const char **inbuf,
284 size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
285 {
286 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, 0);
287 }
288
289 int
290 iconv_conv_case(void *handle, const char **inbuf,
291 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
292 {
293 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 0, casetype);
294 }
295
296 int
297 iconv_convchr(void *handle, const char **inbuf,
298 size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
299 {
300 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, 0);
301 }
302
303 int
304 iconv_convchr_case(void *handle, const char **inbuf,
305 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int casetype)
306 {
307 return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype);
308 }
309
310 int
311 towlower(int c, void *handle)
312 {
313 return ICONV_CONVERTER_TOLOWER(handle, c);
314 }
315
316 int
317 towupper(int c, void *handle)
318 {
319 return ICONV_CONVERTER_TOUPPER(handle, c);
320 }
321
322 /*
323 * Give a list of loaded converters. Each name terminated with 0.
324 * An empty string terminates the list.
325 */
326 static int
327 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
328 {
329 struct iconv_converter_class *dcp;
330 const char *name;
331 char spc;
332 int error;
333
334 error = 0;
335 sx_slock(&iconv_lock);
336 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
337 name = ICONV_CONVERTER_NAME(dcp);
338 if (name == NULL)
339 continue;
340 error = SYSCTL_OUT(req, name, strlen(name) + 1);
341 if (error)
342 break;
343 }
344 sx_sunlock(&iconv_lock);
345 if (error)
346 return error;
347 spc = 0;
348 error = SYSCTL_OUT(req, &spc, sizeof(spc));
349 return error;
350 }
351
352 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
353 NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
354
355 /*
356 * List all available charset pairs.
357 */
358 static int
359 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
360 {
361 struct iconv_cspair *csp;
362 struct iconv_cspair_info csi;
363 int error;
364
365 error = 0;
366 bzero(&csi, sizeof(csi));
367 csi.cs_version = ICONV_CSPAIR_INFO_VER;
368 sx_slock(&iconv_lock);
369 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
370 csi.cs_id = csp->cp_id;
371 csi.cs_refcount = csp->cp_refcount;
372 csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
373 strcpy(csi.cs_to, csp->cp_to);
374 strcpy(csi.cs_from, csp->cp_from);
375 error = SYSCTL_OUT(req, &csi, sizeof(csi));
376 if (error)
377 break;
378 }
379 sx_sunlock(&iconv_lock);
380 return error;
381 }
382
383 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
384 NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
385
386 /*
387 * Add new charset pair
388 */
389 static int
390 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
391 {
392 struct iconv_converter_class *dcp;
393 struct iconv_cspair *csp;
394 struct iconv_add_in din;
395 struct iconv_add_out dout;
396 int error;
397
398 error = SYSCTL_IN(req, &din, sizeof(din));
399 if (error)
400 return error;
401 if (din.ia_version != ICONV_ADD_VER)
402 return EINVAL;
403 if (din.ia_datalen > ICONV_CSMAXDATALEN)
404 return EINVAL;
405 if (strlen(din.ia_from) >= ICONV_CSNMAXLEN)
406 return EINVAL;
407 if (strlen(din.ia_to) >= ICONV_CSNMAXLEN)
408 return EINVAL;
409 if (strlen(din.ia_converter) >= ICONV_CNVNMAXLEN)
410 return EINVAL;
411 if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
412 return EINVAL;
413 sx_xlock(&iconv_lock);
414 error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
415 if (error) {
416 sx_xunlock(&iconv_lock);
417 return error;
418 }
419 if (din.ia_datalen) {
420 csp->cp_data = malloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
421 error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
422 if (error)
423 goto bad;
424 }
425 dout.ia_csid = csp->cp_id;
426 error = SYSCTL_OUT(req, &dout, sizeof(dout));
427 if (error)
428 goto bad;
429 sx_xunlock(&iconv_lock);
430 ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
431 return 0;
432 bad:
433 iconv_unregister_cspair(csp);
434 sx_xunlock(&iconv_lock);
435 return error;
436 }
437
438 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
439 NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
440
441 /*
442 * Default stubs for converters
443 */
444 int
445 iconv_converter_initstub(struct iconv_converter_class *dp)
446 {
447 return 0;
448 }
449
450 int
451 iconv_converter_donestub(struct iconv_converter_class *dp)
452 {
453 return 0;
454 }
455
456 int
457 iconv_converter_tolowerstub(int c, void *handle)
458 {
459 return (c);
460 }
461
462 int
463 iconv_converter_handler(module_t mod, int type, void *data)
464 {
465 struct iconv_converter_class *dcp = data;
466 int error;
467
468 switch (type) {
469 case MOD_LOAD:
470 sx_xlock(&iconv_lock);
471 error = iconv_register_converter(dcp);
472 if (error) {
473 sx_xunlock(&iconv_lock);
474 break;
475 }
476 error = ICONV_CONVERTER_INIT(dcp);
477 if (error)
478 iconv_unregister_converter(dcp);
479 sx_xunlock(&iconv_lock);
480 break;
481 case MOD_UNLOAD:
482 sx_xlock(&iconv_lock);
483 ICONV_CONVERTER_DONE(dcp);
484 error = iconv_unregister_converter(dcp);
485 sx_xunlock(&iconv_lock);
486 break;
487 default:
488 error = EINVAL;
489 }
490 return error;
491 }
492
493 /*
494 * Common used functions (don't use with unicode)
495 */
496 char *
497 iconv_convstr(void *handle, char *dst, const char *src)
498 {
499 char *p = dst;
500 size_t inlen, outlen;
501 int error;
502
503 if (handle == NULL) {
504 strcpy(dst, src);
505 return dst;
506 }
507 inlen = outlen = strlen(src);
508 error = iconv_conv(handle, NULL, NULL, &p, &outlen);
509 if (error)
510 return NULL;
511 error = iconv_conv(handle, &src, &inlen, &p, &outlen);
512 if (error)
513 return NULL;
514 *p = 0;
515 return dst;
516 }
517
518 void *
519 iconv_convmem(void *handle, void *dst, const void *src, int size)
520 {
521 const char *s = src;
522 char *d = dst;
523 size_t inlen, outlen;
524 int error;
525
526 if (size == 0)
527 return dst;
528 if (handle == NULL) {
529 memcpy(dst, src, size);
530 return dst;
531 }
532 inlen = outlen = size;
533 error = iconv_conv(handle, NULL, NULL, &d, &outlen);
534 if (error)
535 return NULL;
536 error = iconv_conv(handle, &s, &inlen, &d, &outlen);
537 if (error)
538 return NULL;
539 return dst;
540 }
541
542 int
543 iconv_lookupcp(char **cpp, const char *s)
544 {
545 if (cpp == NULL) {
546 ICDEBUG("warning a NULL list passed\n", ""); /* XXX ISO variadic macros cannot
547 leave out the
548 variadic args */
549 return ENOENT;
550 }
551 for (; *cpp; cpp++)
552 if (strcmp(*cpp, s) == 0)
553 return 0;
554 return ENOENT;
555 }
556
557 /*
558 * Return if fsname is in use of not
559 */
560 int
561 iconv_vfs_refcount(const char *fsname)
562 {
563 struct vfsconf *vfsp;
564
565 vfsp = vfs_byname(fsname);
566 if (vfsp != NULL && vfsp->vfc_refcount > 0)
567 return (EBUSY);
568 return (0);
569 }
Cache object: ece79a9f3ba9752922126f5d16845e10
|