1 /* $NetBSD: citrus_mskanji.c,v 1.13 2008/06/14 16:01:08 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.13 2008/06/14 16:01:08 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 <wchar.h> 75 #include <sys/types.h> 76 #include <limits.h> 77 78 #include "citrus_namespace.h" 79 #include "citrus_types.h" 80 #include "citrus_bcs.h" 81 #include "citrus_module.h" 82 #include "citrus_ctype.h" 83 #include "citrus_stdenc.h" 84 #include "citrus_mskanji.h" 85 86 87 /* ---------------------------------------------------------------------- 88 * private stuffs used by templates 89 */ 90 91 typedef struct _MSKanjiState { 92 char ch[2]; 93 int chlen; 94 } _MSKanjiState; 95 96 typedef struct { 97 int mode; 98 #define MODE_JIS2004 1 99 } _MSKanjiEncodingInfo; 100 101 typedef struct { 102 _MSKanjiEncodingInfo ei; 103 struct { 104 /* for future multi-locale facility */ 105 _MSKanjiState s_mblen; 106 _MSKanjiState s_mbrlen; 107 _MSKanjiState s_mbrtowc; 108 _MSKanjiState s_mbtowc; 109 _MSKanjiState s_mbsrtowcs; 110 _MSKanjiState s_wcrtomb; 111 _MSKanjiState s_wcsrtombs; 112 _MSKanjiState s_wctomb; 113 } states; 114 } _MSKanjiCTypeInfo; 115 116 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei) 117 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ 118 119 #define _FUNCNAME(m) _citrus_MSKanji_##m 120 #define _ENCODING_INFO _MSKanjiEncodingInfo 121 #define _CTYPE_INFO _MSKanjiCTypeInfo 122 #define _ENCODING_STATE _MSKanjiState 123 #define _ENCODING_MB_CUR_MAX(_ei_) 2 124 #define _ENCODING_IS_STATE_DEPENDENT 0 125 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 126 127 128 static int 129 _mskanji1(int c) 130 { 131 132 if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) 133 return 1; 134 else 135 return 0; 136 } 137 138 static int 139 _mskanji2(int c) 140 { 141 142 if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc)) 143 return 1; 144 else 145 return 0; 146 } 147 148 static __inline void 149 /*ARGSUSED*/ 150 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei, 151 _MSKanjiState * __restrict s) 152 { 153 s->chlen = 0; 154 } 155 156 static __inline void 157 /*ARGSUSED*/ 158 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei, 159 void * __restrict pspriv, 160 const _MSKanjiState * __restrict s) 161 { 162 memcpy(pspriv, (const void *)s, sizeof(*s)); 163 } 164 165 static __inline void 166 /*ARGSUSED*/ 167 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei, 168 _MSKanjiState * __restrict s, 169 const void * __restrict pspriv) 170 { 171 memcpy((void *)s, pspriv, sizeof(*s)); 172 } 173 174 static int 175 /*ARGSUSED*/ 176 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei, 177 wchar_t * __restrict pwc, 178 const char ** __restrict s, size_t n, 179 _MSKanjiState * __restrict psenc, 180 size_t * __restrict nresult) 181 { 182 wchar_t wchar; 183 int len; 184 int chlenbak; 185 const char *s0; 186 187 _DIAGASSERT(nresult != 0); 188 _DIAGASSERT(ei != NULL); 189 _DIAGASSERT(s != NULL); 190 _DIAGASSERT(psenc != NULL); 191 192 s0 = *s; 193 194 if (s0 == NULL) { 195 _citrus_MSKanji_init_state(ei, psenc); 196 *nresult = 0; /* state independent */ 197 return (0); 198 } 199 200 chlenbak = psenc->chlen; 201 202 /* make sure we have the first byte in the buffer */ 203 switch (psenc->chlen) { 204 case 0: 205 if (n < 1) 206 goto restart; 207 psenc->ch[0] = *s0++; 208 psenc->chlen = 1; 209 n--; 210 break; 211 case 1: 212 break; 213 default: 214 /* illegal state */ 215 goto encoding_error; 216 } 217 218 len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1; 219 while (psenc->chlen < len) { 220 if (n < 1) 221 goto restart; 222 psenc->ch[psenc->chlen] = *s0++; 223 psenc->chlen++; 224 n--; 225 } 226 227 *s = s0; 228 229 switch (len) { 230 case 1: 231 wchar = psenc->ch[0] & 0xff; 232 break; 233 case 2: 234 if (!_mskanji2(psenc->ch[1] & 0xff)) 235 goto encoding_error; 236 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); 237 break; 238 default: 239 /* illegal state */ 240 goto encoding_error; 241 } 242 243 psenc->chlen = 0; 244 245 if (pwc) 246 *pwc = wchar; 247 248 if (!wchar) 249 *nresult = 0; 250 else 251 *nresult = len - chlenbak; 252 253 return (0); 254 255 encoding_error: 256 psenc->chlen = 0; 257 *nresult = (size_t)-1; 258 return (EILSEQ); 259 260 restart: 261 *nresult = (size_t)-2; 262 *s = s0; 263 return (0); 264 } 265 266 267 static int 268 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei, 269 char * __restrict s, size_t n, wchar_t wc, 270 _MSKanjiState * __restrict psenc, 271 size_t * __restrict nresult) 272 { 273 int ret; 274 275 _DIAGASSERT(ei != NULL); 276 _DIAGASSERT(psenc != NULL); 277 _DIAGASSERT(s != NULL); 278 279 /* check invalid sequence */ 280 if (wc & ~0xffff) { 281 ret = EILSEQ; 282 goto err; 283 } 284 285 if (wc & 0xff00) { 286 if (n < 2) { 287 ret = E2BIG; 288 goto err; 289 } 290 291 s[0] = (wc >> 8) & 0xff; 292 s[1] = wc & 0xff; 293 if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) { 294 ret = EILSEQ; 295 goto err; 296 } 297 298 *nresult = 2; 299 return 0; 300 } else { 301 if (n < 1) { 302 ret = E2BIG; 303 goto err; 304 } 305 306 s[0] = wc & 0xff; 307 if (_mskanji1(s[0] & 0xff)) { 308 ret = EILSEQ; 309 goto err; 310 } 311 312 *nresult = 1; 313 return 0; 314 } 315 316 err: 317 *nresult = (size_t)-1; 318 return ret; 319 } 320 321 322 static __inline int 323 /*ARGSUSED*/ 324 _citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei, 325 _csid_t * __restrict csid, 326 _index_t * __restrict idx, wchar_t wc) 327 { 328 _index_t row, col; 329 int offset; 330 331 _DIAGASSERT(csid != NULL && idx != NULL); 332 333 if ((_wc_t)wc < 0x80) { 334 /* ISO-646 */ 335 *csid = 0; 336 *idx = (_index_t)wc; 337 } else if ((_wc_t)wc < 0x100) { 338 /* KANA */ 339 *csid = 1; 340 *idx = (_index_t)wc & 0x7F; 341 } else { 342 /* Kanji (containing Gaiji zone) */ 343 /* 344 * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)): 345 * 0x8140 - 0x817E -> 0x2121 - 0x215F 346 * 0x8180 - 0x819E -> 0x2160 - 0x217E 347 * 0x819F - 0x81FC -> 0x2221 - 0x227E 348 * 349 * 0x8240 - 0x827E -> 0x2321 - 0x235F 350 * ... 351 * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E 352 * 353 * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F 354 * ... 355 * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E 356 * 357 * extended Gaiji zone: 358 * 0xF040 - 0xFCFC 359 * 360 * JIS X0213-plane2: 361 * 0xF040 - 0xF09E -> 0x2121 - 0x217E 362 * 0xF140 - 0xF19E -> 0x2321 - 0x237E 363 * ... 364 * 0xF240 - 0xF29E -> 0x2521 - 0x257E 365 * 366 * 0xF09F - 0xF0FC -> 0x2821 - 0x287E 367 * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E 368 * ... 369 * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E 370 * 371 * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E 372 * ... 373 * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E 374 */ 375 row = ((_wc_t)wc >> 8) & 0xFF; 376 col = (_wc_t)wc & 0xFF; 377 if (!_mskanji1(row) || !_mskanji2(col)) 378 return EILSEQ; 379 if ((ei->mode & MODE_JIS2004) == 0 || row < 0xF0) { 380 *csid = 2; 381 offset = 0x81; 382 } else { 383 *csid = 3; 384 if ((_wc_t)wc <= 0xF49E) { 385 offset = (_wc_t)wc >= 0xF29F || 386 ((_wc_t)wc >= 0xF09F && (_wc_t)wc <= 0xF0FC) 387 ? 0xED : 0xF0; 388 } else 389 offset = 0xCE; 390 } 391 row -= offset; 392 if (row >= 0x5F) 393 row -= 0x40; 394 row = row * 2 + 0x21; 395 col -= 0x1F; 396 if (col >= 0x61) 397 col -= 1; 398 if (col > 0x7E) { 399 row += 1; 400 col -= 0x5E; 401 } 402 *idx = ((_index_t)row << 8) | col; 403 } 404 405 return 0; 406 } 407 408 static __inline int 409 /*ARGSUSED*/ 410 _citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei, 411 wchar_t * __restrict wc, 412 _csid_t csid, _index_t idx) 413 { 414 u_int32_t row, col; 415 int offset; 416 417 _DIAGASSERT(wc != NULL); 418 419 switch (csid) { 420 case 0: 421 /* ISO-646 */ 422 if (idx >= 0x80) 423 return EILSEQ; 424 *wc = (wchar_t)idx; 425 break; 426 case 1: 427 /* kana */ 428 if (idx >= 0x80) 429 return EILSEQ; 430 *wc = (wchar_t)idx + 0x80; 431 break; 432 case 3: 433 if ((ei->mode & MODE_JIS2004) == 0) 434 return EILSEQ; 435 /*FALLTHROUGH*/ 436 case 2: 437 /* kanji */ 438 row = (idx >> 8); 439 if (row < 0x21) 440 return EILSEQ; 441 if (csid == 3) { 442 if (row <= 0x2F) 443 offset = (row == 0x22 || row >= 0x26) 444 ? 0xED : 0xF0; 445 else if (row >= 0x4D && row <= 0x7E) 446 offset = 0xCE; 447 else 448 return EILSEQ; 449 } else { 450 if (row > 0x97) 451 return EILSEQ; 452 offset = (row < 0x5F) ? 0x81 : 0xC1; 453 } 454 col = idx & 0xFF; 455 if (col < 0x21 || col > 0x7E) 456 return EILSEQ; 457 row -= 0x21; col -= 0x21; 458 if ((row & 1) == 0) { 459 col += 0x40; 460 if (col >= 0x7F) 461 col += 1; 462 } else 463 col += 0x9F; 464 row = row / 2 + offset; 465 *wc = ((wchar_t)row << 8) | col; 466 break; 467 default: 468 return EILSEQ; 469 } 470 471 return 0; 472 } 473 474 static __inline int 475 /*ARGSUSED*/ 476 _citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * __restrict ei, 477 _MSKanjiState * __restrict psenc, 478 int * __restrict rstate) 479 { 480 481 if (psenc->chlen == 0) 482 *rstate = _STDENC_SDGEN_INITIAL; 483 else 484 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR; 485 486 return 0; 487 } 488 489 static int 490 /*ARGSUSED*/ 491 _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei, 492 const void * __restrict var, 493 size_t lenvar) 494 { 495 const char *p; 496 497 _DIAGASSERT(ei != NULL); 498 499 p = var; 500 #define MATCH(x, act) \ 501 do { \ 502 if (lenvar >= (sizeof(#x)-1) && \ 503 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \ 504 act; \ 505 lenvar -= sizeof(#x)-1; \ 506 p += sizeof(#x)-1; \ 507 } \ 508 } while (/*CONSTCOND*/0) 509 memset((void *)ei, 0, sizeof(*ei)); 510 while (lenvar > 0) { 511 switch (_bcs_toupper(*p)) { 512 case 'J': 513 MATCH(JIS2004, ei->mode |= MODE_JIS2004); 514 break; 515 } 516 ++p; 517 --lenvar; 518 } 519 520 return 0; 521 } 522 523 static void 524 _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei) 525 { 526 } 527 528 /* ---------------------------------------------------------------------- 529 * public interface for ctype 530 */ 531 532 _CITRUS_CTYPE_DECLS(MSKanji); 533 _CITRUS_CTYPE_DEF_OPS(MSKanji); 534 535 #include "citrus_ctype_template.h" 536 537 /* ---------------------------------------------------------------------- 538 * public interface for stdenc 539 */ 540 541 _CITRUS_STDENC_DECLS(MSKanji); 542 _CITRUS_STDENC_DEF_OPS(MSKanji); 543 544 #include "citrus_stdenc_template.h" 545