Home | History | Annotate | Line # | Download | only in modules
citrus_gbk2k.c revision 1.6
      1  1.6   tnozaki /* $NetBSD: citrus_gbk2k.c,v 1.6 2006/02/15 19:50:27 tnozaki Exp $ */
      2  1.1  tshiozak 
      3  1.1  tshiozak /*-
      4  1.1  tshiozak  * Copyright (c)2003 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 #include <sys/cdefs.h>
     30  1.1  tshiozak #if defined(LIBC_SCCS) && !defined(lint)
     31  1.6   tnozaki __RCSID("$NetBSD: citrus_gbk2k.c,v 1.6 2006/02/15 19:50:27 tnozaki Exp $");
     32  1.1  tshiozak #endif /* LIBC_SCCS and not lint */
     33  1.1  tshiozak 
     34  1.1  tshiozak #include <assert.h>
     35  1.1  tshiozak #include <errno.h>
     36  1.1  tshiozak #include <string.h>
     37  1.1  tshiozak #include <stdio.h>
     38  1.1  tshiozak #include <stdlib.h>
     39  1.1  tshiozak #include <stddef.h>
     40  1.1  tshiozak #include <locale.h>
     41  1.1  tshiozak #include <wchar.h>
     42  1.1  tshiozak #include <sys/types.h>
     43  1.1  tshiozak #include <limits.h>
     44  1.3  tshiozak 
     45  1.3  tshiozak #include "citrus_namespace.h"
     46  1.3  tshiozak #include "citrus_types.h"
     47  1.3  tshiozak #include "citrus_bcs.h"
     48  1.1  tshiozak #include "citrus_module.h"
     49  1.1  tshiozak #include "citrus_ctype.h"
     50  1.3  tshiozak #include "citrus_stdenc.h"
     51  1.1  tshiozak #include "citrus_gbk2k.h"
     52  1.1  tshiozak 
     53  1.1  tshiozak 
     54  1.1  tshiozak /* ----------------------------------------------------------------------
     55  1.1  tshiozak  * private stuffs used by templates
     56  1.1  tshiozak  */
     57  1.1  tshiozak 
     58  1.1  tshiozak typedef struct _GBK2KState {
     59  1.1  tshiozak 	char ch[4];
     60  1.1  tshiozak 	int chlen;
     61  1.1  tshiozak } _GBK2KState;
     62  1.1  tshiozak 
     63  1.1  tshiozak typedef struct {
     64  1.6   tnozaki 	int mb_cur_max;
     65  1.1  tshiozak } _GBK2KEncodingInfo;
     66  1.1  tshiozak 
     67  1.1  tshiozak typedef struct {
     68  1.1  tshiozak 	_GBK2KEncodingInfo	ei;
     69  1.1  tshiozak 	struct {
     70  1.1  tshiozak 		/* for future multi-locale facility */
     71  1.1  tshiozak 		_GBK2KState	s_mblen;
     72  1.1  tshiozak 		_GBK2KState	s_mbrlen;
     73  1.1  tshiozak 		_GBK2KState	s_mbrtowc;
     74  1.1  tshiozak 		_GBK2KState	s_mbtowc;
     75  1.1  tshiozak 		_GBK2KState	s_mbsrtowcs;
     76  1.1  tshiozak 		_GBK2KState	s_wcrtomb;
     77  1.1  tshiozak 		_GBK2KState	s_wcsrtombs;
     78  1.1  tshiozak 		_GBK2KState	s_wctomb;
     79  1.1  tshiozak 	} states;
     80  1.1  tshiozak } _GBK2KCTypeInfo;
     81  1.1  tshiozak 
     82  1.1  tshiozak #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
     83  1.1  tshiozak #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
     84  1.1  tshiozak 
     85  1.1  tshiozak #define _FUNCNAME(m)			_citrus_GBK2K_##m
     86  1.1  tshiozak #define _ENCODING_INFO			_GBK2KEncodingInfo
     87  1.1  tshiozak #define _CTYPE_INFO			_GBK2KCTypeInfo
     88  1.1  tshiozak #define _ENCODING_STATE			_GBK2KState
     89  1.6   tnozaki #define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
     90  1.1  tshiozak #define _ENCODING_IS_STATE_DEPENDENT	0
     91  1.1  tshiozak #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
     92  1.1  tshiozak 
     93  1.1  tshiozak static __inline void
     94  1.1  tshiozak /*ARGSUSED*/
     95  1.1  tshiozak _citrus_GBK2K_init_state(_GBK2KEncodingInfo * __restrict ei,
     96  1.1  tshiozak 			 _GBK2KState * __restrict s)
     97  1.1  tshiozak {
     98  1.1  tshiozak 	memset(s, 0, sizeof(*s));
     99  1.1  tshiozak }
    100  1.1  tshiozak 
    101  1.1  tshiozak static __inline void
    102  1.1  tshiozak /*ARGSUSED*/
    103  1.1  tshiozak _citrus_GBK2K_pack_state(_GBK2KEncodingInfo * __restrict ei,
    104  1.1  tshiozak 			 void * __restrict pspriv,
    105  1.1  tshiozak 			 const _GBK2KState * __restrict s)
    106  1.1  tshiozak {
    107  1.1  tshiozak 	memcpy(pspriv, (const void *)s, sizeof(*s));
    108  1.1  tshiozak }
    109  1.1  tshiozak 
    110  1.1  tshiozak static __inline void
    111  1.1  tshiozak /*ARGSUSED*/
    112  1.1  tshiozak _citrus_GBK2K_unpack_state(_GBK2KEncodingInfo * __restrict ei,
    113  1.1  tshiozak 			   _GBK2KState * __restrict s,
    114  1.1  tshiozak 			   const void * __restrict pspriv)
    115  1.1  tshiozak {
    116  1.1  tshiozak 	memcpy((void *)s, pspriv, sizeof(*s));
    117  1.1  tshiozak }
    118  1.1  tshiozak 
    119  1.1  tshiozak static  __inline int
    120  1.1  tshiozak _mb_singlebyte(int c)
    121  1.1  tshiozak {
    122  1.1  tshiozak 	c &= 0xff;
    123  1.1  tshiozak 	return (c <= 0x7f);
    124  1.1  tshiozak }
    125  1.1  tshiozak 
    126  1.1  tshiozak static __inline int
    127  1.1  tshiozak _mb_leadbyte(int c)
    128  1.1  tshiozak {
    129  1.1  tshiozak 	c &= 0xff;
    130  1.1  tshiozak 	return (0x81 <= c && c <= 0xfe);
    131  1.1  tshiozak }
    132  1.1  tshiozak 
    133  1.1  tshiozak static __inline int
    134  1.1  tshiozak _mb_trailbyte(int c)
    135  1.1  tshiozak {
    136  1.1  tshiozak 	c &= 0xff;
    137  1.1  tshiozak 	return ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfe));
    138  1.1  tshiozak }
    139  1.1  tshiozak 
    140  1.1  tshiozak static __inline int
    141  1.1  tshiozak _mb_surrogate(int c)
    142  1.1  tshiozak {
    143  1.1  tshiozak 	c &= 0xff;
    144  1.1  tshiozak 	return (0x30 <= c && c <= 0x39);
    145  1.1  tshiozak }
    146  1.1  tshiozak 
    147  1.1  tshiozak static __inline int
    148  1.1  tshiozak _mb_count(wchar_t v)
    149  1.1  tshiozak {
    150  1.1  tshiozak 	u_int32_t c;
    151  1.1  tshiozak 
    152  1.1  tshiozak 	c = (u_int32_t)v; /* XXX */
    153  1.1  tshiozak 	if (!(c & 0xffffff00))
    154  1.1  tshiozak 		return (1);
    155  1.1  tshiozak 	if (!(c & 0xffff0000))
    156  1.1  tshiozak 		return (2);
    157  1.1  tshiozak 	return (4);
    158  1.1  tshiozak }
    159  1.1  tshiozak 
    160  1.1  tshiozak #define	_PSENC		(psenc->ch[psenc->chlen - 1])
    161  1.1  tshiozak #define	_PUSH_PSENC(c)	(psenc->ch[psenc->chlen++] = (c))
    162  1.1  tshiozak 
    163  1.1  tshiozak static int
    164  1.1  tshiozak _citrus_GBK2K_mbrtowc_priv(_GBK2KEncodingInfo * __restrict ei,
    165  1.1  tshiozak 			   wchar_t * __restrict pwc,
    166  1.1  tshiozak 			   const char ** __restrict s, size_t n,
    167  1.1  tshiozak 			   _GBK2KState * __restrict psenc,
    168  1.1  tshiozak 			   size_t * __restrict nresult)
    169  1.1  tshiozak {
    170  1.1  tshiozak 	int chlenbak, len;
    171  1.1  tshiozak 	const char *s0, *s1;
    172  1.1  tshiozak 	wchar_t wc;
    173  1.1  tshiozak 
    174  1.1  tshiozak 	_DIAGASSERT(ei != NULL);
    175  1.1  tshiozak 	/* pwc may be NULL */
    176  1.1  tshiozak 	_DIAGASSERT(s != NULL);
    177  1.1  tshiozak 	_DIAGASSERT(psenc != NULL);
    178  1.1  tshiozak 
    179  1.1  tshiozak 	s0 = *s;
    180  1.1  tshiozak 
    181  1.1  tshiozak 	if (s0 == NULL) {
    182  1.1  tshiozak 		/* _citrus_GBK2K_init_state(ei, psenc); */
    183  1.1  tshiozak 		psenc->chlen = 0;
    184  1.1  tshiozak 		*nresult = 0;
    185  1.1  tshiozak 		return (0);
    186  1.1  tshiozak 	}
    187  1.1  tshiozak 
    188  1.1  tshiozak 	chlenbak = psenc->chlen;
    189  1.1  tshiozak 
    190  1.1  tshiozak 	switch (psenc->chlen) {
    191  1.1  tshiozak 	case 3:
    192  1.1  tshiozak 		if (!_mb_leadbyte (_PSENC))
    193  1.1  tshiozak 			goto invalid;
    194  1.1  tshiozak 	/* FALLTHROUGH */
    195  1.1  tshiozak 	case 2:
    196  1.1  tshiozak 		if (!_mb_surrogate(_PSENC) || _mb_trailbyte(_PSENC))
    197  1.1  tshiozak 			goto invalid;
    198  1.1  tshiozak 	/* FALLTHROUGH */
    199  1.1  tshiozak 	case 1:
    200  1.1  tshiozak 		if (!_mb_leadbyte (_PSENC))
    201  1.1  tshiozak 			goto invalid;
    202  1.1  tshiozak 	/* FALLTHOROUGH */
    203  1.1  tshiozak 	case 0:
    204  1.1  tshiozak 		break;
    205  1.1  tshiozak 	default:
    206  1.1  tshiozak 		goto invalid;
    207  1.1  tshiozak 	}
    208  1.1  tshiozak 
    209  1.1  tshiozak 	for (;;) {
    210  1.1  tshiozak 		if (n-- < 1)
    211  1.1  tshiozak 			goto restart;
    212  1.1  tshiozak 
    213  1.1  tshiozak 		_PUSH_PSENC(*s0++);
    214  1.1  tshiozak 
    215  1.1  tshiozak 		switch (psenc->chlen) {
    216  1.1  tshiozak 		case 1:
    217  1.1  tshiozak 			if (_mb_singlebyte(_PSENC))
    218  1.1  tshiozak 				goto convert;
    219  1.1  tshiozak 			if (_mb_leadbyte  (_PSENC))
    220  1.1  tshiozak 				continue;
    221  1.1  tshiozak 			goto ilseq;
    222  1.1  tshiozak 		case 2:
    223  1.1  tshiozak 			if (_mb_trailbyte (_PSENC))
    224  1.1  tshiozak 				goto convert;
    225  1.6   tnozaki 			if (ei->mb_cur_max == 4 &&
    226  1.3  tshiozak 			    _mb_surrogate (_PSENC))
    227  1.1  tshiozak 				continue;
    228  1.1  tshiozak 			goto ilseq;
    229  1.1  tshiozak 		case 3:
    230  1.1  tshiozak 			if (_mb_leadbyte  (_PSENC))
    231  1.1  tshiozak 				continue;
    232  1.1  tshiozak 			goto ilseq;
    233  1.1  tshiozak 		case 4:
    234  1.1  tshiozak 			if (_mb_surrogate (_PSENC))
    235  1.1  tshiozak 				goto convert;
    236  1.1  tshiozak 			goto ilseq;
    237  1.1  tshiozak 		}
    238  1.1  tshiozak 	}
    239  1.1  tshiozak 
    240  1.1  tshiozak convert:
    241  1.1  tshiozak 	len = psenc->chlen;
    242  1.1  tshiozak 	s1  = &psenc->ch[0];
    243  1.1  tshiozak 	wc  = 0;
    244  1.1  tshiozak 	while (len-- > 0)
    245  1.1  tshiozak 		wc = (wc << 8) | (*s1++ & 0xff);
    246  1.1  tshiozak 
    247  1.1  tshiozak 	if (pwc != NULL)
    248  1.1  tshiozak 		*pwc = wc;
    249  1.1  tshiozak 	*s = s0;
    250  1.1  tshiozak 	*nresult = (wc == 0) ? 0 : psenc->chlen - chlenbak;
    251  1.1  tshiozak 	/* _citrus_GBK2K_init_state(ei, psenc); */
    252  1.1  tshiozak 	psenc->chlen = 0;
    253  1.1  tshiozak 
    254  1.1  tshiozak 	return (0);
    255  1.1  tshiozak 
    256  1.1  tshiozak restart:
    257  1.1  tshiozak 	*s = s0;
    258  1.1  tshiozak 	*nresult = (size_t)-2;
    259  1.1  tshiozak 
    260  1.1  tshiozak 	return (0);
    261  1.1  tshiozak 
    262  1.1  tshiozak invalid:
    263  1.1  tshiozak 	return (EINVAL);
    264  1.1  tshiozak 
    265  1.1  tshiozak ilseq:
    266  1.1  tshiozak 	*nresult = (size_t)-1;
    267  1.1  tshiozak 	return (EILSEQ);
    268  1.1  tshiozak }
    269  1.1  tshiozak 
    270  1.1  tshiozak static int
    271  1.1  tshiozak _citrus_GBK2K_wcrtomb_priv(_GBK2KEncodingInfo * __restrict ei,
    272  1.1  tshiozak 			   char * __restrict s, size_t n, wchar_t wc,
    273  1.1  tshiozak 			   _GBK2KState * __restrict psenc,
    274  1.1  tshiozak 			   size_t * __restrict nresult)
    275  1.1  tshiozak {
    276  1.3  tshiozak 	int len, ret;
    277  1.1  tshiozak 
    278  1.1  tshiozak 	_DIAGASSERT(ei != NULL);
    279  1.1  tshiozak 	_DIAGASSERT(s != NULL);
    280  1.1  tshiozak 	_DIAGASSERT(psenc != NULL);
    281  1.1  tshiozak 
    282  1.3  tshiozak 	if (psenc->chlen != 0) {
    283  1.3  tshiozak 		ret = EINVAL;
    284  1.3  tshiozak 		goto err;
    285  1.3  tshiozak 	}
    286  1.1  tshiozak 
    287  1.1  tshiozak 	len = _mb_count(wc);
    288  1.3  tshiozak 	if (n < len) {
    289  1.3  tshiozak 		ret = E2BIG;
    290  1.3  tshiozak 		goto err;
    291  1.3  tshiozak 	}
    292  1.1  tshiozak 
    293  1.1  tshiozak 	switch (len) {
    294  1.1  tshiozak 	case 1:
    295  1.3  tshiozak 		if (!_mb_singlebyte(_PUSH_PSENC(wc     ))) {
    296  1.3  tshiozak 			ret = EILSEQ;
    297  1.3  tshiozak 			goto err;
    298  1.3  tshiozak 		}
    299  1.1  tshiozak 		break;
    300  1.1  tshiozak 	case 2:
    301  1.1  tshiozak 		if (!_mb_leadbyte  (_PUSH_PSENC(wc >> 8)) ||
    302  1.3  tshiozak 		    !_mb_trailbyte (_PUSH_PSENC(wc     ))) {
    303  1.3  tshiozak 			ret = EILSEQ;
    304  1.3  tshiozak 			goto err;
    305  1.3  tshiozak 		}
    306  1.1  tshiozak 		break;
    307  1.1  tshiozak 	case 4:
    308  1.6   tnozaki 		if (ei->mb_cur_max != 4 ||
    309  1.3  tshiozak 		    !_mb_leadbyte  (_PUSH_PSENC(wc >> 24)) ||
    310  1.1  tshiozak 		    !_mb_surrogate (_PUSH_PSENC(wc >> 16)) ||
    311  1.1  tshiozak 		    !_mb_leadbyte  (_PUSH_PSENC(wc >>  8)) ||
    312  1.3  tshiozak 		    !_mb_surrogate (_PUSH_PSENC(wc      ))) {
    313  1.3  tshiozak 			ret = EILSEQ;
    314  1.3  tshiozak 			goto err;
    315  1.3  tshiozak 		}
    316  1.1  tshiozak 		break;
    317  1.1  tshiozak 	}
    318  1.1  tshiozak 
    319  1.1  tshiozak 	_DIAGASSERT(len == psenc->chlen);
    320  1.1  tshiozak 
    321  1.1  tshiozak 	memcpy(s, psenc->ch, psenc->chlen);
    322  1.1  tshiozak 	*nresult = psenc->chlen;
    323  1.1  tshiozak 	/* _citrus_GBK2K_init_state(ei, psenc); */
    324  1.1  tshiozak 	psenc->chlen = 0;
    325  1.1  tshiozak 
    326  1.1  tshiozak 	return (0);
    327  1.1  tshiozak 
    328  1.3  tshiozak err:
    329  1.3  tshiozak 	*nresult = (size_t)-1;
    330  1.3  tshiozak 	return ret;
    331  1.3  tshiozak }
    332  1.3  tshiozak 
    333  1.3  tshiozak static __inline int
    334  1.3  tshiozak /*ARGSUSED*/
    335  1.3  tshiozak _citrus_GBK2K_stdenc_wctocs(_GBK2KEncodingInfo * __restrict ei,
    336  1.3  tshiozak 			    _csid_t * __restrict csid,
    337  1.3  tshiozak 			    _index_t * __restrict idx, wchar_t wc)
    338  1.3  tshiozak {
    339  1.3  tshiozak 	u_int8_t ch, cl;
    340  1.3  tshiozak 
    341  1.3  tshiozak 	_DIAGASSERT(csid != NULL && idx != NULL);
    342  1.3  tshiozak 
    343  1.3  tshiozak 	if ((u_int32_t)wc<0x80) {
    344  1.3  tshiozak 		/* ISO646 */
    345  1.3  tshiozak 		*csid = 0;
    346  1.3  tshiozak 		*idx = (_index_t)wc;
    347  1.3  tshiozak 	} else if ((u_int32_t)wc>=0x10000) {
    348  1.3  tshiozak 		/* GBKUCS : XXX */
    349  1.3  tshiozak 		*csid = 3;
    350  1.3  tshiozak 		*idx = (_index_t)wc;
    351  1.3  tshiozak 	} else {
    352  1.3  tshiozak 		ch = (u_int8_t)(wc >> 8);
    353  1.3  tshiozak 		cl = (u_int8_t)wc;
    354  1.3  tshiozak 		if (ch>=0xA1 && cl>=0xA1) {
    355  1.3  tshiozak 			/* EUC G1 */
    356  1.3  tshiozak 			*csid = 1;
    357  1.3  tshiozak 			*idx = (_index_t)wc & 0x7F7FU;
    358  1.3  tshiozak 		} else {
    359  1.3  tshiozak 			/* extended area (0x8140-) */
    360  1.3  tshiozak 			*csid = 2;
    361  1.3  tshiozak 			*idx = (_index_t)wc;
    362  1.3  tshiozak 		}
    363  1.3  tshiozak 	}
    364  1.3  tshiozak 
    365  1.3  tshiozak 	return 0;
    366  1.3  tshiozak }
    367  1.3  tshiozak 
    368  1.3  tshiozak static __inline int
    369  1.3  tshiozak /*ARGSUSED*/
    370  1.3  tshiozak _citrus_GBK2K_stdenc_cstowc(_GBK2KEncodingInfo * __restrict ei,
    371  1.3  tshiozak 			    wchar_t * __restrict wc,
    372  1.3  tshiozak 			    _csid_t csid, _index_t idx)
    373  1.3  tshiozak {
    374  1.3  tshiozak 
    375  1.3  tshiozak 	_DIAGASSERT(wc != NULL);
    376  1.3  tshiozak 
    377  1.3  tshiozak 	switch (csid) {
    378  1.3  tshiozak 	case 0:
    379  1.3  tshiozak 		/* ISO646 */
    380  1.3  tshiozak 		*wc = (wchar_t)idx;
    381  1.3  tshiozak 		break;
    382  1.3  tshiozak 	case 1:
    383  1.3  tshiozak 		/* EUC G1 */
    384  1.3  tshiozak 		*wc = (wchar_t)idx | 0x8080U;
    385  1.3  tshiozak 		break;
    386  1.3  tshiozak 	case 2:
    387  1.3  tshiozak 		/* extended area */
    388  1.3  tshiozak 		*wc = (wchar_t)idx;
    389  1.3  tshiozak 		break;
    390  1.3  tshiozak 	case 3:
    391  1.3  tshiozak 		/* GBKUCS : XXX */
    392  1.6   tnozaki 		if (ei->mb_cur_max != 4)
    393  1.3  tshiozak 			return EINVAL;
    394  1.3  tshiozak 		*wc = (wchar_t)idx;
    395  1.3  tshiozak 		break;
    396  1.3  tshiozak 	default:
    397  1.3  tshiozak 		return EILSEQ;
    398  1.3  tshiozak 	}
    399  1.1  tshiozak 
    400  1.3  tshiozak 	return 0;
    401  1.1  tshiozak }
    402  1.1  tshiozak 
    403  1.5  tshiozak static __inline int
    404  1.5  tshiozak /*ARGSUSED*/
    405  1.5  tshiozak _citrus_GBK2K_stdenc_get_state_desc_generic(_GBK2KEncodingInfo * __restrict ei,
    406  1.5  tshiozak 					    _GBK2KState * __restrict psenc,
    407  1.5  tshiozak 					    int * __restrict rstate)
    408  1.5  tshiozak {
    409  1.5  tshiozak 
    410  1.5  tshiozak 	if (psenc->chlen == 0)
    411  1.5  tshiozak 		*rstate = _STDENC_SDGEN_INITIAL;
    412  1.5  tshiozak 	else
    413  1.5  tshiozak 		*rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
    414  1.5  tshiozak 
    415  1.5  tshiozak 	return 0;
    416  1.5  tshiozak }
    417  1.5  tshiozak 
    418  1.1  tshiozak static int
    419  1.1  tshiozak /*ARGSUSED*/
    420  1.3  tshiozak _citrus_GBK2K_encoding_module_init(_GBK2KEncodingInfo * __restrict ei,
    421  1.3  tshiozak 				   const void * __restrict var, size_t lenvar)
    422  1.1  tshiozak {
    423  1.3  tshiozak 	const char *p;
    424  1.3  tshiozak 
    425  1.1  tshiozak 	_DIAGASSERT(ei != NULL);
    426  1.1  tshiozak 
    427  1.3  tshiozak 	p = var;
    428  1.3  tshiozak #define MATCH(x, act)                                           \
    429  1.3  tshiozak do {                                                            \
    430  1.3  tshiozak         if (lenvar >= (sizeof(#x)-1) &&                         \
    431  1.3  tshiozak             _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) {       \
    432  1.3  tshiozak                 act;                                            \
    433  1.3  tshiozak                 lenvar -= sizeof(#x)-1;                         \
    434  1.3  tshiozak                 p += sizeof(#x)-1;                              \
    435  1.3  tshiozak         }                                                       \
    436  1.3  tshiozak } while (/*CONSTCOND*/0)
    437  1.6   tnozaki 	memset((void *)ei, 0, sizeof(*ei));
    438  1.6   tnozaki 	ei->mb_cur_max = 4;
    439  1.3  tshiozak 	while (lenvar>0) {
    440  1.3  tshiozak 		switch (_bcs_tolower(*p)) {
    441  1.3  tshiozak 		case '2':
    442  1.6   tnozaki 			MATCH("2byte", ei->mb_cur_max = 2);
    443  1.3  tshiozak 			break;
    444  1.3  tshiozak 		}
    445  1.3  tshiozak 		p++;
    446  1.3  tshiozak 		lenvar--;
    447  1.3  tshiozak 	}
    448  1.3  tshiozak 
    449  1.1  tshiozak 	return (0);
    450  1.1  tshiozak }
    451  1.1  tshiozak 
    452  1.1  tshiozak static void
    453  1.1  tshiozak /*ARGSUSED*/
    454  1.3  tshiozak _citrus_GBK2K_encoding_module_uninit(_GBK2KEncodingInfo *ei)
    455  1.1  tshiozak {
    456  1.1  tshiozak }
    457  1.1  tshiozak 
    458  1.1  tshiozak 
    459  1.1  tshiozak /* ----------------------------------------------------------------------
    460  1.1  tshiozak  * public interface for ctype
    461  1.1  tshiozak  */
    462  1.1  tshiozak 
    463  1.1  tshiozak _CITRUS_CTYPE_DECLS(GBK2K);
    464  1.1  tshiozak _CITRUS_CTYPE_DEF_OPS(GBK2K);
    465  1.1  tshiozak 
    466  1.1  tshiozak #include "citrus_ctype_template.h"
    467  1.3  tshiozak 
    468  1.3  tshiozak /* ----------------------------------------------------------------------
    469  1.3  tshiozak  * public interface for stdenc
    470  1.3  tshiozak  */
    471  1.3  tshiozak 
    472  1.3  tshiozak _CITRUS_STDENC_DECLS(GBK2K);
    473  1.3  tshiozak _CITRUS_STDENC_DEF_OPS(GBK2K);
    474  1.3  tshiozak 
    475  1.3  tshiozak #include "citrus_stdenc_template.h"
    476