Home | History | Annotate | Line # | Download | only in modules
citrus_mskanji.c revision 1.3
      1 /*	$NetBSD: citrus_mskanji.c,v 1.3 2002/03/27 17:54:42 yamt 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.3 2002/03/27 17:54:42 yamt 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 #include "citrus_module.h"
     79 #include "citrus_ctype.h"
     80 #include "citrus_mskanji.h"
     81 
     82 
     83 /* ----------------------------------------------------------------------
     84  * private stuffs used by templates
     85  */
     86 
     87 typedef struct _MSKanjiState {
     88 	char ch[2];
     89 	int chlen;
     90 } _MSKanjiState;
     91 
     92 typedef struct {
     93 	int dummy;
     94 } _MSKanjiEncodingInfo;
     95 
     96 typedef struct {
     97 	_MSKanjiEncodingInfo	ei;
     98 	struct {
     99 		/* for future multi-locale facility */
    100 		_MSKanjiState	s_mblen;
    101 		_MSKanjiState	s_mbrlen;
    102 		_MSKanjiState	s_mbrtowc;
    103 		_MSKanjiState	s_mbtowc;
    104 		_MSKanjiState	s_mbsrtowcs;
    105 		_MSKanjiState	s_wcrtomb;
    106 		_MSKanjiState	s_wcsrtombs;
    107 		_MSKanjiState	s_wctomb;
    108 	} states;
    109 } _MSKanjiCTypeInfo;
    110 
    111 #define	_TO_EI(_cl_)			((_MSKanjiEncodingInfo *)(_cl_))
    112 #define	_TO_CEI(_cl_)			((_MSKanjiCTypeInfo *)(_cl_))
    113 #define _TO_STATE(_ps_)			((_MSKanjiState *)(_ps_))
    114 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
    115 #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
    116 
    117 #define _FUNCNAME(m)			_citrus_MSKanji_##m
    118 #define _ENCODING_INFO			_MSKanjiEncodingInfo
    119 #define _CTYPE_INFO			_MSKanjiCTypeInfo
    120 #define _ENCODING_STATE			_MSKanjiState
    121 #define _ENCODING_MB_CUR_MAX(_ei_)	2
    122 #define _ENCODING_IS_STATE_DEPENDENT	0
    123 
    124 
    125 static int
    126 _mskanji1(int c)
    127 {
    128 
    129 	if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef))
    130 		return 1;
    131 	else
    132 		return 0;
    133 }
    134 
    135 static int
    136 _mskanji2(int c)
    137 {
    138 
    139 	if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
    140 		return 1;
    141 	else
    142 		return 0;
    143 }
    144 
    145 static __inline void
    146 /*ARGSUSED*/
    147 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei,
    148 			   _MSKanjiState * __restrict s)
    149 {
    150 	memset(s, 0, sizeof(*s));
    151 }
    152 
    153 static __inline void
    154 /*ARGSUSED*/
    155 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei,
    156 			   void * __restrict pspriv,
    157 			   const _MSKanjiState * __restrict s)
    158 {
    159 	memcpy(pspriv, (const void *)s, sizeof(*s));
    160 }
    161 
    162 static __inline void
    163 /*ARGSUSED*/
    164 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei,
    165 			     _MSKanjiState * __restrict s,
    166 			     const void * __restrict pspriv)
    167 {
    168 	memcpy((void *)s, pspriv, sizeof(*s));
    169 }
    170 
    171 static int
    172 /*ARGSUSED*/
    173 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
    174 			     wchar_t * __restrict pwc,
    175 			     const char ** __restrict s, size_t n,
    176 			     _MSKanjiState * __restrict psenc,
    177 			     size_t * __restrict nresult)
    178 {
    179 	wchar_t wchar;
    180 	int len;
    181 	int chlenbak;
    182 	const char *s0;
    183 
    184 	_DIAGASSERT(nresult != 0);
    185 	_DIAGASSERT(ei != NULL);
    186 	_DIAGASSERT(s != NULL);
    187 	_DIAGASSERT(psenc != NULL);
    188 
    189 	s0 = *s;
    190 
    191 	if (s0 == NULL) {
    192 		_citrus_MSKanji_init_state(ei, psenc);
    193 		*nresult = 0; /* state independent */
    194 		return (0);
    195 	}
    196 
    197 	chlenbak = psenc->chlen;
    198 
    199 	/* make sure we have the first byte in the buffer */
    200 	switch (psenc->chlen) {
    201 	case 0:
    202 		if (n < 1)
    203 			goto restart;
    204 		psenc->ch[0] = *s0++;
    205 		psenc->chlen = 1;
    206 		n--;
    207 		break;
    208 	case 1:
    209 		break;
    210 	default:
    211 		/* illegal state */
    212 		goto encoding_error;
    213 	}
    214 
    215 	len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
    216 	while (psenc->chlen < len) {
    217 		if (n < 1)
    218 			goto restart;
    219 		psenc->ch[psenc->chlen] = *s0++;
    220 		psenc->chlen++;
    221 		n--;
    222 	}
    223 
    224 	*s = s0;
    225 
    226 	switch (len) {
    227 	case 1:
    228 		wchar = psenc->ch[0] & 0xff;
    229 		break;
    230 	case 2:
    231 		if (!_mskanji2(psenc->ch[1] & 0xff))
    232 			goto encoding_error;
    233 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
    234 		break;
    235 	default:
    236 		/* illegal state */
    237 		goto encoding_error;
    238 	}
    239 
    240 	psenc->chlen = 0;
    241 
    242 	if (pwc)
    243 		*pwc = wchar;
    244 
    245 	if (!wchar)
    246 		*nresult = 0;
    247 	else
    248 		*nresult = len - chlenbak;
    249 
    250 	return (0);
    251 
    252 encoding_error:
    253 	psenc->chlen = 0;
    254 	*nresult = (size_t)-1;
    255 	return (EILSEQ);
    256 
    257 restart:
    258 	*nresult = (size_t)-2;
    259 	*s = s0;
    260 	return (0);
    261 }
    262 
    263 
    264 static int
    265 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei,
    266 			     char * __restrict s, size_t n, wchar_t wc,
    267 			     _MSKanjiState * __restrict psenc,
    268 			     size_t * __restrict nresult)
    269 {
    270 
    271 	_DIAGASSERT(ei != NULL);
    272 	_DIAGASSERT(psenc != NULL);
    273 	_DIAGASSERT(s != NULL);
    274 
    275 	/* check invalid sequence */
    276 	if (wc & ~0xffff)
    277 		goto ilseq;
    278 
    279 	if (wc & 0xff00) {
    280 		if (n < 2)
    281 			goto ilseq;
    282 
    283 		s[0] = (wc >> 8) & 0xff;
    284 		s[1] = wc & 0xff;
    285 		if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff))
    286 			goto ilseq;
    287 
    288 		*nresult = 2;
    289 		return (0);
    290 	} else {
    291 		s[0] = wc & 0xff;
    292 		if (_mskanji1(s[0] & 0xff))
    293 			goto ilseq;
    294 
    295 		*nresult = 1;
    296 		return (0);
    297 	}
    298 
    299 ilseq:
    300 	*nresult = (size_t)-1;
    301 	return EILSEQ;
    302 }
    303 
    304 
    305 static int
    306 /*ARGSUSED*/
    307 _citrus_MSKanji_stdencoding_init(_MSKanjiEncodingInfo *  __restrict ei,
    308 				 const void * __restrict var, size_t lenvar)
    309 {
    310 
    311 	_DIAGASSERT(cl != NULL);
    312 
    313 	return (0);
    314 }
    315 
    316 static void
    317 _citrus_MSKanji_stdencoding_uninit(_MSKanjiEncodingInfo *ei)
    318 {
    319 }
    320 
    321 /* ----------------------------------------------------------------------
    322  * public interface for ctype
    323  */
    324 
    325 _CITRUS_CTYPE_DECLS(MSKanji);
    326 _CITRUS_CTYPE_DEF_OPS(MSKanji);
    327 
    328 #include "citrus_ctype_template.h"
    329