| 1 |
/* |
| 2 |
* Copyright (C) 1999-2001, 2008, 2016 Free Software Foundation, Inc. |
| 3 |
* This file is part of the GNU LIBICONV Library. |
| 4 |
* |
| 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. |
| 9 |
* |
| 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. |
| 14 |
* |
| 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, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* ISO-2022-KR |
| 22 |
*/ |
| 23 |
|
| 24 |
/* Specification: RFC 1557 */ |
| 25 |
|
| 26 |
/* Note: CJK.INF says the SO designator needs to appear only once at the |
| 27 |
beginning of a text, but to decrease the risk of ambiguities, when |
| 28 |
producing ISO-2022-KR, we repeat the designator in every line containing |
| 29 |
SO characters. RFC 1557 does not mandate this. */ |
| 30 |
|
| 31 |
#define ESC 0x1b |
| 32 |
#define SO 0x0e |
| 33 |
#define SI 0x0f |
| 34 |
|
| 35 |
/* |
| 36 |
* The state is composed of one of the following values |
| 37 |
*/ |
| 38 |
#define STATE_ASCII 0 |
| 39 |
#define STATE_TWOBYTE 1 |
| 40 |
/* |
| 41 |
* and one of the following values, << 8 |
| 42 |
*/ |
| 43 |
#define STATE2_NONE 0 |
| 44 |
#define STATE2_DESIGNATED_KSC5601 1 |
| 45 |
|
| 46 |
#define SPLIT_STATE \ |
| 47 |
unsigned int state1 = state & 0xff, state2 = state >> 8 |
| 48 |
#define COMBINE_STATE \ |
| 49 |
state = (state2 << 8) | state1 |
| 50 |
|
| 51 |
static int |
| 52 |
iso2022_kr_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n) |
| 53 |
{ |
| 54 |
state_t state = conv->istate; |
| 55 |
SPLIT_STATE; |
| 56 |
int count = 0; |
| 57 |
unsigned char c; |
| 58 |
for (;;) { |
| 59 |
c = *s; |
| 60 |
if (c == ESC) { |
| 61 |
if (n < count+4) |
| 62 |
goto none; |
| 63 |
if (s[1] == '$') { |
| 64 |
if (s[2] == ')') { |
| 65 |
if (s[3] == 'C') { |
| 66 |
state2 = STATE2_DESIGNATED_KSC5601; |
| 67 |
s += 4; count += 4; |
| 68 |
if (n < count+1) |
| 69 |
goto none; |
| 70 |
continue; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
goto ilseq; |
| 75 |
} |
| 76 |
if (c == SO) { |
| 77 |
if (state2 != STATE2_DESIGNATED_KSC5601) |
| 78 |
goto ilseq; |
| 79 |
state1 = STATE_TWOBYTE; |
| 80 |
s++; count++; |
| 81 |
if (n < count+1) |
| 82 |
goto none; |
| 83 |
continue; |
| 84 |
} |
| 85 |
if (c == SI) { |
| 86 |
state1 = STATE_ASCII; |
| 87 |
s++; count++; |
| 88 |
if (n < count+1) |
| 89 |
goto none; |
| 90 |
continue; |
| 91 |
} |
| 92 |
break; |
| 93 |
} |
| 94 |
switch (state1) { |
| 95 |
case STATE_ASCII: |
| 96 |
if (c < 0x80) { |
| 97 |
int ret = ascii_mbtowc(conv,pwc,s,1); |
| 98 |
if (ret == RET_ILSEQ) |
| 99 |
goto ilseq; |
| 100 |
if (ret != 1) abort(); |
| 101 |
#if 0 /* Accept ISO-2022-KR according to CJK.INF. */ |
| 102 |
if (*pwc == 0x000a || *pwc == 0x000d) |
| 103 |
state2 = STATE2_NONE; |
| 104 |
#endif |
| 105 |
COMBINE_STATE; |
| 106 |
conv->istate = state; |
| 107 |
return count+1; |
| 108 |
} else |
| 109 |
goto ilseq; |
| 110 |
case STATE_TWOBYTE: |
| 111 |
if (n < count+2) |
| 112 |
goto none; |
| 113 |
if (state2 != STATE2_DESIGNATED_KSC5601) abort(); |
| 114 |
if (s[0] < 0x80 && s[1] < 0x80) { |
| 115 |
int ret = ksc5601_mbtowc(conv,pwc,s,2); |
| 116 |
if (ret == RET_ILSEQ) |
| 117 |
goto ilseq; |
| 118 |
if (ret != 2) abort(); |
| 119 |
COMBINE_STATE; |
| 120 |
conv->istate = state; |
| 121 |
return count+2; |
| 122 |
} else |
| 123 |
goto ilseq; |
| 124 |
default: abort(); |
| 125 |
} |
| 126 |
|
| 127 |
none: |
| 128 |
COMBINE_STATE; |
| 129 |
conv->istate = state; |
| 130 |
return RET_TOOFEW(count); |
| 131 |
|
| 132 |
ilseq: |
| 133 |
COMBINE_STATE; |
| 134 |
conv->istate = state; |
| 135 |
return RET_SHIFT_ILSEQ(count); |
| 136 |
} |
| 137 |
|
| 138 |
static int |
| 139 |
iso2022_kr_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n) |
| 140 |
{ |
| 141 |
state_t state = conv->ostate; |
| 142 |
SPLIT_STATE; |
| 143 |
unsigned char buf[2]; |
| 144 |
int ret; |
| 145 |
|
| 146 |
/* Try ASCII. */ |
| 147 |
ret = ascii_wctomb(conv,buf,wc,1); |
| 148 |
if (ret != RET_ILUNI) { |
| 149 |
if (ret != 1) abort(); |
| 150 |
if (buf[0] < 0x80) { |
| 151 |
int count = (state1 == STATE_ASCII ? 1 : 2); |
| 152 |
if (n < count) |
| 153 |
return RET_TOOSMALL; |
| 154 |
if (state1 != STATE_ASCII) { |
| 155 |
r[0] = SI; |
| 156 |
r += 1; |
| 157 |
state1 = STATE_ASCII; |
| 158 |
} |
| 159 |
r[0] = buf[0]; |
| 160 |
if (wc == 0x000a || wc == 0x000d) |
| 161 |
state2 = STATE2_NONE; |
| 162 |
COMBINE_STATE; |
| 163 |
conv->ostate = state; |
| 164 |
return count; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/* Try KS C 5601-1992. */ |
| 169 |
ret = ksc5601_wctomb(conv,buf,wc,2); |
| 170 |
if (ret != RET_ILUNI) { |
| 171 |
if (ret != 2) abort(); |
| 172 |
if (buf[0] < 0x80 && buf[1] < 0x80) { |
| 173 |
int count = (state2 == STATE2_DESIGNATED_KSC5601 ? 0 : 4) + (state1 == STATE_TWOBYTE ? 0 : 1) + 2; |
| 174 |
if (n < count) |
| 175 |
return RET_TOOSMALL; |
| 176 |
if (state2 != STATE2_DESIGNATED_KSC5601) { |
| 177 |
r[0] = ESC; |
| 178 |
r[1] = '$'; |
| 179 |
r[2] = ')'; |
| 180 |
r[3] = 'C'; |
| 181 |
r += 4; |
| 182 |
state2 = STATE2_DESIGNATED_KSC5601; |
| 183 |
} |
| 184 |
if (state1 != STATE_TWOBYTE) { |
| 185 |
r[0] = SO; |
| 186 |
r += 1; |
| 187 |
state1 = STATE_TWOBYTE; |
| 188 |
} |
| 189 |
r[0] = buf[0]; |
| 190 |
r[1] = buf[1]; |
| 191 |
COMBINE_STATE; |
| 192 |
conv->ostate = state; |
| 193 |
return count; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return RET_ILUNI; |
| 198 |
} |
| 199 |
|
| 200 |
static int |
| 201 |
iso2022_kr_reset (conv_t conv, unsigned char *r, size_t n) |
| 202 |
{ |
| 203 |
state_t state = conv->ostate; |
| 204 |
SPLIT_STATE; |
| 205 |
(void)state2; |
| 206 |
if (state1 != STATE_ASCII) { |
| 207 |
if (n < 1) |
| 208 |
return RET_TOOSMALL; |
| 209 |
r[0] = SI; |
| 210 |
/* conv->ostate = 0; will be done by the caller */ |
| 211 |
return 1; |
| 212 |
} else |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
#undef COMBINE_STATE |
| 217 |
#undef SPLIT_STATE |
| 218 |
#undef STATE2_DESIGNATED_KSC5601 |
| 219 |
#undef STATE2_NONE |
| 220 |
#undef STATE_TWOBYTE |
| 221 |
#undef STATE_ASCII |