2 * Copyright (C) 1999-2004, 2008, 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 Library General Public
7 * License as published by the Free Software Foundation; either version 2
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 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17 * If not, see <http://www.gnu.org/licenses/>.
29 * The state is composed of one of the following values
31 #define STATE_ASCII 0 /* Esc ( B */
32 #define STATE_JISX0201ROMAN 1 /* Esc ( J */
33 #define STATE_JISX0201KATAKANA 2 /* Esc ( I */
34 #define STATE_JISX0208 3 /* Esc $ @ or Esc $ B */
35 #define STATE_JISX02131 4 /* Esc $ ( O or Esc $ ( Q*/
36 #define STATE_JISX02132 5 /* Esc $ ( P */
39 * In the ISO-2022-JP-3 to UCS-4 direction, the state also holds the last
40 * character to be output, shifted by 3 bits.
44 iso2022_jp3_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
46 ucs4_t last_wc = conv->istate >> 3;
48 /* Output the buffered character. */
51 return 0; /* Don't advance the input pointer. */
53 state_t state = conv->istate;
70 state = STATE_JISX0201ROMAN;
77 state = STATE_JISX0201KATAKANA;
86 if (s[2] == '@' || s[2] == 'B') {
87 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
88 state = STATE_JISX0208;
97 if (s[3] == 'O' || s[3] == 'Q') {
98 state = STATE_JISX02131;
105 state = STATE_JISX02132;
121 int ret = ascii_mbtowc(conv,pwc,s,1);
122 if (ret == RET_ILSEQ)
124 if (ret != 1) abort();
125 conv->istate = state;
129 case STATE_JISX0201ROMAN:
131 int ret = jisx0201_mbtowc(conv,pwc,s,1);
132 if (ret == RET_ILSEQ)
134 if (ret != 1) abort();
135 conv->istate = state;
139 case STATE_JISX0201KATAKANA:
141 unsigned char buf = c+0x80;
142 int ret = jisx0201_mbtowc(conv,pwc,&buf,1);
143 if (ret == RET_ILSEQ)
145 if (ret != 1) abort();
146 conv->istate = state;
153 if (s[0] < 0x80 && s[1] < 0x80) {
154 int ret = jisx0208_mbtowc(conv,pwc,s,2);
155 if (ret == RET_ILSEQ)
157 if (ret != 2) abort();
158 conv->istate = state;
162 case STATE_JISX02131:
163 case STATE_JISX02132:
166 if (s[0] < 0x80 && s[1] < 0x80) {
167 ucs4_t wc = jisx0213_to_ucs4(((state-STATE_JISX02131+1)<<8)+s[0],s[1]);
170 /* It's a combining character. */
171 ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
172 ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
173 /* We cannot output two Unicode characters at once. So,
174 output the first character and buffer the second one. */
176 conv->istate = (wc2 << 3) | state;
179 conv->istate = state;
188 conv->istate = state;
189 return RET_TOOFEW(count);
192 conv->istate = state;
193 return RET_SHIFT_ILSEQ(count);
198 iso2022_jp3_flushwc (conv_t conv, ucs4_t *pwc)
200 ucs4_t last_wc = conv->istate >> 3;
202 /* Output the buffered character. */
211 * In the UCS-4 to ISO-2022-JP-3 direction, the state also holds the last two
212 * bytes to be output, shifted by 3 bits, and the STATE_xxxxx value that was
213 * effective before this buffered character, shifted by 19 bits.
216 /* Composition tables for each of the relevant combining characters. */
217 static const struct { unsigned short base; unsigned short composed; } iso2022_jp3_comp_table_data[] = {
218 #define iso2022_jp3_comp_table02e5_idx 0
219 #define iso2022_jp3_comp_table02e5_len 1
220 { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */
221 #define iso2022_jp3_comp_table02e9_idx (iso2022_jp3_comp_table02e5_idx+iso2022_jp3_comp_table02e5_len)
222 #define iso2022_jp3_comp_table02e9_len 1
223 { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */
224 #define iso2022_jp3_comp_table0300_idx (iso2022_jp3_comp_table02e9_idx+iso2022_jp3_comp_table02e9_len)
225 #define iso2022_jp3_comp_table0300_len 5
226 { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */
227 { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */
228 { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */
229 { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */
230 { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */
231 #define iso2022_jp3_comp_table0301_idx (iso2022_jp3_comp_table0300_idx+iso2022_jp3_comp_table0300_len)
232 #define iso2022_jp3_comp_table0301_len 4
233 { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */
234 { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */
235 { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */
236 { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */
237 #define iso2022_jp3_comp_table309a_idx (iso2022_jp3_comp_table0301_idx+iso2022_jp3_comp_table0301_len)
238 #define iso2022_jp3_comp_table309a_len 14
239 { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */
240 { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */
241 { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */
242 { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */
243 { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */
244 { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */
245 { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */
246 { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */
247 { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */
248 { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */
249 { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */
250 { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */
251 { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */
252 { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */
255 #define SPLIT_STATE \
256 unsigned short lasttwo = state >> 3; state_t prevstate = state >> 19; state &= 7
257 #define COMBINE_STATE \
258 state |= (prevstate << 19) | (lasttwo << 3)
259 #define COMBINE_STATE_NO_LASTTWO \
260 /* assume lasttwo == 0, then prevstate is ignored */
263 iso2022_jp3_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
266 unsigned char buf[2];
269 state_t state = conv->ostate;
273 /* Attempt to combine the last character with this one. */
278 idx = iso2022_jp3_comp_table02e5_idx,
279 len = iso2022_jp3_comp_table02e5_len;
280 else if (wc == 0x02e9)
281 idx = iso2022_jp3_comp_table02e9_idx,
282 len = iso2022_jp3_comp_table02e9_len;
283 else if (wc == 0x0300)
284 idx = iso2022_jp3_comp_table0300_idx,
285 len = iso2022_jp3_comp_table0300_len;
286 else if (wc == 0x0301)
287 idx = iso2022_jp3_comp_table0301_idx,
288 len = iso2022_jp3_comp_table0301_len;
289 else if (wc == 0x309a)
290 idx = iso2022_jp3_comp_table309a_idx,
291 len = iso2022_jp3_comp_table309a_len;
296 if (iso2022_jp3_comp_table_data[idx].base == lasttwo)
298 while (++idx, --len > 0);
301 /* Output the combined character. */
302 /* We know the combined character is in JISX0213 plane 1, but
303 the buffered character may have been in JISX0208 or in
305 count = (state != STATE_JISX02131 ? 4 : 0) + 2;
308 if (state != STATE_JISX02131) {
314 state = STATE_JISX02131;
316 lasttwo = iso2022_jp3_comp_table_data[idx].composed;
317 r[0] = (lasttwo >> 8) & 0xff;
318 r[1] = lasttwo & 0xff;
319 COMBINE_STATE_NO_LASTTWO;
320 conv->ostate = state;
325 /* Output the buffered character. */
326 /* We know it is in JISX0208 or in JISX0213 plane 1. */
327 count = (prevstate != state ? 3 : 0) + 2;
330 if (prevstate != state) {
331 if (state != STATE_JISX0208) abort();
337 r[0] = (lasttwo >> 8) & 0xff;
338 r[1] = lasttwo & 0xff;
343 ret = ascii_wctomb(conv,buf,wc,1);
344 if (ret != RET_ILUNI) {
345 if (ret != 1) abort();
347 count += (state == STATE_ASCII ? 1 : 4);
350 if (state != STATE_ASCII) {
358 COMBINE_STATE_NO_LASTTWO;
359 conv->ostate = state;
364 /* Try JIS X 0201-1976 Roman. */
365 ret = jisx0201_wctomb(conv,buf,wc,1);
366 if (ret != RET_ILUNI) {
367 if (ret != 1) abort();
369 count += (state == STATE_JISX0201ROMAN ? 1 : 4);
372 if (state != STATE_JISX0201ROMAN) {
377 state = STATE_JISX0201ROMAN;
380 COMBINE_STATE_NO_LASTTWO;
381 conv->ostate = state;
386 jch = ucs4_to_jisx0213(wc);
388 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
389 ret = jisx0208_wctomb(conv,buf,wc,2);
390 if (ret != RET_ILUNI) {
391 if (ret != 2) abort();
392 if (buf[0] < 0x80 && buf[1] < 0x80) {
394 /* A possible match in comp_table_data. Buffer it. */
396 lasttwo = jch & 0x7f7f;
397 state = STATE_JISX0208;
399 conv->ostate = state;
402 count += (state == STATE_JISX0208 ? 2 : 5);
405 if (state != STATE_JISX0208) {
410 state = STATE_JISX0208;
414 COMBINE_STATE_NO_LASTTWO;
415 conv->ostate = state;
421 /* Try JISX 0213 plane 1 and JISX 0213 plane 2. */
424 /* JISX 0213 plane 2. */
425 if (state != STATE_JISX02132) {
434 state = STATE_JISX02132;
437 /* JISX 0213 plane 1. */
438 if (state != STATE_JISX02131) {
447 state = STATE_JISX02131;
451 /* A possible match in comp_table_data. We have to buffer it. */
452 /* We know it's a JISX 0213 plane 1 character. */
453 if (jch & 0x8000) abort();
455 lasttwo = jch & 0x7f7f;
457 conv->ostate = state;
463 r[0] = (jch >> 8) & 0x7f;
465 COMBINE_STATE_NO_LASTTWO;
466 conv->ostate = state;
470 /* Try JIS X 0201-1976 Katakana. This is not officially part of
471 ISO-2022-JP-3. Therefore we try it after all other attempts. */
472 ret = jisx0201_wctomb(conv,buf,wc,1);
473 if (ret != RET_ILUNI) {
474 if (ret != 1) abort();
475 if (buf[0] >= 0x80) {
476 count += (state == STATE_JISX0201KATAKANA ? 1 : 4);
479 if (state != STATE_JISX0201KATAKANA) {
484 state = STATE_JISX0201KATAKANA;
487 COMBINE_STATE_NO_LASTTWO;
488 conv->ostate = state;
497 iso2022_jp3_reset (conv_t conv, unsigned char *r, size_t n)
499 state_t state = conv->ostate;
503 (lasttwo ? (prevstate != state ? 3 : 0) + 2 : 0)
504 + (state != STATE_ASCII ? 3 : 0);
508 if (prevstate != state) {
509 if (state != STATE_JISX0208) abort();
515 r[0] = (lasttwo >> 8) & 0xff;
516 r[1] = lasttwo & 0xff;
519 if (state != STATE_ASCII) {
524 /* conv->ostate = 0; will be done by the caller */
529 #undef COMBINE_STATE_NO_LASTTWO
532 #undef STATE_JISX02132
533 #undef STATE_JISX02131
534 #undef STATE_JISX0208
535 #undef STATE_JISX0201KATAKANA
536 #undef STATE_JISX0201ROMAN