Add comments: glibc now implements the X11 encoding names.
[libiconv.git] / lib / iso2022_jp1.h
blob292a85b84c7bfa25674e68021245e8591198544c
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-1
25 /* Specification: RFC 2237 */
27 #define ESC 0x1b
30 * The state can be one of the following values.
32 #define STATE_ASCII 0
33 #define STATE_JISX0201ROMAN 1
34 #define STATE_JISX0208 2
35 #define STATE_JISX0212 3
37 static int
38 iso2022_jp1_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
40 state_t state = conv->istate;
41 int count = 0;
42 unsigned char c;
43 for (;;) {
44 c = *s;
45 if (c == ESC) {
46 if (n < count+3)
47 goto none;
48 if (s[1] == '(') {
49 if (s[2] == 'B') {
50 state = STATE_ASCII;
51 s += 3; count += 3;
52 if (n < count+1)
53 goto none;
54 continue;
56 if (s[2] == 'J') {
57 state = STATE_JISX0201ROMAN;
58 s += 3; count += 3;
59 if (n < count+1)
60 goto none;
61 continue;
63 return RET_ILSEQ;
65 if (s[1] == '$') {
66 if (s[2] == '@' || s[2] == 'B') {
67 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
68 state = STATE_JISX0208;
69 s += 3; count += 3;
70 if (n < count+1)
71 goto none;
72 continue;
74 if (s[2] == '(') {
75 if (n < count+4)
76 goto none;
77 if (s[3] == 'D') {
78 state = STATE_JISX0212;
79 s += 4; count += 4;
80 if (n < count+1)
81 goto none;
82 continue;
85 return RET_ILSEQ;
87 return RET_ILSEQ;
89 break;
91 switch (state) {
92 case STATE_ASCII:
93 if (c < 0x80) {
94 int ret = ascii_mbtowc(conv,pwc,s,1);
95 if (ret == RET_ILSEQ)
96 return RET_ILSEQ;
97 if (ret != 1) abort();
98 conv->istate = state;
99 return count+1;
100 } else
101 return RET_ILSEQ;
102 case STATE_JISX0201ROMAN:
103 if (c < 0x80) {
104 int ret = jisx0201_mbtowc(conv,pwc,s,1);
105 if (ret == RET_ILSEQ)
106 return RET_ILSEQ;
107 if (ret != 1) abort();
108 conv->istate = state;
109 return count+1;
110 } else
111 return RET_ILSEQ;
112 case STATE_JISX0208:
113 if (n < count+2)
114 goto none;
115 if (s[0] < 0x80 && s[1] < 0x80) {
116 int ret = jisx0208_mbtowc(conv,pwc,s,2);
117 if (ret == RET_ILSEQ)
118 return RET_ILSEQ;
119 if (ret != 2) abort();
120 conv->istate = state;
121 return count+2;
122 } else
123 return RET_ILSEQ;
124 case STATE_JISX0212:
125 if (n < count+2)
126 goto none;
127 if (s[0] < 0x80 && s[1] < 0x80) {
128 int ret = jisx0212_mbtowc(conv,pwc,s,2);
129 if (ret == RET_ILSEQ)
130 return RET_ILSEQ;
131 if (ret != 2) abort();
132 conv->istate = state;
133 return count+2;
134 } else
135 return RET_ILSEQ;
136 default: abort();
139 none:
140 conv->istate = state;
141 return RET_TOOFEW(count);
144 static int
145 iso2022_jp1_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
147 state_t state = conv->ostate;
148 unsigned char buf[2];
149 int ret;
151 /* Try ASCII. */
152 ret = ascii_wctomb(conv,buf,wc,1);
153 if (ret != RET_ILSEQ) {
154 if (ret != 1) abort();
155 if (buf[0] < 0x80) {
156 int count = (state == STATE_ASCII ? 1 : 4);
157 if (n < count)
158 return RET_TOOSMALL;
159 if (state != STATE_ASCII) {
160 r[0] = ESC;
161 r[1] = '(';
162 r[2] = 'B';
163 r += 3;
164 state = STATE_ASCII;
166 r[0] = buf[0];
167 conv->ostate = state;
168 return count;
172 /* Try JIS X 0201-1976 Roman. */
173 ret = jisx0201_wctomb(conv,buf,wc,1);
174 if (ret != RET_ILSEQ) {
175 if (ret != 1) abort();
176 if (buf[0] < 0x80) {
177 int count = (state == STATE_JISX0201ROMAN ? 1 : 4);
178 if (n < count)
179 return RET_TOOSMALL;
180 if (state != STATE_JISX0201ROMAN) {
181 r[0] = ESC;
182 r[1] = '(';
183 r[2] = 'J';
184 r += 3;
185 state = STATE_JISX0201ROMAN;
187 r[0] = buf[0];
188 conv->ostate = state;
189 return count;
193 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
194 ret = jisx0208_wctomb(conv,buf,wc,2);
195 if (ret != RET_ILSEQ) {
196 if (ret != 2) abort();
197 if (buf[0] < 0x80 && buf[1] < 0x80) {
198 int count = (state == STATE_JISX0208 ? 2 : 5);
199 if (n < count)
200 return RET_TOOSMALL;
201 if (state != STATE_JISX0208) {
202 r[0] = ESC;
203 r[1] = '$';
204 r[2] = 'B';
205 r += 3;
206 state = STATE_JISX0208;
208 r[0] = buf[0];
209 r[1] = buf[1];
210 conv->ostate = state;
211 return count;
215 /* Try JIS X 0212-1990. */
216 ret = jisx0212_wctomb(conv,buf,wc,2);
217 if (ret != RET_ILSEQ) {
218 if (ret != 2) abort();
219 if (buf[0] < 0x80 && buf[1] < 0x80) {
220 int count = (state == STATE_JISX0212 ? 2 : 6);
221 if (n < count)
222 return RET_TOOSMALL;
223 if (state != STATE_JISX0212) {
224 r[0] = ESC;
225 r[1] = '$';
226 r[2] = '(';
227 r[3] = 'D';
228 r += 4;
229 state = STATE_JISX0212;
231 r[0] = buf[0];
232 r[1] = buf[1];
233 conv->ostate = state;
234 return count;
238 return RET_ILSEQ;
241 static int
242 iso2022_jp1_reset (conv_t conv, unsigned char *r, int n)
244 state_t state = conv->ostate;
245 if (state != STATE_ASCII) {
246 if (n < 3)
247 return RET_TOOSMALL;
248 r[0] = ESC;
249 r[1] = '(';
250 r[2] = 'B';
251 /* conv->ostate = 0; will be done by the caller */
252 return 3;
253 } else
254 return 0;
257 #undef STATE_JISX0212
258 #undef STATE_JISX0208
259 #undef STATE_JISX0201ROMAN
260 #undef STATE_ASCII