5 // Hye-Shik Chang (perky@FreeBSD.org)
6 // Atsushi Enomoto <atsushi@ximian.com>
16 internal class CP949 : KoreanEncoding
18 // Magic number used by Windows for the UHC code page.
19 private const int UHC_CODE_PAGE = 949;
22 public CP949 () : base (UHC_CODE_PAGE, true)
26 // Get the mail body name for this encoding.
27 public override String BodyName
29 get { return "ks_c_5601-1987"; }
32 // Get the human-readable name for this encoding.
33 public override String EncodingName
35 get { return "Korean (UHC)"; }
38 // Get the mail agent header name for this encoding.
39 public override String HeaderName
41 get { return "ks_c_5601-1987"; }
44 // Get the IANA-preferred Web name for this encoding.
45 public override String WebName
47 get { return "ks_c_5601-1987"; }
51 // Get the Windows code page represented by this object.
52 public override int WindowsCodePage
54 get { return UHC_PAGE; }
60 internal class CP51949 : KoreanEncoding
62 // Magic number used by Windows for the euc-kr code page.
63 private const int EUCKR_CODE_PAGE = 51949;
66 public CP51949 () : base (EUCKR_CODE_PAGE, false)
70 // Get the mail body name for this encoding.
71 public override String BodyName
73 get { return "euc-kr"; }
76 // Get the human-readable name for this encoding.
77 public override String EncodingName
79 get { return "Korean (EUC)"; }
82 // Get the mail agent header name for this encoding.
83 public override String HeaderName
85 get { return "euc-kr"; }
88 // Get the IANA-preferred Web name for this encoding.
89 public override String WebName
91 get { return "euc-kr"; }
95 // Get the Windows code page represented by this object.
96 public override int WindowsCodePage
98 get { return UHC_PAGE; }
105 internal class KoreanEncoding : DbcsEncoding
108 public KoreanEncoding (int codepage, bool useUHC)
109 : base (codepage, 949) {
110 this.useUHC = useUHC;
113 internal override DbcsConvert GetConvert ()
115 return DbcsConvert.KS;
121 // Get the bytes that result from encoding a character buffer.
122 public unsafe override int GetByteCountImpl (char* chars, int count)
127 DbcsConvert convert = GetConvert ();
130 for (int i = 0; i < end; i++, charCount--) {
132 if (c <= 0x80 || c == 0xFF) { // ASCII
136 byte b1 = convert.u2n[((int)c) * 2];
137 byte b2 = convert.u2n[((int)c) * 2 + 1];
138 if (b1 == 0 && b2 == 0) {
139 // FIXME: handle fallback for GetByteCountImpl().
148 // Get the bytes that result from encoding a character buffer.
149 public unsafe override int GetBytesImpl (char* chars, int charCount,
150 byte* bytes, int byteCount)
155 DbcsConvert convert = GetConvert ();
156 EncoderFallbackBuffer buffer = null;
159 int origIndex = byteIndex;
160 for (int = charIndex; i < end; i++, charCount--) {
162 if (c <= 0x80 || c == 0xFF) { // ASCII
163 bytes[byteIndex++] = (byte)c;
166 byte b1 = convert.u2n[((int)c) * 2];
167 byte b2 = convert.u2n[((int)c) * 2 + 1];
168 if (b1 == 0 && b2 == 0) {
169 HandleFallback (ref buffer, chars, ref i, ref charCount,
170 bytes, ref byteIndex, ref byteCount, null);
172 bytes[byteIndex++] = b1;
173 bytes[byteIndex++] = b2;
176 return byteIndex - origIndex;
179 // Get the bytes that result from encoding a character buffer.
180 public override int GetByteCount(char[] chars, int index, int count)
183 DbcsConvert convert = GetConvert();
188 char c = chars[index++];
189 if (c <= 0x80 || c == 0xFF)
194 byte b1 = convert.u2n[((int)c) * 2];
195 byte b2 = convert.u2n[((int)c) * 2 + 1];
196 if (b1 == 0 && b2 == 0)
198 // FIXME: handle fallback for GetByteCountImpl().
207 // Get the bytes that result from encoding a character buffer.
208 public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
210 int byteCount = bytes.Length;
211 int end = charIndex + charCount;
213 DbcsConvert convert = GetConvert();
214 EncoderFallbackBuffer buffer = null;
217 int origIndex = byteIndex;
218 for (int i = charIndex; i < end; i++, charCount--)
221 if (c <= 0x80 || c == 0xFF)
223 bytes[byteIndex++] = (byte)c;
226 byte b1 = convert.u2n[((int)c) * 2];
227 byte b2 = convert.u2n[((int)c) * 2 + 1];
228 if (b1 == 0 && b2 == 0)
230 HandleFallback (ref buffer, chars, ref i, ref charCount,
231 bytes, ref byteIndex, ref byteCount, null);
235 bytes[byteIndex++] = b1;
236 bytes[byteIndex++] = b2;
239 return byteIndex - origIndex;
242 // Get the characters that result from decoding a byte buffer.
243 public override int GetCharCount (byte[] bytes, int index, int count)
245 return GetDecoder ().GetCharCount (bytes, index, count);
248 // Get the characters that result from decoding a byte buffer.
249 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
250 char[] chars, int charIndex)
252 return GetDecoder ().GetChars (bytes, byteIndex, byteCount, chars, charIndex);
255 // Get a decoder that handles a rolling UHC state.
256 public override Decoder GetDecoder()
258 return new KoreanDecoder (GetConvert (), useUHC);
261 // Decoder that handles a rolling UHC state.
262 private sealed class KoreanDecoder : DbcsDecoder
265 public KoreanDecoder (DbcsConvert convert, bool useUHC)
268 this.useUHC = useUHC;
271 int last_byte_count, last_byte_conv;
273 public override int GetCharCount (byte[] bytes, int index, int count)
275 return GetCharCount (bytes, index, count, false);
279 int GetCharCount (byte [] bytes, int index, int count, bool refresh)
281 CheckRange (bytes, index, count);
283 int lastByte = last_byte_count;
286 while (count-- > 0) {
287 int b = bytes[index++];
289 if (b <= 0x80 || b == 0xFF) { // ASCII
299 if (useUHC && lastByte < 0xa1) { // UHC Level 1
300 int ord = 8836 + (lastByte - 0x81) * 178;
302 if (b >= 0x41 && b <= 0x5A)
304 else if (b >= 0x61 && b <= 0x7A)
305 ord += b - 0x61 + 26;
306 else if (b >= 0x81 && b <= 0xFE)
307 ord += b - 0x81 + 52;
311 if (ord >= 0 && ord * 2 <= convert.n2u.Length)
312 c1 = (char)(convert.n2u[ord*2] +
313 convert.n2u[ord*2 + 1] * 256);
316 } else if (useUHC && lastByte <= 0xC6 && b < 0xA1) { // UHC Level 2
317 int ord = 14532 + (lastByte - 0xA1) * 84;
319 if (b >= 0x41 && b <= 0x5A)
321 else if (b >= 0x61 && b <= 0x7A)
322 ord += b - 0x61 + 26;
323 else if (b >= 0x81 && b <= 0xA0)
324 ord += b - 0x81 + 52;
328 if (ord >= 0 && ord * 2 <= convert.n2u.Length)
329 c1 = (char)(convert.n2u[ord*2] +
330 convert.n2u[ord*2 + 1] * 256);
333 } else if (b >= 0xA1 && b <= 0xFE) { // KS X 1001
334 int ord = ((lastByte - 0xA1) * 94 + b - 0xA1) * 2;
336 c1 = ord < 0 || ord >= convert.n2u.Length ?
337 '\0' : (char)(convert.n2u[ord] +
338 convert.n2u[ord + 1] * 256);
357 last_byte_count = lastByte;
362 public override int GetChars(byte[] bytes, int byteIndex,
363 int byteCount, char[] chars, int charIndex)
365 return GetChars (bytes, byteIndex, byteCount, chars, charIndex, false);
369 int GetChars(byte[] bytes, int byteIndex,
370 int byteCount, char[] chars, int charIndex, bool refresh)
372 CheckRange (bytes, byteIndex, byteCount, chars, charIndex);
373 int origIndex = charIndex;
374 int lastByte = last_byte_conv;
376 while (byteCount-- > 0) {
377 int b = bytes[byteIndex++];
379 if (b <= 0x80 || b == 0xFF) { // ASCII
380 chars[charIndex++] = (char)b;
389 if (useUHC && lastByte < 0xa1) { // UHC Level 1
390 int ord = 8836 + (lastByte - 0x81) * 178;
392 if (b >= 0x41 && b <= 0x5A)
394 else if (b >= 0x61 && b <= 0x7A)
395 ord += b - 0x61 + 26;
396 else if (b >= 0x81 && b <= 0xFE)
397 ord += b - 0x81 + 52;
401 if (ord >= 0 && ord * 2 <= convert.n2u.Length)
402 c1 = (char)(convert.n2u[ord*2] +
403 convert.n2u[ord*2 + 1] * 256);
406 } else if (useUHC && lastByte <= 0xC6 && b < 0xA1) { // UHC Level 2
407 int ord = 14532 + (lastByte - 0xA1) * 84;
409 if (b >= 0x41 && b <= 0x5A)
411 else if (b >= 0x61 && b <= 0x7A)
412 ord += b - 0x61 + 26;
413 else if (b >= 0x81 && b <= 0xA0)
414 ord += b - 0x81 + 52;
418 if (ord >= 0 && ord * 2 <= convert.n2u.Length)
419 c1 = (char)(convert.n2u[ord*2] +
420 convert.n2u[ord*2 + 1] * 256);
423 } else if (b >= 0xA1 && b <= 0xFE) { // KS X 1001
424 int ord = ((lastByte - 0xA1) * 94 + b - 0xA1) * 2;
426 c1 = ord < 0 || ord >= convert.n2u.Length ?
427 '\0' : (char)(convert.n2u[ord] +
428 convert.n2u[ord + 1] * 256);
433 chars[charIndex++] = '?';
435 chars[charIndex++] = c1;
441 chars[charIndex++] = '?';
445 last_byte_conv = lastByte;
447 return charIndex - origIndex;
453 internal class ENCuhc : CP949
459 internal class ENCeuc_kr: CP51949
461 public ENCeuc_kr() {}