1 /* $NetBSD: citrus_dechanyu.c,v 1.4 2011/11/19 18:20:13 tnozaki Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c)2007 Citrus Project,
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_bcs.h"
46 #include "citrus_module.h"
47 #include "citrus_stdenc.h"
48 #include "citrus_dechanyu.h"
50 /* ----------------------------------------------------------------------
51 * private stuffs used by templates
61 } _DECHanyuEncodingInfo;
63 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
64 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.__CONCAT(s_,_func_)
66 #define _FUNCNAME(m) __CONCAT(_citrus_DECHanyu_,m)
67 #define _ENCODING_INFO _DECHanyuEncodingInfo
68 #define _ENCODING_STATE _DECHanyuState
69 #define _ENCODING_MB_CUR_MAX(_ei_) 4
70 #define _ENCODING_IS_STATE_DEPENDENT 0
71 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
75 _citrus_DECHanyu_init_state(_DECHanyuEncodingInfo * __restrict ei __unused,
76 _DECHanyuState * __restrict psenc)
85 _citrus_DECHanyu_pack_state(_DECHanyuEncodingInfo * __restrict ei __unused,
86 void * __restrict pspriv, const _DECHanyuState * __restrict psenc)
89 memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
94 _citrus_DECHanyu_unpack_state(_DECHanyuEncodingInfo * __restrict ei __unused,
95 _DECHanyuState * __restrict psenc,
96 const void * __restrict pspriv)
99 memcpy((void *)psenc, pspriv, sizeof(*psenc));
105 _citrus_DECHanyu_encoding_module_uninit(_DECHanyuEncodingInfo *ei __unused)
113 _citrus_DECHanyu_encoding_module_init(_DECHanyuEncodingInfo * __restrict ei __unused,
114 const void * __restrict var __unused, size_t lenvar __unused)
132 return (c >= 0xA1 && c <= 0xFE);
140 return (c >= 0x21 && c <= 0x7E);
157 #define HANYUBIT 0xC2CB0000
163 return (c >= 0x21 && c <= 0x7E);
168 _citrus_DECHanyu_mbrtowc_priv(_DECHanyuEncodingInfo * __restrict ei,
169 wchar_t * __restrict pwc, char ** __restrict s, size_t n,
170 _DECHanyuState * __restrict psenc, size_t * __restrict nresult)
177 _citrus_DECHanyu_init_state(ei, psenc);
178 *nresult = _ENCODING_IS_STATE_DEPENDENT;
184 switch (psenc->chlen) {
189 if (is_singlebyte(ch)) {
192 *nresult = (size_t)((ch == 0) ? 0 : 1);
196 if (!is_leadbyte(ch))
198 psenc->ch[psenc->chlen++] = ch;
201 ch = psenc->ch[0] & 0xFF;
202 if (!is_leadbyte(ch))
206 ch = psenc->ch[0] & 0xFF;
208 ch = psenc->ch[1] & 0xFF;
210 wc |= (wchar_t)HANYUBIT;
219 switch (psenc->chlen) {
227 psenc->ch[psenc->chlen++] = ch;
228 wc |= (wchar_t)HANYUBIT;
232 if (!is_leadbyte(ch))
234 psenc->ch[psenc->chlen++] = ch;
241 if (!is_leadbyte(ch))
243 psenc->ch[psenc->chlen++] = ch;
246 ch = psenc->ch[2] & 0xFF;
247 if (!is_leadbyte(ch))
252 wc |= (wchar_t)(ch << 8);
254 if (!is_trailbyte(ch))
259 *nresult = (size_t)(s0 - *s);
266 *nresult = (size_t)-2;
271 *nresult = (size_t)-1;
277 _citrus_DECHanyu_wcrtomb_priv(_DECHanyuEncodingInfo * __restrict ei __unused,
278 char * __restrict s, size_t n, wchar_t wc,
279 _DECHanyuState * __restrict psenc, size_t * __restrict nresult)
283 if (psenc->chlen != 0)
286 /* XXX: assume wchar_t as int */
287 if ((uint32_t)wc <= 0x7F) {
290 if ((uint32_t)wc > 0xFFFF) {
291 if ((wc & ~0xFFFF) != (wchar_t)HANYUBIT)
293 psenc->ch[psenc->chlen++] = (wc >> 24) & 0xFF;
294 psenc->ch[psenc->chlen++] = (wc >> 16) & 0xFF;
297 ch = (wc >> 8) & 0xFF;
298 if (!is_leadbyte(ch))
300 psenc->ch[psenc->chlen++] = ch;
302 if (!is_trailbyte(ch))
305 psenc->ch[psenc->chlen++] = ch;
306 if (n < psenc->chlen) {
307 *nresult = (size_t)-1;
310 memcpy(s, psenc->ch, psenc->chlen);
311 *nresult = psenc->chlen;
317 *nresult = (size_t)-1;
323 _citrus_DECHanyu_stdenc_wctocs(_DECHanyuEncodingInfo * __restrict ei __unused,
324 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
331 /* XXX: assume wchar_t as int */
332 if ((uint32_t)wc > 0x7F) {
333 if ((uint32_t)wc > 0xFFFF) {
334 if ((wc & ~0xFFFF) != (wchar_t)HANYUBIT)
338 if (!is_leadbyte((wc >> 8) & 0xFF) ||
339 !is_trailbyte(wc & 0xFF))
341 plane += (wc & 0x80) ? 1 : 2;
345 *idx = (_index_t)(wc & mask);
352 _citrus_DECHanyu_stdenc_cstowc(_DECHanyuEncodingInfo * __restrict ei __unused,
353 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
359 } else if (csid <= 4) {
360 if (!is_94charset(idx >> 8))
362 if (!is_94charset(idx & 0xFF))
377 _citrus_DECHanyu_stdenc_get_state_desc_generic(
378 _DECHanyuEncodingInfo * __restrict ei __unused,
379 _DECHanyuState * __restrict psenc, int * __restrict rstate)
382 *rstate = (psenc->chlen == 0)
383 ? _STDENC_SDGEN_INITIAL
384 : _STDENC_SDGEN_INCOMPLETE_CHAR;
388 /* ----------------------------------------------------------------------
389 * public interface for stdenc
392 _CITRUS_STDENC_DECLS(DECHanyu);
393 _CITRUS_STDENC_DEF_OPS(DECHanyu);
395 #include "citrus_stdenc_template.h"