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, write to the Free Software Foundation, Inc., 51 Franklin Street,
18 * Fifth Floor, Boston, MA 02110-1301, USA.
25 /* Specification: RFC 2237 */
30 * The state can be one of the following values.
33 #define STATE_JISX0201ROMAN 1
34 #define STATE_JISX0208 2
35 #define STATE_JISX0212 3
38 iso2022_jp1_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, int n
)
40 state_t state
= conv
->istate
;
57 state
= STATE_JISX0201ROMAN
;
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
;
78 state
= STATE_JISX0212
;
94 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
97 if (ret
!= 1) abort();
102 case STATE_JISX0201ROMAN
:
104 int ret
= jisx0201_mbtowc(conv
,pwc
,s
,1);
105 if (ret
== RET_ILSEQ
)
107 if (ret
!= 1) abort();
108 conv
->istate
= state
;
115 if (s
[0] < 0x80 && s
[1] < 0x80) {
116 int ret
= jisx0208_mbtowc(conv
,pwc
,s
,2);
117 if (ret
== RET_ILSEQ
)
119 if (ret
!= 2) abort();
120 conv
->istate
= state
;
127 if (s
[0] < 0x80 && s
[1] < 0x80) {
128 int ret
= jisx0212_mbtowc(conv
,pwc
,s
,2);
129 if (ret
== RET_ILSEQ
)
131 if (ret
!= 2) abort();
132 conv
->istate
= state
;
140 conv
->istate
= state
;
141 return RET_TOOFEW(count
);
144 conv
->istate
= state
;
145 return RET_SHIFT_ILSEQ(count
);
149 iso2022_jp1_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
151 state_t state
= conv
->ostate
;
152 unsigned char buf
[2];
156 ret
= ascii_wctomb(conv
,buf
,wc
,1);
157 if (ret
!= RET_ILUNI
) {
158 if (ret
!= 1) abort();
160 int count
= (state
== STATE_ASCII
? 1 : 4);
163 if (state
!= STATE_ASCII
) {
171 conv
->ostate
= state
;
176 /* Try JIS X 0201-1976 Roman. */
177 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
178 if (ret
!= RET_ILUNI
) {
179 if (ret
!= 1) abort();
181 int count
= (state
== STATE_JISX0201ROMAN
? 1 : 4);
184 if (state
!= STATE_JISX0201ROMAN
) {
189 state
= STATE_JISX0201ROMAN
;
192 conv
->ostate
= state
;
197 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
198 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
199 if (ret
!= RET_ILUNI
) {
200 if (ret
!= 2) abort();
201 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
202 int count
= (state
== STATE_JISX0208
? 2 : 5);
205 if (state
!= STATE_JISX0208
) {
210 state
= STATE_JISX0208
;
214 conv
->ostate
= state
;
219 /* Try JIS X 0212-1990. */
220 ret
= jisx0212_wctomb(conv
,buf
,wc
,2);
221 if (ret
!= RET_ILUNI
) {
222 if (ret
!= 2) abort();
223 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
224 int count
= (state
== STATE_JISX0212
? 2 : 6);
227 if (state
!= STATE_JISX0212
) {
233 state
= STATE_JISX0212
;
237 conv
->ostate
= state
;
246 iso2022_jp1_reset (conv_t conv
, unsigned char *r
, int n
)
248 state_t state
= conv
->ostate
;
249 if (state
!= STATE_ASCII
) {
255 /* conv->ostate = 0; will be done by the caller */
261 #undef STATE_JISX0212
262 #undef STATE_JISX0208
263 #undef STATE_JISX0201ROMAN