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