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