1 /* $NetBSD: citrus_johab.c,v 1.4 2008/06/14 16:01:07 tnozaki Exp $ */
4 * Copyright (c)2006 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 #if defined(LIBC_SCCS) && !defined(lint)
30 __RCSID("$NetBSD: citrus_johab.c,v 1.4 2008/06/14 16:01:07 tnozaki Exp $");
31 #endif /* LIBC_SCCS and not lint */
33 #include <sys/types.h>
44 #include "citrus_namespace.h"
45 #include "citrus_types.h"
46 #include "citrus_bcs.h"
47 #include "citrus_module.h"
48 #include "citrus_ctype.h"
49 #include "citrus_stdenc.h"
50 #include "citrus_johab.h"
52 /* ----------------------------------------------------------------------
53 * private stuffs used by templates
66 _JOHABEncodingInfo ei
;
68 /* for future multi-locale facility */
71 _JOHABState s_mbrtowc
;
73 _JOHABState s_mbsrtowcs
;
74 _JOHABState s_wcrtomb
;
75 _JOHABState s_wcsrtombs
;
80 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
81 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
83 #define _FUNCNAME(m) _citrus_JOHAB_##m
84 #define _ENCODING_INFO _JOHABEncodingInfo
85 #define _CTYPE_INFO _JOHABCTypeInfo
86 #define _ENCODING_STATE _JOHABState
87 #define _ENCODING_MB_CUR_MAX(_ei_) 2
88 #define _ENCODING_IS_STATE_DEPENDENT 0
89 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
94 _citrus_JOHAB_init_state(_JOHABEncodingInfo
* __restrict ei
,
95 _JOHABState
* __restrict psenc
)
98 _DIAGASSERT(psenc
!= NULL
);
105 _citrus_JOHAB_pack_state(_JOHABEncodingInfo
* __restrict ei
,
106 void * __restrict pspriv
,
107 const _JOHABState
* __restrict psenc
)
110 _DIAGASSERT(pspriv
!= NULL
);
111 _DIAGASSERT(psenc
!= NULL
);
113 memcpy(pspriv
, (const void *)psenc
, sizeof(*psenc
));
118 _citrus_JOHAB_unpack_state(_JOHABEncodingInfo
* __restrict ei
,
119 _JOHABState
* __restrict psenc
,
120 const void * __restrict pspriv
)
123 _DIAGASSERT(psenc
!= NULL
);
124 _DIAGASSERT(pspriv
!= NULL
);
126 memcpy((void *)psenc
, pspriv
, sizeof(*psenc
));
131 _citrus_JOHAB_encoding_module_uninit(_JOHABEncodingInfo
*ei
)
138 _citrus_JOHAB_encoding_module_init(_JOHABEncodingInfo
* __restrict ei
,
139 const void * __restrict var
, size_t lenvar
)
146 ishangul(int l
, int t
)
149 return (l
>= 0x84 && l
<= 0xD3) &&
150 ((t
>= 0x41 && t
<= 0x7E) || (t
>= 0x81 && t
<= 0xFE));
156 return (l
== 0xD8) &&
157 ((t
>= 0x31 && t
<= 0x7E) || (t
>= 0x91 && t
<= 0xFE));
161 ishanja(int l
, int t
)
163 return ((l
>= 0xD9 && l
<= 0xDE) || (l
>= 0xE0 && l
<= 0xF9)) &&
164 ((t
>= 0x31 && t
<= 0x7E) || (t
>= 0x91 && t
<= 0xFE));
169 _citrus_JOHAB_mbrtowc_priv(_JOHABEncodingInfo
* __restrict ei
,
170 wchar_t * __restrict pwc
, const char ** __restrict s
, size_t n
,
171 _JOHABState
* __restrict psenc
, size_t * __restrict nresult
)
176 /* ei may be unused */
177 _DIAGASSERT(s
!= NULL
);
178 _DIAGASSERT(psenc
!= NULL
);
179 _DIAGASSERT(nresult
!= 0);
182 _citrus_JOHAB_init_state(ei
, psenc
);
183 *nresult
= _ENCODING_IS_STATE_DEPENDENT
;
188 switch (psenc
->chlen
) {
196 *nresult
= (l
== 0) ? 0 : 1;
200 psenc
->ch
[psenc
->chlen
++] = l
;
203 l
= psenc
->ch
[0] & 0xFF;
210 *nresult
= (size_t)-2;
215 if (!ishangul(l
, t
) && !isuda(l
, t
) && !ishanja(l
, t
)) {
216 *nresult
= (size_t)-1;
220 *pwc
= (wchar_t)(l
<< 8 | t
);
230 _citrus_JOHAB_wcrtomb_priv(_JOHABEncodingInfo
* __restrict ei
,
231 char * __restrict s
, size_t n
, wchar_t wc
,
232 _JOHABState
* __restrict psenc
, size_t * __restrict nresult
)
236 /* ei may be unused */
237 _DIAGASSERT(s
!= NULL
);
238 _DIAGASSERT(psenc
!= NULL
);
239 _DIAGASSERT(nresult
!= NULL
);
241 if (psenc
->chlen
!= 0)
244 /* XXX assume wchar_t as int */
245 if ((uint32_t)wc
<= 0x7F) {
250 } else if ((uint32_t)wc
<= 0xFFFF) {
253 *nresult
= (size_t)-1;
256 l
= (wc
>> 8) & 0xFF;
258 if (!ishangul(l
, t
) && !isuda(l
, t
) && !ishanja(l
, t
))
265 *nresult
= (size_t)-1;
274 _citrus_JOHAB_stdenc_wctocs(_JOHABEncodingInfo
* __restrict ei
,
275 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
279 /* ei may be unused */
280 _DIAGASSERT(csid
!= NULL
);
281 _DIAGASSERT(idx
!= NULL
);
283 /* XXX assume wchar_t as int */
284 if ((uint32_t)wc
<= 0x7F) {
287 } else if ((uint32_t)wc
<= 0xFFFF) {
288 l
= (wc
>> 8) & 0xFF;
290 if (ishangul(l
, t
) || isuda(l
, t
)) {
294 if (l
>= 0xD9 && l
<= 0xDE) {
297 } else if (l
>= 0xE0 && l
<= 0xF9) {
304 if (t
>= 0x31 && t
<= 0x7E) {
306 } else if (t
>= 0x91 && t
<= 0xFE) {
311 l
= (linear
/ 94) + m
;
312 t
= (linear
% 94) + 0x21;
313 *idx
= (_index_t
)((l
<< 8) | t
);
324 _citrus_JOHAB_stdenc_cstowc(_JOHABEncodingInfo
* __restrict ei
,
325 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
327 int m
, n
, l
, t
, linear
;
329 /* ei may be unused */
330 _DIAGASSERT(wc
!= NULL
);
338 if (idx
>= 0x2121 && idx
<= 0x2C71) {
341 } else if (idx
>= 0x4A21 && idx
<= 0x7D7E) {
347 l
= ((idx
>> 8) & 0xFF) - n
;
348 t
= (idx
& 0xFF) - 0x21;
349 linear
= (l
* 94) + t
;
350 l
= (linear
/ 188) + m
;
352 t
+= (t
<= 0x4D) ? 0x31 : 0x43;
362 _citrus_JOHAB_stdenc_get_state_desc_generic(_JOHABEncodingInfo
* __restrict ei
,
363 _JOHABState
* __restrict psenc
, int * __restrict rstate
)
365 /* ei may be unused */
366 _DIAGASSERT(psenc
!= NULL
);
367 _DIAGASSERT(rstate
!= NULL
);
369 *rstate
= (psenc
->chlen
== 0)
370 ? _STDENC_SDGEN_INITIAL
371 : _STDENC_SDGEN_INCOMPLETE_CHAR
;
375 /* ----------------------------------------------------------------------
376 * public interface for ctype
379 _CITRUS_CTYPE_DECLS(JOHAB
);
380 _CITRUS_CTYPE_DEF_OPS(JOHAB
);
382 #include "citrus_ctype_template.h"
385 /* ----------------------------------------------------------------------
386 * public interface for stdenc
389 _CITRUS_STDENC_DECLS(JOHAB
);
390 _CITRUS_STDENC_DEF_OPS(JOHAB
);
392 #include "citrus_stdenc_template.h"