| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
using System; |
| 8 |
using System.Reflection; |
| 9 |
using System.Text; |
| 10 |
using I18N.Common; |
| 11 |
|
| 12 |
#if DISABLE_UNSAFE |
| 13 |
using MonoEncoder = I18N.Common.MonoSafeEncoder; |
| 14 |
using MonoEncoding = I18N.Common.MonoSafeEncoding; |
| 15 |
#endif |
| 16 |
|
| 17 |
namespace I18N.CJK |
| 18 |
{ |
| 19 |
[Serializable] |
| 20 |
internal class ENCgb18030 : GB18030Encoding |
| 21 |
{ |
| 22 |
public ENCgb18030 (): base () {} |
| 23 |
} |
| 24 |
|
| 25 |
[Serializable] |
| 26 |
public class CP54936 : GB18030Encoding { } |
| 27 |
|
| 28 |
[Serializable] |
| 29 |
public class GB18030Encoding : MonoEncoding |
| 30 |
{ |
| 31 |
|
| 32 |
public GB18030Encoding () |
| 33 |
: base (54936, 936) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
public override string EncodingName { |
| 38 |
get { return "Chinese Simplified (GB18030)"; } |
| 39 |
} |
| 40 |
|
| 41 |
public override string HeaderName { |
| 42 |
get { return "GB18030"; } |
| 43 |
} |
| 44 |
|
| 45 |
public override string BodyName { |
| 46 |
get { return "GB18030"; } |
| 47 |
} |
| 48 |
|
| 49 |
public override string WebName { |
| 50 |
get { return "GB18030"; } |
| 51 |
} |
| 52 |
|
| 53 |
public override bool IsMailNewsDisplay { |
| 54 |
get { return true; } |
| 55 |
} |
| 56 |
|
| 57 |
public override bool IsMailNewsSave { |
| 58 |
get { return true; } |
| 59 |
} |
| 60 |
|
| 61 |
public override bool IsBrowserDisplay { |
| 62 |
get { return true; } |
| 63 |
} |
| 64 |
|
| 65 |
public override bool IsBrowserSave { |
| 66 |
get { return true; } |
| 67 |
} |
| 68 |
|
| 69 |
public override int GetMaxByteCount (int len) |
| 70 |
{ |
| 71 |
|
| 72 |
return len * 4; |
| 73 |
} |
| 74 |
|
| 75 |
public override int GetMaxCharCount (int len) |
| 76 |
{ |
| 77 |
return len; |
| 78 |
} |
| 79 |
|
| 80 |
#if !DISABLE_UNSAFE |
| 81 |
public unsafe override int GetByteCountImpl (char* chars, int count) |
| 82 |
{ |
| 83 |
return new GB18030Encoder (this).GetByteCountImpl (chars, count, true); |
| 84 |
} |
| 85 |
|
| 86 |
public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount) |
| 87 |
{ |
| 88 |
return new GB18030Encoder (this).GetBytesImpl (chars, charCount, bytes, byteCount, true); |
| 89 |
} |
| 90 |
#else |
| 91 |
public override int GetByteCount (char [] chars, int index, int length) |
| 92 |
{ |
| 93 |
return new GB18030Encoder (this).GetByteCount (chars, index, length, true); |
| 94 |
} |
| 95 |
|
| 96 |
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) |
| 97 |
{ |
| 98 |
return new GB18030Encoder (this).GetBytes (chars, charIndex, charCount, bytes, byteIndex, true); |
| 99 |
} |
| 100 |
#endif |
| 101 |
|
| 102 |
public override int GetCharCount (byte [] bytes, int start, int len) |
| 103 |
{ |
| 104 |
return new GB18030Decoder ().GetCharCount (bytes, start, len); |
| 105 |
} |
| 106 |
|
| 107 |
public override int GetChars (byte [] bytes, int byteIdx, int srclen, char [] chars, int charIdx) |
| 108 |
{ |
| 109 |
return new GB18030Decoder ().GetChars (bytes, byteIdx, srclen, chars, charIdx); |
| 110 |
} |
| 111 |
|
| 112 |
public override Encoder GetEncoder () |
| 113 |
{ |
| 114 |
return new GB18030Encoder (this); |
| 115 |
} |
| 116 |
|
| 117 |
public override Decoder GetDecoder () |
| 118 |
{ |
| 119 |
return new GB18030Decoder (); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
class GB18030Decoder : DbcsEncoding.DbcsDecoder |
| 124 |
{ |
| 125 |
static DbcsConvert gb2312 = DbcsConvert.Gb2312; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public GB18030Decoder () |
| 130 |
: base (null) |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
public override int GetCharCount (byte [] bytes, int start, int len) |
| 135 |
{ |
| 136 |
CheckRange (bytes, start, len); |
| 137 |
|
| 138 |
int end = start + len; |
| 139 |
int ret = 0; |
| 140 |
while (start < end) { |
| 141 |
if (bytes [start] < 0x80) { |
| 142 |
ret++; |
| 143 |
start++; |
| 144 |
continue; |
| 145 |
} |
| 146 |
else if (bytes [start] == 0x80) { |
| 147 |
|
| 148 |
|
| 149 |
ret++; |
| 150 |
start++; |
| 151 |
continue; |
| 152 |
} |
| 153 |
else if (bytes [start] == 0xFF) { |
| 154 |
|
| 155 |
ret++; |
| 156 |
start++; |
| 157 |
continue; |
| 158 |
} |
| 159 |
else if (start + 1 >= end) { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
ret++; |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
byte second = bytes [start + 1]; |
| 168 |
if (second == 0x7F || second == 0xFF) { |
| 169 |
|
| 170 |
ret++; |
| 171 |
start += 2; |
| 172 |
continue; |
| 173 |
} |
| 174 |
else if (0x30 <= second && second <= 0x39) { |
| 175 |
|
| 176 |
if (start + 3 >= end) { |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
ret += start + 3 == end ? 3 : 2; |
| 183 |
break; |
| 184 |
} |
| 185 |
long value = GB18030Source.FromGBX (bytes, start); |
| 186 |
if (value < 0) { |
| 187 |
|
| 188 |
ret++; |
| 189 |
start -= (int) value; |
| 190 |
} else if (value >= 0x10000) { |
| 191 |
|
| 192 |
ret += 2; |
| 193 |
start += 4; |
| 194 |
} else { |
| 195 |
|
| 196 |
ret++; |
| 197 |
start+= 4; |
| 198 |
} |
| 199 |
} else { |
| 200 |
|
| 201 |
start += 2; |
| 202 |
ret++; |
| 203 |
} |
| 204 |
} |
| 205 |
return ret; |
| 206 |
} |
| 207 |
|
| 208 |
public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex) |
| 209 |
{ |
| 210 |
CheckRange (bytes, byteIndex, byteCount, chars, charIndex); |
| 211 |
|
| 212 |
int byteEnd = byteIndex + byteCount; |
| 213 |
int charStart = charIndex; |
| 214 |
|
| 215 |
while (byteIndex < byteEnd) { |
| 216 |
if (bytes [byteIndex] < 0x80) { |
| 217 |
chars [charIndex++] = (char) bytes [byteIndex++]; |
| 218 |
continue; |
| 219 |
} |
| 220 |
else if (bytes [byteIndex] == 0x80) { |
| 221 |
|
| 222 |
|
| 223 |
chars [charIndex++] = '\u20AC'; |
| 224 |
byteIndex++; |
| 225 |
continue; |
| 226 |
} |
| 227 |
else if (bytes [byteIndex] == 0xFF) { |
| 228 |
|
| 229 |
chars [charIndex++] = '?'; |
| 230 |
byteIndex++; |
| 231 |
continue; |
| 232 |
} |
| 233 |
else if (byteIndex + 1 >= byteEnd) { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
break; |
| 238 |
} |
| 239 |
|
| 240 |
byte second = bytes [byteIndex + 1]; |
| 241 |
if (second == 0x7F || second == 0xFF) { |
| 242 |
|
| 243 |
chars [charIndex++] = '?'; |
| 244 |
byteIndex += 2; |
| 245 |
} |
| 246 |
else if (0x30 <= second && second <= 0x39) { |
| 247 |
|
| 248 |
if (byteIndex + 3 >= byteEnd) { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
break; |
| 255 |
} |
| 256 |
long value = GB18030Source.FromGBX (bytes, byteIndex); |
| 257 |
if (value < 0) { |
| 258 |
|
| 259 |
chars [charIndex++] = '?'; |
| 260 |
byteIndex -= (int) value; |
| 261 |
} else if (value >= 0x10000) { |
| 262 |
|
| 263 |
value -= 0x10000; |
| 264 |
chars [charIndex++] = (char) (value / 0x400 + 0xD800); |
| 265 |
chars [charIndex++] = (char) (value % 0x400 + 0xDC00); |
| 266 |
byteIndex += 4; |
| 267 |
} else { |
| 268 |
|
| 269 |
chars [charIndex++] = (char) value; |
| 270 |
byteIndex += 4; |
| 271 |
} |
| 272 |
} else { |
| 273 |
byte first = bytes [byteIndex]; |
| 274 |
int ord = ((first - 0x81) * 191 + second - 0x40) * 2; |
| 275 |
char c1 = ord < 0 || ord >= gb2312.n2u.Length ? |
| 276 |
'\0' : (char) (gb2312.n2u [ord] + gb2312.n2u [ord + 1] * 256); |
| 277 |
if (c1 == 0) |
| 278 |
chars [charIndex++] = '?'; |
| 279 |
else |
| 280 |
chars [charIndex++] = c1; |
| 281 |
byteIndex += 2; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return charIndex - charStart; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
class GB18030Encoder : MonoEncoder |
| 290 |
{ |
| 291 |
static DbcsConvert gb2312 = DbcsConvert.Gb2312; |
| 292 |
|
| 293 |
public GB18030Encoder (MonoEncoding owner) |
| 294 |
: base (owner) |
| 295 |
{ |
| 296 |
} |
| 297 |
|
| 298 |
char incomplete_byte_count; |
| 299 |
char incomplete_bytes; |
| 300 |
|
| 301 |
#if !DISABLE_UNSAFE |
| 302 |
public unsafe override int GetByteCountImpl (char* chars, int count, bool refresh) |
| 303 |
{ |
| 304 |
int start = 0; |
| 305 |
int end = count; |
| 306 |
int ret = 0; |
| 307 |
while (start < end) { |
| 308 |
char ch = chars [start]; |
| 309 |
if (ch < 0x80) { |
| 310 |
|
| 311 |
ret++; |
| 312 |
start++; |
| 313 |
continue; |
| 314 |
} else if (Char.IsSurrogate (ch)) { |
| 315 |
|
| 316 |
if (start + 1 == end) { |
| 317 |
incomplete_byte_count = ch; |
| 318 |
start++; |
| 319 |
} else { |
| 320 |
ret += 4; |
| 321 |
start += 2; |
| 322 |
} |
| 323 |
continue; |
| 324 |
} |
| 325 |
|
| 326 |
if (ch < 0x80 || ch == 0xFF) { |
| 327 |
|
| 328 |
ret++; |
| 329 |
start++; |
| 330 |
continue; |
| 331 |
} |
| 332 |
|
| 333 |
byte b1 = gb2312.u2n [((int) ch) * 2 + 1]; |
| 334 |
byte b2 = gb2312.u2n [((int) ch) * 2]; |
| 335 |
if (b1 != 0 && b2 != 0) { |
| 336 |
|
| 337 |
ret += 2; |
| 338 |
start++; |
| 339 |
continue; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
long value = GB18030Source.FromUCS (ch); |
| 344 |
if (value < 0) |
| 345 |
ret++; |
| 346 |
else |
| 347 |
ret += 4; |
| 348 |
start++; |
| 349 |
} |
| 350 |
|
| 351 |
if (refresh) { |
| 352 |
if (incomplete_byte_count != char.MinValue) |
| 353 |
ret++; |
| 354 |
incomplete_byte_count = char.MinValue; |
| 355 |
} |
| 356 |
return ret; |
| 357 |
} |
| 358 |
|
| 359 |
public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount, bool refresh) |
| 360 |
{ |
| 361 |
int charIndex = 0; |
| 362 |
int byteIndex = 0; |
| 363 |
|
| 364 |
int charEnd = charIndex + charCount; |
| 365 |
int byteStart = byteIndex; |
| 366 |
char ch = incomplete_bytes; |
| 367 |
|
| 368 |
while (charIndex < charEnd) { |
| 369 |
if (incomplete_bytes == char.MinValue) |
| 370 |
ch = chars [charIndex++]; |
| 371 |
else |
| 372 |
incomplete_bytes = char.MinValue; |
| 373 |
|
| 374 |
if (ch < 0x80) { |
| 375 |
|
| 376 |
bytes [byteIndex++] = (byte) ch; |
| 377 |
continue; |
| 378 |
} else if (Char.IsSurrogate (ch)) { |
| 379 |
|
| 380 |
if (charIndex == charEnd) { |
| 381 |
incomplete_bytes = ch; |
| 382 |
break; |
| 383 |
} |
| 384 |
char ch2 = chars [charIndex++]; |
| 385 |
if (!Char.IsSurrogate (ch2)) { |
| 386 |
|
| 387 |
HandleFallback ( |
| 388 |
chars, ref charIndex, ref charCount, |
| 389 |
bytes, ref byteIndex, ref byteCount, null); |
| 390 |
continue; |
| 391 |
} |
| 392 |
int cp = (ch - 0xD800) * 0x400 + ch2 - 0xDC00; |
| 393 |
GB18030Source.Unlinear (bytes + byteIndex, GB18030Source.FromUCSSurrogate (cp)); |
| 394 |
byteIndex += 4; |
| 395 |
continue; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
if (ch <= 0x80 || ch == 0xFF) { |
| 400 |
|
| 401 |
bytes [byteIndex++] = (byte) ch; |
| 402 |
continue; |
| 403 |
} |
| 404 |
|
| 405 |
byte b1 = gb2312.u2n [((int) ch) * 2 + 1]; |
| 406 |
byte b2 = gb2312.u2n [((int) ch) * 2]; |
| 407 |
if (b1 != 0 && b2 != 0) { |
| 408 |
bytes [byteIndex++] = b1; |
| 409 |
bytes [byteIndex++] = b2; |
| 410 |
continue; |
| 411 |
} |
| 412 |
|
| 413 |
long value = GB18030Source.FromUCS (ch); |
| 414 |
if (value < 0) |
| 415 |
bytes [byteIndex++] = 0x3F; |
| 416 |
else { |
| 417 |
|
| 418 |
GB18030Source.Unlinear (bytes + byteIndex, value); |
| 419 |
byteIndex += 4; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
if (refresh) { |
| 424 |
if (incomplete_bytes != char.MinValue) |
| 425 |
bytes [byteIndex++] = 0x3F; |
| 426 |
incomplete_bytes = char.MinValue; |
| 427 |
} |
| 428 |
|
| 429 |
return byteIndex - byteStart; |
| 430 |
} |
| 431 |
#else |
| 432 |
|
| 433 |
public override int GetByteCount(char[] chars, int start, int count, bool refresh) |
| 434 |
{ |
| 435 |
int end = start + count; |
| 436 |
int ret = 0; |
| 437 |
while (start < end) |
| 438 |
{ |
| 439 |
char ch = chars[start]; |
| 440 |
if (ch < 0x80) |
| 441 |
{ |
| 442 |
|
| 443 |
ret++; |
| 444 |
start++; |
| 445 |
continue; |
| 446 |
} |
| 447 |
else if (Char.IsSurrogate(ch)) |
| 448 |
{ |
| 449 |
|
| 450 |
if (start + 1 == end) |
| 451 |
{ |
| 452 |
incomplete_byte_count = ch; |
| 453 |
start++; |
| 454 |
} |
| 455 |
else |
| 456 |
{ |
| 457 |
ret += 4; |
| 458 |
start += 2; |
| 459 |
} |
| 460 |
continue; |
| 461 |
} |
| 462 |
|
| 463 |
if (ch < 0x80 || ch == 0xFF) |
| 464 |
{ |
| 465 |
|
| 466 |
ret++; |
| 467 |
start++; |
| 468 |
continue; |
| 469 |
} |
| 470 |
|
| 471 |
byte b1 = gb2312.u2n[((int)ch) * 2 + 1]; |
| 472 |
byte b2 = gb2312.u2n[((int)ch) * 2]; |
| 473 |
if (b1 != 0 && b2 != 0) |
| 474 |
{ |
| 475 |
|
| 476 |
ret += 2; |
| 477 |
start++; |
| 478 |
continue; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
long value = GB18030Source.FromUCS(ch); |
| 483 |
if (value < 0) |
| 484 |
ret++; |
| 485 |
else |
| 486 |
ret += 4; |
| 487 |
start++; |
| 488 |
} |
| 489 |
|
| 490 |
if (refresh) |
| 491 |
{ |
| 492 |
if (incomplete_byte_count != char.MinValue) |
| 493 |
ret++; |
| 494 |
incomplete_byte_count = char.MinValue; |
| 495 |
} |
| 496 |
return ret; |
| 497 |
} |
| 498 |
|
| 499 |
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool refresh) |
| 500 |
{ |
| 501 |
int byteCount = bytes.Length; |
| 502 |
int charEnd = charIndex + charCount; |
| 503 |
int byteStart = byteIndex; |
| 504 |
char ch = incomplete_bytes; |
| 505 |
|
| 506 |
while (charIndex < charEnd) |
| 507 |
{ |
| 508 |
if (incomplete_bytes == char.MinValue) |
| 509 |
ch = chars[charIndex++]; |
| 510 |
else |
| 511 |
incomplete_bytes = char.MinValue; |
| 512 |
|
| 513 |
if (ch < 0x80) |
| 514 |
{ |
| 515 |
|
| 516 |
bytes[byteIndex++] = (byte)ch; |
| 517 |
continue; |
| 518 |
} |
| 519 |
else if (Char.IsSurrogate(ch)) |
| 520 |
{ |
| 521 |
|
| 522 |
if (charIndex == charEnd) |
| 523 |
{ |
| 524 |
incomplete_bytes = ch; |
| 525 |
break; |
| 526 |
} |
| 527 |
char ch2 = chars[charIndex++]; |
| 528 |
if (!Char.IsSurrogate(ch2)) |
| 529 |
{ |
| 530 |
|
| 531 |
HandleFallback (chars, ref charIndex, ref charCount, |
| 532 |
bytes, ref byteIndex, ref byteCount, null); |
| 533 |
continue; |
| 534 |
} |
| 535 |
int cp = (ch - 0xD800) * 0x400 + ch2 - 0xDC00; |
| 536 |
GB18030Source.Unlinear(bytes, byteIndex, GB18030Source.FromUCSSurrogate(cp)); |
| 537 |
byteIndex += 4; |
| 538 |
continue; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
if (ch <= 0x80 || ch == 0xFF) |
| 543 |
{ |
| 544 |
|
| 545 |
bytes[byteIndex++] = (byte)ch; |
| 546 |
continue; |
| 547 |
} |
| 548 |
|
| 549 |
byte b1 = gb2312.u2n[((int)ch) * 2 + 1]; |
| 550 |
byte b2 = gb2312.u2n[((int)ch) * 2]; |
| 551 |
if (b1 != 0 && b2 != 0) |
| 552 |
{ |
| 553 |
bytes[byteIndex++] = b1; |
| 554 |
bytes[byteIndex++] = b2; |
| 555 |
continue; |
| 556 |
} |
| 557 |
|
| 558 |
long value = GB18030Source.FromUCS(ch); |
| 559 |
if (value < 0) |
| 560 |
bytes[byteIndex++] = 0x3F; |
| 561 |
else |
| 562 |
{ |
| 563 |
|
| 564 |
GB18030Source.Unlinear(bytes, byteIndex, value); |
| 565 |
byteIndex += 4; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
if (refresh) |
| 570 |
{ |
| 571 |
if (incomplete_bytes != char.MinValue) |
| 572 |
bytes[byteIndex++] = 0x3F; |
| 573 |
incomplete_bytes = char.MinValue; |
| 574 |
} |
| 575 |
|
| 576 |
return byteIndex - byteStart; |
| 577 |
} |
| 578 |
#endif |
| 579 |
} |
| 580 |
} |