2 /* $NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2002 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c)1999 Citrus Project,
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
60 #include <sys/cdefs.h>
61 #include <sys/types.h>
72 #include "citrus_namespace.h"
73 #include "citrus_types.h"
74 #include "citrus_module.h"
75 #include "citrus_stdenc.h"
76 #include "citrus_euctw.h"
79 /* ----------------------------------------------------------------------
80 * private stuffs used by templates
95 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
96 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
98 #define _FUNCNAME(m) _citrus_EUCTW_##m
99 #define _ENCODING_INFO _EUCTWEncodingInfo
100 #define _ENCODING_STATE _EUCTWState
101 #define _ENCODING_MB_CUR_MAX(_ei_) 4
102 #define _ENCODING_IS_STATE_DEPENDENT 0
103 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
106 _citrus_EUCTW_cs(unsigned int c)
111 return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
115 _citrus_EUCTW_count(int cs)
134 _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei __unused,
135 _EUCTWState * __restrict s)
138 memset(s, 0, sizeof(*s));
144 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei __unused,
145 void * __restrict pspriv, const _EUCTWState * __restrict s)
148 memcpy(pspriv, (const void *)s, sizeof(*s));
153 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei __unused,
154 _EUCTWState * __restrict s, const void * __restrict pspriv)
157 memcpy((void *)s, pspriv, sizeof(*s));
163 _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,
164 const void * __restrict var __unused, size_t lenvar __unused)
167 memset((void *)ei, 0, sizeof(*ei));
174 _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei __unused)
180 _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,
181 wchar_t * __restrict pwc, char ** __restrict s,
182 size_t n, _EUCTWState * __restrict psenc, size_t * __restrict nresult)
191 _citrus_EUCTW_init_state(ei, psenc);
192 *nresult = 0; /* state independent */
196 chlenbak = psenc->chlen;
198 /* make sure we have the first byte in the buffer */
199 switch (psenc->chlen) {
203 psenc->ch[0] = *s0++;
215 c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
218 while (psenc->chlen < c) {
221 psenc->ch[psenc->chlen] = *s0++;
229 if (psenc->ch[0] & 0x80)
231 wchar = psenc->ch[0] & 0xff;
234 if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
236 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
240 if ((unsigned char)psenc->ch[1] < 0xa1 ||
241 0xa7 < (unsigned char)psenc->ch[1])
243 if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
245 wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
246 wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
257 *nresult = wchar ? c - chlenbak : 0;
262 *nresult = (size_t)-1;
267 *nresult = (size_t)-1;
272 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei __unused,
273 char * __restrict s, size_t n, wchar_t wc,
274 _EUCTWState * __restrict psenc __unused, size_t * __restrict nresult)
280 cs = wc & 0x7f000080;
284 if ((wc & 0x007f0000) && !(wc & 0x00800000))
287 if (clen == 1 && cs == 0x00000000) {
295 } else if (clen == 2 && cs == ('G' << 24)) {
304 } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
305 /* CNS-11643-[2-7] */
312 *s++ = (cs >> 24) - 'H' + 0xa2;
322 *s++ = (v >> (i << 3)) & 0xff;
328 *nresult = (size_t)-1;
334 _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei __unused,
335 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
338 *csid = (_csid_t)(wc >> 24) & 0xFF;
339 *idx = (_index_t)(wc & 0x7F7F);
346 _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei __unused,
347 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
351 if ((idx & ~0x7F) != 0)
355 if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0)
357 *wc = (wchar_t)idx | ((wchar_t)csid << 24);
365 _citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei __unused,
366 _EUCTWState * __restrict psenc, int * __restrict rstate)
369 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
370 _STDENC_SDGEN_INCOMPLETE_CHAR;
374 /* ----------------------------------------------------------------------
375 * public interface for stdenc
378 _CITRUS_STDENC_DECLS(EUCTW);
379 _CITRUS_STDENC_DEF_OPS(EUCTW);
381 #include "citrus_stdenc_template.h"