Home | History | Annotate | Line # | Download | only in modules
citrus_mskanji.c revision 1.1
      1  1.1  tshiozak /*	$NetBSD: citrus_mskanji.c,v 1.1 2002/03/17 22:14:23 tshiozak Exp $	*/
      2  1.1  tshiozak 
      3  1.1  tshiozak /*-
      4  1.1  tshiozak  * Copyright (c)2002 Citrus Project,
      5  1.1  tshiozak  * All rights reserved.
      6  1.1  tshiozak  *
      7  1.1  tshiozak  * Redistribution and use in source and binary forms, with or without
      8  1.1  tshiozak  * modification, are permitted provided that the following conditions
      9  1.1  tshiozak  * are met:
     10  1.1  tshiozak  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tshiozak  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tshiozak  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tshiozak  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tshiozak  *    documentation and/or other materials provided with the distribution.
     15  1.1  tshiozak  *
     16  1.1  tshiozak  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  tshiozak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  tshiozak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  tshiozak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  tshiozak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  tshiozak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  tshiozak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  tshiozak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  tshiozak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  tshiozak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  tshiozak  * SUCH DAMAGE.
     27  1.1  tshiozak  */
     28  1.1  tshiozak 
     29  1.1  tshiozak /*
     30  1.1  tshiozak  *    ja_JP.SJIS locale table for BSD4.4/rune
     31  1.1  tshiozak  *    version 1.0
     32  1.1  tshiozak  *    (C) Sin'ichiro MIYATANI / Phase One, Inc
     33  1.1  tshiozak  *    May 12, 1995
     34  1.1  tshiozak  *
     35  1.1  tshiozak  * Redistribution and use in source and binary forms, with or without
     36  1.1  tshiozak  * modification, are permitted provided that the following conditions
     37  1.1  tshiozak  * are met:
     38  1.1  tshiozak  * 1. Redistributions of source code must retain the above copyright
     39  1.1  tshiozak  *    notice, this list of conditions and the following disclaimer.
     40  1.1  tshiozak  * 2. Redistributions in binary form must reproduce the above copyright
     41  1.1  tshiozak  *    notice, this list of conditions and the following disclaimer in the
     42  1.1  tshiozak  *    documentation and/or other materials provided with the distribution.
     43  1.1  tshiozak  * 3. All advertising materials mentioning features or use of this software
     44  1.1  tshiozak  *    must display the following acknowledgement:
     45  1.1  tshiozak  *      This product includes software developed by Phase One, Inc.
     46  1.1  tshiozak  * 4. The name of Phase One, Inc. may be used to endorse or promote products
     47  1.1  tshiozak  *    derived from this software without specific prior written permission.
     48  1.1  tshiozak  *
     49  1.1  tshiozak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  1.1  tshiozak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  1.1  tshiozak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  1.1  tshiozak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  1.1  tshiozak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  1.1  tshiozak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  1.1  tshiozak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  1.1  tshiozak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  1.1  tshiozak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  1.1  tshiozak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  1.1  tshiozak  * SUCH DAMAGE.
     60  1.1  tshiozak  */
     61  1.1  tshiozak 
     62  1.1  tshiozak 
     63  1.1  tshiozak #include <sys/cdefs.h>
     64  1.1  tshiozak #if defined(LIBC_SCCS) && !defined(lint)
     65  1.1  tshiozak __RCSID("$NetBSD: citrus_mskanji.c,v 1.1 2002/03/17 22:14:23 tshiozak Exp $");
     66  1.1  tshiozak #endif /* LIBC_SCCS and not lint */
     67  1.1  tshiozak 
     68  1.1  tshiozak #include <assert.h>
     69  1.1  tshiozak #include <errno.h>
     70  1.1  tshiozak #include <string.h>
     71  1.1  tshiozak #include <stdio.h>
     72  1.1  tshiozak #include <stdlib.h>
     73  1.1  tshiozak #include <stddef.h>
     74  1.1  tshiozak #include <locale.h>
     75  1.1  tshiozak #include <wchar.h>
     76  1.1  tshiozak #include <sys/types.h>
     77  1.1  tshiozak #include <limits.h>
     78  1.1  tshiozak #include "citrus_module.h"
     79  1.1  tshiozak #include "citrus_ctype.h"
     80  1.1  tshiozak #include "citrus_mskanji.h"
     81  1.1  tshiozak 
     82  1.1  tshiozak 
     83  1.1  tshiozak /* ----------------------------------------------------------------------
     84  1.1  tshiozak  * private stuffs used by templates
     85  1.1  tshiozak  */
     86  1.1  tshiozak 
     87  1.1  tshiozak typedef struct _MSKanjiState {
     88  1.1  tshiozak 	char ch[2];
     89  1.1  tshiozak 	int chlen;
     90  1.1  tshiozak } _MSKanjiState;
     91  1.1  tshiozak 
     92  1.1  tshiozak typedef struct {
     93  1.1  tshiozak 	int dummy;
     94  1.1  tshiozak } _MSKanjiEncodingInfo;
     95  1.1  tshiozak 
     96  1.1  tshiozak typedef struct {
     97  1.1  tshiozak 	_MSKanjiEncodingInfo	ei;
     98  1.1  tshiozak 	struct {
     99  1.1  tshiozak 		/* for future multi-locale facility */
    100  1.1  tshiozak 		_MSKanjiState	s_mblen;
    101  1.1  tshiozak 		_MSKanjiState	s_mbrlen;
    102  1.1  tshiozak 		_MSKanjiState	s_mbrtowc;
    103  1.1  tshiozak 		_MSKanjiState	s_mbtowc;
    104  1.1  tshiozak 		_MSKanjiState	s_mbsrtowcs;
    105  1.1  tshiozak 		_MSKanjiState	s_wcrtomb;
    106  1.1  tshiozak 		_MSKanjiState	s_wcsrtombs;
    107  1.1  tshiozak 		_MSKanjiState	s_wcstombs;
    108  1.1  tshiozak 		_MSKanjiState	s_wctomb;
    109  1.1  tshiozak 	} states;
    110  1.1  tshiozak } _MSKanjiCTypeInfo;
    111  1.1  tshiozak 
    112  1.1  tshiozak #define	_TO_EI(_cl_)			((_MSKanjiEncodingInfo *)(_cl_))
    113  1.1  tshiozak #define	_TO_CEI(_cl_)			((_MSKanjiCTypeInfo *)(_cl_))
    114  1.1  tshiozak #define _TO_STATE(_ps_)			((_MSKanjiState *)(_ps_))
    115  1.1  tshiozak #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
    116  1.1  tshiozak #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
    117  1.1  tshiozak 
    118  1.1  tshiozak #define _FUNCNAME(m)			_citrus_MSKanji_##m
    119  1.1  tshiozak #define _ENCODING_INFO			_MSKanjiEncodingInfo
    120  1.1  tshiozak #define _CTYPE_INFO			_MSKanjiCTypeInfo
    121  1.1  tshiozak #define _ENCODING_STATE			_MSKanjiState
    122  1.1  tshiozak #define _ENCODING_MB_CUR_MAX(_cl_)	2
    123  1.1  tshiozak #define _ENCODING_IS_STATE_DEPENDENT	0
    124  1.1  tshiozak 
    125  1.1  tshiozak 
    126  1.1  tshiozak static int
    127  1.1  tshiozak _mskanji1(int c)
    128  1.1  tshiozak {
    129  1.1  tshiozak 
    130  1.1  tshiozak 	if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef))
    131  1.1  tshiozak 		return 1;
    132  1.1  tshiozak 	else
    133  1.1  tshiozak 		return 0;
    134  1.1  tshiozak }
    135  1.1  tshiozak 
    136  1.1  tshiozak static int
    137  1.1  tshiozak _mskanji2(int c)
    138  1.1  tshiozak {
    139  1.1  tshiozak 
    140  1.1  tshiozak 	if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
    141  1.1  tshiozak 		return 1;
    142  1.1  tshiozak 	else
    143  1.1  tshiozak 		return 0;
    144  1.1  tshiozak }
    145  1.1  tshiozak 
    146  1.1  tshiozak static __inline void
    147  1.1  tshiozak /*ARGSUSED*/
    148  1.1  tshiozak _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei,
    149  1.1  tshiozak 			   _MSKanjiState * __restrict s)
    150  1.1  tshiozak {
    151  1.1  tshiozak 	memset(s, 0, sizeof(*s));
    152  1.1  tshiozak }
    153  1.1  tshiozak 
    154  1.1  tshiozak static __inline void
    155  1.1  tshiozak /*ARGSUSED*/
    156  1.1  tshiozak _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei,
    157  1.1  tshiozak 			   void * __restrict pspriv,
    158  1.1  tshiozak 			   const _MSKanjiState * __restrict s)
    159  1.1  tshiozak {
    160  1.1  tshiozak 	memcpy(pspriv, (const void *)s, sizeof(*s));
    161  1.1  tshiozak }
    162  1.1  tshiozak 
    163  1.1  tshiozak static __inline void
    164  1.1  tshiozak /*ARGSUSED*/
    165  1.1  tshiozak _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei,
    166  1.1  tshiozak 			     _MSKanjiState * __restrict s,
    167  1.1  tshiozak 			     const void * __restrict pspriv)
    168  1.1  tshiozak {
    169  1.1  tshiozak 	memcpy((void *)s, pspriv, sizeof(*s));
    170  1.1  tshiozak }
    171  1.1  tshiozak 
    172  1.1  tshiozak static int
    173  1.1  tshiozak /*ARGSUSED*/
    174  1.1  tshiozak _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
    175  1.1  tshiozak 			     wchar_t * __restrict pwc,
    176  1.1  tshiozak 			     const char ** __restrict s, size_t n,
    177  1.1  tshiozak 			     _MSKanjiState * __restrict psenc,
    178  1.1  tshiozak 			     size_t * __restrict nresult)
    179  1.1  tshiozak {
    180  1.1  tshiozak 	wchar_t wchar;
    181  1.1  tshiozak 	int len;
    182  1.1  tshiozak 	int chlenbak;
    183  1.1  tshiozak 	const char *s0;
    184  1.1  tshiozak 
    185  1.1  tshiozak 	_DIAGASSERT(nresult != 0);
    186  1.1  tshiozak 	_DIAGASSERT(ei != NULL);
    187  1.1  tshiozak 	_DIAGASSERT(s != NULL);
    188  1.1  tshiozak 	_DIAGASSERT(psenc != NULL);
    189  1.1  tshiozak 
    190  1.1  tshiozak 	s0 = *s;
    191  1.1  tshiozak 
    192  1.1  tshiozak 	if (s0 == NULL) {
    193  1.1  tshiozak 		_citrus_MSKanji_init_state(ei, psenc);
    194  1.1  tshiozak 		*nresult = 0; /* state independent */
    195  1.1  tshiozak 		return (0);
    196  1.1  tshiozak 	}
    197  1.1  tshiozak 
    198  1.1  tshiozak 	chlenbak = psenc->chlen;
    199  1.1  tshiozak 
    200  1.1  tshiozak 	/* make sure we have the first byte in the buffer */
    201  1.1  tshiozak 	switch (psenc->chlen) {
    202  1.1  tshiozak 	case 0:
    203  1.1  tshiozak 		if (n < 1)
    204  1.1  tshiozak 			goto restart;
    205  1.1  tshiozak 		psenc->ch[0] = *s0++;
    206  1.1  tshiozak 		psenc->chlen = 1;
    207  1.1  tshiozak 		n--;
    208  1.1  tshiozak 		break;
    209  1.1  tshiozak 	case 1:
    210  1.1  tshiozak 		break;
    211  1.1  tshiozak 	default:
    212  1.1  tshiozak 		/* illegal state */
    213  1.1  tshiozak 		goto encoding_error;
    214  1.1  tshiozak 	}
    215  1.1  tshiozak 
    216  1.1  tshiozak 	len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
    217  1.1  tshiozak 	while (psenc->chlen < len) {
    218  1.1  tshiozak 		if (n < 1)
    219  1.1  tshiozak 			goto restart;
    220  1.1  tshiozak 		psenc->ch[psenc->chlen] = *s0++;
    221  1.1  tshiozak 		psenc->chlen++;
    222  1.1  tshiozak 		n--;
    223  1.1  tshiozak 	}
    224  1.1  tshiozak 
    225  1.1  tshiozak 	*s = s0;
    226  1.1  tshiozak 
    227  1.1  tshiozak 	switch (len) {
    228  1.1  tshiozak 	case 1:
    229  1.1  tshiozak 		wchar = psenc->ch[0] & 0xff;
    230  1.1  tshiozak 		break;
    231  1.1  tshiozak 	case 2:
    232  1.1  tshiozak 		if (!_mskanji2(psenc->ch[1] & 0xff))
    233  1.1  tshiozak 			goto encoding_error;
    234  1.1  tshiozak 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
    235  1.1  tshiozak 		break;
    236  1.1  tshiozak 	default:
    237  1.1  tshiozak 		/* illegal state */
    238  1.1  tshiozak 		goto encoding_error;
    239  1.1  tshiozak 	}
    240  1.1  tshiozak 
    241  1.1  tshiozak 	psenc->chlen = 0;
    242  1.1  tshiozak 
    243  1.1  tshiozak 	if (pwc)
    244  1.1  tshiozak 		*pwc = wchar;
    245  1.1  tshiozak 
    246  1.1  tshiozak 	if (!wchar)
    247  1.1  tshiozak 		*nresult = 0;
    248  1.1  tshiozak 	else
    249  1.1  tshiozak 		*nresult = len - chlenbak;
    250  1.1  tshiozak 
    251  1.1  tshiozak 	return (0);
    252  1.1  tshiozak 
    253  1.1  tshiozak encoding_error:
    254  1.1  tshiozak 	psenc->chlen = 0;
    255  1.1  tshiozak 	*nresult = (size_t)-1;
    256  1.1  tshiozak 	return (EILSEQ);
    257  1.1  tshiozak 
    258  1.1  tshiozak restart:
    259  1.1  tshiozak 	*nresult = (size_t)-2;
    260  1.1  tshiozak 	*s = s0;
    261  1.1  tshiozak 	return (0);
    262  1.1  tshiozak }
    263  1.1  tshiozak 
    264  1.1  tshiozak 
    265  1.1  tshiozak static int
    266  1.1  tshiozak _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei,
    267  1.1  tshiozak 			     char * __restrict s, size_t n, wchar_t wc,
    268  1.1  tshiozak 			     _MSKanjiState * __restrict psenc,
    269  1.1  tshiozak 			     size_t * __restrict nresult)
    270  1.1  tshiozak {
    271  1.1  tshiozak 
    272  1.1  tshiozak 	_DIAGASSERT(ei != NULL);
    273  1.1  tshiozak 	_DIAGASSERT(psenc != NULL);
    274  1.1  tshiozak 	_DIAGASSERT(s != NULL);
    275  1.1  tshiozak 
    276  1.1  tshiozak 	/* check invalid sequence */
    277  1.1  tshiozak 	if (wc & ~0xffff)
    278  1.1  tshiozak 		goto ilseq;
    279  1.1  tshiozak 
    280  1.1  tshiozak 	if (wc & 0xff00) {
    281  1.1  tshiozak 		if (n < 2)
    282  1.1  tshiozak 			goto ilseq;
    283  1.1  tshiozak 
    284  1.1  tshiozak 		s[0] = (wc >> 8) & 0xff;
    285  1.1  tshiozak 		s[1] = wc & 0xff;
    286  1.1  tshiozak 		if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff))
    287  1.1  tshiozak 			goto ilseq;
    288  1.1  tshiozak 
    289  1.1  tshiozak 		*nresult = 2;
    290  1.1  tshiozak 		return (0);
    291  1.1  tshiozak 	} else {
    292  1.1  tshiozak 		s[0] = wc & 0xff;
    293  1.1  tshiozak 		if (_mskanji1(s[0] & 0xff))
    294  1.1  tshiozak 			goto ilseq;
    295  1.1  tshiozak 
    296  1.1  tshiozak 		*nresult = 1;
    297  1.1  tshiozak 		return (0);
    298  1.1  tshiozak 	}
    299  1.1  tshiozak 
    300  1.1  tshiozak ilseq:
    301  1.1  tshiozak 	*nresult = (size_t)-1;
    302  1.1  tshiozak 	return EILSEQ;
    303  1.1  tshiozak }
    304  1.1  tshiozak 
    305  1.1  tshiozak 
    306  1.1  tshiozak static int
    307  1.1  tshiozak /*ARGSUSED*/
    308  1.1  tshiozak _citrus_MSKanji_stdencoding_init(_MSKanjiEncodingInfo *  __restrict ei,
    309  1.1  tshiozak 				 const void * __restrict var, size_t lenvar)
    310  1.1  tshiozak {
    311  1.1  tshiozak 
    312  1.1  tshiozak 	_DIAGASSERT(cl != NULL);
    313  1.1  tshiozak 
    314  1.1  tshiozak 	return (0);
    315  1.1  tshiozak }
    316  1.1  tshiozak 
    317  1.1  tshiozak static void
    318  1.1  tshiozak _citrus_MSKanji_stdencoding_uninit(_MSKanjiEncodingInfo *ei)
    319  1.1  tshiozak {
    320  1.1  tshiozak }
    321  1.1  tshiozak 
    322  1.1  tshiozak /* ----------------------------------------------------------------------
    323  1.1  tshiozak  * public interface for ctype
    324  1.1  tshiozak  */
    325  1.1  tshiozak 
    326  1.1  tshiozak _CITRUS_CTYPE_DECLS(MSKanji);
    327  1.1  tshiozak _CITRUS_CTYPE_DEF_OPS(MSKanji);
    328  1.1  tshiozak 
    329  1.1  tshiozak #include "citrus_ctype_template.h"
    330