1 /*- 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 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: head/sys/libkern/iconv_xlat16.c 194638 2009-06-22 17:09:46Z delphij $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/systm.h> 32 #include <sys/malloc.h> 33 #include <sys/iconv.h> 34 35 #include "iconv_converter_if.h" 36 37 /* 38 * "XLAT16" converter 39 */ 40 41 #ifdef MODULE_DEPEND 42 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2); 43 #endif 44 45 #define C2I1(c) ((c) & 0x8000 ? ((c) & 0xff) | 0x100 : (c) & 0xff) 46 #define C2I2(c) ((c) & 0x8000 ? ((c) >> 8) & 0x7f : ((c) >> 8) & 0xff) 47 48 /* 49 * XLAT16 converter instance 50 */ 51 struct iconv_xlat16 { 52 KOBJ_FIELDS; 53 uint32_t * d_table[0x200]; 54 void * f_ctp; 55 void * t_ctp; 56 struct iconv_cspair * d_csp; 57 }; 58 59 static int 60 iconv_xlat16_open(struct iconv_converter_class *dcp, 61 struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp) 62 { 63 struct iconv_xlat16 *dp; 64 uint32_t *headp, **idxp; 65 int i; 66 67 dp = (struct iconv_xlat16 *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK); 68 headp = (uint32_t *)((caddr_t)csp->cp_data + sizeof(dp->d_table)); 69 idxp = (uint32_t **)csp->cp_data; 70 for (i = 0 ; i < 0x200 ; i++) { 71 if (*idxp) { 72 dp->d_table[i] = headp; 73 headp += 0x80; 74 } else { 75 dp->d_table[i] = NULL; 76 } 77 idxp++; 78 } 79 80 if (strcmp(csp->cp_to, KICONV_WCTYPE_NAME) != 0) { 81 if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_from, &dp->f_ctp) != 0) 82 dp->f_ctp = NULL; 83 if (iconv_open(KICONV_WCTYPE_NAME, csp->cp_to, &dp->t_ctp) != 0) 84 dp->t_ctp = NULL; 85 } else { 86 dp->f_ctp = dp->t_ctp = dp; 87 } 88 89 dp->d_csp = csp; 90 csp->cp_refcount++; 91 *dpp = (void*)dp; 92 return (0); 93 } 94 95 static int 96 iconv_xlat16_close(void *data) 97 { 98 struct iconv_xlat16 *dp = data; 99 100 if (dp->f_ctp && dp->f_ctp != data) 101 iconv_close(dp->f_ctp); 102 if (dp->t_ctp && dp->t_ctp != data) 103 iconv_close(dp->t_ctp); 104 dp->d_csp->cp_refcount--; 105 kobj_delete((struct kobj*)data, M_ICONV); 106 return (0); 107 } 108 109 static int 110 iconv_xlat16_conv(void *d2p, const char **inbuf, 111 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, 112 int convchar, int casetype) 113 { 114 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p; 115 const char *src; 116 char *dst; 117 int nullin, ret = 0; 118 size_t in, on, ir, or, inlen; 119 uint32_t code; 120 u_char u, l; 121 uint16_t c1, c2, ctmp; 122 123 if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL) 124 return (0); 125 ir = in = *inbytesleft; 126 or = on = *outbytesleft; 127 src = *inbuf; 128 dst = *outbuf; 129 130 while(ir > 0 && or > 0) { 131 132 inlen = 0; 133 code = 0; 134 135 c1 = ir > 1 ? *(src+1) & 0xff : 0; 136 c2 = *src & 0xff; 137 ctmp = 0; 138 139 c1 = c2 & 0x80 ? c1 | 0x100 : c1; 140 c2 = c2 & 0x80 ? c2 & 0x7f : c2; 141 142 if (ir > 1 && dp->d_table[c1] && dp->d_table[c1][c2]) { 143 /* 144 * inbuf char is a double byte char 145 */ 146 inlen = 2; 147 148 /* toupper,tolower */ 149 if (casetype == KICONV_FROM_LOWER && dp->f_ctp) 150 ctmp = towlower(((u_char)*src << 8) | (u_char)*(src + 1), 151 dp->f_ctp); 152 else if (casetype == KICONV_FROM_UPPER && dp->f_ctp) 153 ctmp = towupper(((u_char)*src << 8) | (u_char)*(src + 1), 154 dp->f_ctp); 155 if (ctmp) { 156 c1 = C2I1(ctmp); 157 c2 = C2I2(ctmp); 158 } 159 } 160 161 if (inlen == 0) { 162 c1 &= 0xff00; 163 if (!dp->d_table[c1]) { 164 ret = -1; 165 break; 166 } 167 /* 168 * inbuf char is a single byte char 169 */ 170 inlen = 1; 171 172 if (casetype & (KICONV_FROM_LOWER|KICONV_FROM_UPPER)) 173 code = dp->d_table[c1][c2]; 174 175 if (casetype == KICONV_FROM_LOWER) { 176 if (dp->f_ctp) 177 ctmp = towlower((u_char)*src, dp->f_ctp); 178 else if (code & XLAT16_HAS_FROM_LOWER_CASE) 179 ctmp = (u_char)(code >> 16); 180 } else if (casetype == KICONV_FROM_UPPER) { 181 if (dp->f_ctp) 182 ctmp = towupper((u_char)*src, dp->f_ctp); 183 else if (code & XLAT16_HAS_FROM_UPPER_CASE) 184 ctmp = (u_char)(code >> 16); 185 } 186 if (ctmp) { 187 c1 = C2I1(ctmp << 8); 188 c2 = C2I2(ctmp << 8); 189 } 190 } 191 192 code = dp->d_table[c1][c2]; 193 if (!code) { 194 ret = -1; 195 break; 196 } 197 198 nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0; 199 if (inlen == 1 && nullin) { 200 /* 201 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte 202 */ 203 ret = -1; 204 break; 205 } 206 207 /* 208 * now start translation 209 */ 210 u = (u_char)(code >> 8); 211 l = (u_char)code; 212 213 #ifdef XLAT16_ACCEPT_3BYTE_CHR 214 if (code & XLAT16_IS_3BYTE_CHR) { 215 if (or < 3) { 216 ret = -1; 217 break; 218 } 219 *dst++ = u; 220 *dst++ = l; 221 *dst++ = (u_char)(code >> 16); 222 or -= 3; 223 } else 224 #endif 225 if (u || code & XLAT16_ACCEPT_NULL_OUT) { 226 if (or < 2) { 227 ret = -1; 228 break; 229 } 230 231 /* toupper,tolower */ 232 if (casetype == KICONV_LOWER && dp->t_ctp) { 233 code = towlower((uint16_t)code, dp->t_ctp); 234 u = (u_char)(code >> 8); 235 l = (u_char)code; 236 } 237 if (casetype == KICONV_UPPER && dp->t_ctp) { 238 code = towupper((uint16_t)code, dp->t_ctp); 239 u = (u_char)(code >> 8); 240 l = (u_char)code; 241 } 242 243 *dst++ = u; 244 *dst++ = l; 245 or -= 2; 246 } else { 247 /* toupper,tolower */ 248 if (casetype == KICONV_LOWER) { 249 if (dp->t_ctp) 250 l = (u_char)towlower(l, dp->t_ctp); 251 else if (code & XLAT16_HAS_LOWER_CASE) 252 l = (u_char)(code >> 16); 253 } 254 if (casetype == KICONV_UPPER) { 255 if (dp->t_ctp) 256 l = (u_char)towupper(l, dp->t_ctp); 257 else if (code & XLAT16_HAS_UPPER_CASE) 258 l = (u_char)(code >> 16); 259 } 260 261 *dst++ = l; 262 or--; 263 } 264 265 if (inlen == 2) { 266 /* 267 * there is a case that inbuf char is a single 268 * byte char while inlen == 2 269 */ 270 if ((u_char)*(src+1) == 0 && !nullin ) { 271 src++; 272 ir--; 273 } else { 274 src += 2; 275 ir -= 2; 276 } 277 } else { 278 src++; 279 ir--; 280 } 281 282 if (convchar == 1) 283 break; 284 } 285 286 *inbuf += in - ir; 287 *outbuf += on - or; 288 *inbytesleft -= in - ir; 289 *outbytesleft -= on - or; 290 return (ret); 291 } 292 293 static const char * 294 iconv_xlat16_name(struct iconv_converter_class *dcp) 295 { 296 return ("xlat16"); 297 } 298 299 static int 300 iconv_xlat16_tolower(void *d2p, int c) 301 { 302 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p; 303 int c1, c2, out; 304 305 if (c < 0x100) { 306 c1 = C2I1(c << 8); 307 c2 = C2I2(c << 8); 308 } else if (c < 0x10000) { 309 c1 = C2I1(c); 310 c2 = C2I2(c); 311 } else 312 return (c); 313 314 if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_LOWER_CASE) { 315 /*return (int)(dp->d_table[c1][c2] & 0xffff);*/ 316 out = dp->d_table[c1][c2] & 0xffff; 317 if ((out & 0xff) == 0) 318 out = (out >> 8) & 0xff; 319 return (out); 320 } else 321 return (c); 322 } 323 324 static int 325 iconv_xlat16_toupper(void *d2p, int c) 326 { 327 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p; 328 int c1, c2, out; 329 330 if (c < 0x100) { 331 c1 = C2I1(c << 8); 332 c2 = C2I2(c << 8); 333 } else if (c < 0x10000) { 334 c1 = C2I1(c); 335 c2 = C2I2(c); 336 } else 337 return (c); 338 339 if (dp->d_table[c1] && dp->d_table[c1][c2] & XLAT16_HAS_UPPER_CASE) { 340 out = dp->d_table[c1][c2] & 0xffff; 341 if ((out & 0xff) == 0) 342 out = (out >> 8) & 0xff; 343 return (out); 344 } else 345 return (c); 346 } 347 348 static kobj_method_t iconv_xlat16_methods[] = { 349 KOBJMETHOD(iconv_converter_open, iconv_xlat16_open), 350 KOBJMETHOD(iconv_converter_close, iconv_xlat16_close), 351 KOBJMETHOD(iconv_converter_conv, iconv_xlat16_conv), 352 #if 0 353 KOBJMETHOD(iconv_converter_init, iconv_xlat16_init), 354 KOBJMETHOD(iconv_converter_done, iconv_xlat16_done), 355 #endif 356 KOBJMETHOD(iconv_converter_name, iconv_xlat16_name), 357 KOBJMETHOD(iconv_converter_tolower, iconv_xlat16_tolower), 358 KOBJMETHOD(iconv_converter_toupper, iconv_xlat16_toupper), 359 KOBJMETHOD_END 360 }; 361 362 KICONV_CONVERTER(xlat16, sizeof(struct iconv_xlat16)); 363