1 /* $FreeBSD: head/lib/libiconv_modules/MSKanji/citrus_mskanji.c 281550 2015-04-15 09:09:20Z tijl $ */
2 /* $NetBSD: citrus_mskanji.c,v 1.13 2008/06/14 16:01:08 tnozaki Exp $ */
5 * Copyright (c)2002 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
31 * ja_JP.SJIS locale table for BSD4.4/rune
33 * (C) Sin'ichiro MIYATANI / Phase One, Inc
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by Phase One, Inc.
47 * 4. The name of Phase One, Inc. may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include <sys/cdefs.h>
65 #include <sys/types.h>
77 #include "citrus_namespace.h"
78 #include "citrus_types.h"
79 #include "citrus_bcs.h"
80 #include "citrus_module.h"
81 #include "citrus_stdenc.h"
82 #include "citrus_mskanji.h"
85 /* ----------------------------------------------------------------------
86 * private stuffs used by templates
89 typedef struct _MSKanjiState {
96 #define MODE_JIS2004 1
97 } _MSKanjiEncodingInfo;
99 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
100 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
102 #define _FUNCNAME(m) _citrus_MSKanji_##m
103 #define _ENCODING_INFO _MSKanjiEncodingInfo
104 #define _ENCODING_STATE _MSKanjiState
105 #define _ENCODING_MB_CUR_MAX(_ei_) 2
106 #define _ENCODING_IS_STATE_DEPENDENT 0
107 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
114 return ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc));
121 return ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc));
126 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei __unused,
127 _MSKanjiState * __restrict s)
136 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei __unused,
137 void * __restrict pspriv, const _MSKanjiState * __restrict s)
140 memcpy(pspriv, (const void *)s, sizeof(*s));
145 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei __unused,
146 _MSKanjiState * __restrict s, const void * __restrict pspriv)
149 memcpy((void *)s, pspriv, sizeof(*s));
155 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
156 wchar_t * __restrict pwc, char ** __restrict s, size_t n,
157 _MSKanjiState * __restrict psenc, size_t * __restrict nresult)
166 _citrus_MSKanji_init_state(ei, psenc);
167 *nresult = 0; /* state independent */
171 chlenbak = psenc->chlen;
173 /* make sure we have the first byte in the buffer */
174 switch (psenc->chlen) {
178 psenc->ch[0] = *s0++;
189 len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
190 while (psenc->chlen < len) {
193 psenc->ch[psenc->chlen] = *s0++;
202 wchar = psenc->ch[0] & 0xff;
205 if (!_mskanji2(psenc->ch[1] & 0xff))
207 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
218 *nresult = wchar ? len - chlenbak : 0;
223 *nresult = (size_t)-1;
227 *nresult = (size_t)-2;
234 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei __unused,
235 char * __restrict s, size_t n, wchar_t wc,
236 _MSKanjiState * __restrict psenc __unused, size_t * __restrict nresult)
240 /* check invalid sequence */
252 s[0] = (wc >> 8) & 0xff;
254 if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) {
268 if (_mskanji1(s[0] & 0xff)) {
278 *nresult = (size_t)-1;
285 _citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * __restrict ei,
286 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
291 if ((_wc_t)wc < 0x80) {
295 } else if ((_wc_t)wc < 0x100) {
298 *idx = (_index_t)wc & 0x7F;
300 /* Kanji (containing Gaiji zone) */
302 * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)):
303 * 0x8140 - 0x817E -> 0x2121 - 0x215F
304 * 0x8180 - 0x819E -> 0x2160 - 0x217E
305 * 0x819F - 0x81FC -> 0x2221 - 0x227E
307 * 0x8240 - 0x827E -> 0x2321 - 0x235F
309 * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E
311 * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F
313 * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E
315 * extended Gaiji zone:
319 * 0xF040 - 0xF09E -> 0x2121 - 0x217E
320 * 0xF140 - 0xF19E -> 0x2321 - 0x237E
322 * 0xF240 - 0xF29E -> 0x2521 - 0x257E
324 * 0xF09F - 0xF0FC -> 0x2821 - 0x287E
325 * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E
327 * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E
329 * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E
331 * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E
333 row = ((_wc_t)wc >> 8) & 0xFF;
334 col = (_wc_t)wc & 0xFF;
335 if (!_mskanji1(row) || !_mskanji2(col))
337 if ((ei->mode & MODE_JIS2004) == 0 || row < 0xF0) {
342 if ((_wc_t)wc <= 0xF49E) {
343 offset = (_wc_t)wc >= 0xF29F ||
344 ((_wc_t)wc >= 0xF09F &&
345 (_wc_t)wc <= 0xF0FC) ? 0xED : 0xF0;
352 row = row * 2 + 0x21;
360 *idx = ((_index_t)row << 8) | col;
368 _citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * __restrict ei,
369 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
385 *wc = (wchar_t)idx + 0x80;
388 if ((ei->mode & MODE_JIS2004) == 0)
398 offset = (row == 0x22 || row >= 0x26) ?
400 else if (row >= 0x4D && row <= 0x7E)
407 offset = (row < 0x5F) ? 0x81 : 0xC1;
410 if (col < 0x21 || col > 0x7E)
412 row -= 0x21; col -= 0x21;
413 if ((row & 1) == 0) {
419 row = row / 2 + offset;
420 *wc = ((wchar_t)row << 8) | col;
431 _citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * __restrict ei __unused,
432 _MSKanjiState * __restrict psenc, int * __restrict rstate)
435 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
436 _STDENC_SDGEN_INCOMPLETE_CHAR;
442 _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei,
443 const void * __restrict var, size_t lenvar)
448 memset((void *)ei, 0, sizeof(*ei));
450 switch (_bcs_toupper(*p)) {
452 MATCH(JIS2004, ei->mode |= MODE_JIS2004);
463 _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei __unused)
468 /* ----------------------------------------------------------------------
469 * public interface for stdenc
472 _CITRUS_STDENC_DECLS(MSKanji);
473 _CITRUS_STDENC_DEF_OPS(MSKanji);
475 #include "citrus_stdenc_template.h"