Stage 4/5: Import libkiconv from FreeBSD.
[dragonfly.git] / lib / libkiconv / xlat16_iconv.c
1 /*-
2  * Copyright (c) 2003 Ryuichiro Imura
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libkiconv/xlat16_iconv.c,v 1.4.8.1 2009/04/15 03:14:26 kensmith Exp $
27  */
28
29 /*
30  * kiconv(3) requires shared linked, and reduce module size
31  * when statically linked.
32  */
33 #ifdef PIC
34
35 #include <sys/types.h>
36 #include <sys/iconv.h>
37 #include <sys/sysctl.h>
38
39 #include <ctype.h>
40 #include <dlfcn.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "quirks.h"
48
49 typedef void *iconv_t;
50
51 struct xlat16_table {
52         uint32_t *      idx[0x200];
53         void *          data;
54         size_t          size;
55 };
56
57 static struct xlat16_table kiconv_xlat16_open(const char *, const char *, int);
58
59 static int my_iconv_init(void);
60 static iconv_t (*my_iconv_open)(const char *, const char *);
61 static size_t (*my_iconv)(iconv_t, const char **, size_t *, char **, size_t *);
62 static int (*my_iconv_close)(iconv_t);
63 static size_t my_iconv_char(iconv_t, const u_char **, size_t *, u_char **, size_t *);
64
65 int
66 kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
67 {
68         int error;
69         size_t i, size, idxsize;
70         struct iconv_cspair_info *csi;
71         struct xlat16_table xt;
72         void *data;
73         char *p;
74         if (sysctlbyname("kern.iconv.cslist", NULL, &size, NULL, 0) == -1)
75                 return (-1);
76         if (size > 0) {
77                 csi = malloc(size);
78                 if (csi == NULL)
79                         return (-1);
80                 if (sysctlbyname("kern.iconv.cslist", csi, &size, NULL, 0) == -1) {
81                         free(csi);
82                         return (-1);
83                 }
84                 for (i = 0; i < (size/sizeof(*csi)); i++, csi++){
85                         if (strcmp(csi->cs_to, tocode) == 0 &&
86                             strcmp(csi->cs_from, fromcode) == 0)
87                                 return (0);
88                 }
89         }
90
91         xt = kiconv_xlat16_open(tocode, fromcode, flag);
92         if (xt.size == 0)
93                 return (-1);
94
95         idxsize = sizeof(xt.idx);
96
97         if ((idxsize + xt.size) > ICONV_CSMAXDATALEN) {
98                 errno = E2BIG;
99 fprintf(stderr, "%d > %d (%d)\n", (idxsize+xt.size), ICONV_CSMAXDATALEN, sizeof(u_int32_t));
100                 return (-1);
101         }
102
103         if ((data = malloc(idxsize + xt.size)) != NULL) {
104                 p = data;
105                 memcpy(p, xt.idx, idxsize);
106                 p += idxsize;
107                 memcpy(p, xt.data, xt.size);
108                 error = kiconv_add_xlat16_table(tocode, fromcode, data,
109                     (int)(idxsize + xt.size));
110                 return (error);
111         }
112         return (-1);
113 }
114
115 int
116 kiconv_add_xlat16_cspairs(const char *foreigncode, const char *localcode)
117 {
118         int error;
119
120         error = kiconv_add_xlat16_cspair(foreigncode, localcode,
121             KICONV_FROM_LOWER | KICONV_FROM_UPPER);
122         if (error)
123                 return (error);
124         error = kiconv_add_xlat16_cspair(localcode, foreigncode,
125             KICONV_LOWER | KICONV_UPPER);
126         if (error)
127                 return (error);
128         return (0);
129 }
130
131 static struct xlat16_table
132 kiconv_xlat16_open(const char *tocode, const char *fromcode, int lcase)
133 {
134         u_char src[3], dst[4], *srcp, *dstp, ud, ld;
135         int us, ls, ret;
136         uint16_t c;
137         uint32_t table[0x80];
138         size_t inbytesleft, outbytesleft, pre_q_size, post_q_size;
139         struct xlat16_table xt;
140         struct quirk_replace_list *pre_q_list, *post_q_list;
141         iconv_t cd;
142         char *p;
143
144         xt.data = NULL;
145         xt.size = 0;
146
147         src[2] = '\0';
148         dst[3] = '\0';
149         ret = my_iconv_init();
150         if (ret)
151                 return (xt);
152         cd = my_iconv_open(search_quirk(tocode, fromcode, &pre_q_list, &pre_q_size),
153             search_quirk(fromcode, tocode, &post_q_list, &post_q_size));
154         if (cd == (iconv_t) (-1))
155                 return (xt);
156
157         if ((xt.data = malloc(0x200 * 0x80 * sizeof(uint32_t))) == NULL)
158                 return (xt);
159
160         p = xt.data;
161
162         for (ls = 0 ; ls < 0x200 ; ls++) {
163                 xt.idx[ls] = NULL;
164                 for (us = 0 ; us < 0x80 ; us++) {
165                         srcp = src;
166                         dstp = dst;
167
168                         inbytesleft = 2;
169                         outbytesleft = 3;
170                         bzero(dst, outbytesleft);
171
172                         c = ((ls & 0x100 ? us | 0x80 : us) << 8) | (u_char)ls;
173                         c = quirk_vendor2unix(c, pre_q_list, pre_q_size);
174                         src[0] = (u_char)(c >> 8);
175                         src[1] = (u_char)c;
176
177                         ret = my_iconv_char(cd, (const u_char **)&srcp,
178                             &inbytesleft, &dstp, &outbytesleft);
179                         if (ret == -1) {
180                                 table[us] = 0;
181                                 continue;
182                         }
183
184                         ud = (u_char)dst[0];
185                         ld = (u_char)dst[1];
186
187                         switch(outbytesleft) {
188                         case 0:
189 #ifdef XLAT16_ACCEPT_3BYTE_CHR
190                                 table[us] = (ud << 8) | ld;
191                                 table[us] |= (u_char)dst[2] << 16;
192                                 table[us] |= XLAT16_IS_3BYTE_CHR;
193 #else
194                                 table[us] = 0;
195                                 continue;
196 #endif
197                                 break;
198                         case 1:
199                                 table[us] = quirk_unix2vendor((ud << 8) | ld,
200                                     post_q_list, post_q_size);
201                                 if ((table[us] >> 8) == 0)
202                                         table[us] |= XLAT16_ACCEPT_NULL_OUT;
203                                 break;
204                         case 2:
205                                 table[us] = ud;
206                                 if (lcase & KICONV_LOWER && ud != tolower(ud)) {
207                                         table[us] |= (u_char)tolower(ud) << 16;
208                                         table[us] |= XLAT16_HAS_LOWER_CASE;
209                                 }
210                                 if (lcase & KICONV_UPPER && ud != toupper(ud)) {
211                                         table[us] |= (u_char)toupper(ud) << 16;
212                                         table[us] |= XLAT16_HAS_UPPER_CASE;
213                                 }
214                                 break;
215                         }
216
217                         switch(inbytesleft) {
218                         case 0:
219                                 if ((ls & 0xff) == 0)
220                                         table[us] |= XLAT16_ACCEPT_NULL_IN;
221                                 break;
222                         case 1:
223                                 c = ls > 0xff ? us | 0x80 : us;
224                                 if (lcase & KICONV_FROM_LOWER && c != tolower(c)) {
225                                         table[us] |= (u_char)tolower(c) << 16;
226                                         table[us] |= XLAT16_HAS_FROM_LOWER_CASE;
227                                 }
228                                 if (lcase & KICONV_FROM_UPPER && c != toupper(c)) {
229                                         table[us] |= (u_char)toupper(c) << 16;
230                                         table[us] |= XLAT16_HAS_FROM_UPPER_CASE;
231                                 }
232                                 break;
233                         }
234
235                         if (table[us] == 0)
236                                 continue;
237
238                         /*
239                          * store not NULL
240                          */
241                         xt.idx[ls] = table;
242                 }
243                 if (xt.idx[ls]) {
244                         memcpy(p, table, sizeof(table));
245                         p += sizeof(table);
246                 }
247         }
248         my_iconv_close(cd);
249
250         xt.size = p - (char *)xt.data;
251         xt.data = realloc(xt.data, xt.size);
252         return (xt);
253 }
254
255 static int
256 my_iconv_init(void)
257 {
258         void *iconv_lib;
259
260         iconv_lib = dlopen("libc.so", RTLD_LAZY | RTLD_GLOBAL);
261         if (iconv_lib == NULL) {
262                 warn("Unable to load iconv library: %s\n", dlerror());
263                 errno = ENOENT;
264                 return (-1);
265         }
266         my_iconv_open = dlsym(iconv_lib, "iconv_open");
267         my_iconv = dlsym(iconv_lib, "iconv");
268         my_iconv_close = dlsym(iconv_lib, "iconv_close");
269
270         return (0);
271 }
272
273 static size_t
274 my_iconv_char(iconv_t cd, const u_char **ibuf, size_t * ilen, u_char **obuf,
275         size_t * olen)
276 {
277         const u_char *sp;
278         u_char *dp, ilocal[3], olocal[3];
279         u_char c1, c2;
280         int ret;
281         size_t ir, or;
282
283         sp = *ibuf;
284         dp = *obuf;
285         ir = *ilen;
286
287         bzero(*obuf, *olen);
288         ret = my_iconv(cd, (const char **)&sp, ilen, (char **)&dp, olen);
289         c1 = (*obuf)[0];
290         c2 = (*obuf)[1];
291
292         if (ret == -1) {
293                 if (*ilen == ir - 1 && (*ibuf)[1] == '\0' && (c1 || c2))
294                         return (0);
295                 else
296                         return (-1);
297         }
298
299         /*
300          * We must judge if inbuf is a single byte char or double byte char.
301          * Here, to judge, try first byte(*sp) conversion and compare.
302          */
303         ir = 1;
304         or = 3;
305
306         bzero(olocal, or);
307         memcpy(ilocal, *ibuf, sizeof(ilocal));
308         sp = ilocal;
309         dp = olocal;
310
311         if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
312                 if (olocal[0] != c1)
313                         return (ret);
314
315                 if (olocal[1] == c2 && (*ibuf)[1] == '\0') {
316                         /*
317                          * inbuf is a single byte char
318                          */
319                         *ilen = 1;
320                         *olen = or;
321                         return (ret);
322                 }
323
324                 switch(or) {
325                 case 0:
326                 case 1:
327                         if (olocal[1] == c2) {
328                                 /*
329                                  * inbuf is a single byte char,
330                                  * so return false here.
331                                  */
332                                 return (-1);
333                         } else {
334                                 /*
335                                  * inbuf is a double byte char
336                                  */
337                                 return (ret);
338                         }
339                         break;
340                 case 2:
341                         /*
342                          * should compare second byte of inbuf
343                          */
344                         break;
345                 }
346         } else {
347                 /*
348                  * inbuf clould not be splitted, so inbuf is
349                  * a double byte char.
350                  */
351                 return (ret);
352         }
353
354         /*
355          * try second byte(*(sp+1)) conversion, and compare
356          */
357         ir = 1;
358         or = 3;
359
360         bzero(olocal, or);
361
362         sp = ilocal + 1;
363         dp = olocal;
364
365         if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
366                 if (olocal[0] == c2)
367                         /*
368                          * inbuf is a single byte char
369                          */
370                         return (-1);
371         }
372
373         return (ret);
374 }
375
376 #else /* statically linked */
377
378 #include <errno.h>
379
380 int
381 kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
382 {
383         errno = EINVAL;
384         return (-1);
385 }
386
387 int
388 kiconv_add_xlat16_cspairs(const char *tocode, const char *fromcode)
389 {
390         errno = EINVAL;
391         return (-1);
392 }
393
394 #endif /* PIC */