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