1 /* $FreeBSD: head/lib/libiconv_modules/DECHanyu/citrus_dechanyu.c 281550 2015-04-15 09:09:20Z tijl $ */
2 /* $NetBSD: citrus_dechanyu.c,v 1.4 2011/11/19 18:20:13 tnozaki 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
29 #include <sys/cdefs.h>
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_stdenc.h"
47 #include "citrus_dechanyu.h"
49 /* ----------------------------------------------------------------------
50 * private stuffs used by templates
60 } _DECHanyuEncodingInfo
;
62 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
63 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.__CONCAT(s_,_func_)
65 #define _FUNCNAME(m) __CONCAT(_citrus_DECHanyu_,m)
66 #define _ENCODING_INFO _DECHanyuEncodingInfo
67 #define _ENCODING_STATE _DECHanyuState
68 #define _ENCODING_MB_CUR_MAX(_ei_) 4
69 #define _ENCODING_IS_STATE_DEPENDENT 0
70 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
74 _citrus_DECHanyu_init_state(_DECHanyuEncodingInfo
* __restrict ei __unused
,
75 _DECHanyuState
* __restrict psenc
)
84 _citrus_DECHanyu_pack_state(_DECHanyuEncodingInfo
* __restrict ei __unused
,
85 void * __restrict pspriv
, const _DECHanyuState
* __restrict psenc
)
88 memcpy(pspriv
, (const void *)psenc
, sizeof(*psenc
));
93 _citrus_DECHanyu_unpack_state(_DECHanyuEncodingInfo
* __restrict ei __unused
,
94 _DECHanyuState
* __restrict psenc
,
95 const void * __restrict pspriv
)
98 memcpy((void *)psenc
, pspriv
, sizeof(*psenc
));
104 _citrus_DECHanyu_encoding_module_uninit(_DECHanyuEncodingInfo
*ei __unused
)
112 _citrus_DECHanyu_encoding_module_init(_DECHanyuEncodingInfo
* __restrict ei __unused
,
113 const void * __restrict var __unused
, size_t lenvar __unused
)
131 return (c
>= 0xA1 && c
<= 0xFE);
139 return (c
>= 0x21 && c
<= 0x7E);
156 #define HANYUBIT 0xC2CB0000
162 return (c
>= 0x21 && c
<= 0x7E);
167 _citrus_DECHanyu_mbrtowc_priv(_DECHanyuEncodingInfo
* __restrict ei
,
168 wchar_t * __restrict pwc
, char ** __restrict s
, size_t n
,
169 _DECHanyuState
* __restrict psenc
, size_t * __restrict nresult
)
176 _citrus_DECHanyu_init_state(ei
, psenc
);
177 *nresult
= _ENCODING_IS_STATE_DEPENDENT
;
183 switch (psenc
->chlen
) {
188 if (is_singlebyte(ch
)) {
191 *nresult
= (size_t)((ch
== 0) ? 0 : 1);
195 if (!is_leadbyte(ch
))
197 psenc
->ch
[psenc
->chlen
++] = ch
;
200 ch
= psenc
->ch
[0] & 0xFF;
201 if (!is_leadbyte(ch
))
205 ch
= psenc
->ch
[0] & 0xFF;
207 ch
= psenc
->ch
[1] & 0xFF;
209 wc
|= (wchar_t)HANYUBIT
;
218 switch (psenc
->chlen
) {
226 psenc
->ch
[psenc
->chlen
++] = ch
;
227 wc
|= (wchar_t)HANYUBIT
;
231 if (!is_leadbyte(ch
))
233 psenc
->ch
[psenc
->chlen
++] = ch
;
240 if (!is_leadbyte(ch
))
242 psenc
->ch
[psenc
->chlen
++] = ch
;
245 ch
= psenc
->ch
[2] & 0xFF;
246 if (!is_leadbyte(ch
))
251 wc
|= (wchar_t)(ch
<< 8);
253 if (!is_trailbyte(ch
))
258 *nresult
= (size_t)(s0
- *s
);
265 *nresult
= (size_t)-2;
270 *nresult
= (size_t)-1;
276 _citrus_DECHanyu_wcrtomb_priv(_DECHanyuEncodingInfo
* __restrict ei __unused
,
277 char * __restrict s
, size_t n
, wchar_t wc
,
278 _DECHanyuState
* __restrict psenc
, size_t * __restrict nresult
)
282 if (psenc
->chlen
!= 0)
285 /* XXX: assume wchar_t as int */
286 if ((uint32_t)wc
<= 0x7F) {
289 if ((uint32_t)wc
> 0xFFFF) {
290 if ((wc
& ~0xFFFF) != (wchar_t)HANYUBIT
)
292 psenc
->ch
[psenc
->chlen
++] = (wc
>> 24) & 0xFF;
293 psenc
->ch
[psenc
->chlen
++] = (wc
>> 16) & 0xFF;
296 ch
= (wc
>> 8) & 0xFF;
297 if (!is_leadbyte(ch
))
299 psenc
->ch
[psenc
->chlen
++] = ch
;
301 if (!is_trailbyte(ch
))
304 psenc
->ch
[psenc
->chlen
++] = ch
;
305 if (n
< psenc
->chlen
) {
306 *nresult
= (size_t)-1;
309 memcpy(s
, psenc
->ch
, psenc
->chlen
);
310 *nresult
= psenc
->chlen
;
316 *nresult
= (size_t)-1;
322 _citrus_DECHanyu_stdenc_wctocs(_DECHanyuEncodingInfo
* __restrict ei __unused
,
323 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
330 /* XXX: assume wchar_t as int */
331 if ((uint32_t)wc
> 0x7F) {
332 if ((uint32_t)wc
> 0xFFFF) {
333 if ((wc
& ~0xFFFF) != (wchar_t)HANYUBIT
)
337 if (!is_leadbyte((wc
>> 8) & 0xFF) ||
338 !is_trailbyte(wc
& 0xFF))
340 plane
+= (wc
& 0x80) ? 1 : 2;
344 *idx
= (_index_t
)(wc
& mask
);
351 _citrus_DECHanyu_stdenc_cstowc(_DECHanyuEncodingInfo
* __restrict ei __unused
,
352 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
358 } else if (csid
<= 4) {
359 if (!is_94charset(idx
>> 8))
361 if (!is_94charset(idx
& 0xFF))
376 _citrus_DECHanyu_stdenc_get_state_desc_generic(
377 _DECHanyuEncodingInfo
* __restrict ei __unused
,
378 _DECHanyuState
* __restrict psenc
, int * __restrict rstate
)
381 *rstate
= (psenc
->chlen
== 0)
382 ? _STDENC_SDGEN_INITIAL
383 : _STDENC_SDGEN_INCOMPLETE_CHAR
;
387 /* ----------------------------------------------------------------------
388 * public interface for stdenc
391 _CITRUS_STDENC_DECLS(DECHanyu
);
392 _CITRUS_STDENC_DEF_OPS(DECHanyu
);
394 #include "citrus_stdenc_template.h"