2 * Copyright (C) 1999-2002, 2016 Free Software Foundation, Inc.
3 * This file is part of the GNU LIBICONV Library.
5 * The GNU LIBICONV Library is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * The GNU LIBICONV Library is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17 * If not, see <https://www.gnu.org/licenses/>.
24 /* The structure of EUC-JISX0213 is as follows:
28 0x8E{A1..FE}: JISX0201 Katakana, with prefix 0x8E, offset by +0x80.
30 0x8F{A1..FE}{A1..FE}: JISX0213 plane 2, with prefix 0x8F, offset by +0x8080.
32 0x{A1..FE}{A1..FE}: JISX0213 plane 1, offset by +0x8080.
34 Note that some JISX0213 characters are not contained in Unicode 3.2
35 and are therefore best represented as sequences of Unicode characters.
42 euc_jisx0213_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
44 ucs4_t last_wc
= conv
->istate
;
46 /* Output the buffered character. */
49 return 0; /* Don't advance the input pointer. */
53 /* Plain ASCII character. */
57 if ((c
>= 0xa1 && c
<= 0xfe) || c
== 0x8e || c
== 0x8f) {
58 /* Two or three byte character. */
60 unsigned char c2
= s
[1];
61 if (c2
>= 0xa1 && c2
<= 0xfe) {
63 /* Half-width katakana. */
71 /* JISX 0213 plane 2. */
73 unsigned char c3
= s
[2];
74 wc
= jisx0213_to_ucs4(0x200-0x80+c2
,c3
^0x80);
78 /* JISX 0213 plane 1. */
79 wc
= jisx0213_to_ucs4(0x100-0x80+c
,c2
^0x80);
83 /* It's a combining character. */
84 ucs4_t wc1
= jisx0213_to_ucs_combining
[wc
- 1][0];
85 ucs4_t wc2
= jisx0213_to_ucs_combining
[wc
- 1][1];
86 /* We cannot output two Unicode characters at once. So,
87 output the first character and buffer the second one. */
92 return (c
== 0x8f ? 3 : 2);
104 #define euc_jisx0213_flushwc normal_flushwc
106 /* Composition tables for each of the relevant combining characters. */
107 static const struct { unsigned short base
; unsigned short composed
; } euc_jisx0213_comp_table_data
[] = {
108 #define euc_jisx0213_comp_table02e5_idx 0
109 #define euc_jisx0213_comp_table02e5_len 1
110 { 0xabe4, 0xabe5 }, /* 0x12B65 = 0x12B64 U+02E5 */
111 #define euc_jisx0213_comp_table02e9_idx (euc_jisx0213_comp_table02e5_idx+euc_jisx0213_comp_table02e5_len)
112 #define euc_jisx0213_comp_table02e9_len 1
113 { 0xabe0, 0xabe6 }, /* 0x12B66 = 0x12B60 U+02E9 */
114 #define euc_jisx0213_comp_table0300_idx (euc_jisx0213_comp_table02e9_idx+euc_jisx0213_comp_table02e9_len)
115 #define euc_jisx0213_comp_table0300_len 5
116 { 0xa9dc, 0xabc4 }, /* 0x12B44 = 0x1295C U+0300 */
117 { 0xabb8, 0xabc8 }, /* 0x12B48 = 0x12B38 U+0300 */
118 { 0xabb7, 0xabca }, /* 0x12B4A = 0x12B37 U+0300 */
119 { 0xabb0, 0xabcc }, /* 0x12B4C = 0x12B30 U+0300 */
120 { 0xabc3, 0xabce }, /* 0x12B4E = 0x12B43 U+0300 */
121 #define euc_jisx0213_comp_table0301_idx (euc_jisx0213_comp_table0300_idx+euc_jisx0213_comp_table0300_len)
122 #define euc_jisx0213_comp_table0301_len 4
123 { 0xabb8, 0xabc9 }, /* 0x12B49 = 0x12B38 U+0301 */
124 { 0xabb7, 0xabcb }, /* 0x12B4B = 0x12B37 U+0301 */
125 { 0xabb0, 0xabcd }, /* 0x12B4D = 0x12B30 U+0301 */
126 { 0xabc3, 0xabcf }, /* 0x12B4F = 0x12B43 U+0301 */
127 #define euc_jisx0213_comp_table309a_idx (euc_jisx0213_comp_table0301_idx+euc_jisx0213_comp_table0301_len)
128 #define euc_jisx0213_comp_table309a_len 14
129 { 0xa4ab, 0xa4f7 }, /* 0x12477 = 0x1242B U+309A */
130 { 0xa4ad, 0xa4f8 }, /* 0x12478 = 0x1242D U+309A */
131 { 0xa4af, 0xa4f9 }, /* 0x12479 = 0x1242F U+309A */
132 { 0xa4b1, 0xa4fa }, /* 0x1247A = 0x12431 U+309A */
133 { 0xa4b3, 0xa4fb }, /* 0x1247B = 0x12433 U+309A */
134 { 0xa5ab, 0xa5f7 }, /* 0x12577 = 0x1252B U+309A */
135 { 0xa5ad, 0xa5f8 }, /* 0x12578 = 0x1252D U+309A */
136 { 0xa5af, 0xa5f9 }, /* 0x12579 = 0x1252F U+309A */
137 { 0xa5b1, 0xa5fa }, /* 0x1257A = 0x12531 U+309A */
138 { 0xa5b3, 0xa5fb }, /* 0x1257B = 0x12533 U+309A */
139 { 0xa5bb, 0xa5fc }, /* 0x1257C = 0x1253B U+309A */
140 { 0xa5c4, 0xa5fd }, /* 0x1257D = 0x12544 U+309A */
141 { 0xa5c8, 0xa5fe }, /* 0x1257E = 0x12548 U+309A */
142 { 0xa6f5, 0xa6f8 }, /* 0x12678 = 0x12675 U+309A */
146 euc_jisx0213_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, size_t n
)
149 unsigned short lasttwo
= conv
->ostate
;
152 /* Attempt to combine the last character with this one. */
157 idx
= euc_jisx0213_comp_table02e5_idx
,
158 len
= euc_jisx0213_comp_table02e5_len
;
159 else if (wc
== 0x02e9)
160 idx
= euc_jisx0213_comp_table02e9_idx
,
161 len
= euc_jisx0213_comp_table02e9_len
;
162 else if (wc
== 0x0300)
163 idx
= euc_jisx0213_comp_table0300_idx
,
164 len
= euc_jisx0213_comp_table0300_len
;
165 else if (wc
== 0x0301)
166 idx
= euc_jisx0213_comp_table0301_idx
,
167 len
= euc_jisx0213_comp_table0301_len
;
168 else if (wc
== 0x309a)
169 idx
= euc_jisx0213_comp_table309a_idx
,
170 len
= euc_jisx0213_comp_table309a_len
;
175 if (euc_jisx0213_comp_table_data
[idx
].base
== lasttwo
)
177 while (++idx
, --len
> 0);
180 /* Output the combined character. */
182 lasttwo
= euc_jisx0213_comp_table_data
[idx
].composed
;
183 r
[0] = (lasttwo
>> 8) & 0xff;
184 r
[1] = lasttwo
& 0xff;
192 /* Output the buffered character. */
195 r
[0] = (lasttwo
>> 8) & 0xff;
196 r
[1] = lasttwo
& 0xff;
202 /* Plain ASCII character. */
204 r
[0] = (unsigned char) wc
;
209 } else if (wc
>= 0xff61 && wc
<= 0xff9f) {
210 /* Half-width katakana. */
219 unsigned short jch
= ucs4_to_jisx0213(wc
);
222 /* A possible match in comp_table_data. We have to buffer it. */
223 /* We know it's a JISX 0213 plane 1 character. */
224 if (jch
& 0x8000) abort();
225 conv
->ostate
= jch
| 0x8080;
229 /* JISX 0213 plane 2. */
232 r
[1] = (jch
>> 8) | 0x80;
233 r
[2] = (jch
& 0xff) | 0x80;
239 /* JISX 0213 plane 1. */
241 r
[0] = (jch
>> 8) | 0x80;
242 r
[1] = (jch
& 0xff) | 0x80;
254 euc_jisx0213_reset (conv_t conv
, unsigned char *r
, size_t n
)
256 state_t lasttwo
= conv
->ostate
;
261 r
[0] = (lasttwo
>> 8) & 0xff;
262 r
[1] = lasttwo
& 0xff;
263 /* conv->ostate = 0; will be done by the caller */