Add comments: glibc now implements the X11 encoding names.
[libiconv.git] / lib / iso2022_jp2.h
blobcb5ba296a3b164dc40728ca2a5b688261735783d
1 /*
2 * Copyright (C) 1999-2000 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, write to the Free Software Foundation, Inc., 59 Temple Place -
18 * Suite 330, Boston, MA 02111-1307, USA.
22 * ISO-2022-JP-2
25 /* Specification: RFC 1554 */
26 /* ESC '(' 'I' for JISX0201 Katakana is an extension not found in RFC 1554 or
27 CJK.INF, but implemented in glibc-2.1 and qt-2.0. */
29 #define ESC 0x1b
32 * The state is composed of one of the following values
34 #define STATE_ASCII 0
35 #define STATE_JISX0201ROMAN 1
36 #define STATE_JISX0201KATAKANA 2
37 #define STATE_JISX0208 3
38 #define STATE_JISX0212 4
39 #define STATE_GB2312 5
40 #define STATE_KSC5601 6
42 * and one of the following values, << 8
44 #define STATE_G2_NONE 0
45 #define STATE_G2_ISO8859_1 1
46 #define STATE_G2_ISO8859_7 2
48 #define SPLIT_STATE \
49 unsigned int state1 = state & 0xff, state2 = state >> 8
50 #define COMBINE_STATE \
51 state = (state2 << 8) | state1
53 static int
54 iso2022_jp2_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
56 state_t state = conv->istate;
57 SPLIT_STATE;
58 int count = 0;
59 unsigned char c;
60 for (;;) {
61 c = *s;
62 if (c == ESC) {
63 if (n < count+3)
64 goto none;
65 if (s[1] == '(') {
66 if (s[2] == 'B') {
67 state1 = STATE_ASCII;
68 s += 3; count += 3;
69 if (n < count+1)
70 goto none;
71 continue;
73 if (s[2] == 'J') {
74 state1 = STATE_JISX0201ROMAN;
75 s += 3; count += 3;
76 if (n < count+1)
77 goto none;
78 continue;
80 if (s[2] == 'I') {
81 state1 = STATE_JISX0201KATAKANA;
82 s += 3; count += 3;
83 if (n < count+1)
84 goto none;
85 continue;
87 return RET_ILSEQ;
89 if (s[1] == '$') {
90 if (s[2] == '@' || s[2] == 'B') {
91 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
92 state1 = STATE_JISX0208;
93 s += 3; count += 3;
94 if (n < count+1)
95 goto none;
96 continue;
98 if (s[2] == 'A') {
99 state1 = STATE_GB2312;
100 s += 3; count += 3;
101 if (n < count+1)
102 goto none;
103 continue;
105 if (s[2] == '(') {
106 if (n < count+4)
107 goto none;
108 if (s[3] == 'D') {
109 state1 = STATE_JISX0212;
110 s += 4; count += 4;
111 if (n < count+1)
112 goto none;
113 continue;
115 if (s[3] == 'C') {
116 state1 = STATE_KSC5601;
117 s += 4; count += 4;
118 if (n < count+1)
119 goto none;
120 continue;
122 return RET_ILSEQ;
124 return RET_ILSEQ;
126 if (s[1] == '.') {
127 if (n < count+3)
128 goto none;
129 if (s[2] == 'A') {
130 state2 = STATE_G2_ISO8859_1;
131 s += 3; count += 3;
132 if (n < count+1)
133 goto none;
134 continue;
136 if (s[2] == 'F') {
137 state2 = STATE_G2_ISO8859_7;
138 s += 3; count += 3;
139 if (n < count+1)
140 goto none;
141 continue;
143 return RET_ILSEQ;
145 if (s[1] == 'N') {
146 switch (state2) {
147 case STATE_G2_NONE:
148 return RET_ILSEQ;
149 case STATE_G2_ISO8859_1:
150 if (s[2] < 0x80) {
151 unsigned char buf = s[2]+0x80;
152 int ret = iso8859_1_mbtowc(conv,pwc,&buf,1);
153 if (ret == RET_ILSEQ)
154 return RET_ILSEQ;
155 if (ret != 1) abort();
156 COMBINE_STATE;
157 conv->istate = state;
158 return count+3;
159 } else
160 return RET_ILSEQ;
161 case STATE_G2_ISO8859_7:
162 if (s[2] < 0x80) {
163 unsigned char buf = s[2]+0x80;
164 int ret = iso8859_7_mbtowc(conv,pwc,&buf,1);
165 if (ret == RET_ILSEQ)
166 return RET_ILSEQ;
167 if (ret != 1) abort();
168 COMBINE_STATE;
169 conv->istate = state;
170 return count+3;
171 } else
172 return RET_ILSEQ;
173 default: abort();
176 return RET_ILSEQ;
178 break;
180 switch (state1) {
181 case STATE_ASCII:
182 if (c < 0x80) {
183 int ret = ascii_mbtowc(conv,pwc,s,1);
184 if (ret == RET_ILSEQ)
185 return RET_ILSEQ;
186 if (ret != 1) abort();
187 if (*pwc == 0x000a || *pwc == 0x000d)
188 state2 = STATE_G2_NONE;
189 COMBINE_STATE;
190 conv->istate = state;
191 return count+1;
192 } else
193 return RET_ILSEQ;
194 case STATE_JISX0201ROMAN:
195 if (c < 0x80) {
196 int ret = jisx0201_mbtowc(conv,pwc,s,1);
197 if (ret == RET_ILSEQ)
198 return RET_ILSEQ;
199 if (ret != 1) abort();
200 if (*pwc == 0x000a || *pwc == 0x000d)
201 state2 = STATE_G2_NONE;
202 COMBINE_STATE;
203 conv->istate = state;
204 return count+1;
205 } else
206 return RET_ILSEQ;
207 case STATE_JISX0201KATAKANA:
208 if (c < 0x80) {
209 unsigned char buf = c+0x80;
210 int ret = jisx0201_mbtowc(conv,pwc,&buf,1);
211 if (ret == RET_ILSEQ)
212 return RET_ILSEQ;
213 if (ret != 1) abort();
214 COMBINE_STATE;
215 conv->istate = state;
216 return count+1;
217 } else
218 return RET_ILSEQ;
219 case STATE_JISX0208:
220 if (n < count+2)
221 goto none;
222 if (s[0] < 0x80 && s[1] < 0x80) {
223 int ret = jisx0208_mbtowc(conv,pwc,s,2);
224 if (ret == RET_ILSEQ)
225 return RET_ILSEQ;
226 if (ret != 2) abort();
227 COMBINE_STATE;
228 conv->istate = state;
229 return count+2;
230 } else
231 return RET_ILSEQ;
232 case STATE_JISX0212:
233 if (n < count+2)
234 goto none;
235 if (s[0] < 0x80 && s[1] < 0x80) {
236 int ret = jisx0212_mbtowc(conv,pwc,s,2);
237 if (ret == RET_ILSEQ)
238 return RET_ILSEQ;
239 if (ret != 2) abort();
240 COMBINE_STATE;
241 conv->istate = state;
242 return count+2;
243 } else
244 return RET_ILSEQ;
245 case STATE_GB2312:
246 if (n < count+2)
247 goto none;
248 if (s[0] < 0x80 && s[1] < 0x80) {
249 int ret = gb2312_mbtowc(conv,pwc,s,2);
250 if (ret == RET_ILSEQ)
251 return RET_ILSEQ;
252 if (ret != 2) abort();
253 COMBINE_STATE;
254 conv->istate = state;
255 return count+2;
256 } else
257 return RET_ILSEQ;
258 case STATE_KSC5601:
259 if (n < count+2)
260 goto none;
261 if (s[0] < 0x80 && s[1] < 0x80) {
262 int ret = ksc5601_mbtowc(conv,pwc,s,2);
263 if (ret == RET_ILSEQ)
264 return RET_ILSEQ;
265 if (ret != 2) abort();
266 COMBINE_STATE;
267 conv->istate = state;
268 return count+2;
269 } else
270 return RET_ILSEQ;
271 default: abort();
274 none:
275 COMBINE_STATE;
276 conv->istate = state;
277 return RET_TOOFEW(count);
280 static int
281 iso2022_jp2_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
283 state_t state = conv->ostate;
284 SPLIT_STATE;
285 unsigned char buf[2];
286 int ret;
288 /* Try ASCII. */
289 ret = ascii_wctomb(conv,buf,wc,1);
290 if (ret != RET_ILSEQ) {
291 if (ret != 1) abort();
292 if (buf[0] < 0x80) {
293 int count = (state1 == STATE_ASCII ? 1 : 4);
294 if (n < count)
295 return RET_TOOSMALL;
296 if (state1 != STATE_ASCII) {
297 r[0] = ESC;
298 r[1] = '(';
299 r[2] = 'B';
300 r += 3;
301 state1 = STATE_ASCII;
303 r[0] = buf[0];
304 if (wc == 0x000a || wc == 0x000d)
305 state2 = STATE_G2_NONE;
306 COMBINE_STATE;
307 conv->ostate = state;
308 return count;
312 /* Try ISO-8859-1. */
313 ret = iso8859_1_wctomb(conv,buf,wc,1);
314 if (ret != RET_ILSEQ) {
315 if (ret != 1) abort();
316 if (buf[0] >= 0x80) {
317 int count = (state2 == STATE_G2_ISO8859_1 ? 3 : 6);
318 if (n < count)
319 return RET_TOOSMALL;
320 if (state2 != STATE_G2_ISO8859_1) {
321 r[0] = ESC;
322 r[1] = '.';
323 r[2] = 'A';
324 r += 3;
325 state2 = STATE_G2_ISO8859_1;
327 r[0] = ESC;
328 r[1] = 'N';
329 r[2] = buf[0]-0x80;
330 COMBINE_STATE;
331 conv->ostate = state;
332 return count;
336 /* Try ISO-8859-7. */
337 ret = iso8859_7_wctomb(conv,buf,wc,1);
338 if (ret != RET_ILSEQ) {
339 if (ret != 1) abort();
340 if (buf[0] >= 0x80) {
341 int count = (state2 == STATE_G2_ISO8859_7 ? 3 : 6);
342 if (n < count)
343 return RET_TOOSMALL;
344 if (state2 != STATE_G2_ISO8859_7) {
345 r[0] = ESC;
346 r[1] = '.';
347 r[2] = 'F';
348 r += 3;
349 state2 = STATE_G2_ISO8859_7;
351 r[0] = ESC;
352 r[1] = 'N';
353 r[2] = buf[0]-0x80;
354 COMBINE_STATE;
355 conv->ostate = state;
356 return count;
360 /* Try JIS X 0201-1976 Roman and Kana. */
361 ret = jisx0201_wctomb(conv,buf,wc,1);
362 if (ret != RET_ILSEQ) {
363 if (ret != 1) abort();
364 if (buf[0] < 0x80) {
365 int count = (state1 == STATE_JISX0201ROMAN ? 1 : 4);
366 if (n < count)
367 return RET_TOOSMALL;
368 if (state1 != STATE_JISX0201ROMAN) {
369 r[0] = ESC;
370 r[1] = '(';
371 r[2] = 'J';
372 r += 3;
373 state1 = STATE_JISX0201ROMAN;
375 r[0] = buf[0];
376 if (wc == 0x000a || wc == 0x000d)
377 state2 = STATE_G2_NONE;
378 COMBINE_STATE;
379 conv->ostate = state;
380 return count;
381 } else {
382 int count = (state1 == STATE_JISX0201KATAKANA ? 1 : 4);
383 if (n < count)
384 return RET_TOOSMALL;
385 if (state1 != STATE_JISX0201KATAKANA) {
386 r[0] = ESC;
387 r[1] = '(';
388 r[2] = 'I';
389 r += 3;
390 state1 = STATE_JISX0201KATAKANA;
392 r[0] = buf[0]-0x80;
393 COMBINE_STATE;
394 conv->ostate = state;
395 return count;
399 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
400 ret = jisx0208_wctomb(conv,buf,wc,2);
401 if (ret != RET_ILSEQ) {
402 if (ret != 2) abort();
403 if (buf[0] < 0x80 && buf[1] < 0x80) {
404 int count = (state1 == STATE_JISX0208 ? 2 : 5);
405 if (n < count)
406 return RET_TOOSMALL;
407 if (state1 != STATE_JISX0208) {
408 r[0] = ESC;
409 r[1] = '$';
410 r[2] = 'B';
411 r += 3;
412 state1 = STATE_JISX0208;
414 r[0] = buf[0];
415 r[1] = buf[1];
416 COMBINE_STATE;
417 conv->ostate = state;
418 return count;
422 /* Try JIS X 0212-1990. */
423 ret = jisx0212_wctomb(conv,buf,wc,2);
424 if (ret != RET_ILSEQ) {
425 if (ret != 2) abort();
426 if (buf[0] < 0x80 && buf[1] < 0x80) {
427 int count = (state1 == STATE_JISX0212 ? 2 : 6);
428 if (n < count)
429 return RET_TOOSMALL;
430 if (state1 != STATE_JISX0212) {
431 r[0] = ESC;
432 r[1] = '$';
433 r[2] = '(';
434 r[3] = 'D';
435 r += 4;
436 state1 = STATE_JISX0212;
438 r[0] = buf[0];
439 r[1] = buf[1];
440 COMBINE_STATE;
441 conv->ostate = state;
442 return count;
446 /* Try GB 2312-1980. */
447 ret = gb2312_wctomb(conv,buf,wc,2);
448 if (ret != RET_ILSEQ) {
449 if (ret != 2) abort();
450 if (buf[0] < 0x80 && buf[1] < 0x80) {
451 int count = (state1 == STATE_GB2312 ? 2 : 5);
452 if (n < count)
453 return RET_TOOSMALL;
454 if (state1 != STATE_GB2312) {
455 r[0] = ESC;
456 r[1] = '$';
457 r[2] = 'A';
458 r += 3;
459 state1 = STATE_GB2312;
461 r[0] = buf[0];
462 r[1] = buf[1];
463 COMBINE_STATE;
464 conv->ostate = state;
465 return count;
469 /* Try KS C 5601-1992. */
470 ret = ksc5601_wctomb(conv,buf,wc,2);
471 if (ret != RET_ILSEQ) {
472 if (ret != 2) abort();
473 if (buf[0] < 0x80 && buf[1] < 0x80) {
474 int count = (state1 == STATE_KSC5601 ? 2 : 6);
475 if (n < count)
476 return RET_TOOSMALL;
477 if (state1 != STATE_KSC5601) {
478 r[0] = ESC;
479 r[1] = '$';
480 r[2] = '(';
481 r[3] = 'C';
482 r += 4;
483 state1 = STATE_KSC5601;
485 r[0] = buf[0];
486 r[1] = buf[1];
487 COMBINE_STATE;
488 conv->ostate = state;
489 return count;
493 return RET_ILSEQ;
496 static int
497 iso2022_jp2_reset (conv_t conv, unsigned char *r, int n)
499 state_t state = conv->ostate;
500 SPLIT_STATE;
501 (void)state2;
502 if (state1 != STATE_ASCII) {
503 if (n < 3)
504 return RET_TOOSMALL;
505 r[0] = ESC;
506 r[1] = '(';
507 r[2] = 'B';
508 /* conv->ostate = 0; will be done by the caller */
509 return 3;
510 } else
511 return 0;
514 #undef COMBINE_STATE
515 #undef SPLIT_STATE
516 #undef STATE_G2_ISO8859_7
517 #undef STATE_G2_ISO8859_1
518 #undef STATE_G2_NONE
519 #undef STATE_KSC5601
520 #undef STATE_GB2312
521 #undef STATE_JISX0212
522 #undef STATE_JISX0208
523 #undef STATE_JISX0201KATAKANA
524 #undef STATE_JISX0201ROMAN
525 #undef STATE_ASCII