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 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/systm.h> 33 #include <sys/malloc.h> 34 #include <sys/iconv.h> 35 36 #include "iconv_converter_if.h" 37 38 /* 39 * "XLAT16" converter 40 */ 41 42 #ifdef MODULE_DEPEND 43 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2); 44 #endif 45 46 /* 47 * XLAT16 converter instance 48 */ 49 struct iconv_xlat16 { 50 KOBJ_FIELDS; 51 uint32_t * d_table[0x200]; 52 struct iconv_cspair * d_csp; 53 }; 54 55 static int 56 iconv_xlat16_open(struct iconv_converter_class *dcp, 57 struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp) 58 { 59 struct iconv_xlat16 *dp; 60 uint32_t *headp, *idxp, dist = 0; 61 int i; 62 63 dp = (struct iconv_xlat16 *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK); 64 headp = idxp = (uint32_t *)csp->cp_data; 65 dist = 0x200; 66 for (i = 0 ; i < 0x200 ; i++) { 67 if (*idxp) { 68 dp->d_table[i] = headp + dist; 69 dist += 0x80; 70 } else { 71 dp->d_table[i] = NULL; 72 } 73 idxp++; 74 } 75 dp->d_csp = csp; 76 csp->cp_refcount++; 77 *dpp = (void*)dp; 78 return (0); 79 } 80 81 static int 82 iconv_xlat16_close(void *data) 83 { 84 struct iconv_xlat16 *dp = data; 85 86 dp->d_csp->cp_refcount--; 87 kobj_delete((struct kobj*)data, M_ICONV); 88 return (0); 89 } 90 91 static int 92 iconv_xlat16_conv(void *d2p, const char **inbuf, 93 size_t *inbytesleft, char **outbuf, size_t *outbytesleft, 94 int convchar, int casetype) 95 { 96 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p; 97 const char *src; 98 char *dst; 99 int ret = 0; 100 size_t in, on, ir, or, inlen; 101 uint32_t code; 102 u_char u, l; 103 u_int16_t c1, c2; 104 105 if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL) 106 return (0); 107 ir = in = *inbytesleft; 108 or = on = *outbytesleft; 109 src = *inbuf; 110 dst = *outbuf; 111 112 while(ir > 0 && or > 0) { 113 114 inlen = 0; 115 code = '\0'; 116 117 c1 = ir > 1 ? *(src+1) & 0xff : 0; 118 c2 = *src & 0xff; 119 120 c1 = c2 & 0x80 ? c1 | 0x100 : c1; 121 c2 = c2 & 0x80 ? c2 & 0x7f : c2; 122 123 if (ir > 1 && dp->d_table[c1]) { 124 /* 125 * inbuf char is a double byte char 126 */ 127 code = dp->d_table[c1][c2]; 128 if (code) 129 inlen = 2; 130 } 131 132 if (inlen == 0) { 133 c1 &= 0xff00; 134 if (!dp->d_table[c1]) { 135 ret = -1; 136 break; 137 } 138 /* 139 * inbuf char is a single byte char 140 */ 141 inlen = 1; 142 code = dp->d_table[c1][c2]; 143 if (!code) { 144 ret = -1; 145 break; 146 } 147 } 148 149 if ((inlen == 1) && (code & XLAT16_ACCEPT_NULL_IN)) { 150 /* 151 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte 152 */ 153 ret = -1; 154 break; 155 } 156 157 /* 158 * now start translation 159 */ 160 u = (u_char)(code >> 8); 161 l = (u_char)code; 162 163 #ifdef XLAT16_ACCEPT_3BYTE_CHR 164 if (code & XLAT16_IS_3BYTE_CHR) { 165 if (or < 3) { 166 ret = -1; 167 break; 168 } 169 *dst++ = u; 170 *dst++ = l; 171 *dst++ = (u_char)(code >> 16); 172 or -= 3; 173 } else 174 #endif 175 if (u || code & XLAT16_ACCEPT_NULL_OUT) { 176 if (or < 2) { 177 ret = -1; 178 break; 179 } 180 *dst++ = u; 181 *dst++ = l; 182 or -= 2; 183 } else { 184 if ((casetype == KICONV_LOWER && code & XLAT16_HAS_LOWER_CASE) || 185 (casetype == KICONV_UPPER && code & XLAT16_HAS_UPPER_CASE)) 186 *dst++ = (u_char)(code >> 16); 187 else if ((casetype == KICONV_FROM_LOWER && code & XLAT16_HAS_FROM_LOWER_CASE) || 188 (casetype == KICONV_FROM_UPPER && code & XLAT16_HAS_FROM_UPPER_CASE)) 189 *dst++ = dp->d_table[0][(u_char)(code >> 16)]; 190 else 191 *dst++ = l; 192 or--; 193 } 194 195 if (inlen == 2) { 196 /* 197 * there is a case that inbuf char is a single 198 * byte char while inlen == 2 199 */ 200 if ((u_char)*(src+1) == 0 && 201 (code & XLAT16_ACCEPT_NULL_IN) == 0 ) { 202 src++; 203 ir--; 204 } else { 205 src += 2; 206 ir -= 2; 207 } 208 } else { 209 src++; 210 ir--; 211 } 212 213 if (convchar == 1) 214 break; 215 } 216 217 *inbuf += in - ir; 218 *outbuf += on - or; 219 *inbytesleft -= in - ir; 220 *outbytesleft -= on - or; 221 return (ret); 222 } 223 224 static const char * 225 iconv_xlat16_name(struct iconv_converter_class *dcp) 226 { 227 return ("xlat16"); 228 } 229 230 static kobj_method_t iconv_xlat16_methods[] = { 231 KOBJMETHOD(iconv_converter_open, iconv_xlat16_open), 232 KOBJMETHOD(iconv_converter_close, iconv_xlat16_close), 233 KOBJMETHOD(iconv_converter_conv, iconv_xlat16_conv), 234 #if 0 235 KOBJMETHOD(iconv_converter_init, iconv_xlat16_init), 236 KOBJMETHOD(iconv_converter_done, iconv_xlat16_done), 237 #endif 238 KOBJMETHOD(iconv_converter_name, iconv_xlat16_name), 239 {0, 0} 240 }; 241 242 KICONV_CONVERTER(xlat16, sizeof(struct iconv_xlat16)); 243