2 * Copyright (c) 2003 Ryuichiro Imura
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/lib/libkiconv/xlat16_iconv.c,v 1.4.8.1 2009/04/15 03:14:26 kensmith Exp $
30 * kiconv(3) requires shared linked, and reduce module size
31 * when statically linked.
35 #include <sys/types.h>
36 #include <sys/iconv.h>
37 #include <sys/sysctl.h>
49 typedef void *iconv_t;
52 uint32_t * idx[0x200];
57 static struct xlat16_table kiconv_xlat16_open(const char *, const char *, int);
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 *);
66 kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
69 size_t i, size, idxsize;
70 struct iconv_cspair_info *csi;
71 struct xlat16_table xt;
74 if (sysctlbyname("kern.iconv.cslist", NULL, &size, NULL, 0) == -1)
80 if (sysctlbyname("kern.iconv.cslist", csi, &size, NULL, 0) == -1) {
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)
91 xt = kiconv_xlat16_open(tocode, fromcode, flag);
95 idxsize = sizeof(xt.idx);
97 if ((idxsize + xt.size) > ICONV_CSMAXDATALEN) {
99 fprintf(stderr, "%d > %d (%d)\n", (idxsize+xt.size), ICONV_CSMAXDATALEN, sizeof(u_int32_t));
103 if ((data = malloc(idxsize + xt.size)) != NULL) {
105 memcpy(p, xt.idx, idxsize);
107 memcpy(p, xt.data, xt.size);
108 error = kiconv_add_xlat16_table(tocode, fromcode, data,
109 (int)(idxsize + xt.size));
116 kiconv_add_xlat16_cspairs(const char *foreigncode, const char *localcode)
120 error = kiconv_add_xlat16_cspair(foreigncode, localcode,
121 KICONV_FROM_LOWER | KICONV_FROM_UPPER);
124 error = kiconv_add_xlat16_cspair(localcode, foreigncode,
125 KICONV_LOWER | KICONV_UPPER);
131 static struct xlat16_table
132 kiconv_xlat16_open(const char *tocode, const char *fromcode, int lcase)
134 u_char src[3], dst[4], *srcp, *dstp, ud, ld;
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;
149 ret = my_iconv_init();
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))
157 if ((xt.data = malloc(0x200 * 0x80 * sizeof(uint32_t))) == NULL)
162 for (ls = 0 ; ls < 0x200 ; ls++) {
164 for (us = 0 ; us < 0x80 ; us++) {
170 bzero(dst, outbytesleft);
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);
177 ret = my_iconv_char(cd, (const u_char **)&srcp,
178 &inbytesleft, &dstp, &outbytesleft);
187 switch(outbytesleft) {
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;
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;
206 if (lcase & KICONV_LOWER && ud != tolower(ud)) {
207 table[us] |= (u_char)tolower(ud) << 16;
208 table[us] |= XLAT16_HAS_LOWER_CASE;
210 if (lcase & KICONV_UPPER && ud != toupper(ud)) {
211 table[us] |= (u_char)toupper(ud) << 16;
212 table[us] |= XLAT16_HAS_UPPER_CASE;
217 switch(inbytesleft) {
219 if ((ls & 0xff) == 0)
220 table[us] |= XLAT16_ACCEPT_NULL_IN;
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;
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;
244 memcpy(p, table, sizeof(table));
250 xt.size = p - (char *)xt.data;
251 xt.data = realloc(xt.data, xt.size);
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());
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");
274 my_iconv_char(iconv_t cd, const u_char **ibuf, size_t * ilen, u_char **obuf,
278 u_char *dp, ilocal[3], olocal[3];
288 ret = my_iconv(cd, (const char **)&sp, ilen, (char **)&dp, olen);
293 if (*ilen == ir - 1 && (*ibuf)[1] == '\0' && (c1 || c2))
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.
307 memcpy(ilocal, *ibuf, sizeof(ilocal));
311 if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
315 if (olocal[1] == c2 && (*ibuf)[1] == '\0') {
317 * inbuf is a single byte char
327 if (olocal[1] == c2) {
329 * inbuf is a single byte char,
330 * so return false here.
335 * inbuf is a double byte char
342 * should compare second byte of inbuf
348 * inbuf clould not be splitted, so inbuf is
349 * a double byte char.
355 * try second byte(*(sp+1)) conversion, and compare
365 if ((my_iconv(cd,(const char **)&sp, &ir, (char **)&dp, &or)) != -1) {
368 * inbuf is a single byte char
376 #else /* statically linked */
381 kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
388 kiconv_add_xlat16_cspairs(const char *tocode, const char *fromcode)