2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003, 2005 Ryuichiro Imura
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * kiconv(3) requires shared linked, and reduce module size
33 * when statically linked.
38 #include <sys/types.h>
39 #include <sys/iconv.h>
40 #include <sys/sysctl.h>
55 uint32_t * idx[0x200];
60 static struct xlat16_table kiconv_xlat16_open(const char *, const char *, int);
61 static int chklocale(int, const char *);
64 typedef void *iconv_t;
65 static int my_iconv_init(void);
66 static iconv_t (*my_iconv_open)(const char *, const char *);
67 static size_t (*my_iconv)(iconv_t, char **, size_t *, char **, size_t *);
68 static int (*my_iconv_close)(iconv_t);
71 #define my_iconv_init() 0
72 #define my_iconv_open iconv_open
73 #define my_iconv iconv
74 #define my_iconv_close iconv_close
76 static size_t my_iconv_char(iconv_t, u_char **, size_t *, u_char **, size_t *);
79 kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag)
83 struct xlat16_table xt;
86 const char unicode[] = ENCODING_UNICODE;
88 if ((flag & KICONV_WCTYPE) == 0 &&
89 strcmp(unicode, tocode) != 0 &&
90 strcmp(unicode, fromcode) != 0 &&
91 kiconv_lookupconv(unicode) == 0) {
92 error = kiconv_add_xlat16_cspair(unicode, fromcode, flag);
95 error = kiconv_add_xlat16_cspair(tocode, unicode, flag);
99 if (kiconv_lookupcs(tocode, fromcode) == 0)
102 if (flag & KICONV_WCTYPE)
103 xt = kiconv_xlat16_open(fromcode, fromcode, flag);
105 xt = kiconv_xlat16_open(tocode, fromcode, flag);
109 idxsize = sizeof(xt.idx);
111 if ((idxsize + xt.size) > ICONV_CSMAXDATALEN) {
116 if ((data = malloc(idxsize + xt.size)) != NULL) {
118 memcpy(p, xt.idx, idxsize);
120 memcpy(p, xt.data, xt.size);
121 error = kiconv_add_xlat16_table(tocode, fromcode, data,
122 (int)(idxsize + xt.size));
130 kiconv_add_xlat16_cspairs(const char *foreigncode, const char *localcode)
134 error = kiconv_add_xlat16_cspair(foreigncode, localcode,
135 KICONV_FROM_LOWER | KICONV_FROM_UPPER);
138 error = kiconv_add_xlat16_cspair(localcode, foreigncode,
139 KICONV_LOWER | KICONV_UPPER);
142 locale = chklocale(LC_CTYPE, localcode);
144 error = kiconv_add_xlat16_cspair(KICONV_WCTYPE_NAME, localcode,
153 static struct xlat16_table
154 kiconv_xlat16_open(const char *tocode, const char *fromcode, int lcase)
156 u_char src[3], dst[4], *srcp, *dstp, ud, ld;
159 uint32_t table[0x80];
160 size_t inbytesleft, outbytesleft, pre_q_size, post_q_size;
161 struct xlat16_table xt;
162 struct quirk_replace_list *pre_q_list, *post_q_list;
172 ret = my_iconv_init();
176 cd = my_iconv_open(search_quirk(tocode, fromcode, &pre_q_list, &pre_q_size),
177 search_quirk(fromcode, tocode, &post_q_list, &post_q_size));
178 if (cd == (iconv_t) (-1))
181 if ((xt.data = malloc(0x200 * 0x80 * sizeof(uint32_t))) == NULL)
186 for (ls = 0 ; ls < 0x200 ; ls++) {
188 for (us = 0 ; us < 0x80 ; us++) {
194 bzero(dst, outbytesleft);
196 c = ((ls & 0x100 ? us | 0x80 : us) << 8) | (u_char)ls;
198 if (lcase & KICONV_WCTYPE) {
203 if ((c & 0xff00) == 0)
205 table[us] = c | XLAT16_HAS_LOWER_CASE;
206 } else if (iswlower(c)) {
208 if ((c & 0xff00) == 0)
210 table[us] = c | XLAT16_HAS_UPPER_CASE;
222 c = quirk_vendor2unix(c, pre_q_list, pre_q_size);
223 src[0] = (u_char)(c >> 8);
226 ret = my_iconv_char(cd, &srcp, &inbytesleft,
227 &dstp, &outbytesleft);
236 switch(outbytesleft) {
238 #ifdef XLAT16_ACCEPT_3BYTE_CHR
239 table[us] = (ud << 8) | ld;
240 table[us] |= (u_char)dst[2] << 16;
241 table[us] |= XLAT16_IS_3BYTE_CHR;
248 table[us] = quirk_unix2vendor((ud << 8) | ld,
249 post_q_list, post_q_size);
250 if ((table[us] >> 8) == 0)
251 table[us] |= XLAT16_ACCEPT_NULL_OUT;
255 if (lcase & KICONV_LOWER && ud != tolower(ud)) {
256 table[us] |= (u_char)tolower(ud) << 16;
257 table[us] |= XLAT16_HAS_LOWER_CASE;
259 if (lcase & KICONV_UPPER && ud != toupper(ud)) {
260 table[us] |= (u_char)toupper(ud) << 16;
261 table[us] |= XLAT16_HAS_UPPER_CASE;
266 switch(inbytesleft) {
268 if ((ls & 0xff) == 0)
269 table[us] |= XLAT16_ACCEPT_NULL_IN;
272 c = ls > 0xff ? us | 0x80 : us;
273 if (lcase & KICONV_FROM_LOWER && c != tolower(c)) {
274 table[us] |= (u_char)tolower(c) << 16;
275 table[us] |= XLAT16_HAS_FROM_LOWER_CASE;
277 if (lcase & KICONV_FROM_UPPER && c != toupper(c)) {
278 table[us] |= (u_char)toupper(c) << 16;
279 table[us] |= XLAT16_HAS_FROM_UPPER_CASE;
293 memcpy(p, table, sizeof(table));
299 xt.size = p - (char *)xt.data;
300 xt.data = realloc(xt.data, xt.size);
305 chklocale(int category, const char *code)
310 p = strchr(setlocale(category, NULL), '.');
312 error = strcasecmp(code, p);
314 /* XXX - can't avoid calling quirk here... */
315 error = strcasecmp(code, kiconv_quirkcs(p,
316 KICONV_VENDOR_MICSFT));
328 iconv_lib = dlopen("libiconv.so", RTLD_LAZY | RTLD_GLOBAL);
329 if (iconv_lib == NULL) {
330 warn("Unable to load iconv library: %s\n", dlerror());
334 my_iconv_open = dlsym(iconv_lib, "iconv_open");
335 my_iconv = dlsym(iconv_lib, "iconv");
336 my_iconv_close = dlsym(iconv_lib, "iconv_close");
343 my_iconv_char(iconv_t cd, u_char **ibuf, size_t * ilen, u_char **obuf,
346 u_char *sp, *dp, ilocal[3], olocal[3];
356 ret = my_iconv(cd, (char **)&sp, ilen, (char **)&dp, olen);
361 if (*ilen == ir - 1 && (*ibuf)[1] == '\0' && (c1 || c2))
368 * We must judge if inbuf is a single byte char or double byte char.
369 * Here, to judge, try first byte(*sp) conversion and compare.
375 memcpy(ilocal, *ibuf, sizeof(ilocal));
379 if ((my_iconv(cd,(char **)&sp, &ir, (char **)&dp, &or)) != -1) {
383 if (olocal[1] == c2 && (*ibuf)[1] == '\0') {
385 * inbuf is a single byte char
395 if (olocal[1] == c2) {
397 * inbuf is a single byte char,
398 * so return false here.
403 * inbuf is a double byte char
410 * should compare second byte of inbuf
416 * inbuf clould not be splitted, so inbuf is
417 * a double byte char.
423 * try second byte(*(sp+1)) conversion, and compare
433 if ((my_iconv(cd,(char **)&sp, &ir, (char **)&dp, &or)) != -1) {
436 * inbuf is a single byte char
444 #else /* statically linked */
446 #include <sys/types.h>
447 #include <sys/iconv.h>
451 kiconv_add_xlat16_cspair(const char *tocode __unused, const char *fromcode __unused,
460 kiconv_add_xlat16_cspairs(const char *tocode __unused, const char *fromcode __unused)