2 * Copyright (C) 1999-2001, 2008 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 /* Specification: RFC 2237 */
29 * The state can be one of the following values.
32 #define STATE_JISX0201ROMAN 1
33 #define STATE_JISX0208 2
34 #define STATE_JISX0212 3
37 iso2022_jp1_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, int n
)
39 state_t state
= conv
->istate
;
56 state
= STATE_JISX0201ROMAN
;
65 if (s
[2] == '@' || s
[2] == 'B') {
66 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
67 state
= STATE_JISX0208
;
77 state
= STATE_JISX0212
;
93 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
96 if (ret
!= 1) abort();
101 case STATE_JISX0201ROMAN
:
103 int ret
= jisx0201_mbtowc(conv
,pwc
,s
,1);
104 if (ret
== RET_ILSEQ
)
106 if (ret
!= 1) abort();
107 conv
->istate
= state
;
114 if (s
[0] < 0x80 && s
[1] < 0x80) {
115 int ret
= jisx0208_mbtowc(conv
,pwc
,s
,2);
116 if (ret
== RET_ILSEQ
)
118 if (ret
!= 2) abort();
119 conv
->istate
= state
;
126 if (s
[0] < 0x80 && s
[1] < 0x80) {
127 int ret
= jisx0212_mbtowc(conv
,pwc
,s
,2);
128 if (ret
== RET_ILSEQ
)
130 if (ret
!= 2) abort();
131 conv
->istate
= state
;
139 conv
->istate
= state
;
140 return RET_TOOFEW(count
);
143 conv
->istate
= state
;
144 return RET_SHIFT_ILSEQ(count
);
148 iso2022_jp1_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
150 state_t state
= conv
->ostate
;
151 unsigned char buf
[2];
155 ret
= ascii_wctomb(conv
,buf
,wc
,1);
156 if (ret
!= RET_ILUNI
) {
157 if (ret
!= 1) abort();
159 int count
= (state
== STATE_ASCII
? 1 : 4);
162 if (state
!= STATE_ASCII
) {
170 conv
->ostate
= state
;
175 /* Try JIS X 0201-1976 Roman. */
176 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
177 if (ret
!= RET_ILUNI
) {
178 if (ret
!= 1) abort();
180 int count
= (state
== STATE_JISX0201ROMAN
? 1 : 4);
183 if (state
!= STATE_JISX0201ROMAN
) {
188 state
= STATE_JISX0201ROMAN
;
191 conv
->ostate
= state
;
196 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
197 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
198 if (ret
!= RET_ILUNI
) {
199 if (ret
!= 2) abort();
200 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
201 int count
= (state
== STATE_JISX0208
? 2 : 5);
204 if (state
!= STATE_JISX0208
) {
209 state
= STATE_JISX0208
;
213 conv
->ostate
= state
;
218 /* Try JIS X 0212-1990. */
219 ret
= jisx0212_wctomb(conv
,buf
,wc
,2);
220 if (ret
!= RET_ILUNI
) {
221 if (ret
!= 2) abort();
222 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
223 int count
= (state
== STATE_JISX0212
? 2 : 6);
226 if (state
!= STATE_JISX0212
) {
232 state
= STATE_JISX0212
;
236 conv
->ostate
= state
;
245 iso2022_jp1_reset (conv_t conv
, unsigned char *r
, int n
)
247 state_t state
= conv
->ostate
;
248 if (state
!= STATE_ASCII
) {
254 /* conv->ostate = 0; will be done by the caller */
260 #undef STATE_JISX0212
261 #undef STATE_JISX0208
262 #undef STATE_JISX0201ROMAN