2 /* $NetBSD: citrus_johab.c,v 1.4 2008/06/14 16:01:07 tnozaki Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2006 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
45 #include "citrus_namespace.h"
46 #include "citrus_types.h"
47 #include "citrus_bcs.h"
48 #include "citrus_module.h"
49 #include "citrus_stdenc.h"
50 #include "citrus_johab.h"
52 /* ----------------------------------------------------------------------
53 * private stuffs used by templates
65 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
66 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
68 #define _FUNCNAME(m) _citrus_JOHAB_##m
69 #define _ENCODING_INFO _JOHABEncodingInfo
70 #define _ENCODING_STATE _JOHABState
71 #define _ENCODING_MB_CUR_MAX(_ei_) 2
72 #define _ENCODING_IS_STATE_DEPENDENT 0
73 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
78 _citrus_JOHAB_init_state(_JOHABEncodingInfo * __restrict ei __unused,
79 _JOHABState * __restrict psenc)
88 _citrus_JOHAB_pack_state(_JOHABEncodingInfo * __restrict ei __unused,
89 void * __restrict pspriv, const _JOHABState * __restrict psenc)
92 memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
97 _citrus_JOHAB_unpack_state(_JOHABEncodingInfo * __restrict ei __unused,
98 _JOHABState * __restrict psenc, const void * __restrict pspriv)
101 memcpy((void *)psenc, pspriv, sizeof(*psenc));
107 _citrus_JOHAB_encoding_module_uninit(_JOHABEncodingInfo *ei __unused)
115 _citrus_JOHAB_encoding_module_init(_JOHABEncodingInfo * __restrict ei __unused,
116 const void * __restrict var __unused, size_t lenvar __unused)
124 ishangul(int l, int t)
127 return ((l >= 0x84 && l <= 0xD3) &&
128 ((t >= 0x41 && t <= 0x7E) || (t >= 0x81 && t <= 0xFE)));
135 return ((l == 0xD8) &&
136 ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)));
140 ishanja(int l, int t)
143 return (((l >= 0xD9 && l <= 0xDE) || (l >= 0xE0 && l <= 0xF9)) &&
144 ((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)));
149 _citrus_JOHAB_mbrtowc_priv(_JOHABEncodingInfo * __restrict ei,
150 wchar_t * __restrict pwc, char ** __restrict s, size_t n,
151 _JOHABState * __restrict psenc, size_t * __restrict nresult)
157 _citrus_JOHAB_init_state(ei, psenc);
158 *nresult = _ENCODING_IS_STATE_DEPENDENT;
163 switch (psenc->chlen) {
171 *nresult = (l == 0) ? 0 : 1;
175 psenc->ch[psenc->chlen++] = l;
178 l = psenc->ch[0] & 0xFF;
185 *nresult = (size_t)-2;
190 if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) {
191 *nresult = (size_t)-1;
195 *pwc = (wchar_t)(l << 8 | t);
205 _citrus_JOHAB_wcrtomb_priv(_JOHABEncodingInfo * __restrict ei __unused,
206 char * __restrict s, size_t n, wchar_t wc,
207 _JOHABState * __restrict psenc, size_t * __restrict nresult)
211 if (psenc->chlen != 0)
214 /* XXX assume wchar_t as int */
215 if ((uint32_t)wc <= 0x7F) {
220 } else if ((uint32_t)wc <= 0xFFFF) {
223 *nresult = (size_t)-1;
226 l = (wc >> 8) & 0xFF;
228 if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t))
235 *nresult = (size_t)-1;
244 _citrus_JOHAB_stdenc_wctocs(_JOHABEncodingInfo * __restrict ei __unused,
245 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
249 /* XXX assume wchar_t as int */
250 if ((uint32_t)wc <= 0x7F) {
253 } else if ((uint32_t)wc <= 0xFFFF) {
254 l = (wc >> 8) & 0xFF;
256 if (ishangul(l, t) || isuda(l, t)) {
260 if (l >= 0xD9 && l <= 0xDE) {
263 } else if (l >= 0xE0 && l <= 0xF9) {
269 if (t >= 0x31 && t <= 0x7E)
271 else if (t >= 0x91 && t <= 0xFE)
275 l = (linear / 94) + m;
276 t = (linear % 94) + 0x21;
277 *idx = (_index_t)((l << 8) | t);
287 _citrus_JOHAB_stdenc_cstowc(_JOHABEncodingInfo * __restrict ei __unused,
288 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
290 int m, n, l, linear, t;
298 if (idx >= 0x2121 && idx <= 0x2C71) {
301 } else if (idx >= 0x4A21 && idx <= 0x7D7E) {
306 l = ((idx >> 8) & 0xFF) - n;
307 t = (idx & 0xFF) - 0x21;
308 linear = (l * 94) + t;
309 l = (linear / 188) + m;
311 t += (t <= 0x4D) ? 0x31 : 0x43;
321 _citrus_JOHAB_stdenc_get_state_desc_generic(_JOHABEncodingInfo * __restrict ei __unused,
322 _JOHABState * __restrict psenc, int * __restrict rstate)
325 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
326 _STDENC_SDGEN_INCOMPLETE_CHAR;
330 /* ----------------------------------------------------------------------
331 * public interface for stdenc
334 _CITRUS_STDENC_DECLS(JOHAB);
335 _CITRUS_STDENC_DEF_OPS(JOHAB);
337 #include "citrus_stdenc_template.h"