1 /* $NetBSD: src/lib/libc/citrus/modules/citrus_big5.c,v 1.8 2003/08/07 16:42:38 agc Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_big5.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */
5 * Copyright (c)2002 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
35 * Paul Borman at Krystal Technologies.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/types.h>
73 #include "citrus_namespace.h"
74 #include "citrus_types.h"
75 #include "citrus_module.h"
76 #include "citrus_ctype.h"
77 #include "citrus_stdenc.h"
78 #include "citrus_big5.h"
80 /* ----------------------------------------------------------------------
81 * private stuffs used by templates
96 /* for future multi-locale facility */
101 _BIG5State s_mbsrtowcs;
102 _BIG5State s_wcrtomb;
103 _BIG5State s_wcsrtombs;
108 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
109 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
111 #define _FUNCNAME(m) _citrus_BIG5_##m
112 #define _ENCODING_INFO _BIG5EncodingInfo
113 #define _CTYPE_INFO _BIG5CTypeInfo
114 #define _ENCODING_STATE _BIG5State
115 #define _ENCODING_MB_CUR_MAX(_ei_) 2
116 #define _ENCODING_IS_STATE_DEPENDENT 0
117 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
122 _citrus_BIG5_init_state(_BIG5EncodingInfo * __restrict ei,
123 _BIG5State * __restrict s)
125 memset(s, 0, sizeof(*s));
130 _citrus_BIG5_pack_state(_BIG5EncodingInfo * __restrict ei,
131 void * __restrict pspriv,
132 const _BIG5State * __restrict s)
134 memcpy(pspriv, (const void *)s, sizeof(*s));
139 _citrus_BIG5_unpack_state(_BIG5EncodingInfo * __restrict ei,
140 _BIG5State * __restrict s,
141 const void * __restrict pspriv)
143 memcpy((void *)s, pspriv, sizeof(*s));
147 _citrus_BIG5_check(u_int c)
150 return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1);
154 _citrus_BIG5_check2(u_int c)
157 if ((c >= 0x40 && c <= 0x7f) || (c >= 0xa1 && c <= 0xfe))
165 _citrus_BIG5_encoding_module_init(_BIG5EncodingInfo * __restrict ei,
166 const void * __restrict var, size_t lenvar)
168 _DIAGASSERT(ei != NULL);
170 memset((void *)ei, 0, sizeof(*ei));
177 _citrus_BIG5_encoding_module_uninit(_BIG5EncodingInfo *ei)
183 _citrus_BIG5_mbrtowc_priv(_BIG5EncodingInfo * __restrict ei,
184 wchar_t * __restrict pwc,
185 const char ** __restrict s, size_t n,
186 _BIG5State * __restrict psenc,
187 size_t * __restrict nresult)
194 _DIAGASSERT(nresult != 0);
195 _DIAGASSERT(ei != NULL);
196 _DIAGASSERT(psenc != NULL);
197 _DIAGASSERT(s != NULL && *s != NULL);
202 _citrus_BIG5_init_state(ei, psenc);
207 chlenbak = psenc->chlen;
209 /* make sure we have the first byte in the buffer */
210 switch (psenc->chlen) {
214 psenc->ch[0] = *s0++;
225 c = _citrus_BIG5_check(psenc->ch[0] & 0xff);
228 while (psenc->chlen < c) {
232 psenc->ch[psenc->chlen] = *s0++;
239 wchar = psenc->ch[0] & 0xff;
242 if (!_citrus_BIG5_check2(psenc->ch[1] & 0xff))
244 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
258 *nresult = c - chlenbak;
264 *nresult = (size_t)-1;
269 *nresult = (size_t)-2;
275 _citrus_BIG5_wcrtomb_priv(_BIG5EncodingInfo * __restrict ei,
277 size_t n, wchar_t wc, _BIG5State * __restrict psenc,
278 size_t * __restrict nresult)
282 _DIAGASSERT(ei != NULL);
283 _DIAGASSERT(nresult != 0);
284 _DIAGASSERT(s != NULL);
286 /* check invalid sequence */
293 if (_citrus_BIG5_check((wc >> 8) & 0xff) != 2 ||
294 !_citrus_BIG5_check2(wc & 0xff)) {
300 if (wc & ~0xff || !_citrus_BIG5_check(wc & 0xff)) {
308 /* bound check failure */
314 s[0] = (wc >> 8) & 0xff;
324 *nresult = (size_t)-1;
330 _citrus_BIG5_stdenc_wctocs(_BIG5EncodingInfo * __restrict ei,
331 _csid_t * __restrict csid,
332 _index_t * __restrict idx, wchar_t wc)
335 _DIAGASSERT(csid != NULL && idx != NULL);
348 _citrus_BIG5_stdenc_cstowc(_BIG5EncodingInfo * __restrict ei,
349 wchar_t * __restrict wc,
350 _csid_t csid, _index_t idx)
354 _DIAGASSERT(wc != NULL);
367 if (h<0xA1 || h>0xF9 || l<0x40 || l>0xFE)
378 /* ----------------------------------------------------------------------
379 * public interface for ctype
382 _CITRUS_CTYPE_DECLS(BIG5);
383 _CITRUS_CTYPE_DEF_OPS(BIG5);
385 #include "citrus_ctype_template.h"
388 /* ----------------------------------------------------------------------
389 * public interface for stdenc
392 _CITRUS_STDENC_DECLS(BIG5);
393 _CITRUS_STDENC_DEF_OPS(BIG5);
395 #include "citrus_stdenc_template.h"