1 /* $NetBSD: citrus_mskanji.c,v 1.14 2013/05/28 16:57:56 joerg Exp $ */
4 * Copyright (c)2002 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
30 * ja_JP.SJIS locale table for BSD4.4/rune
32 * (C) Sin'ichiro MIYATANI / Phase One, Inc
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Phase One, Inc.
46 * 4. The name of Phase One, Inc. may be used to endorse or promote products
47 * derived from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
64 #if defined(LIBC_SCCS) && !defined(lint)
65 __RCSID("$NetBSD: citrus_mskanji.c,v 1.14 2013/05/28 16:57:56 joerg Exp $");
66 #endif /* LIBC_SCCS and not lint */
75 #include <sys/types.h>
78 #include "citrus_namespace.h"
79 #include "citrus_types.h"
80 #include "citrus_bcs.h"
81 #include "citrus_module.h"
82 #include "citrus_ctype.h"
83 #include "citrus_stdenc.h"
84 #include "citrus_mskanji.h"
87 /* ----------------------------------------------------------------------
88 * private stuffs used by templates
91 typedef struct _MSKanjiState
{
98 #define MODE_JIS2004 1
99 } _MSKanjiEncodingInfo
;
102 _MSKanjiEncodingInfo ei
;
104 /* for future multi-locale facility */
105 _MSKanjiState s_mblen
;
106 _MSKanjiState s_mbrlen
;
107 _MSKanjiState s_mbrtowc
;
108 _MSKanjiState s_mbtowc
;
109 _MSKanjiState s_mbsrtowcs
;
110 _MSKanjiState s_mbsnrtowcs
;
111 _MSKanjiState s_wcrtomb
;
112 _MSKanjiState s_wcsrtombs
;
113 _MSKanjiState s_wcsnrtombs
;
114 _MSKanjiState s_wctomb
;
118 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
119 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
121 #define _FUNCNAME(m) _citrus_MSKanji_##m
122 #define _ENCODING_INFO _MSKanjiEncodingInfo
123 #define _CTYPE_INFO _MSKanjiCTypeInfo
124 #define _ENCODING_STATE _MSKanjiState
125 #define _ENCODING_MB_CUR_MAX(_ei_) 2
126 #define _ENCODING_IS_STATE_DEPENDENT 0
127 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
134 if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
144 if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
152 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo
* __restrict ei
,
153 _MSKanjiState
* __restrict s
)
160 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo
* __restrict ei
,
161 void * __restrict pspriv
,
162 const _MSKanjiState
* __restrict s
)
164 memcpy(pspriv
, (const void *)s
, sizeof(*s
));
169 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo
* __restrict ei
,
170 _MSKanjiState
* __restrict s
,
171 const void * __restrict pspriv
)
173 memcpy((void *)s
, pspriv
, sizeof(*s
));
178 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo
* __restrict ei
,
179 wchar_t * __restrict pwc
,
180 const char ** __restrict s
, size_t n
,
181 _MSKanjiState
* __restrict psenc
,
182 size_t * __restrict nresult
)
189 _DIAGASSERT(nresult
!= 0);
190 _DIAGASSERT(ei
!= NULL
);
191 _DIAGASSERT(s
!= NULL
);
192 _DIAGASSERT(psenc
!= NULL
);
197 _citrus_MSKanji_init_state(ei
, psenc
);
198 *nresult
= 0; /* state independent */
202 chlenbak
= psenc
->chlen
;
204 /* make sure we have the first byte in the buffer */
205 switch (psenc
->chlen
) {
209 psenc
->ch
[0] = *s0
++;
220 len
= _mskanji1(psenc
->ch
[0] & 0xff) ? 2 : 1;
221 while (psenc
->chlen
< len
) {
224 psenc
->ch
[psenc
->chlen
] = *s0
++;
233 wchar
= psenc
->ch
[0] & 0xff;
236 if (!_mskanji2(psenc
->ch
[1] & 0xff))
238 wchar
= ((psenc
->ch
[0] & 0xff) << 8) | (psenc
->ch
[1] & 0xff);
253 *nresult
= len
- chlenbak
;
259 *nresult
= (size_t)-1;
263 *nresult
= (size_t)-2;
270 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo
* __restrict ei
,
271 char * __restrict s
, size_t n
, wchar_t wc
,
272 _MSKanjiState
* __restrict psenc
,
273 size_t * __restrict nresult
)
277 _DIAGASSERT(ei
!= NULL
);
278 _DIAGASSERT(psenc
!= NULL
);
279 _DIAGASSERT(s
!= NULL
);
281 /* check invalid sequence */
293 s
[0] = (wc
>> 8) & 0xff;
295 if (!_mskanji1(s
[0] & 0xff) || !_mskanji2(s
[1] & 0xff)) {
309 if (_mskanji1(s
[0] & 0xff)) {
319 *nresult
= (size_t)-1;
326 _citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo
* __restrict ei
,
327 _csid_t
* __restrict csid
,
328 _index_t
* __restrict idx
, wchar_t wc
)
333 _DIAGASSERT(csid
!= NULL
&& idx
!= NULL
);
335 if ((_wc_t
)wc
< 0x80) {
339 } else if ((_wc_t
)wc
< 0x100) {
342 *idx
= (_index_t
)wc
& 0x7F;
344 /* Kanji (containing Gaiji zone) */
346 * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)):
347 * 0x8140 - 0x817E -> 0x2121 - 0x215F
348 * 0x8180 - 0x819E -> 0x2160 - 0x217E
349 * 0x819F - 0x81FC -> 0x2221 - 0x227E
351 * 0x8240 - 0x827E -> 0x2321 - 0x235F
353 * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E
355 * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F
357 * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E
359 * extended Gaiji zone:
363 * 0xF040 - 0xF09E -> 0x2121 - 0x217E
364 * 0xF140 - 0xF19E -> 0x2321 - 0x237E
366 * 0xF240 - 0xF29E -> 0x2521 - 0x257E
368 * 0xF09F - 0xF0FC -> 0x2821 - 0x287E
369 * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E
371 * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E
373 * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E
375 * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E
377 row
= ((_wc_t
)wc
>> 8) & 0xFF;
378 col
= (_wc_t
)wc
& 0xFF;
379 if (!_mskanji1(row
) || !_mskanji2(col
))
381 if ((ei
->mode
& MODE_JIS2004
) == 0 || row
< 0xF0) {
386 if ((_wc_t
)wc
<= 0xF49E) {
387 offset
= (_wc_t
)wc
>= 0xF29F ||
388 ((_wc_t
)wc
>= 0xF09F && (_wc_t
)wc
<= 0xF0FC)
396 row
= row
* 2 + 0x21;
404 *idx
= ((_index_t
)row
<< 8) | col
;
412 _citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo
* __restrict ei
,
413 wchar_t * __restrict wc
,
414 _csid_t csid
, _index_t idx
)
419 _DIAGASSERT(wc
!= NULL
);
432 *wc
= (wchar_t)idx
+ 0x80;
435 if ((ei
->mode
& MODE_JIS2004
) == 0)
445 offset
= (row
== 0x22 || row
>= 0x26)
447 else if (row
>= 0x4D && row
<= 0x7E)
454 offset
= (row
< 0x5F) ? 0x81 : 0xC1;
457 if (col
< 0x21 || col
> 0x7E)
459 row
-= 0x21; col
-= 0x21;
460 if ((row
& 1) == 0) {
466 row
= row
/ 2 + offset
;
467 *wc
= ((wchar_t)row
<< 8) | col
;
478 _citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo
* __restrict ei
,
479 _MSKanjiState
* __restrict psenc
,
480 int * __restrict rstate
)
483 if (psenc
->chlen
== 0)
484 *rstate
= _STDENC_SDGEN_INITIAL
;
486 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
;
493 _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo
* __restrict ei
,
494 const void * __restrict var
,
499 _DIAGASSERT(ei
!= NULL
);
502 #define MATCH(x, act) \
504 if (lenvar >= (sizeof(#x)-1) && \
505 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
507 lenvar -= sizeof(#x)-1; \
510 } while (/*CONSTCOND*/0)
511 memset((void *)ei
, 0, sizeof(*ei
));
513 switch (_bcs_toupper(*p
)) {
515 MATCH(JIS2004
, ei
->mode
|= MODE_JIS2004
);
526 _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo
*ei
)
530 /* ----------------------------------------------------------------------
531 * public interface for ctype
534 _CITRUS_CTYPE_DECLS(MSKanji
);
535 _CITRUS_CTYPE_DEF_OPS(MSKanji
);
537 #include "citrus_ctype_template.h"
539 /* ----------------------------------------------------------------------
540 * public interface for stdenc
543 _CITRUS_STDENC_DECLS(MSKanji
);
544 _CITRUS_STDENC_DEF_OPS(MSKanji
);
546 #include "citrus_stdenc_template.h"