1 /* $NetBSD: citrus_mskanji.c,v 1.12 2007/03/05 16:57:06 tnozaki 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 * ja_JP.SJIS locale table for BSD4.4/rune 31 * version 1.0 32 * (C) Sin'ichiro MIYATANI / Phase One, Inc 33 * May 12, 1995 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgement: 45 * This product includes software developed by Phase One, Inc. 46 * 4. The name of Phase One, Inc. may be used to endorse or promote products 47 * derived from this software without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 */ 61 62 63 #include <sys/cdefs.h> 64 #if defined(LIBC_SCCS) && !defined(lint) 65 __RCSID("$NetBSD: citrus_mskanji.c,v 1.12 2007/03/05 16:57:06 tnozaki Exp $"); 66 #endif /* LIBC_SCCS and not lint */ 67 68 #include <assert.h> 69 #include <errno.h> 70 #include <string.h> 71 #include <stdio.h> 72 #include <stdlib.h> 73 #include <stddef.h> 74 #include <locale.h> 75 #include <wchar.h> 76 #include <sys/types.h> 77 #include <limits.h> 78 79 #include "citrus_namespace.h" 80 #include "citrus_types.h" 81 #include "citrus_bcs.h" 82 #include "citrus_module.h" 83 #include "citrus_ctype.h" 84 #include "citrus_stdenc.h" 85 #include "citrus_mskanji.h" 86 87 88 /* ---------------------------------------------------------------------- 89 * private stuffs used by templates 90 */ 91 92 typedef struct _MSKanjiState { 93 char ch[2]; 94 int chlen; 95 } _MSKanjiState; 96 97 typedef struct { 98 int mode; 99 #define MODE_JIS2004 1 100 } _MSKanjiEncodingInfo; 101 102 typedef struct { 103 _MSKanjiEncodingInfo ei; 104 struct { 105 /* for future multi-locale facility */ 106 _MSKanjiState s_mblen; 107 _MSKanjiState s_mbrlen; 108 _MSKanjiState s_mbrtowc; 109 _MSKanjiState s_mbtowc; 110 _MSKanjiState s_mbsrtowcs; 111 _MSKanjiState s_wcrtomb; 112 _MSKanjiState s_wcsrtombs; 113 _MSKanjiState s_wctomb; 114 } states; 115 } _MSKanjiCTypeInfo; 116 117 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 118 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 119 120 #define _FUNCNAME(m) _citrus_MSKanji_##m 121 #define _ENCODING_INFO _MSKanjiEncodingInfo 122 #define _CTYPE_INFO _MSKanjiCTypeInfo 123 #define _ENCODING_STATE _MSKanjiState 124 #define _ENCODING_MB_CUR_MAX(_ei_) 2 125 #define _ENCODING_IS_STATE_DEPENDENT 0 126 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 127 128 129 static int 130 _mskanji1(int c) 131 { 132 133 if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) 134 return 1; 135 else 136 return 0; 137 } 138 139 static int 140 _mskanji2(int c) 141 { 142 143 if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc)) 144 return 1; 145 else 146 return 0; 147 } 148 149 static __inline void 150 /*ARGSUSED*/ 151 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei, 152 _MSKanjiState * __restrict s) 153 { 154 s->chlen = 0; 155 } 156 157 static __inline void 158 /*ARGSUSED*/ 159 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei, 160 void * __restrict pspriv, 161 const _MSKanjiState * __restrict s) 162 { 163 memcpy(pspriv, (const void *)s, sizeof(*s)); 164 } 165 166 static __inline void 167 /*ARGSUSED*/ 168 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei, 169 _MSKanjiState * __restrict s, 170 const void * __restrict pspriv) 171 { 172 memcpy((void *)s, pspriv, sizeof(*s)); 173 } 174 175 static int 176 /*ARGSUSED*/ 177 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei, 178 wchar_t * __restrict pwc, 179 const char ** __restrict s, size_t n, 180 _MSKanjiState * __restrict psenc, 181 size_t * __restrict nresult) 182 { 183 wchar_t wchar; 184 int len; 185 int chlenbak; 186 const char *s0; 187 188 _DIAGASSERT(nresult != 0); 189 _DIAGASSERT(ei != NULL); 190 _DIAGASSERT(s != NULL); 191 _DIAGASSERT(psenc != NULL); 192 193 s0 = *s; 194 195 if (s0 == NULL) { 196 _citrus_MSKanji_init_state(ei, psenc); 197 *nresult = 0; /* state independent */ 198 return (0); 199 } 200 201 chlenbak = psenc->chlen; 202 203 /* make sure we have the first byte in the buffer */ 204 switch (psenc->chlen) { 205 case 0: 206 if (n < 1) 207 goto restart; 208 psenc->ch[0] = *s0++; 209 psenc->chlen = 1; 210 n--; 211 break; 212 case 1: 213 break; 214 default: 215 /* illegal state */ 216 goto encoding_error; 217 } 218 219 len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1; 220 while (psenc->chlen < len) { 221 if (n < 1) 222 goto restart; 223 psenc->ch[psenc->chlen] = *s0++; 224 psenc->chlen++; 225 n--; 226 } 227 228 *s = s0; 229 230 switch (len) { 231 case 1: 232 wchar = psenc->ch[0] & 0xff; 233 break; 234 case 2: 235 if (!_mskanji2(psenc->ch[1] & 0xff)) 236 goto encoding_error; 237 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); 238 break; 239 default: 240 /* illegal state */ 241 goto encoding_error; 242 } 243 244 psenc->chlen = 0; 245 246 if (pwc) 247 *pwc = wchar; 248 249 if (!wchar) 250 *nresult = 0; 251 else 252 *nresult = len - chlenbak; 253 254 return (0); 255 256 encoding_error: 257 psenc->chlen = 0; 258 *nresult = (size_t)-1; 259 return (EILSEQ); 260 261 restart: 262 *nresult = (size_t)-2; 263 *s = s0; 264 return (0); 265 } 266 267 268 static int 269 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei, 270 char * __restrict s, size_t n, wchar_t wc, 271 _MSKanjiState * __restrict psenc, 272 size_t * __restrict nresult) 273 { 274 int ret; 275 276 _DIAGASSERT(ei != NULL); 277 _DIAGASSERT(psenc != NULL); 278 _DIAGASSERT(s != NULL); 279 280 /* check invalid sequence */ 281 if (wc & ~0xffff) { 282 ret = EILSEQ; 283 goto err; 284 } 285 286 if (wc & 0xff00) { 287 if (n < 2) { 288 ret = E2BIG; 289 goto err; 290 } 291 292 s[0] = (wc >> 8) & 0xff; 293 s[1] = wc & 0xff; 294 if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) { 295 ret = EILSEQ; 296 goto err; 297 } 298 299 *nresult = 2; 300 return 0; 301 } else { 302 if (n < 1) { 303 ret = E2BIG; 304 goto err; 305 } 306 307 s[0] = wc & 0xff; 308 if (_mskanji1(s[0] & 0xff)) { 309 ret = EILSEQ; 310 goto err; 311 } 312 313 *nresult = 1; 314 return 0; 315 } 316 317 err: 318 *nresult = (size_t)-1; 319 return ret; 320 } 321 322 323 static __inline int 324 /*ARGSUSED*/ 325 _citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei, 326 _csid_t * __restrict csid, 327 _index_t * __restrict idx, wchar_t wc) 328 { 329 _index_t row, col; 330 int offset; 331 332 _DIAGASSERT(csid != NULL && idx != NULL); 333 334 if ((_wc_t)wc < 0x80) { 335 /* ISO-646 */ 336 *csid = 0; 337 *idx = (_index_t)wc; 338 } else if ((_wc_t)wc < 0x100) { 339 /* KANA */ 340 *csid = 1; 341 *idx = (_index_t)wc & 0x7F; 342 } else { 343 /* Kanji (containing Gaiji zone) */ 344 /* 345 * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)): 346 * 0x8140 - 0x817E -> 0x2121 - 0x215F 347 * 0x8180 - 0x819E -> 0x2160 - 0x217E 348 * 0x819F - 0x81FC -> 0x2221 - 0x227E 349 * 350 * 0x8240 - 0x827E -> 0x2321 - 0x235F 351 * ... 352 * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E 353 * 354 * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F 355 * ... 356 * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E 357 * 358 * extended Gaiji zone: 359 * 0xF040 - 0xFCFC 360 * 361 * JIS X0213-plane2: 362 * 0xF040 - 0xF09E -> 0x2121 - 0x217E 363 * 0xF140 - 0xF19E -> 0x2321 - 0x237E 364 * ... 365 * 0xF240 - 0xF29E -> 0x2521 - 0x257E 366 * 367 * 0xF09F - 0xF0FC -> 0x2821 - 0x287E 368 * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E 369 * ... 370 * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E 371 * 372 * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E 373 * ... 374 * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E 375 */ 376 row = ((_wc_t)wc >> 8) & 0xFF; 377 col = (_wc_t)wc & 0xFF; 378 if (!_mskanji1(row) || !_mskanji2(col)) 379 return EILSEQ; 380 if ((ei->mode & MODE_JIS2004) == 0 || row < 0xF0) { 381 *csid = 2; 382 offset = 0x81; 383 } else { 384 *csid = 3; 385 if ((_wc_t)wc <= 0xF49E) { 386 offset = (_wc_t)wc >= 0xF29F || 387 ((_wc_t)wc >= 0xF09F && (_wc_t)wc <= 0xF0FC) 388 ? 0xED : 0xF0; 389 } else 390 offset = 0xCE; 391 } 392 row -= offset; 393 if (row >= 0x5F) 394 row -= 0x40; 395 row = row * 2 + 0x21; 396 col -= 0x1F; 397 if (col >= 0x61) 398 col -= 1; 399 if (col > 0x7E) { 400 row += 1; 401 col -= 0x5E; 402 } 403 *idx = ((_index_t)row << 8) | col; 404 } 405 406 return 0; 407 } 408 409 static __inline int 410 /*ARGSUSED*/ 411 _citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei, 412 wchar_t * __restrict wc, 413 _csid_t csid, _index_t idx) 414 { 415 u_int32_t row, col; 416 int offset; 417 418 _DIAGASSERT(wc != NULL); 419 420 switch (csid) { 421 case 0: 422 /* ISO-646 */ 423 if (idx >= 0x80) 424 return EILSEQ; 425 *wc = (wchar_t)idx; 426 break; 427 case 1: 428 /* kana */ 429 if (idx >= 0x80) 430 return EILSEQ; 431 *wc = (wchar_t)idx + 0x80; 432 break; 433 case 3: 434 if ((ei->mode & MODE_JIS2004) == 0) 435 return EILSEQ; 436 /*FALLTHROUGH*/ 437 case 2: 438 /* kanji */ 439 row = (idx >> 8); 440 if (row < 0x21) 441 return EILSEQ; 442 if (csid == 3) { 443 if (row <= 0x2F) 444 offset = (row == 0x22 || row >= 0x26) 445 ? 0xED : 0xF0; 446 else if (row >= 0x4D && row <= 0x7E) 447 offset = 0xCE; 448 else 449 return EILSEQ; 450 } else { 451 if (row > 0x97) 452 return EILSEQ; 453 offset = (row < 0x5F) ? 0x81 : 0xC1; 454 } 455 col = idx & 0xFF; 456 if (col < 0x21 || col > 0x7E) 457 return EILSEQ; 458 row -= 0x21; col -= 0x21; 459 if ((row & 1) == 0) { 460 col += 0x40; 461 if (col >= 0x7F) 462 col += 1; 463 } else 464 col += 0x9F; 465 row = row / 2 + offset; 466 *wc = ((wchar_t)row << 8) | col; 467 break; 468 default: 469 return EILSEQ; 470 } 471 472 return 0; 473 } 474 475 static __inline int 476 /*ARGSUSED*/ 477 _citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * __restrict ei, 478 _MSKanjiState * __restrict psenc, 479 int * __restrict rstate) 480 { 481 482 if (psenc->chlen == 0) 483 *rstate = _STDENC_SDGEN_INITIAL; 484 else 485 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; 486 487 return 0; 488 } 489 490 static int 491 /*ARGSUSED*/ 492 _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei, 493 const void * __restrict var, 494 size_t lenvar) 495 { 496 const char *p; 497 498 _DIAGASSERT(ei != NULL); 499 500 p = var; 501 #define MATCH(x, act) \ 502 do { \ 503 if (lenvar >= (sizeof(#x)-1) && \ 504 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ 505 act; \ 506 lenvar -= sizeof(#x)-1; \ 507 p += sizeof(#x)-1; \ 508 } \ 509 } while (/*CONSTCOND*/0) 510 memset((void *)ei, 0, sizeof(*ei)); 511 while (lenvar > 0) { 512 switch (_bcs_toupper(*p)) { 513 case 'J': 514 MATCH(JIS2004, ei->mode |= MODE_JIS2004); 515 break; 516 } 517 ++p; 518 --lenvar; 519 } 520 521 return 0; 522 } 523 524 static void 525 _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei) 526 { 527 } 528 529 /* ---------------------------------------------------------------------- 530 * public interface for ctype 531 */ 532 533 _CITRUS_CTYPE_DECLS(MSKanji); 534 _CITRUS_CTYPE_DEF_OPS(MSKanji); 535 536 #include "citrus_ctype_template.h" 537 538 /* ---------------------------------------------------------------------- 539 * public interface for stdenc 540 */ 541 542 _CITRUS_STDENC_DECLS(MSKanji); 543 _CITRUS_STDENC_DEF_OPS(MSKanji); 544 545 #include "citrus_stdenc_template.h" 546