1 /* Routine to translate between Japanese characters and Unicode */
3 /* Copyright (c) 2002 Red Hat Incorporated.
5 Modified (m) 2017 Thomas Wolff: consider locale, add dummy uc2jp
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
13 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 The name of Red Hat Incorporated may not be used to endorse
18 or promote products derived from this software without specific
19 prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY
25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 /* Under Cygwin, the incoming wide character is already given in UTF due
37 to the requirements of the underlying OS. */
45 /* Japanese encoding types supported */
50 /* Japanese to Unicode conversion routine */
54 __jp2uc (wint_t c
, int type
)
57 unsigned char byte1
, byte2
;
60 /* we actually use tables of EUCJP to Unicode. For JIS, we simply
61 note that EUCJP is essentially JIS with the top bits on in each
62 byte and translate to EUCJP. For SJIS, we do a translation to EUCJP before
63 accessing the tables. */
67 byte1
= (c
>> 8) + 0x80;
68 byte2
= (c
& 0xff) + 0x80;
80 byte2
= (byte2
- 31) + 0xa1;
85 byte2
= (byte2
- 126) + 0xa1;
88 byte1
= ((byte1
- 112) << 1) + adj
;
90 byte1
= ((byte1
- 176) << 1) + adj
;
96 /* find conversion in jp2uc arrays */
98 /* handle larger ranges first */
99 if (byte1
>= 0xb0 && byte1
<= 0xcf && c
<= 0xcfd3)
101 index
= (byte1
- 0xb0) * 0xfe + (byte2
- 0xa1);
104 else if (byte1
>= 0xd0 && byte1
<= 0xf4 && c
<= 0xf4a6)
106 index
= (byte1
- 0xd0) * 0xfe + (byte2
- 0xa1);
110 /* handle smaller ranges here */
114 return (wint_t)a1
[byte2
- 0xa1];
116 ret
= a2
[byte2
- 0xa1];
121 if (a3
[byte2
- 0xa1])
122 return (wint_t)(0xff00 + (byte2
- 0xa0));
126 return (wint_t)(0x3000 + (byte2
- 0x60));
130 return (wint_t)(0x3000 + byte2
);
135 ret
= (wint_t)a6
[byte2
- 0xa1];
142 ret
= (wint_t)a7
[byte2
- 0xa1];
148 return (wint_t)a8
[byte2
- 0xa1];
157 /* Unicode to Japanese conversion routine */
159 __uc2jp (wint_t c
, int type
)
161 #warning back-conversion Unicode to Japanese not implemented; needed for towupper/towlower
165 /* Japanese to Unicode conversion interface */
167 _jp2uc_l (wint_t c
, struct __locale_t
* l
)
169 const char * cs
= l
? __locale_charset(l
) : __current_locale_charset();
170 if (0 == strcmp (cs
, "JIS"))
171 c
= __jp2uc (c
, JP_JIS
);
172 else if (0 == strcmp (cs
, "SJIS"))
173 c
= __jp2uc (c
, JP_SJIS
);
174 else if (0 == strcmp (cs
, "EUCJP"))
175 c
= __jp2uc (c
, JP_EUCJP
);
182 return _jp2uc_l (c
, 0);
185 /* Unicode to Japanese conversion interface */
187 _uc2jp_l (wint_t c
, struct __locale_t
* l
)
189 const char * cs
= l
? __locale_charset(l
) : __current_locale_charset();
190 if (0 == strcmp (cs
, "JIS"))
191 c
= __uc2jp (c
, JP_JIS
);
192 else if (0 == strcmp (cs
, "SJIS"))
193 c
= __uc2jp (c
, JP_SJIS
);
194 else if (0 == strcmp (cs
, "EUCJP"))
195 c
= __uc2jp (c
, JP_EUCJP
);
199 #endif /* !__CYGWIN__ */
200 #endif /* _MB_CAPABLE */