2 * Copyright (C) 1999-2001, 2008, 2016 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 Lesser General Public
7 * License as published by the Free Software Foundation; either version 2.1
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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17 * If not, see <https://www.gnu.org/licenses/>.
24 /* Specification: RFC 1468 */
29 * The state can be one of the following values.
32 #define STATE_JISX0201ROMAN 1
33 #define STATE_JISX0208 2
36 iso2022_jp_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
38 state_t state
= conv
->istate
;
55 state
= STATE_JISX0201ROMAN
;
64 if (s
[2] == '@' || s
[2] == 'B') {
65 /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
66 state
= STATE_JISX0208
;
81 int ret
= ascii_mbtowc(conv
,pwc
,s
,1);
84 if (ret
!= 1) abort();
89 case STATE_JISX0201ROMAN
:
91 int ret
= jisx0201_mbtowc(conv
,pwc
,s
,1);
94 if (ret
!= 1) abort();
102 if (s
[0] < 0x80 && s
[1] < 0x80) {
103 int ret
= jisx0208_mbtowc(conv
,pwc
,s
,2);
104 if (ret
== RET_ILSEQ
)
106 if (ret
!= 2) abort();
107 conv
->istate
= state
;
115 conv
->istate
= state
;
116 return RET_TOOFEW(count
);
119 conv
->istate
= state
;
120 return RET_SHIFT_ILSEQ(count
);
124 iso2022_jp_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, size_t n
)
126 state_t state
= conv
->ostate
;
127 unsigned char buf
[2];
131 ret
= ascii_wctomb(conv
,buf
,wc
,1);
132 if (ret
!= RET_ILUNI
) {
133 if (ret
!= 1) abort();
135 int count
= (state
== STATE_ASCII
? 1 : 4);
138 if (state
!= STATE_ASCII
) {
146 conv
->ostate
= state
;
151 /* Try JIS X 0201-1976 Roman. */
152 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
153 if (ret
!= RET_ILUNI
) {
154 if (ret
!= 1) abort();
156 int count
= (state
== STATE_JISX0201ROMAN
? 1 : 4);
159 if (state
!= STATE_JISX0201ROMAN
) {
164 state
= STATE_JISX0201ROMAN
;
167 conv
->ostate
= state
;
172 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
173 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
174 if (ret
!= RET_ILUNI
) {
175 if (ret
!= 2) abort();
176 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
177 int count
= (state
== STATE_JISX0208
? 2 : 5);
180 if (state
!= STATE_JISX0208
) {
185 state
= STATE_JISX0208
;
189 conv
->ostate
= state
;
198 iso2022_jp_reset (conv_t conv
, unsigned char *r
, size_t n
)
200 state_t state
= conv
->ostate
;
201 if (state
!= STATE_ASCII
) {
207 /* conv->ostate = 0; will be done by the caller */
213 #undef STATE_JISX0208
214 #undef STATE_JISX0201ROMAN