xlat16_iconv.c (148717) | xlat16_iconv.c (194638) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 2003 Ryuichiro Imura | 2 * Copyright (c) 2003, 2005 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 --- 7 unchanged lines hidden (view full) --- 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 * | 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 --- 7 unchanged lines hidden (view full) --- 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: head/lib/libkiconv/xlat16_iconv.c 148717 2005-08-05 07:28:26Z stefanf $ | 26 * $FreeBSD: head/lib/libkiconv/xlat16_iconv.c 194638 2009-06-22 17:09:46Z delphij $ |
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> | 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 <locale.h> |
|
44#include <stdio.h> 45#include <stdlib.h> 46#include <string.h> | 45#include <stdio.h> 46#include <stdlib.h> 47#include <string.h> |
48#include <wctype.h> |
|
47 48#include "quirks.h" 49 50typedef void *iconv_t; 51 52struct xlat16_table { 53 uint32_t * idx[0x200]; 54 void * data; 55 size_t size; 56}; 57 58static struct xlat16_table kiconv_xlat16_open(const char *, const char *, int); | 49 50#include "quirks.h" 51 52typedef void *iconv_t; 53 54struct xlat16_table { 55 uint32_t * idx[0x200]; 56 void * data; 57 size_t size; 58}; 59 60static struct xlat16_table kiconv_xlat16_open(const char *, const char *, int); |
61static int chklocale(int, const char *); |
|
59 60static int my_iconv_init(void); 61static iconv_t (*my_iconv_open)(const char *, const char *); 62static size_t (*my_iconv)(iconv_t, const char **, size_t *, char **, size_t *); 63static int (*my_iconv_close)(iconv_t); 64static size_t my_iconv_char(iconv_t, const u_char **, size_t *, u_char **, size_t *); 65 66int 67kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag) 68{ 69 int error; | 62 63static int my_iconv_init(void); 64static iconv_t (*my_iconv_open)(const char *, const char *); 65static size_t (*my_iconv)(iconv_t, const char **, size_t *, char **, size_t *); 66static int (*my_iconv_close)(iconv_t); 67static size_t my_iconv_char(iconv_t, const u_char **, size_t *, u_char **, size_t *); 68 69int 70kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag) 71{ 72 int error; |
70 size_t i, size, idxsize; 71 struct iconv_cspair_info *csi; | 73 size_t idxsize; |
72 struct xlat16_table xt; 73 void *data; 74 char *p; 75 | 74 struct xlat16_table xt; 75 void *data; 76 char *p; 77 |
76 if (sysctlbyname("kern.iconv.cslist", NULL, &size, NULL, 0) == -1) 77 return (-1); 78 if (size > 0) { 79 csi = malloc(size); 80 if (csi == NULL) 81 return (-1); 82 if (sysctlbyname("kern.iconv.cslist", csi, &size, NULL, 0) == -1) { 83 free(csi); 84 return (-1); 85 } 86 for (i = 0; i < (size/sizeof(*csi)); i++, csi++){ 87 if (strcmp(csi->cs_to, tocode) == 0 && 88 strcmp(csi->cs_from, fromcode) == 0) 89 return (0); 90 } 91 } | 78 if (kiconv_lookupcs(tocode, fromcode) == 0) 79 return (0); |
92 | 80 |
93 xt = kiconv_xlat16_open(tocode, fromcode, flag); | 81 if (flag & KICONV_WCTYPE) 82 xt = kiconv_xlat16_open(fromcode, fromcode, flag); 83 else 84 xt = kiconv_xlat16_open(tocode, fromcode, flag); |
94 if (xt.size == 0) 95 return (-1); 96 97 idxsize = sizeof(xt.idx); 98 99 if ((idxsize + xt.size) > ICONV_CSMAXDATALEN) { 100 errno = E2BIG; 101 return (-1); --- 10 unchanged lines hidden (view full) --- 112 } 113 114 return (-1); 115} 116 117int 118kiconv_add_xlat16_cspairs(const char *foreigncode, const char *localcode) 119{ | 85 if (xt.size == 0) 86 return (-1); 87 88 idxsize = sizeof(xt.idx); 89 90 if ((idxsize + xt.size) > ICONV_CSMAXDATALEN) { 91 errno = E2BIG; 92 return (-1); --- 10 unchanged lines hidden (view full) --- 103 } 104 105 return (-1); 106} 107 108int 109kiconv_add_xlat16_cspairs(const char *foreigncode, const char *localcode) 110{ |
120 int error; | 111 int error, locale; |
121 122 error = kiconv_add_xlat16_cspair(foreigncode, localcode, 123 KICONV_FROM_LOWER | KICONV_FROM_UPPER); 124 if (error) 125 return (error); 126 error = kiconv_add_xlat16_cspair(localcode, foreigncode, 127 KICONV_LOWER | KICONV_UPPER); 128 if (error) 129 return (error); | 112 113 error = kiconv_add_xlat16_cspair(foreigncode, localcode, 114 KICONV_FROM_LOWER | KICONV_FROM_UPPER); 115 if (error) 116 return (error); 117 error = kiconv_add_xlat16_cspair(localcode, foreigncode, 118 KICONV_LOWER | KICONV_UPPER); 119 if (error) 120 return (error); |
130 | 121 locale = chklocale(LC_CTYPE, localcode); 122 if (locale == 0) { 123 error = kiconv_add_xlat16_cspair(KICONV_WCTYPE_NAME, localcode, 124 KICONV_WCTYPE); 125 if (error) 126 return (error); 127 } 128 |
131 return (0); 132} 133 134static struct xlat16_table 135kiconv_xlat16_open(const char *tocode, const char *fromcode, int lcase) 136{ 137 u_char src[3], dst[4], *srcp, *dstp, ud, ld; 138 int us, ls, ret; --- 31 unchanged lines hidden (view full) --- 170 srcp = src; 171 dstp = dst; 172 173 inbytesleft = 2; 174 outbytesleft = 3; 175 bzero(dst, outbytesleft); 176 177 c = ((ls & 0x100 ? us | 0x80 : us) << 8) | (u_char)ls; | 129 return (0); 130} 131 132static struct xlat16_table 133kiconv_xlat16_open(const char *tocode, const char *fromcode, int lcase) 134{ 135 u_char src[3], dst[4], *srcp, *dstp, ud, ld; 136 int us, ls, ret; --- 31 unchanged lines hidden (view full) --- 168 srcp = src; 169 dstp = dst; 170 171 inbytesleft = 2; 172 outbytesleft = 3; 173 bzero(dst, outbytesleft); 174 175 c = ((ls & 0x100 ? us | 0x80 : us) << 8) | (u_char)ls; |
176 177 if (lcase & KICONV_WCTYPE) { 178 if ((c & 0xff) == 0) 179 c >>= 8; 180 if (iswupper(c)) { 181 c = towlower(c); 182 if ((c & 0xff00) == 0) 183 c <<= 8; 184 table[us] = c | XLAT16_HAS_LOWER_CASE; 185 } else if (iswlower(c)) { 186 c = towupper(c); 187 if ((c & 0xff00) == 0) 188 c <<= 8; 189 table[us] = c | XLAT16_HAS_UPPER_CASE; 190 } else 191 table[us] = 0; 192 /* 193 * store not NULL 194 */ 195 if (table[us]) 196 xt.idx[ls] = table; 197 198 continue; 199 } 200 |
|
178 c = quirk_vendor2unix(c, pre_q_list, pre_q_size); 179 src[0] = (u_char)(c >> 8); 180 src[1] = (u_char)c; 181 182 ret = my_iconv_char(cd, (const u_char **)&srcp, 183 &inbytesleft, &dstp, &outbytesleft); 184 if (ret == -1) { 185 table[us] = 0; --- 67 unchanged lines hidden (view full) --- 253 my_iconv_close(cd); 254 255 xt.size = p - (char *)xt.data; 256 xt.data = realloc(xt.data, xt.size); 257 return (xt); 258} 259 260static int | 201 c = quirk_vendor2unix(c, pre_q_list, pre_q_size); 202 src[0] = (u_char)(c >> 8); 203 src[1] = (u_char)c; 204 205 ret = my_iconv_char(cd, (const u_char **)&srcp, 206 &inbytesleft, &dstp, &outbytesleft); 207 if (ret == -1) { 208 table[us] = 0; --- 67 unchanged lines hidden (view full) --- 276 my_iconv_close(cd); 277 278 xt.size = p - (char *)xt.data; 279 xt.data = realloc(xt.data, xt.size); 280 return (xt); 281} 282 283static int |
284chklocale(int category, const char *code) 285{ 286 char *p; 287 int error = -1; 288 289 p = strchr(setlocale(category, NULL), '.'); 290 if (p++) { 291 error = strcasecmp(code, p); 292 if (error) { 293 /* XXX - can't avoid calling quirk here... */ 294 error = strcasecmp(code, kiconv_quirkcs(p, 295 KICONV_VENDOR_MICSFT)); 296 } 297 } 298 return (error); 299} 300 301static int |
|
261my_iconv_init(void) 262{ 263 void *iconv_lib; 264 265 iconv_lib = dlopen("libiconv.so", RTLD_LAZY | RTLD_GLOBAL); 266 if (iconv_lib == NULL) { 267 warn("Unable to load iconv library: %s\n", dlerror()); 268 errno = ENOENT; --- 106 unchanged lines hidden (view full) --- 375 return (-1); 376 } 377 378 return (ret); 379} 380 381#else /* statically linked */ 382 | 302my_iconv_init(void) 303{ 304 void *iconv_lib; 305 306 iconv_lib = dlopen("libiconv.so", RTLD_LAZY | RTLD_GLOBAL); 307 if (iconv_lib == NULL) { 308 warn("Unable to load iconv library: %s\n", dlerror()); 309 errno = ENOENT; --- 106 unchanged lines hidden (view full) --- 416 return (-1); 417 } 418 419 return (ret); 420} 421 422#else /* statically linked */ 423 |
424#include <sys/types.h> 425#include <sys/iconv.h> |
|
383#include <errno.h> 384 385int | 426#include <errno.h> 427 428int |
386kiconv_add_xlat16_cspair(const char *tocode, const char *fromcode, int flag) | 429kiconv_add_xlat16_cspair(const char *tocode __unused, const char *fromcode __unused, 430 int flag __unused) |
387{ | 431{ |
432 |
|
388 errno = EINVAL; 389 return (-1); 390} 391 392int | 433 errno = EINVAL; 434 return (-1); 435} 436 437int |
393kiconv_add_xlat16_cspairs(const char *tocode, const char *fromcode) | 438kiconv_add_xlat16_cspairs(const char *tocode __unused, const char *fromcode __unused) |
394{ 395 errno = EINVAL; 396 return (-1); 397} 398 399#endif /* PIC */ | 439{ 440 errno = EINVAL; 441 return (-1); 442} 443 444#endif /* PIC */ |