1 /* $NetBSD: citrus_euctw.c,v 1.6 2003/06/25 09:51:42 tshiozak Exp $ */ 2 3 /*- 4 * Copyright (c)2002 Citrus Project, 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /*- 30 * Copyright (c)1999 Citrus Project, 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * 54 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $ 55 */ 56 57 #include <sys/cdefs.h> 58 #if defined(LIBC_SCCS) && !defined(lint) 59 __RCSID("$NetBSD: citrus_euctw.c,v 1.6 2003/06/25 09:51:42 tshiozak Exp $"); 60 #endif /* LIBC_SCCS and not lint */ 61 62 #include <assert.h> 63 #include <errno.h> 64 #include <string.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <stddef.h> 68 #include <locale.h> 69 #include <wchar.h> 70 #include <sys/types.h> 71 #include <limits.h> 72 73 #include "citrus_namespace.h" 74 #include "citrus_types.h" 75 #include "citrus_module.h" 76 #include "citrus_ctype.h" 77 #include "citrus_stdenc.h" 78 #include "citrus_euctw.h" 79 80 81 /* ---------------------------------------------------------------------- 82 * private stuffs used by templates 83 */ 84 85 typedef struct { 86 char ch[4]; 87 int chlen; 88 } _EUCTWState; 89 90 typedef struct { 91 int dummy; 92 } _EUCTWEncodingInfo; 93 typedef struct { 94 _EUCTWEncodingInfo ei; 95 struct { 96 /* for future multi-locale facility */ 97 _EUCTWState s_mblen; 98 _EUCTWState s_mbrlen; 99 _EUCTWState s_mbrtowc; 100 _EUCTWState s_mbtowc; 101 _EUCTWState s_mbsrtowcs; 102 _EUCTWState s_wcrtomb; 103 _EUCTWState s_wcsrtombs; 104 _EUCTWState s_wctomb; 105 } states; 106 } _EUCTWCTypeInfo; 107 108 #define _SS2 0x008e 109 #define _SS3 0x008f 110 111 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 112 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 113 114 #define _FUNCNAME(m) _citrus_EUCTW_##m 115 #define _ENCODING_INFO _EUCTWEncodingInfo 116 #define _CTYPE_INFO _EUCTWCTypeInfo 117 #define _ENCODING_STATE _EUCTWState 118 #define _ENCODING_MB_CUR_MAX(_ei_) 4 119 #define _ENCODING_IS_STATE_DEPENDENT 0 120 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 121 122 static __inline int 123 _citrus_EUCTW_cs(u_int c) 124 { 125 c &= 0xff; 126 127 return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0); 128 } 129 130 static __inline int 131 _citrus_EUCTW_count(int cs) 132 { 133 switch (cs) { 134 case 0: 135 return 1; 136 case 1: 137 return 2; 138 case 2: 139 return 4; 140 case 3: 141 abort(); 142 /*NOTREACHED*/ 143 } 144 return 0; 145 } 146 147 static __inline void 148 /*ARGSUSED*/ 149 _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei, 150 _EUCTWState * __restrict s) 151 { 152 memset(s, 0, sizeof(*s)); 153 } 154 155 static __inline void 156 /*ARGSUSED*/ 157 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei, 158 void * __restrict pspriv, 159 const _EUCTWState * __restrict s) 160 { 161 memcpy(pspriv, (const void *)s, sizeof(*s)); 162 } 163 164 static __inline void 165 /*ARGSUSED*/ 166 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei, 167 _EUCTWState * __restrict s, 168 const void * __restrict pspriv) 169 { 170 memcpy((void *)s, pspriv, sizeof(*s)); 171 } 172 173 static int 174 /*ARGSUSED*/ 175 _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei, 176 const void * __restrict var, size_t lenvar) 177 { 178 179 _DIAGASSERT(ei != NULL); 180 181 memset((void *)ei, 0, sizeof(*ei)); 182 183 return 0; 184 } 185 186 static void 187 /*ARGSUSED*/ 188 _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei) 189 { 190 } 191 192 static int 193 _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei, 194 wchar_t * __restrict pwc, 195 const char ** __restrict s, 196 size_t n, _EUCTWState * __restrict psenc, 197 size_t * __restrict nresult) 198 { 199 wchar_t wchar; 200 int c, cs; 201 int chlenbak; 202 const char *s0; 203 204 _DIAGASSERT(nresult != 0); 205 _DIAGASSERT(ei != NULL); 206 _DIAGASSERT(psenc != NULL); 207 _DIAGASSERT(s != NULL); 208 209 s0 = *s; 210 211 if (s0 == NULL) { 212 _citrus_EUCTW_init_state(ei, psenc); 213 *nresult = 0; /* state independent */ 214 return (0); 215 } 216 217 /* make sure we have the first byte in the buffer */ 218 switch (psenc->chlen) { 219 case 0: 220 if (n < 1) 221 goto restart; 222 psenc->ch[0] = *s0++; 223 psenc->chlen = 1; 224 n--; 225 break; 226 case 1: 227 case 2: 228 break; 229 default: 230 /* illgeal state */ 231 goto ilseq; 232 } 233 234 c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff)); 235 if (c == 0) 236 goto ilseq; 237 while (psenc->chlen < c) { 238 if (n < 1) 239 goto ilseq; 240 psenc->ch[psenc->chlen] = *s0++; 241 psenc->chlen++; 242 n--; 243 } 244 245 wchar = 0; 246 switch (cs) { 247 case 0: 248 if (psenc->ch[0] & 0x80) 249 goto ilseq; 250 wchar = psenc->ch[0] & 0xff; 251 break; 252 case 1: 253 if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80)) 254 goto ilseq; 255 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); 256 wchar |= 'G' << 24; 257 break; 258 case 2: 259 if ((u_char)psenc->ch[1] < 0xa1 || 0xa7 < (u_char)psenc->ch[1]) 260 goto ilseq; 261 if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80)) 262 goto ilseq; 263 wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff); 264 wchar |= ('G' + psenc->ch[1] - 0xa1) << 24; 265 break; 266 default: 267 goto ilseq; 268 } 269 270 *s = s0; 271 psenc->chlen = 0; 272 273 if (pwc) 274 *pwc = wchar; 275 276 if (!wchar) 277 *nresult = 0; 278 else 279 *nresult = c - chlenbak; 280 281 return (0); 282 283 ilseq: 284 psenc->chlen = 0; 285 *nresult = (size_t)-1; 286 return (EILSEQ); 287 288 restart: 289 *s = s0; 290 *nresult = (size_t)-1; 291 return (0); 292 } 293 294 static int 295 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei, 296 char * __restrict s, size_t n, wchar_t wc, 297 _EUCTWState * __restrict psenc, 298 size_t * __restrict nresult) 299 { 300 wchar_t cs = wc & 0x7f000080; 301 wchar_t v; 302 int i, len, clen, ret; 303 304 _DIAGASSERT(ei != NULL); 305 _DIAGASSERT(nresult != 0); 306 _DIAGASSERT(s != NULL); 307 308 /* reset state */ 309 if (wc == 0) { 310 *nresult = 0; /* stateless */ 311 return 0; 312 } 313 314 clen = 1; 315 if (wc & 0x00007f00) 316 clen = 2; 317 if ((wc & 0x007f0000) && !(wc & 0x00800000)) 318 clen = 3; 319 320 if (clen == 1 && cs == 0x00000000) { 321 /* ASCII */ 322 len = 1; 323 if (n < len) { 324 ret = E2BIG; 325 goto err; 326 } 327 v = wc & 0x0000007f; 328 } else if (clen == 2 && cs == ('G' << 24)) { 329 /* CNS-11643-1 */ 330 len = 2; 331 if (n < len) { 332 ret = E2BIG; 333 goto err; 334 } 335 v = wc & 0x00007f7f; 336 v |= 0x00008080; 337 } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') { 338 /* CNS-11643-[2-7] */ 339 len = 4; 340 if (n < len) { 341 ret = E2BIG; 342 goto err; 343 } 344 *s++ = _SS2; 345 *s++ = (cs >> 24) - 'H' + 0xa2; 346 v = wc & 0x00007f7f; 347 v |= 0x00008080; 348 } else { 349 ret = EILSEQ; 350 goto err; 351 } 352 353 i = clen; 354 while (i-- > 0) 355 *s++ = (v >> (i << 3)) & 0xff; 356 357 *nresult = len; 358 return 0; 359 360 err: 361 *nresult = (size_t)-1; 362 return ret; 363 } 364 365 static __inline int 366 /*ARGSUSED*/ 367 _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei, 368 _csid_t * __restrict csid, 369 _index_t * __restrict idx, wchar_t wc) 370 { 371 372 _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL); 373 374 *csid = (_csid_t)(wc >> 24) & 0xFF; 375 *idx = (_index_t)(wc & 0x7F7F); 376 377 return (0); 378 } 379 380 static __inline int 381 /*ARGSUSED*/ 382 _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei, 383 wchar_t * __restrict wc, 384 _csid_t csid, _index_t idx) 385 { 386 387 _DIAGASSERT(ei != NULL && wc != NULL); 388 389 if (csid > 7 || (idx & ~0x7F7F) != 0) 390 return (EINVAL); 391 392 if (csid==0) { 393 if ((idx & ~0x7F) != 0) 394 return (EINVAL); 395 *wc = (wchar_t)idx; 396 } else { 397 if ((idx & ~0x7F7F) != 0) 398 return (EINVAL); 399 *wc = (wchar_t)idx | ((wchar_t)csid<<24); 400 } 401 402 return (0); 403 } 404 405 /* ---------------------------------------------------------------------- 406 * public interface for ctype 407 */ 408 409 _CITRUS_CTYPE_DECLS(EUCTW); 410 _CITRUS_CTYPE_DEF_OPS(EUCTW); 411 412 #include "citrus_ctype_template.h" 413 414 /* ---------------------------------------------------------------------- 415 * public interface for stdenc 416 */ 417 418 _CITRUS_STDENC_DECLS(EUCTW); 419 _CITRUS_STDENC_DEF_OPS(EUCTW); 420 421 #include "citrus_stdenc_template.h" 422