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.
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
);
121 iso2022_jp_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
123 state_t state
= conv
->ostate
;
124 unsigned char buf
[2];
128 ret
= ascii_wctomb(conv
,buf
,wc
,1);
129 if (ret
!= RET_ILSEQ
) {
130 if (ret
!= 1) abort();
132 int count
= (state
== STATE_ASCII
? 1 : 4);
135 if (state
!= STATE_ASCII
) {
143 conv
->ostate
= state
;
148 /* Try JIS X 0201-1976 Roman. */
149 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
150 if (ret
!= RET_ILSEQ
) {
151 if (ret
!= 1) abort();
153 int count
= (state
== STATE_JISX0201ROMAN
? 1 : 4);
156 if (state
!= STATE_JISX0201ROMAN
) {
161 state
= STATE_JISX0201ROMAN
;
164 conv
->ostate
= state
;
169 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
170 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
171 if (ret
!= RET_ILSEQ
) {
172 if (ret
!= 2) abort();
173 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
174 int count
= (state
== STATE_JISX0208
? 2 : 5);
177 if (state
!= STATE_JISX0208
) {
182 state
= STATE_JISX0208
;
186 conv
->ostate
= state
;
195 iso2022_jp_reset (conv_t conv
, unsigned char *r
, int n
)
197 state_t state
= conv
->ostate
;
198 if (state
!= STATE_ASCII
) {
204 /* conv->ostate = 0; will be done by the caller */
210 #undef STATE_JISX0208
211 #undef STATE_JISX0201ROMAN