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$");
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 /*
311 * Give a list of loaded converters. Each name terminated with 0.
312 * An empty string terminates the list.
313 */
314 static int
315 iconv_sysctl_drvlist(SYSCTL_HANDLER_ARGS)
316 {
317 struct iconv_converter_class *dcp;
318 const char *name;
319 char spc;
320 int error;
321
322 error = 0;
323 sx_slock(&iconv_lock);
324 TAILQ_FOREACH(dcp, &iconv_converters, cc_link) {
325 name = ICONV_CONVERTER_NAME(dcp);
326 if (name == NULL)
327 continue;
328 error = SYSCTL_OUT(req, name, strlen(name) + 1);
329 if (error)
330 break;
331 }
332 sx_sunlock(&iconv_lock);
333 if (error)
334 return error;
335 spc = 0;
336 error = SYSCTL_OUT(req, &spc, sizeof(spc));
337 return error;
338 }
339
340 SYSCTL_PROC(_kern_iconv, OID_AUTO, drvlist, CTLFLAG_RD | CTLTYPE_OPAQUE,
341 NULL, 0, iconv_sysctl_drvlist, "S,xlat", "registered converters");
342
343 /*
344 * List all available charset pairs.
345 */
346 static int
347 iconv_sysctl_cslist(SYSCTL_HANDLER_ARGS)
348 {
349 struct iconv_cspair *csp;
350 struct iconv_cspair_info csi;
351 int error;
352
353 error = 0;
354 bzero(&csi, sizeof(csi));
355 csi.cs_version = ICONV_CSPAIR_INFO_VER;
356 sx_slock(&iconv_lock);
357 TAILQ_FOREACH(csp, &iconv_cslist, cp_link) {
358 csi.cs_id = csp->cp_id;
359 csi.cs_refcount = csp->cp_refcount;
360 csi.cs_base = csp->cp_base ? csp->cp_base->cp_id : 0;
361 strcpy(csi.cs_to, csp->cp_to);
362 strcpy(csi.cs_from, csp->cp_from);
363 error = SYSCTL_OUT(req, &csi, sizeof(csi));
364 if (error)
365 break;
366 }
367 sx_sunlock(&iconv_lock);
368 return error;
369 }
370
371 SYSCTL_PROC(_kern_iconv, OID_AUTO, cslist, CTLFLAG_RD | CTLTYPE_OPAQUE,
372 NULL, 0, iconv_sysctl_cslist, "S,xlat", "registered charset pairs");
373
374 /*
375 * Add new charset pair
376 */
377 static int
378 iconv_sysctl_add(SYSCTL_HANDLER_ARGS)
379 {
380 struct iconv_converter_class *dcp;
381 struct iconv_cspair *csp;
382 struct iconv_add_in din;
383 struct iconv_add_out dout;
384 int error;
385
386 error = SYSCTL_IN(req, &din, sizeof(din));
387 if (error)
388 return error;
389 if (din.ia_version != ICONV_ADD_VER)
390 return EINVAL;
391 if (din.ia_datalen > ICONV_CSMAXDATALEN)
392 return EINVAL;
393 if (strlen(din.ia_from) >= ICONV_CSNMAXLEN)
394 return EINVAL;
395 if (strlen(din.ia_to) >= ICONV_CSNMAXLEN)
396 return EINVAL;
397 if (strlen(din.ia_converter) >= ICONV_CNVNMAXLEN)
398 return EINVAL;
399 if (iconv_lookupconv(din.ia_converter, &dcp) != 0)
400 return EINVAL;
401 sx_xlock(&iconv_lock);
402 error = iconv_register_cspair(din.ia_to, din.ia_from, dcp, NULL, &csp);
403 if (error) {
404 sx_xunlock(&iconv_lock);
405 return error;
406 }
407 if (din.ia_datalen) {
408 csp->cp_data = malloc(din.ia_datalen, M_ICONVDATA, M_WAITOK);
409 error = copyin(din.ia_data, csp->cp_data, din.ia_datalen);
410 if (error)
411 goto bad;
412 }
413 dout.ia_csid = csp->cp_id;
414 error = SYSCTL_OUT(req, &dout, sizeof(dout));
415 if (error)
416 goto bad;
417 sx_xunlock(&iconv_lock);
418 ICDEBUG("%s => %s, %d bytes\n",din.ia_from, din.ia_to, din.ia_datalen);
419 return 0;
420 bad:
421 iconv_unregister_cspair(csp);
422 sx_xunlock(&iconv_lock);
423 return error;
424 }
425
426 SYSCTL_PROC(_kern_iconv, OID_AUTO, add, CTLFLAG_RW | CTLTYPE_OPAQUE,
427 NULL, 0, iconv_sysctl_add, "S,xlat", "register charset pair");
428
429 /*
430 * Default stubs for converters
431 */
432 int
433 iconv_converter_initstub(struct iconv_converter_class *dp)
434 {
435 return 0;
436 }
437
438 int
439 iconv_converter_donestub(struct iconv_converter_class *dp)
440 {
441 return 0;
442 }
443
444 int
445 iconv_converter_handler(module_t mod, int type, void *data)
446 {
447 struct iconv_converter_class *dcp = data;
448 int error;
449
450 switch (type) {
451 case MOD_LOAD:
452 sx_xlock(&iconv_lock);
453 error = iconv_register_converter(dcp);
454 if (error) {
455 sx_xunlock(&iconv_lock);
456 break;
457 }
458 error = ICONV_CONVERTER_INIT(dcp);
459 if (error)
460 iconv_unregister_converter(dcp);
461 sx_xunlock(&iconv_lock);
462 break;
463 case MOD_UNLOAD:
464 sx_xlock(&iconv_lock);
465 ICONV_CONVERTER_DONE(dcp);
466 error = iconv_unregister_converter(dcp);
467 sx_xunlock(&iconv_lock);
468 break;
469 default:
470 error = EINVAL;
471 }
472 return error;
473 }
474
475 /*
476 * Common used functions (don't use with unicode)
477 */
478 char *
479 iconv_convstr(void *handle, char *dst, const char *src)
480 {
481 char *p = dst;
482 size_t inlen, outlen;
483 int error;
484
485 if (handle == NULL) {
486 strcpy(dst, src);
487 return dst;
488 }
489 inlen = outlen = strlen(src);
490 error = iconv_conv(handle, NULL, NULL, &p, &outlen);
491 if (error)
492 return NULL;
493 error = iconv_conv(handle, &src, &inlen, &p, &outlen);
494 if (error)
495 return NULL;
496 *p = 0;
497 return dst;
498 }
499
500 void *
501 iconv_convmem(void *handle, void *dst, const void *src, int size)
502 {
503 const char *s = src;
504 char *d = dst;
505 size_t inlen, outlen;
506 int error;
507
508 if (size == 0)
509 return dst;
510 if (handle == NULL) {
511 memcpy(dst, src, size);
512 return dst;
513 }
514 inlen = outlen = size;
515 error = iconv_conv(handle, NULL, NULL, &d, &outlen);
516 if (error)
517 return NULL;
518 error = iconv_conv(handle, &s, &inlen, &d, &outlen);
519 if (error)
520 return NULL;
521 return dst;
522 }
523
524 int
525 iconv_lookupcp(char **cpp, const char *s)
526 {
527 if (cpp == NULL) {
528 ICDEBUG("warning a NULL list passed\n", ""); /* XXX ISO variadic macros cannot
529 leave out the
530 variadic args */
531 return ENOENT;
532 }
533 for (; *cpp; cpp++)
534 if (strcmp(*cpp, s) == 0)
535 return 0;
536 return ENOENT;
537 }
538
539 /*
540 * Return if fsname is in use of not
541 */
542 int
543 iconv_vfs_refcount(const char *fsname)
544 {
545 struct vfsconf *vfsp;
546
547 vfsp = vfs_byname(fsname);
548 if (vfsp != NULL && vfsp->vfc_refcount > 0)
549 return (EBUSY);
550 return (0);
551 }
Cache object: 4e90cfcfb0158d53198d1573eacd9ef0
|