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 1468 */
30 * The state can be one of the following values.
33 #define STATE_JISX0201ROMAN 1
34 #define STATE_JISX0208 2
37 iso2022_jp_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
;
82 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
85 if (ret
!= 1) abort();
90 case STATE_JISX0201ROMAN
:
92 int ret
= jisx0201_mbtowc(conv
,pwc
,s
,1);
95 if (ret
!= 1) abort();
103 if (s
[0] < 0x80 && s
[1] < 0x80) {
104 int ret
= jisx0208_mbtowc(conv
,pwc
,s
,2);
105 if (ret
== RET_ILSEQ
)
107 if (ret
!= 2) abort();
108 conv
->istate
= state
;
116 conv
->istate
= state
;
117 return RET_TOOFEW(count
);
120 conv
->istate
= state
;
121 return RET_SHIFT_ILSEQ(count
);
125 iso2022_jp_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
127 state_t state
= conv
->ostate
;
128 unsigned char buf
[2];
132 ret
= ascii_wctomb(conv
,buf
,wc
,1);
133 if (ret
!= RET_ILUNI
) {
134 if (ret
!= 1) abort();
136 int count
= (state
== STATE_ASCII
? 1 : 4);
139 if (state
!= STATE_ASCII
) {
147 conv
->ostate
= state
;
152 /* Try JIS X 0201-1976 Roman. */
153 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
154 if (ret
!= RET_ILUNI
) {
155 if (ret
!= 1) abort();
157 int count
= (state
== STATE_JISX0201ROMAN
? 1 : 4);
160 if (state
!= STATE_JISX0201ROMAN
) {
165 state
= STATE_JISX0201ROMAN
;
168 conv
->ostate
= state
;
173 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
174 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
175 if (ret
!= RET_ILUNI
) {
176 if (ret
!= 2) abort();
177 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
178 int count
= (state
== STATE_JISX0208
? 2 : 5);
181 if (state
!= STATE_JISX0208
) {
186 state
= STATE_JISX0208
;
190 conv
->ostate
= state
;
199 iso2022_jp_reset (conv_t conv
, unsigned char *r
, int n
)
201 state_t state
= conv
->ostate
;
202 if (state
!= STATE_ASCII
) {
208 /* conv->ostate = 0; will be done by the caller */
214 #undef STATE_JISX0208
215 #undef STATE_JISX0201ROMAN