2 * Copyright (C) 1999-2001 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 1557 */
27 /* Note: CJK.INF says the SO designator needs to appear only once at the
28 beginning of a text, but to decrease the risk of ambiguities, when
29 producing ISO-2022-KR, we repeat the designator in every line containing
30 SO characters. RFC 1557 does not mandate this. */
37 * The state is composed of one of the following values
40 #define STATE_TWOBYTE 1
42 * and one of the following values, << 8
45 #define STATE2_DESIGNATED_KSC5601 1
48 unsigned int state1 = state & 0xff, state2 = state >> 8
49 #define COMBINE_STATE \
50 state = (state2 << 8) | state1
53 iso2022_kr_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
55 state_t state = conv->istate;
67 state2 = STATE2_DESIGNATED_KSC5601;
78 if (state2 != STATE2_DESIGNATED_KSC5601)
80 state1 = STATE_TWOBYTE;
98 int ret = ascii_mbtowc(conv,pwc,s,1);
101 if (ret != 1) abort();
102 #if 0 /* Accept ISO-2022-KR according to CJK.INF. */
103 if (*pwc == 0x000a || *pwc == 0x000d)
104 state2 = STATE2_NONE;
107 conv->istate = state;
114 if (state2 != STATE2_DESIGNATED_KSC5601) abort();
115 if (s[0] < 0x80 && s[1] < 0x80) {
116 int ret = ksc5601_mbtowc(conv,pwc,s,2);
117 if (ret == RET_ILSEQ)
119 if (ret != 2) abort();
121 conv->istate = state;
130 conv->istate = state;
131 return RET_TOOFEW(count);
135 iso2022_kr_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
137 state_t state = conv->ostate;
139 unsigned char buf[2];
143 ret = ascii_wctomb(conv,buf,wc,1);
144 if (ret != RET_ILUNI) {
145 if (ret != 1) abort();
147 int count = (state1 == STATE_ASCII ? 1 : 2);
150 if (state1 != STATE_ASCII) {
153 state1 = STATE_ASCII;
156 if (wc == 0x000a || wc == 0x000d)
157 state2 = STATE2_NONE;
159 conv->ostate = state;
164 /* Try KS C 5601-1992. */
165 ret = ksc5601_wctomb(conv,buf,wc,2);
166 if (ret != RET_ILUNI) {
167 if (ret != 2) abort();
168 if (buf[0] < 0x80 && buf[1] < 0x80) {
169 int count = (state2 == STATE2_DESIGNATED_KSC5601 ? 0 : 4) + (state1 == STATE_TWOBYTE ? 0 : 1) + 2;
172 if (state2 != STATE2_DESIGNATED_KSC5601) {
178 state2 = STATE2_DESIGNATED_KSC5601;
180 if (state1 != STATE_TWOBYTE) {
183 state1 = STATE_TWOBYTE;
188 conv->ostate = state;
197 iso2022_kr_reset (conv_t conv, unsigned char *r, int n)
199 state_t state = conv->ostate;
202 if (state1 != STATE_ASCII) {
206 /* conv->ostate = 0; will be done by the caller */
214 #undef STATE2_DESIGNATED_KSC5601