2 * Copyright (C) 1999-2001, 2008, 2011-2012, 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/>.
24 * This is an extension of ISO-2022-JP-1 with larger character sets.
25 * It uses ESC $ B and ESC $ ( D to denote *extensions* of JIS X 0208 and
26 * JIS X 0212, respectively. This violates the principles of ISO 2022,
28 * 1. character sets to be used by ISO 2022 have to be registered at the
29 * ISO IR registry <http://www.itscj.ipsj.or.jp/ISO-IR/>,
30 * 2. different character sets are designated by different escape
32 * It's a typical instance of the "embrace and extend" strategy by Microsoft
33 * <http://en.wikipedia.org/wiki/Embrace,_extend_and_extinguish>.
37 * Windows has three encodings CP50220, CP50221, CP50222.
38 * The common parts are:
39 * - US-ASCII (0x00..0x7F)
40 * - JIS X 0208 extended by
42 * - a private use area (rows 0x75..0x7E = U+E000..U+E3AB),
43 * enabled with ESC $ B, disabled with ESC ( B.
44 * - JIS X 0212 extended by
45 * - two rows (0x73..0x74),
46 * - a private use area (rows 0x75..0x7E = U+E3AC..U+E757),
47 * enabled with ESC $ ( D, disabled with ESC ( B.
48 * They differ in the handling of JIS X 0201 characters (halfwidth Katakana)
49 * in the conversion direction Unicode -> CP5022x:
50 * * CP50220 maps the halfwidth Katakana to fullwidth Katakana characters.
51 * * CP50221 contains the JIS X 0201 halfwidth Katakana characters,
52 * enabled with ESC ( I, disabled with ESC ( B.
53 * * CP50222 contains the JIS X 0201 halfwidth Katakana characters,
54 * enabled with ESC ( J 0x0E, disabled with ESC ( B.
55 * In the conversion direction CP5022x -> Unicode, all three operate the same:
56 * - ESC ( I is supported and understood.
57 * - ESC ( J 0x0E is not accepted. (Tested on Windows XP SP3.)
59 * - CP50222 should not be used, because the multibyte sequence that it
60 * produces cannot be parsed by either of the three encodings.
61 * - CP50221 is preferrable to CP50220, because it can faithfully represent
62 * the halfwidth Katakana characters.
63 * We therefore implement CP50221. As an extension, in the mbtowc conversion
64 * direction, we support also ESC ( J 0x0E, just in case.
67 #include "cp50221_0208_ext.h"
68 #include "cp50221_0212_ext.h"
75 * The state can be one of the following values.
77 #define STATE_ASCII 0 /* Esc ( B */
78 #define STATE_JISX0201ROMAN 1 /* Esc ( J */ /* only in mbtowc direction */
79 #define STATE_JISX0201KATAKANA 2 /* Esc ( I */
80 #define STATE_JISX0208MS 3 /* Esc $ @ or Esc $ B */
81 #define STATE_JISX0212MS 4 /* Esc $ ( D */
84 iso2022_jpms_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
86 state_t state = conv->istate;
103 state = STATE_JISX0201KATAKANA;
110 state = STATE_JISX0201ROMAN;
119 if (s[2] == '@' || s[2] == 'B') {
120 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
121 state = STATE_JISX0208MS;
131 state = STATE_JISX0212MS;
143 if (state == STATE_JISX0201ROMAN)
144 state = STATE_JISX0201KATAKANA;
151 if (state == STATE_JISX0201KATAKANA)
152 state = STATE_JISX0201ROMAN;
163 int ret = ascii_mbtowc(conv,pwc,s,1);
164 if (ret == RET_ILSEQ)
166 if (ret != 1) abort();
167 conv->istate = state;
171 case STATE_JISX0201ROMAN:
173 int ret = jisx0201_mbtowc(conv,pwc,s,1);
174 if (ret == RET_ILSEQ)
176 if (ret != 1) abort();
177 conv->istate = state;
181 case STATE_JISX0201KATAKANA:
183 unsigned char buf = c+0x80;
184 int ret = jisx0201_mbtowc(conv,pwc,&buf,1);
185 if (ret == RET_ILSEQ)
187 if (ret != 1) abort();
188 conv->istate = state;
192 case STATE_JISX0208MS:
195 if (s[0] < 0x80 && s[1] < 0x80) {
199 /* Extension of JIS X 0208. */
200 if (s[1] >= 0x21 && s[1] <= 0x79) {
201 unsigned char i = (s[1] - 0x21) + 1;
202 ret = cp50221_0208_ext_mbtowc(conv,pwc,&i,1);
209 ret = jisx0208_mbtowc(conv,pwc,s,2);
212 /* Extension of JIS X 0208.
213 0x{75..7E}{21..8E} maps to U+E000..U+E3AB.
214 But some rows maps to characters present in CP932. */
215 if (s[0] <= 0x7e && (s[1] >= 0x21 && s[1] <= 0x7e)) {
216 unsigned short wc = 0xfffd;
217 if (s[0] >= 0x79 && s[0] <= 0x7c)
218 wc = cp932ext_2uni_pageed[(s[0] - 0x79) * 94 + (s[1] - 0x21)];
220 wc = (s[0] - 0x75) * 94 + (s[1] - 0x21) + 0xe000;
226 if (ret == RET_ILSEQ)
228 if (ret != 2) abort();
229 conv->istate = state;
233 case STATE_JISX0212MS:
236 if (s[0] < 0x80 && s[1] < 0x80) {
240 ret = jisx0212_mbtowc(conv,pwc,s,2);
243 /* Extension of JIS X 0212. */
244 if (s[1] >= 0x21 && s[1] <= 0x7e) {
245 unsigned char i = (s[0] - 0x73) * 94 + (s[1] - 0x21) + 1;
246 ret = cp50221_0212_ext_mbtowc(conv,pwc,&i,1);
252 /* Extension of JIS X 0208.
253 0x{75..7E}{21..8E} maps to U+E3AC..U+E757. */
254 if (s[0] <= 0x7e && (s[1] >= 0x21 && s[1] <= 0x7e)) {
255 *pwc = (s[0] - 0x75) * 94 + (s[1] - 0x21) + 0xe3ac;
261 if (ret == RET_ILSEQ)
263 if (ret != 2) abort();
264 conv->istate = state;
272 conv->istate = state;
273 return RET_TOOFEW(count);
276 conv->istate = state;
277 return RET_SHIFT_ILSEQ(count);
281 iso2022_jpms_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
283 state_t state = conv->ostate;
284 unsigned char buf[2];
288 ret = ascii_wctomb(conv,buf,wc,1);
289 if (ret != RET_ILUNI) {
290 if (ret != 1) abort();
292 int count = (state == STATE_ASCII ? 1 : 4);
295 if (state != STATE_ASCII) {
303 conv->ostate = state;
308 /* Try JIS X 0201-1976 Katakana. */
309 ret = jisx0201_wctomb(conv,buf,wc,1);
310 if (ret != RET_ILUNI) {
311 if (ret != 1) abort();
312 if (buf[0] >= 0x80) {
313 int count = (state == STATE_JISX0201KATAKANA ? 1 : 4);
316 if (state != STATE_JISX0201KATAKANA) {
321 state = STATE_JISX0201KATAKANA;
324 conv->ostate = state;
329 /* Try JIS X 0208-1990, in place of JIS X 0208-1978 and JIS X 0208-1983,
330 and the extensions mentioned above. */
331 if (wc >= 0xe000 && wc < 0xe3ac) {
332 unsigned short i = wc - 0xe000;
333 buf[0] = (i / 94) + 0x75;
334 buf[1] = (i % 94) + 0x21;
337 ret = jisx0208_wctomb(conv,buf,wc,2);
338 if (ret == RET_ILUNI) {
339 /* Extension of JIS X 0208. */
341 ret = cp50221_0208_ext_wctomb(conv,&i,wc,1);
346 } else if (wc == 0x663B) {
350 } else if (wc == 0xffe2) {
354 } else if (wc == 0xffe4) {
361 if (ret != RET_ILUNI) {
362 if (ret != 2) abort();
363 if (buf[0] < 0x80 && buf[1] < 0x80) {
364 int count = (state == STATE_JISX0208MS ? 2 : 5);
367 if (state != STATE_JISX0208MS) {
372 state = STATE_JISX0208MS;
376 conv->ostate = state;
381 /* Try JIS X 0212-1990 and the extensions mentioned above. */
382 if (wc >= 0xe3ac && wc < 0xe758) {
383 unsigned short i = wc - 0xe3ac;
384 buf[0] = (i / 94) + 0x75;
385 buf[1] = (i % 94) + 0x21;
388 ret = jisx0212_wctomb(conv,buf,wc,2);
389 if (ret == RET_ILUNI) {
390 /* Extension of JIS X 0212. */
392 ret = cp50221_0212_ext_wctomb(conv,&i,wc,1);
395 buf[0] = (i / 94) + 0x73;
396 buf[1] = (i % 94) + 0x21;
401 if (ret != RET_ILUNI) {
402 if (ret != 2) abort();
403 if (buf[0] < 0x80 && buf[1] < 0x80) {
404 int count = (state == STATE_JISX0212MS ? 2 : 6);
407 if (state != STATE_JISX0212MS) {
413 state = STATE_JISX0212MS;
417 conv->ostate = state;
426 iso2022_jpms_reset (conv_t conv, unsigned char *r, size_t n)
428 state_t state = conv->ostate;
429 if (state != STATE_ASCII) {
435 /* conv->ostate = 0; will be done by the caller */
441 #undef STATE_JISX0212MS
442 #undef STATE_JISX0208MS
443 #undef STATE_JISX0201KATAKANA
444 #undef STATE_JISX0201ROMAN