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 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
);
145 iso2022_jp1_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, int n
)
147 state_t state
= conv
->ostate
;
148 unsigned char buf
[2];
152 ret
= ascii_wctomb(conv
,buf
,wc
,1);
153 if (ret
!= RET_ILSEQ
) {
154 if (ret
!= 1) abort();
156 int count
= (state
== STATE_ASCII
? 1 : 4);
159 if (state
!= STATE_ASCII
) {
167 conv
->ostate
= state
;
172 /* Try JIS X 0201-1976 Roman. */
173 ret
= jisx0201_wctomb(conv
,buf
,wc
,1);
174 if (ret
!= RET_ILSEQ
) {
175 if (ret
!= 1) abort();
177 int count
= (state
== STATE_JISX0201ROMAN
? 1 : 4);
180 if (state
!= STATE_JISX0201ROMAN
) {
185 state
= STATE_JISX0201ROMAN
;
188 conv
->ostate
= state
;
193 /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
194 ret
= jisx0208_wctomb(conv
,buf
,wc
,2);
195 if (ret
!= RET_ILSEQ
) {
196 if (ret
!= 2) abort();
197 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
198 int count
= (state
== STATE_JISX0208
? 2 : 5);
201 if (state
!= STATE_JISX0208
) {
206 state
= STATE_JISX0208
;
210 conv
->ostate
= state
;
215 /* Try JIS X 0212-1990. */
216 ret
= jisx0212_wctomb(conv
,buf
,wc
,2);
217 if (ret
!= RET_ILSEQ
) {
218 if (ret
!= 2) abort();
219 if (buf
[0] < 0x80 && buf
[1] < 0x80) {
220 int count
= (state
== STATE_JISX0212
? 2 : 6);
223 if (state
!= STATE_JISX0212
) {
229 state
= STATE_JISX0212
;
233 conv
->ostate
= state
;
242 iso2022_jp1_reset (conv_t conv
, unsigned char *r
, int n
)
244 state_t state
= conv
->ostate
;
245 if (state
!= STATE_ASCII
) {
251 /* conv->ostate = 0; will be done by the caller */
257 #undef STATE_JISX0212
258 #undef STATE_JISX0208
259 #undef STATE_JISX0201ROMAN