1 /* $NetBSD: citrus_dechanyu.c,v 1.2 2007/04/24 15:42:08 tnozaki Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_dechanyu.c,v 1.1 2008/04/10 10:21:01 hasso Exp $ */
5 * Copyright (c)2007 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
30 #include <sys/types.h>
42 #include "citrus_namespace.h"
43 #include "citrus_types.h"
44 #include "citrus_bcs.h"
45 #include "citrus_module.h"
46 #include "citrus_ctype.h"
47 #include "citrus_stdenc.h"
48 #include "citrus_dechanyu.h"
50 /* ----------------------------------------------------------------------
51 * private stuffs used by templates
61 } _DECHanyuEncodingInfo
;
64 _DECHanyuEncodingInfo ei
;
66 /* for future multi-locale facility */
67 _DECHanyuState s_mblen
;
68 _DECHanyuState s_mbrlen
;
69 _DECHanyuState s_mbrtowc
;
70 _DECHanyuState s_mbtowc
;
71 _DECHanyuState s_mbsrtowcs
;
72 _DECHanyuState s_wcrtomb
;
73 _DECHanyuState s_wcsrtombs
;
74 _DECHanyuState s_wctomb
;
78 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
79 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.__CONCAT(s_,_func_)
81 #define _FUNCNAME(m) __CONCAT(_citrus_DECHanyu_,m)
82 #define _ENCODING_INFO _DECHanyuEncodingInfo
83 #define _CTYPE_INFO _DECHanyuCTypeInfo
84 #define _ENCODING_STATE _DECHanyuState
85 #define _ENCODING_MB_CUR_MAX(_ei_) 4
86 #define _ENCODING_IS_STATE_DEPENDENT 0
87 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
91 _citrus_DECHanyu_init_state(_DECHanyuEncodingInfo
* __restrict ei
,
92 _DECHanyuState
* __restrict psenc
)
95 _DIAGASSERT(psenc
!= NULL
);
102 _citrus_DECHanyu_pack_state(_DECHanyuEncodingInfo
* __restrict ei
,
103 void * __restrict pspriv
,
104 const _DECHanyuState
* __restrict psenc
)
107 _DIAGASSERT(pspriv
!= NULL
);
108 _DIAGASSERT(psenc
!= NULL
);
110 memcpy(pspriv
, (const void *)psenc
, sizeof(*psenc
));
115 _citrus_DECHanyu_unpack_state(_DECHanyuEncodingInfo
* __restrict ei
,
116 _DECHanyuState
* __restrict psenc
,
117 const void * __restrict pspriv
)
120 _DIAGASSERT(psenc
!= NULL
);
121 _DIAGASSERT(pspriv
!= NULL
);
123 memcpy((void *)psenc
, pspriv
, sizeof(*psenc
));
128 _citrus_DECHanyu_encoding_module_uninit(_DECHanyuEncodingInfo
*ei
)
135 _citrus_DECHanyu_encoding_module_init(_DECHanyuEncodingInfo
* __restrict ei
,
136 const void * __restrict var
, size_t lenvar
)
151 return c
>= 0xA1 && c
<= 0xFE;
158 return c
>= 0x21 && c
<= 0x7E;
173 #define HANYUBIT 0xC2CB0000
178 return c
>= 0x21 && c
<= 0x7E;
183 _citrus_DECHanyu_mbrtowc_priv(_DECHanyuEncodingInfo
* __restrict ei
,
184 wchar_t * __restrict pwc
, const char ** __restrict s
, size_t n
,
185 _DECHanyuState
* __restrict psenc
, size_t * __restrict nresult
)
191 /* ei may be unused */
192 _DIAGASSERT(s
!= NULL
);
193 _DIAGASSERT(psenc
!= NULL
);
194 _DIAGASSERT(nresult
!= NULL
);
197 _citrus_DECHanyu_init_state(ei
, psenc
);
198 *nresult
= _ENCODING_IS_STATE_DEPENDENT
;
204 switch (psenc
->chlen
) {
209 if (is_singlebyte(ch
) != 0) {
212 *nresult
= (size_t)((ch
== 0) ? 0 : 1);
216 if (is_leadbyte(ch
) == 0)
218 psenc
->ch
[psenc
->chlen
++] = ch
;
221 ch
= psenc
->ch
[0] & 0xFF;
222 if (is_leadbyte(ch
) == 0)
226 ch
= psenc
->ch
[0] & 0xFF;
227 if (is_hanyu1(ch
) != 0) {
228 ch
= psenc
->ch
[1] & 0xFF;
229 if (is_hanyu2(ch
) != 0) {
230 wc
|= (wchar_t)HANYUBIT
;
239 switch (psenc
->chlen
) {
241 if (is_hanyu1(ch
) != 0) {
245 if (is_hanyu2(ch
) == 0)
247 psenc
->ch
[psenc
->chlen
++] = ch
;
248 wc
|= (wchar_t)HANYUBIT
;
252 if (is_leadbyte(ch
) == 0)
254 psenc
->ch
[psenc
->chlen
++] = ch
;
261 if (is_leadbyte(ch
) == 0)
263 psenc
->ch
[psenc
->chlen
++] = ch
;
266 ch
= psenc
->ch
[2] & 0xFF;
267 if (is_leadbyte(ch
) == 0)
272 wc
|= (wchar_t)(ch
<< 8);
274 if (is_trailbyte(ch
) == 0)
279 *nresult
= (size_t)(s0
- *s
);
286 *nresult
= (size_t)-2;
291 *nresult
= (size_t)-1;
297 _citrus_DECHanyu_wcrtomb_priv(_DECHanyuEncodingInfo
* __restrict ei
,
298 char * __restrict s
, size_t n
, wchar_t wc
,
299 _DECHanyuState
* __restrict psenc
, size_t * __restrict nresult
)
303 /* ei may be unused */
304 _DIAGASSERT(s
!= NULL
);
305 _DIAGASSERT(psenc
!= NULL
);
306 _DIAGASSERT(nresult
!= NULL
);
308 if (psenc
->chlen
!= 0)
311 /* XXX: assume wchar_t as int */
312 if ((uint32_t)wc
<= 0x7F) {
315 if ((uint32_t)wc
> 0xFFFF) {
316 if ((wc
& ~0xFFFF) != HANYUBIT
)
318 psenc
->ch
[psenc
->chlen
++] = (wc
>> 24) & 0xFF;
319 psenc
->ch
[psenc
->chlen
++] = (wc
>> 16) & 0xFF;
322 ch
= (wc
>> 8) & 0xFF;
323 if (!is_leadbyte(ch
))
325 psenc
->ch
[psenc
->chlen
++] = ch
;
327 if (is_trailbyte(ch
) == 0)
330 psenc
->ch
[psenc
->chlen
++] = ch
;
331 if (n
< psenc
->chlen
) {
332 *nresult
= (size_t)-1;
335 memcpy(s
, psenc
->ch
, psenc
->chlen
);
336 *nresult
= psenc
->chlen
;
342 *nresult
= (size_t)-1;
348 _citrus_DECHanyu_stdenc_wctocs(_DECHanyuEncodingInfo
* __restrict ei
,
349 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
354 /* ei may be unused */
355 _DIAGASSERT(csid
!= NULL
);
356 _DIAGASSERT(idx
!= NULL
);
360 /* XXX: assume wchar_t as int */
361 if ((uint32_t)wc
> 0x7F) {
362 if ((uint32_t)wc
> 0xFFFF) {
363 if ((wc
& ~0xFFFF) != HANYUBIT
)
367 if (is_leadbyte((wc
>> 8) & 0xFF) == 0 ||
368 is_trailbyte(wc
& 0xFF) == 0)
370 plane
+= (wc
& 0x80) ? 1 : 2;
374 *idx
= (_index_t
)(wc
& mask
);
381 _citrus_DECHanyu_stdenc_cstowc(_DECHanyuEncodingInfo
* __restrict ei
,
382 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
384 /* ei may be unused */
385 _DIAGASSERT(wc
!= NULL
);
390 } else if (csid
<= 4) {
391 if (is_94charset(idx
>> 8) == 0)
393 if (is_94charset(idx
& 0xFF) == 0)
408 _citrus_DECHanyu_stdenc_get_state_desc_generic(
409 _DECHanyuEncodingInfo
* __restrict ei
,
410 _DECHanyuState
* __restrict psenc
, int * __restrict rstate
)
412 /* ei may be unused */
413 _DIAGASSERT(psenc
!= NULL
);
414 _DIAGASSERT(rstate
!= NULL
);
416 *rstate
= (psenc
->chlen
== 0)
417 ? _STDENC_SDGEN_INITIAL
418 : _STDENC_SDGEN_INCOMPLETE_CHAR
;
422 /* ----------------------------------------------------------------------
423 * public interface for ctype
426 _CITRUS_CTYPE_DECLS(DECHanyu
);
427 _CITRUS_CTYPE_DEF_OPS(DECHanyu
);
429 #include "citrus_ctype_template.h"
432 /* ----------------------------------------------------------------------
433 * public interface for stdenc
436 _CITRUS_STDENC_DECLS(DECHanyu
);
437 _CITRUS_STDENC_DEF_OPS(DECHanyu
);
439 #include "citrus_stdenc_template.h"