Index: sys/sys/iconv.h =================================================================== RCS file: /home/ncvs/src/sys/sys/iconv.h,v retrieving revision 1.11 diff -u -r1.11 iconv.h --- sys/sys/iconv.h 3 Jul 2005 01:12:37 -0000 1.11 +++ sys/sys/iconv.h 7 Aug 2005 15:05:49 -0000 @@ -51,6 +51,9 @@ #define KICONV_UPPER 2 /* toupper converted character */ #define KICONV_FROM_LOWER 4 /* tolower source character, then convert */ #define KICONV_FROM_UPPER 8 /* toupper source character, then convert */ +#define KICONV_WCTYPE 16 /* towlower/towupper characters */ + +#define KICONV_WCTYPE_NAME "_wctype" /* * Entry for cslist sysctl @@ -95,6 +98,8 @@ int kiconv_add_xlat16_cspair(const char *, const char *, int); int kiconv_add_xlat16_cspairs(const char *, const char *); int kiconv_add_xlat16_table(const char *, const char *, const void *, int); +int kiconv_lookupconv(const char *drvname); +int kiconv_lookupcs(const char *tocode, const char *fromcode); const char *kiconv_quirkcs(const char *, int); __END_DECLS @@ -165,6 +170,9 @@ void* iconv_convmem(void *handle, void *dst, const void *src, int size); int iconv_vfs_refcount(const char *fsname); +int towlower(int c, void *handle); +int towupper(int c, void *handle); + /* * Bridge struct of iconv functions */ @@ -231,6 +239,7 @@ int iconv_converter_initstub(struct iconv_converter_class *dp); int iconv_converter_donestub(struct iconv_converter_class *dp); +int iconv_converter_tolowerstub(int c, void *handle); int iconv_converter_handler(module_t mod, int type, void *data); #ifdef ICONV_DEBUG Index: sys/libkern/iconv.c =================================================================== RCS file: /home/ncvs/src/sys/libkern/iconv.c,v retrieving revision 1.10 diff -u -r1.10 iconv.c --- sys/libkern/iconv.c 23 Jul 2005 16:52:57 -0000 1.10 +++ sys/libkern/iconv.c 7 Aug 2005 15:04:52 -0000 @@ -298,6 +298,18 @@ return ICONV_CONVERTER_CONV(handle, inbuf, inbytesleft, outbuf, outbytesleft, 1, casetype); } +int +towlower(int c, void *handle) +{ + return ICONV_CONVERTER_TOLOWER(handle, c); +} + +int +towupper(int c, void *handle) +{ + return ICONV_CONVERTER_TOUPPER(handle, c); +} + /* * Give a list of loaded converters. Each name terminated with 0. * An empty string terminates the list. @@ -420,6 +432,12 @@ } int +iconv_converter_tolowerstub(int c, void *handle) +{ + return (c); +} + +int iconv_converter_handler(module_t mod, int type, void *data) { struct iconv_converter_class *dcp = data; Index: sys/libkern/iconv_converter_if.m =================================================================== RCS file: /home/ncvs/src/sys/libkern/iconv_converter_if.m,v retrieving revision 1.3 diff -u -r1.3 iconv_converter_if.m --- sys/libkern/iconv_converter_if.m 7 Jan 2005 00:24:32 -0000 1.3 +++ sys/libkern/iconv_converter_if.m 7 Aug 2005 12:26:20 -0000 @@ -68,3 +68,13 @@ STATICMETHOD const char * name { struct iconv_converter_class *dcp; }; + +METHOD int tolower { + void *handle; + int c; +} DEFAULT iconv_converter_tolowerstub; + +METHOD int toupper { + void *handle; + int c; +} DEFAULT iconv_converter_tolowerstub; Index: sys/libkern/iconv_xlat16.c =================================================================== RCS file: /home/ncvs/src/sys/libkern/iconv_xlat16.c,v retrieving revision 1.3 diff -u -r1.3 iconv_xlat16.c --- sys/libkern/iconv_xlat16.c 24 May 2005 15:38:08 -0000 1.3 +++ sys/libkern/iconv_xlat16.c 7 Aug 2005 12:25:53 -0000 @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2003, Ryuichiro Imura + * Copyright (c) 2003, 2005 Ryuichiro Imura * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,12 +43,17 @@ MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2); #endif +#define C2I1(c) ((c) & 0x8000 ? ((c) & 0xff) | 0x100 : (c) & 0xff) +#define C2I2(c) ((c) & 0x8000 ? ((c) >> 8) & 0x7f : ((c) >> 8) & 0xff) + /* * XLAT16 converter instance */ struct iconv_xlat16 { KOBJ_FIELDS; uint32_t * d_table[0x200]; + void * f_ctp; + void * t_ctp; struct iconv_cspair * d_csp; }; @@ -72,6 +77,16 @@ } idxp++; } + + if (strcmp(csp->cp_to, KICONV_WCTYPE_NAME) != 0) { + if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_from, &dp->f_ctp) != 0) + dp->f_ctp = NULL; + if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_to, &dp->t_ctp) != 0) + dp->t_ctp = NULL; + } else { + dp->f_ctp = dp->t_ctp = dp; + } + dp->d_csp = csp; csp->cp_refcount++; *dpp = (void*)dp; @@ -83,6 +98,10 @@ { struct iconv_xlat16 *dp = data; + if (dp->f_ctp && dp->f_ctp != data) + iconv_close(dp->f_ctp); + if (dp->t_ctp && dp->t_ctp != data) + iconv_close(dp->t_ctp); dp->d_csp->cp_refcount--; kobj_delete((struct kobj*)data, M_ICONV); return (0); @@ -100,7 +119,7 @@ size_t in, on, ir, or, inlen; uint32_t code; u_char u, l; - uint16_t c1, c2; + uint16_t c1, c2, ctmp; if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL) return (0); @@ -112,21 +131,32 @@ while(ir > 0 && or > 0) { inlen = 0; - code = '\0'; + code = 0; c1 = ir > 1 ? *(src+1) & 0xff : 0; c2 = *src & 0xff; + ctmp = 0; c1 = c2 & 0x80 ? c1 | 0x100 : c1; c2 = c2 & 0x80 ? c2 & 0x7f : c2; - if (ir > 1 && dp->d_table[c1]) { + if (ir > 1 && dp->d_table[c1] && dp->d_table[c1][c2]) { /* * inbuf char is a double byte char */ - code = dp->d_table[c1][c2]; - if (code) - inlen = 2; + inlen = 2; + + /* toupper,tolower */ + if (casetype == KICONV_FROM_LOWER && dp->f_ctp) + ctmp = towlower(((u_char)*src << 8) | (u_char)*(src + 1), + dp->f_ctp); + else if (casetype == KICONV_FROM_UPPER && dp->f_ctp) + ctmp = towupper(((u_char)*src << 8) | (u_char)*(src + 1), + dp->f_ctp); + if (ctmp) { + c1 = C2I1(ctmp); + c2 = C2I2(ctmp); + } } if (inlen == 0) { @@ -139,13 +169,33 @@ * inbuf char is a single byte char */ inlen = 1; - code = dp->d_table[c1][c2]; - if (!code) { - ret = -1; - break; + + if (casetype & (KICONV_FROM_LOWER|KICONV_FROM_UPPER)) + code = dp->d_table[c1][c2]; + + if (casetype == KICONV_FROM_LOWER) { + if (dp->f_ctp) + ctmp = towlower((u_char)*src, dp->f_ctp); + else if (code & XLAT16_HAS_FROM_LOWER_CASE) + ctmp = (u_char)(code >> 16); + } else if (casetype == KICONV_FROM_UPPER) { + if (dp->f_ctp) + ctmp = towupper((u_char)*src, dp->f_ctp); + else if (code & XLAT16_HAS_FROM_UPPER_CASE) + ctmp = (u_char)(code >> 16); + } + if (ctmp) { + c1 = C2I1(ctmp << 8); + c2 = C2I2(ctmp << 8); } } + code = dp->d_table[c1][c2]; + if (!code) { + ret = -1; + break; + } + nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0; if (inlen == 1 && nullin) { /* @@ -158,14 +208,6 @@ /* * now start translation */ - if ((casetype == KICONV_FROM_LOWER && code & XLAT16_HAS_FROM_LOWER_CASE) || - (casetype == KICONV_FROM_UPPER && code & XLAT16_HAS_FROM_UPPER_CASE)) { - c2 = (u_char)(code >> 16); - c1 = c2 & 0x80 ? 0x100 : 0; - c2 = c2 & 0x80 ? c2 & 0x7f : c2; - code = dp->d_table[c1][c2]; - } - u = (u_char)(code >> 8); l = (u_char)code; @@ -186,15 +228,38 @@ ret = -1; break; } + + /* toupper,tolower */ + if (casetype == KICONV_LOWER && dp->t_ctp) { + code = towlower((uint16_t)code, dp->t_ctp); + u = (u_char)(code >> 8); + l = (u_char)code; + } + if (casetype == KICONV_UPPER && dp->t_ctp) { + code = towupper((uint16_t)code, dp->t_ctp); + u = (u_char)(code >> 8); + l = (u_char)code; + } + *dst++ = u; *dst++ = l; or -= 2; } else { - if ((casetype == KICONV_LOWER && code & XLAT16_HAS_LOWER_CASE) || - (casetype == KICONV_UPPER && code & XLAT16_HAS_UPPER_CASE)) - *dst++ = (u_char)(code >> 16); - else - *dst++ = l; + /* toupper,tolower */ + if (casetype == KICONV_LOWER) { + if (dp->t_ctp) + l = (u_char)towlower(l, dp->t_ctp); + else if (code & XLAT16_HAS_LOWER_CASE) + l = (u_char)(code >> 16); + } + if (casetype == KICONV_UPPER) { + if (dp->t_ctp) + l = (u_char)towupper(l, dp->t_ctp); + else if (code & XLAT16_HAS_UPPER_CASE) + l = (u_char)(code >> 16); + } + + *dst++ = l; or--; } @@ -232,6 +297,55 @@ return ("xlat16"); } +static int +iconv_xlat16_tolower(void *d2p, register int c) +{ + struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p; + register int c1, c2, out; + + if (c < 0x100) { + c1 = C2I1(c << 8); + c2 = C2I2(c << 8); + } else if (c < 0x10000) { + c1 = C2I1(c); + c2 = C2I2(c); + } else + return (c); + + if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_LOWER_CASE) { + /*return (int)(dp->d_table[c1][c2] & 0xffff);*/ + out = dp->d_table[c1][c2] & 0xffff; + if ((out & 0xff) == 0) + out = (out >> 8) & 0xff; + return (out); + } else + return (c); +} + +static int +iconv_xlat16_toupper(void *d2p, register int c) +{ + struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p; + register int c1, c2, out; + + if (c < 0x100) { + c1 = C2I1(c << 8); + c2 = C2I2(c << 8); + } else if (c < 0x10000) { + c1 = C2I1(c); + c2 = C2I2(c); + } else + return (c); + + if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_UPPER_CASE) { + out = dp->d_table[c1][c2] & 0xffff; + if ((out & 0xff) == 0) + out = (out >> 8) & 0xff; + return (out); + } else + return (c); +} + static kobj_method_t iconv_xlat16_methods[] = { KOBJMETHOD(iconv_converter_open, iconv_xlat16_open), KOBJMETHOD(iconv_converter_close, iconv_xlat16_close), @@ -241,6 +355,8 @@ KOBJMETHOD(iconv_converter_done, iconv_xlat16_done), #endif KOBJMETHOD(iconv_converter_name, iconv_xlat16_name), + KOBJMETHOD(iconv_converter_tolower, iconv_xlat16_tolower), + KOBJMETHOD(iconv_converter_toupper, iconv_xlat16_toupper), {0, 0} };