1ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_mskanji.c,v 1.13 2008/06/14 16:01:08 tnozaki Exp $ */
2ad30f8e7SGabor Kovesdan
3ad30f8e7SGabor Kovesdan /*-
4*5e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause
5*5e53a4f9SPedro F. Giffuni *
6ad30f8e7SGabor Kovesdan * Copyright (c)2002 Citrus Project,
7ad30f8e7SGabor Kovesdan * All rights reserved.
8ad30f8e7SGabor Kovesdan *
9ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan * are met:
12ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan *
18ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan */
30ad30f8e7SGabor Kovesdan
31ad30f8e7SGabor Kovesdan /*
32ad30f8e7SGabor Kovesdan * ja_JP.SJIS locale table for BSD4.4/rune
33ad30f8e7SGabor Kovesdan * version 1.0
34ad30f8e7SGabor Kovesdan * (C) Sin'ichiro MIYATANI / Phase One, Inc
35ad30f8e7SGabor Kovesdan * May 12, 1995
36ad30f8e7SGabor Kovesdan *
37ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
38ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions
39ad30f8e7SGabor Kovesdan * are met:
40ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
41ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer.
42ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
43ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
44ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution.
45ad30f8e7SGabor Kovesdan * 3. All advertising materials mentioning features or use of this software
46ad30f8e7SGabor Kovesdan * must display the following acknowledgement:
47ad30f8e7SGabor Kovesdan * This product includes software developed by Phase One, Inc.
48ad30f8e7SGabor Kovesdan * 4. The name of Phase One, Inc. may be used to endorse or promote products
49ad30f8e7SGabor Kovesdan * derived from this software without specific prior written permission.
50ad30f8e7SGabor Kovesdan *
51ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61ad30f8e7SGabor Kovesdan * SUCH DAMAGE.
62ad30f8e7SGabor Kovesdan */
63ad30f8e7SGabor Kovesdan
64ad30f8e7SGabor Kovesdan #include <sys/types.h>
65ad30f8e7SGabor Kovesdan
66ad30f8e7SGabor Kovesdan #include <assert.h>
67ad30f8e7SGabor Kovesdan #include <errno.h>
68ad30f8e7SGabor Kovesdan #include <limits.h>
69ad30f8e7SGabor Kovesdan #include <stdbool.h>
70ad30f8e7SGabor Kovesdan #include <stddef.h>
71ad30f8e7SGabor Kovesdan #include <stdio.h>
72ad30f8e7SGabor Kovesdan #include <stdlib.h>
73ad30f8e7SGabor Kovesdan #include <string.h>
74ad30f8e7SGabor Kovesdan #include <wchar.h>
75ad30f8e7SGabor Kovesdan
76ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
77ad30f8e7SGabor Kovesdan #include "citrus_types.h"
78ad30f8e7SGabor Kovesdan #include "citrus_bcs.h"
79ad30f8e7SGabor Kovesdan #include "citrus_module.h"
80ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
81ad30f8e7SGabor Kovesdan #include "citrus_mskanji.h"
82ad30f8e7SGabor Kovesdan
83ad30f8e7SGabor Kovesdan
84ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
85ad30f8e7SGabor Kovesdan * private stuffs used by templates
86ad30f8e7SGabor Kovesdan */
87ad30f8e7SGabor Kovesdan
88ad30f8e7SGabor Kovesdan typedef struct _MSKanjiState {
89ad30f8e7SGabor Kovesdan int chlen;
90ad30f8e7SGabor Kovesdan char ch[2];
91ad30f8e7SGabor Kovesdan } _MSKanjiState;
92ad30f8e7SGabor Kovesdan
93ad30f8e7SGabor Kovesdan typedef struct {
94ad30f8e7SGabor Kovesdan int mode;
95ad30f8e7SGabor Kovesdan #define MODE_JIS2004 1
96ad30f8e7SGabor Kovesdan } _MSKanjiEncodingInfo;
97ad30f8e7SGabor Kovesdan
98ad30f8e7SGabor Kovesdan #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
99ad30f8e7SGabor Kovesdan #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
100ad30f8e7SGabor Kovesdan
101ad30f8e7SGabor Kovesdan #define _FUNCNAME(m) _citrus_MSKanji_##m
102ad30f8e7SGabor Kovesdan #define _ENCODING_INFO _MSKanjiEncodingInfo
103ad30f8e7SGabor Kovesdan #define _ENCODING_STATE _MSKanjiState
104ad30f8e7SGabor Kovesdan #define _ENCODING_MB_CUR_MAX(_ei_) 2
105ad30f8e7SGabor Kovesdan #define _ENCODING_IS_STATE_DEPENDENT 0
106ad30f8e7SGabor Kovesdan #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
107ad30f8e7SGabor Kovesdan
108ad30f8e7SGabor Kovesdan
109ad30f8e7SGabor Kovesdan static bool
_mskanji1(int c)110ad30f8e7SGabor Kovesdan _mskanji1(int c)
111ad30f8e7SGabor Kovesdan {
112ad30f8e7SGabor Kovesdan
113ad30f8e7SGabor Kovesdan return ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc));
114ad30f8e7SGabor Kovesdan }
115ad30f8e7SGabor Kovesdan
116ad30f8e7SGabor Kovesdan static bool
_mskanji2(int c)117ad30f8e7SGabor Kovesdan _mskanji2(int c)
118ad30f8e7SGabor Kovesdan {
119ad30f8e7SGabor Kovesdan
120ad30f8e7SGabor Kovesdan return ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc));
121ad30f8e7SGabor Kovesdan }
122ad30f8e7SGabor Kovesdan
123ad30f8e7SGabor Kovesdan static __inline void
124ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei __unused,_MSKanjiState * __restrict s)125ad30f8e7SGabor Kovesdan _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei __unused,
126ad30f8e7SGabor Kovesdan _MSKanjiState * __restrict s)
127ad30f8e7SGabor Kovesdan {
128ad30f8e7SGabor Kovesdan
129ad30f8e7SGabor Kovesdan s->chlen = 0;
130ad30f8e7SGabor Kovesdan }
131ad30f8e7SGabor Kovesdan
132b61949ddSDimitry Andric #if 0
133ad30f8e7SGabor Kovesdan static __inline void
134ad30f8e7SGabor Kovesdan /*ARGSUSED*/
135ad30f8e7SGabor Kovesdan _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei __unused,
136ad30f8e7SGabor Kovesdan void * __restrict pspriv, const _MSKanjiState * __restrict s)
137ad30f8e7SGabor Kovesdan {
138ad30f8e7SGabor Kovesdan
139ad30f8e7SGabor Kovesdan memcpy(pspriv, (const void *)s, sizeof(*s));
140ad30f8e7SGabor Kovesdan }
141ad30f8e7SGabor Kovesdan
142ad30f8e7SGabor Kovesdan static __inline void
143ad30f8e7SGabor Kovesdan /*ARGSUSED*/
144ad30f8e7SGabor Kovesdan _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei __unused,
145ad30f8e7SGabor Kovesdan _MSKanjiState * __restrict s, const void * __restrict pspriv)
146ad30f8e7SGabor Kovesdan {
147ad30f8e7SGabor Kovesdan
148ad30f8e7SGabor Kovesdan memcpy((void *)s, pspriv, sizeof(*s));
149ad30f8e7SGabor Kovesdan }
150b61949ddSDimitry Andric #endif
151ad30f8e7SGabor Kovesdan
152ad30f8e7SGabor Kovesdan static int
153ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,wchar_t * __restrict pwc,char ** __restrict s,size_t n,_MSKanjiState * __restrict psenc,size_t * __restrict nresult)154ad30f8e7SGabor Kovesdan _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
1551243a98eSTijl Coosemans wchar_t * __restrict pwc, char ** __restrict s, size_t n,
156ad30f8e7SGabor Kovesdan _MSKanjiState * __restrict psenc, size_t * __restrict nresult)
157ad30f8e7SGabor Kovesdan {
1581243a98eSTijl Coosemans char *s0;
159ad30f8e7SGabor Kovesdan wchar_t wchar;
160ad30f8e7SGabor Kovesdan int chlenbak, len;
161ad30f8e7SGabor Kovesdan
162ad30f8e7SGabor Kovesdan s0 = *s;
163ad30f8e7SGabor Kovesdan
164ad30f8e7SGabor Kovesdan if (s0 == NULL) {
165ad30f8e7SGabor Kovesdan _citrus_MSKanji_init_state(ei, psenc);
166ad30f8e7SGabor Kovesdan *nresult = 0; /* state independent */
167ad30f8e7SGabor Kovesdan return (0);
168ad30f8e7SGabor Kovesdan }
169ad30f8e7SGabor Kovesdan
170ad30f8e7SGabor Kovesdan chlenbak = psenc->chlen;
171ad30f8e7SGabor Kovesdan
172ad30f8e7SGabor Kovesdan /* make sure we have the first byte in the buffer */
173ad30f8e7SGabor Kovesdan switch (psenc->chlen) {
174ad30f8e7SGabor Kovesdan case 0:
175ad30f8e7SGabor Kovesdan if (n < 1)
176ad30f8e7SGabor Kovesdan goto restart;
177ad30f8e7SGabor Kovesdan psenc->ch[0] = *s0++;
178ad30f8e7SGabor Kovesdan psenc->chlen = 1;
179ad30f8e7SGabor Kovesdan n--;
180ad30f8e7SGabor Kovesdan break;
181ad30f8e7SGabor Kovesdan case 1:
182ad30f8e7SGabor Kovesdan break;
183ad30f8e7SGabor Kovesdan default:
184ad30f8e7SGabor Kovesdan /* illegal state */
185ad30f8e7SGabor Kovesdan goto encoding_error;
186ad30f8e7SGabor Kovesdan }
187ad30f8e7SGabor Kovesdan
188ad30f8e7SGabor Kovesdan len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
189ad30f8e7SGabor Kovesdan while (psenc->chlen < len) {
190ad30f8e7SGabor Kovesdan if (n < 1)
191ad30f8e7SGabor Kovesdan goto restart;
192ad30f8e7SGabor Kovesdan psenc->ch[psenc->chlen] = *s0++;
193ad30f8e7SGabor Kovesdan psenc->chlen++;
194ad30f8e7SGabor Kovesdan n--;
195ad30f8e7SGabor Kovesdan }
196ad30f8e7SGabor Kovesdan
197ad30f8e7SGabor Kovesdan *s = s0;
198ad30f8e7SGabor Kovesdan
199ad30f8e7SGabor Kovesdan switch (len) {
200ad30f8e7SGabor Kovesdan case 1:
201ad30f8e7SGabor Kovesdan wchar = psenc->ch[0] & 0xff;
202ad30f8e7SGabor Kovesdan break;
203ad30f8e7SGabor Kovesdan case 2:
204ad30f8e7SGabor Kovesdan if (!_mskanji2(psenc->ch[1] & 0xff))
205ad30f8e7SGabor Kovesdan goto encoding_error;
206ad30f8e7SGabor Kovesdan wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
207ad30f8e7SGabor Kovesdan break;
208ad30f8e7SGabor Kovesdan default:
209ad30f8e7SGabor Kovesdan /* illegal state */
210ad30f8e7SGabor Kovesdan goto encoding_error;
211ad30f8e7SGabor Kovesdan }
212ad30f8e7SGabor Kovesdan
213ad30f8e7SGabor Kovesdan psenc->chlen = 0;
214ad30f8e7SGabor Kovesdan
215ad30f8e7SGabor Kovesdan if (pwc)
216ad30f8e7SGabor Kovesdan *pwc = wchar;
217ad30f8e7SGabor Kovesdan *nresult = wchar ? len - chlenbak : 0;
218ad30f8e7SGabor Kovesdan return (0);
219ad30f8e7SGabor Kovesdan
220ad30f8e7SGabor Kovesdan encoding_error:
221ad30f8e7SGabor Kovesdan psenc->chlen = 0;
222ad30f8e7SGabor Kovesdan *nresult = (size_t)-1;
223ad30f8e7SGabor Kovesdan return (EILSEQ);
224ad30f8e7SGabor Kovesdan
225ad30f8e7SGabor Kovesdan restart:
226ad30f8e7SGabor Kovesdan *nresult = (size_t)-2;
227ad30f8e7SGabor Kovesdan *s = s0;
228ad30f8e7SGabor Kovesdan return (0);
229ad30f8e7SGabor Kovesdan }
230ad30f8e7SGabor Kovesdan
231ad30f8e7SGabor Kovesdan
232ad30f8e7SGabor Kovesdan static int
_citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei __unused,char * __restrict s,size_t n,wchar_t wc,_MSKanjiState * __restrict psenc __unused,size_t * __restrict nresult)233ad30f8e7SGabor Kovesdan _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei __unused,
234ad30f8e7SGabor Kovesdan char * __restrict s, size_t n, wchar_t wc,
235ad30f8e7SGabor Kovesdan _MSKanjiState * __restrict psenc __unused, size_t * __restrict nresult)
236ad30f8e7SGabor Kovesdan {
237ad30f8e7SGabor Kovesdan int ret;
238ad30f8e7SGabor Kovesdan
239ad30f8e7SGabor Kovesdan /* check invalid sequence */
240ad30f8e7SGabor Kovesdan if (wc & ~0xffff) {
241ad30f8e7SGabor Kovesdan ret = EILSEQ;
242ad30f8e7SGabor Kovesdan goto err;
243ad30f8e7SGabor Kovesdan }
244ad30f8e7SGabor Kovesdan
245ad30f8e7SGabor Kovesdan if (wc & 0xff00) {
246ad30f8e7SGabor Kovesdan if (n < 2) {
247ad30f8e7SGabor Kovesdan ret = E2BIG;
248ad30f8e7SGabor Kovesdan goto err;
249ad30f8e7SGabor Kovesdan }
250ad30f8e7SGabor Kovesdan
251ad30f8e7SGabor Kovesdan s[0] = (wc >> 8) & 0xff;
252ad30f8e7SGabor Kovesdan s[1] = wc & 0xff;
253ad30f8e7SGabor Kovesdan if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) {
254ad30f8e7SGabor Kovesdan ret = EILSEQ;
255ad30f8e7SGabor Kovesdan goto err;
256ad30f8e7SGabor Kovesdan }
257ad30f8e7SGabor Kovesdan
258ad30f8e7SGabor Kovesdan *nresult = 2;
259ad30f8e7SGabor Kovesdan return (0);
260ad30f8e7SGabor Kovesdan } else {
261ad30f8e7SGabor Kovesdan if (n < 1) {
262ad30f8e7SGabor Kovesdan ret = E2BIG;
263ad30f8e7SGabor Kovesdan goto err;
264ad30f8e7SGabor Kovesdan }
265ad30f8e7SGabor Kovesdan
266ad30f8e7SGabor Kovesdan s[0] = wc & 0xff;
267ad30f8e7SGabor Kovesdan if (_mskanji1(s[0] & 0xff)) {
268ad30f8e7SGabor Kovesdan ret = EILSEQ;
269ad30f8e7SGabor Kovesdan goto err;
270ad30f8e7SGabor Kovesdan }
271ad30f8e7SGabor Kovesdan
272ad30f8e7SGabor Kovesdan *nresult = 1;
273ad30f8e7SGabor Kovesdan return (0);
274ad30f8e7SGabor Kovesdan }
275ad30f8e7SGabor Kovesdan
276ad30f8e7SGabor Kovesdan err:
277ad30f8e7SGabor Kovesdan *nresult = (size_t)-1;
278ad30f8e7SGabor Kovesdan return (ret);
279ad30f8e7SGabor Kovesdan }
280ad30f8e7SGabor Kovesdan
281ad30f8e7SGabor Kovesdan
282ad30f8e7SGabor Kovesdan static __inline int
283ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei,_csid_t * __restrict csid,_index_t * __restrict idx,wchar_t wc)284ad30f8e7SGabor Kovesdan _citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei,
285ad30f8e7SGabor Kovesdan _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
286ad30f8e7SGabor Kovesdan {
287ad30f8e7SGabor Kovesdan _index_t col, row;
288ad30f8e7SGabor Kovesdan int offset;
289ad30f8e7SGabor Kovesdan
290ad30f8e7SGabor Kovesdan if ((_wc_t)wc < 0x80) {
291ad30f8e7SGabor Kovesdan /* ISO-646 */
292ad30f8e7SGabor Kovesdan *csid = 0;
293ad30f8e7SGabor Kovesdan *idx = (_index_t)wc;
294ad30f8e7SGabor Kovesdan } else if ((_wc_t)wc < 0x100) {
295ad30f8e7SGabor Kovesdan /* KANA */
296ad30f8e7SGabor Kovesdan *csid = 1;
297ad30f8e7SGabor Kovesdan *idx = (_index_t)wc & 0x7F;
298ad30f8e7SGabor Kovesdan } else {
299ad30f8e7SGabor Kovesdan /* Kanji (containing Gaiji zone) */
300ad30f8e7SGabor Kovesdan /*
301ad30f8e7SGabor Kovesdan * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)):
302ad30f8e7SGabor Kovesdan * 0x8140 - 0x817E -> 0x2121 - 0x215F
303ad30f8e7SGabor Kovesdan * 0x8180 - 0x819E -> 0x2160 - 0x217E
304ad30f8e7SGabor Kovesdan * 0x819F - 0x81FC -> 0x2221 - 0x227E
305ad30f8e7SGabor Kovesdan *
306ad30f8e7SGabor Kovesdan * 0x8240 - 0x827E -> 0x2321 - 0x235F
307ad30f8e7SGabor Kovesdan * ...
308ad30f8e7SGabor Kovesdan * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E
309ad30f8e7SGabor Kovesdan *
310ad30f8e7SGabor Kovesdan * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F
311ad30f8e7SGabor Kovesdan * ...
312ad30f8e7SGabor Kovesdan * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E
313ad30f8e7SGabor Kovesdan *
314ad30f8e7SGabor Kovesdan * extended Gaiji zone:
315ad30f8e7SGabor Kovesdan * 0xF040 - 0xFCFC
316ad30f8e7SGabor Kovesdan *
317ad30f8e7SGabor Kovesdan * JIS X0213-plane2:
318ad30f8e7SGabor Kovesdan * 0xF040 - 0xF09E -> 0x2121 - 0x217E
319ad30f8e7SGabor Kovesdan * 0xF140 - 0xF19E -> 0x2321 - 0x237E
320ad30f8e7SGabor Kovesdan * ...
321ad30f8e7SGabor Kovesdan * 0xF240 - 0xF29E -> 0x2521 - 0x257E
322ad30f8e7SGabor Kovesdan *
323ad30f8e7SGabor Kovesdan * 0xF09F - 0xF0FC -> 0x2821 - 0x287E
324ad30f8e7SGabor Kovesdan * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E
325ad30f8e7SGabor Kovesdan * ...
326ad30f8e7SGabor Kovesdan * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E
327ad30f8e7SGabor Kovesdan *
328ad30f8e7SGabor Kovesdan * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E
329ad30f8e7SGabor Kovesdan * ...
330ad30f8e7SGabor Kovesdan * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E
331ad30f8e7SGabor Kovesdan */
332ad30f8e7SGabor Kovesdan row = ((_wc_t)wc >> 8) & 0xFF;
333ad30f8e7SGabor Kovesdan col = (_wc_t)wc & 0xFF;
334ad30f8e7SGabor Kovesdan if (!_mskanji1(row) || !_mskanji2(col))
335ad30f8e7SGabor Kovesdan return (EILSEQ);
336ad30f8e7SGabor Kovesdan if ((ei->mode & MODE_JIS2004) == 0 || row < 0xF0) {
337ad30f8e7SGabor Kovesdan *csid = 2;
338ad30f8e7SGabor Kovesdan offset = 0x81;
339ad30f8e7SGabor Kovesdan } else {
340ad30f8e7SGabor Kovesdan *csid = 3;
341ad30f8e7SGabor Kovesdan if ((_wc_t)wc <= 0xF49E) {
342ad30f8e7SGabor Kovesdan offset = (_wc_t)wc >= 0xF29F ||
343ad30f8e7SGabor Kovesdan ((_wc_t)wc >= 0xF09F &&
344ad30f8e7SGabor Kovesdan (_wc_t)wc <= 0xF0FC) ? 0xED : 0xF0;
345ad30f8e7SGabor Kovesdan } else
346ad30f8e7SGabor Kovesdan offset = 0xCE;
347ad30f8e7SGabor Kovesdan }
348ad30f8e7SGabor Kovesdan row -= offset;
349ad30f8e7SGabor Kovesdan if (row >= 0x5F)
350ad30f8e7SGabor Kovesdan row -= 0x40;
351ad30f8e7SGabor Kovesdan row = row * 2 + 0x21;
352ad30f8e7SGabor Kovesdan col -= 0x1F;
353ad30f8e7SGabor Kovesdan if (col >= 0x61)
354ad30f8e7SGabor Kovesdan col -= 1;
355ad30f8e7SGabor Kovesdan if (col > 0x7E) {
356ad30f8e7SGabor Kovesdan row += 1;
357ad30f8e7SGabor Kovesdan col -= 0x5E;
358ad30f8e7SGabor Kovesdan }
359ad30f8e7SGabor Kovesdan *idx = ((_index_t)row << 8) | col;
360ad30f8e7SGabor Kovesdan }
361ad30f8e7SGabor Kovesdan
362ad30f8e7SGabor Kovesdan return (0);
363ad30f8e7SGabor Kovesdan }
364ad30f8e7SGabor Kovesdan
365ad30f8e7SGabor Kovesdan static __inline int
366ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei,wchar_t * __restrict wc,_csid_t csid,_index_t idx)367ad30f8e7SGabor Kovesdan _citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei,
368ad30f8e7SGabor Kovesdan wchar_t * __restrict wc, _csid_t csid, _index_t idx)
369ad30f8e7SGabor Kovesdan {
370ad30f8e7SGabor Kovesdan uint32_t col, row;
371ad30f8e7SGabor Kovesdan int offset;
372ad30f8e7SGabor Kovesdan
373ad30f8e7SGabor Kovesdan switch (csid) {
374ad30f8e7SGabor Kovesdan case 0:
375ad30f8e7SGabor Kovesdan /* ISO-646 */
376ad30f8e7SGabor Kovesdan if (idx >= 0x80)
377ad30f8e7SGabor Kovesdan return (EILSEQ);
378ad30f8e7SGabor Kovesdan *wc = (wchar_t)idx;
379ad30f8e7SGabor Kovesdan break;
380ad30f8e7SGabor Kovesdan case 1:
381ad30f8e7SGabor Kovesdan /* kana */
382ad30f8e7SGabor Kovesdan if (idx >= 0x80)
383ad30f8e7SGabor Kovesdan return (EILSEQ);
384ad30f8e7SGabor Kovesdan *wc = (wchar_t)idx + 0x80;
385ad30f8e7SGabor Kovesdan break;
386ad30f8e7SGabor Kovesdan case 3:
387ad30f8e7SGabor Kovesdan if ((ei->mode & MODE_JIS2004) == 0)
388ad30f8e7SGabor Kovesdan return (EILSEQ);
389ad30f8e7SGabor Kovesdan /*FALLTHROUGH*/
390ad30f8e7SGabor Kovesdan case 2:
391ad30f8e7SGabor Kovesdan /* kanji */
392ad30f8e7SGabor Kovesdan row = (idx >> 8);
393ad30f8e7SGabor Kovesdan if (row < 0x21)
394ad30f8e7SGabor Kovesdan return (EILSEQ);
395ad30f8e7SGabor Kovesdan if (csid == 3) {
396ad30f8e7SGabor Kovesdan if (row <= 0x2F)
397ad30f8e7SGabor Kovesdan offset = (row == 0x22 || row >= 0x26) ?
398ad30f8e7SGabor Kovesdan 0xED : 0xF0;
399ad30f8e7SGabor Kovesdan else if (row >= 0x4D && row <= 0x7E)
400ad30f8e7SGabor Kovesdan offset = 0xCE;
401ad30f8e7SGabor Kovesdan else
402ad30f8e7SGabor Kovesdan return (EILSEQ);
403ad30f8e7SGabor Kovesdan } else {
404ad30f8e7SGabor Kovesdan if (row > 0x97)
405ad30f8e7SGabor Kovesdan return (EILSEQ);
406ad30f8e7SGabor Kovesdan offset = (row < 0x5F) ? 0x81 : 0xC1;
407ad30f8e7SGabor Kovesdan }
408ad30f8e7SGabor Kovesdan col = idx & 0xFF;
409ad30f8e7SGabor Kovesdan if (col < 0x21 || col > 0x7E)
410ad30f8e7SGabor Kovesdan return (EILSEQ);
411ad30f8e7SGabor Kovesdan row -= 0x21; col -= 0x21;
412ad30f8e7SGabor Kovesdan if ((row & 1) == 0) {
413ad30f8e7SGabor Kovesdan col += 0x40;
414ad30f8e7SGabor Kovesdan if (col >= 0x7F)
415ad30f8e7SGabor Kovesdan col += 1;
416ad30f8e7SGabor Kovesdan } else
417ad30f8e7SGabor Kovesdan col += 0x9F;
418ad30f8e7SGabor Kovesdan row = row / 2 + offset;
419ad30f8e7SGabor Kovesdan *wc = ((wchar_t)row << 8) | col;
420ad30f8e7SGabor Kovesdan break;
421ad30f8e7SGabor Kovesdan default:
422ad30f8e7SGabor Kovesdan return (EILSEQ);
423ad30f8e7SGabor Kovesdan }
424ad30f8e7SGabor Kovesdan
425ad30f8e7SGabor Kovesdan return (0);
426ad30f8e7SGabor Kovesdan }
427ad30f8e7SGabor Kovesdan
428ad30f8e7SGabor Kovesdan static __inline int
429ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * __restrict ei __unused,_MSKanjiState * __restrict psenc,int * __restrict rstate)430ad30f8e7SGabor Kovesdan _citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * __restrict ei __unused,
431ad30f8e7SGabor Kovesdan _MSKanjiState * __restrict psenc, int * __restrict rstate)
432ad30f8e7SGabor Kovesdan {
433ad30f8e7SGabor Kovesdan
434ad30f8e7SGabor Kovesdan *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
435ad30f8e7SGabor Kovesdan _STDENC_SDGEN_INCOMPLETE_CHAR;
436ad30f8e7SGabor Kovesdan return (0);
437ad30f8e7SGabor Kovesdan }
438ad30f8e7SGabor Kovesdan
439ad30f8e7SGabor Kovesdan static int
440ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei,const void * __restrict var,size_t lenvar)441ad30f8e7SGabor Kovesdan _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei,
442ad30f8e7SGabor Kovesdan const void * __restrict var, size_t lenvar)
443ad30f8e7SGabor Kovesdan {
444ad30f8e7SGabor Kovesdan const char *p;
445ad30f8e7SGabor Kovesdan
446ad30f8e7SGabor Kovesdan p = var;
447ad30f8e7SGabor Kovesdan memset((void *)ei, 0, sizeof(*ei));
448ad30f8e7SGabor Kovesdan while (lenvar > 0) {
449ad30f8e7SGabor Kovesdan switch (_bcs_toupper(*p)) {
450ad30f8e7SGabor Kovesdan case 'J':
451ad30f8e7SGabor Kovesdan MATCH(JIS2004, ei->mode |= MODE_JIS2004);
452ad30f8e7SGabor Kovesdan break;
453ad30f8e7SGabor Kovesdan }
454ad30f8e7SGabor Kovesdan ++p;
455ad30f8e7SGabor Kovesdan --lenvar;
456ad30f8e7SGabor Kovesdan }
457ad30f8e7SGabor Kovesdan
458ad30f8e7SGabor Kovesdan return (0);
459ad30f8e7SGabor Kovesdan }
460ad30f8e7SGabor Kovesdan
461ad30f8e7SGabor Kovesdan static void
_citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo * ei __unused)462ad30f8e7SGabor Kovesdan _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei __unused)
463ad30f8e7SGabor Kovesdan {
464ad30f8e7SGabor Kovesdan
465ad30f8e7SGabor Kovesdan }
466ad30f8e7SGabor Kovesdan
467ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
468ad30f8e7SGabor Kovesdan * public interface for stdenc
469ad30f8e7SGabor Kovesdan */
470ad30f8e7SGabor Kovesdan
471ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(MSKanji);
472ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(MSKanji);
473ad30f8e7SGabor Kovesdan
474ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
475