Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
ISO2022Encoding.cs
Go to the documentation of this file.
1namespace System.Text;
2
3internal sealed class ISO2022Encoding : DBCSCodePageEncoding
4{
5 internal enum ISO2022Modes
6 {
8 ModeJIS0208 = 1,
9 ModeKR = 5,
10 ModeHZ = 6,
11 ModeGB2312 = 7,
13 ModeCNS11643_2 = 10,
14 ModeASCII = 11,
17 ModeNOOP = -3
18 }
19
20 internal sealed class ISO2022Encoder : System.Text.EncoderNLS
21 {
23
25
26 internal override bool HasState
27 {
28 get
29 {
30 if (charLeftOver == '\0')
31 {
32 return currentMode != ISO2022Modes.ModeASCII;
33 }
34 return true;
35 }
36 }
37
38 internal ISO2022Encoder(EncodingNLS encoding)
39 : base(encoding)
40 {
41 }
42
43 public override void Reset()
44 {
45 currentMode = ISO2022Modes.ModeASCII;
46 shiftInOutMode = ISO2022Modes.ModeASCII;
47 charLeftOver = '\0';
48 if (m_fallbackBuffer != null)
49 {
51 }
52 }
53 }
54
55 internal sealed class ISO2022Decoder : System.Text.DecoderNLS
56 {
57 internal byte[] bytesLeftOver;
58
59 internal int bytesLeftOverCount;
60
62
64
65 internal override bool HasState
66 {
67 get
68 {
69 if (bytesLeftOverCount == 0)
70 {
71 return currentMode != ISO2022Modes.ModeASCII;
72 }
73 return true;
74 }
75 }
76
77 internal ISO2022Decoder(EncodingNLS encoding)
78 : base(encoding)
79 {
80 }
81
82 public override void Reset()
83 {
85 bytesLeftOver = new byte[4];
86 currentMode = ISO2022Modes.ModeASCII;
87 shiftInOutMode = ISO2022Modes.ModeASCII;
88 if (m_fallbackBuffer != null)
89 {
91 }
92 }
93 }
94
95 private static readonly int[] s_tableBaseCodePages = new int[12]
96 {
97 932, 932, 932, 0, 0, 949, 936, 0, 0, 0,
98 0, 0
99 };
100
101 private static readonly ushort[] s_HalfToFullWidthKanaTable = new ushort[63]
102 {
103 41379, 41430, 41431, 41378, 41382, 42482, 42401, 42403, 42405, 42407,
104 42409, 42467, 42469, 42471, 42435, 41404, 42402, 42404, 42406, 42408,
105 42410, 42411, 42413, 42415, 42417, 42419, 42421, 42423, 42425, 42427,
106 42429, 42431, 42433, 42436, 42438, 42440, 42442, 42443, 42444, 42445,
107 42446, 42447, 42450, 42453, 42456, 42459, 42462, 42463, 42464, 42465,
108 42466, 42468, 42470, 42472, 42473, 42474, 42475, 42476, 42477, 42479,
109 42483, 41387, 41388
110 };
111
112 internal ISO2022Encoding(int codePage)
113 : base(codePage, s_tableBaseCodePages[codePage % 10])
114 {
115 }
116
117 protected override bool CleanUpBytes(ref int bytes)
118 {
119 switch (CodePage)
120 {
121 case 50220:
122 case 50221:
123 case 50222:
124 if (bytes >= 256)
125 {
126 if (bytes >= 64064 && bytes <= 64587)
127 {
128 if (bytes >= 64064 && bytes <= 64091)
129 {
130 if (bytes <= 64073)
131 {
132 bytes -= 2897;
133 }
134 else if (bytes >= 64074 && bytes <= 64083)
135 {
136 bytes -= 29430;
137 }
138 else if (bytes >= 64084 && bytes <= 64087)
139 {
140 bytes -= 2907;
141 }
142 else if (bytes == 64088)
143 {
144 bytes = 34698;
145 }
146 else if (bytes == 64089)
147 {
148 bytes = 34690;
149 }
150 else if (bytes == 64090)
151 {
152 bytes = 34692;
153 }
154 else if (bytes == 64091)
155 {
156 bytes = 34714;
157 }
158 }
159 else if (bytes >= 64092 && bytes <= 64587)
160 {
161 byte b = (byte)bytes;
162 if (b < 92)
163 {
164 bytes -= 3423;
165 }
166 else if (b >= 128 && b <= 155)
167 {
168 bytes -= 3357;
169 }
170 else
171 {
172 bytes -= 3356;
173 }
174 }
175 }
176 byte b2 = (byte)(bytes >> 8);
177 byte b3 = (byte)bytes;
178 b2 = (byte)(b2 - ((b2 > 159) ? 177 : 113));
179 b2 = (byte)((b2 << 1) + 1);
180 if (b3 > 158)
181 {
182 b3 -= 126;
183 b2++;
184 }
185 else
186 {
187 if (b3 > 126)
188 {
189 b3--;
190 }
191 b3 -= 31;
192 }
193 bytes = (b2 << 8) | b3;
194 }
195 else
196 {
197 if (bytes >= 161 && bytes <= 223)
198 {
199 bytes += 3968;
200 }
201 if (bytes >= 129 && (bytes <= 159 || (bytes >= 224 && bytes <= 252)))
202 {
203 return false;
204 }
205 }
206 break;
207 case 50225:
208 if (bytes >= 128 && bytes <= 255)
209 {
210 return false;
211 }
212 if (bytes >= 256 && ((bytes & 0xFF) < 161 || (bytes & 0xFF) == 255 || (bytes & 0xFF00) < 41216 || (bytes & 0xFF00) == 65280))
213 {
214 return false;
215 }
216 bytes &= 32639;
217 break;
218 case 52936:
219 if (bytes >= 129 && bytes <= 254)
220 {
221 return false;
222 }
223 break;
224 }
225 return true;
226 }
227
228 public unsafe override int GetByteCount(char* chars, int count, System.Text.EncoderNLS baseEncoder)
229 {
230 return GetBytes(chars, count, null, 0, baseEncoder);
231 }
232
233 public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, System.Text.EncoderNLS baseEncoder)
234 {
235 ISO2022Encoder encoder = (ISO2022Encoder)baseEncoder;
236 int result = 0;
237 switch (CodePage)
238 {
239 case 50220:
240 case 50221:
241 case 50222:
242 result = GetBytesCP5022xJP(chars, charCount, bytes, byteCount, encoder);
243 break;
244 case 50225:
245 result = GetBytesCP50225KR(chars, charCount, bytes, byteCount, encoder);
246 break;
247 case 52936:
248 result = GetBytesCP52936(chars, charCount, bytes, byteCount, encoder);
249 break;
250 }
251 return result;
252 }
253
254 public unsafe override int GetCharCount(byte* bytes, int count, System.Text.DecoderNLS baseDecoder)
255 {
256 return GetChars(bytes, count, null, 0, baseDecoder);
257 }
258
259 public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, System.Text.DecoderNLS baseDecoder)
260 {
261 ISO2022Decoder decoder = (ISO2022Decoder)baseDecoder;
262 int result = 0;
263 switch (CodePage)
264 {
265 case 50220:
266 case 50221:
267 case 50222:
268 result = GetCharsCP5022xJP(bytes, byteCount, chars, charCount, decoder);
269 break;
270 case 50225:
271 result = GetCharsCP50225KR(bytes, byteCount, chars, charCount, decoder);
272 break;
273 case 52936:
274 result = GetCharsCP52936(bytes, byteCount, chars, charCount, decoder);
275 break;
276 }
277 return result;
278 }
279
280 private unsafe int GetBytesCP5022xJP(char* chars, int charCount, byte* bytes, int byteCount, ISO2022Encoder encoder)
281 {
282 EncodingByteBuffer encodingByteBuffer = new EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
283 ISO2022Modes iSO2022Modes = ISO2022Modes.ModeASCII;
284 ISO2022Modes iSO2022Modes2 = ISO2022Modes.ModeASCII;
285 if (encoder != null)
286 {
287 char charLeftOver = encoder.charLeftOver;
288 iSO2022Modes = encoder.currentMode;
289 iSO2022Modes2 = encoder.shiftInOutMode;
290 if (charLeftOver > '\0')
291 {
292 encodingByteBuffer.Fallback(charLeftOver);
293 }
294 }
295 while (encodingByteBuffer.MoreData)
296 {
297 char nextChar = encodingByteBuffer.GetNextChar();
298 ushort num = mapUnicodeToBytes[(int)nextChar];
299 byte b;
300 byte b2;
301 while (true)
302 {
303 b = (byte)(num >> 8);
304 b2 = (byte)(num & 0xFFu);
305 if (b != 16)
306 {
307 break;
308 }
309 if (CodePage == 50220)
310 {
311 if (b2 >= 33 && b2 < 33 + s_HalfToFullWidthKanaTable.Length)
312 {
313 num = (ushort)(s_HalfToFullWidthKanaTable[b2 - 33] & 0x7F7Fu);
314 continue;
315 }
316 goto IL_009a;
317 }
318 goto IL_00be;
319 }
320 if (b != 0)
321 {
322 if (CodePage == 50222 && iSO2022Modes == ISO2022Modes.ModeHalfwidthKatakana)
323 {
324 if (!encodingByteBuffer.AddByte(15))
325 {
326 break;
327 }
328 iSO2022Modes = iSO2022Modes2;
329 }
330 if (iSO2022Modes != ISO2022Modes.ModeJIS0208)
331 {
332 if (!encodingByteBuffer.AddByte((byte)27, (byte)36, (byte)66))
333 {
334 break;
335 }
336 iSO2022Modes = ISO2022Modes.ModeJIS0208;
337 }
338 if (!encodingByteBuffer.AddByte(b, b2))
339 {
340 break;
341 }
342 }
343 else if (num != 0 || nextChar == '\0')
344 {
345 if (CodePage == 50222 && iSO2022Modes == ISO2022Modes.ModeHalfwidthKatakana)
346 {
347 if (!encodingByteBuffer.AddByte(15))
348 {
349 break;
350 }
351 iSO2022Modes = iSO2022Modes2;
352 }
353 if (iSO2022Modes != ISO2022Modes.ModeASCII)
354 {
355 if (!encodingByteBuffer.AddByte((byte)27, (byte)40, (byte)66))
356 {
357 break;
358 }
359 iSO2022Modes = ISO2022Modes.ModeASCII;
360 }
361 if (!encodingByteBuffer.AddByte(b2))
362 {
363 break;
364 }
365 }
366 else
367 {
368 encodingByteBuffer.Fallback(nextChar);
369 }
370 continue;
371 IL_009a:
372 encodingByteBuffer.Fallback(nextChar);
373 continue;
374 IL_00be:
375 if (iSO2022Modes != 0)
376 {
377 if (CodePage == 50222)
378 {
379 if (!encodingByteBuffer.AddByte(14))
380 {
381 break;
382 }
383 iSO2022Modes2 = iSO2022Modes;
384 iSO2022Modes = ISO2022Modes.ModeHalfwidthKatakana;
385 }
386 else
387 {
388 if (!encodingByteBuffer.AddByte((byte)27, (byte)40, (byte)73))
389 {
390 break;
391 }
392 iSO2022Modes = ISO2022Modes.ModeHalfwidthKatakana;
393 }
394 }
395 if (!encodingByteBuffer.AddByte((byte)(b2 & 0x7Fu)))
396 {
397 break;
398 }
399 }
400 if (iSO2022Modes != ISO2022Modes.ModeASCII && (encoder == null || encoder.MustFlush))
401 {
402 if (CodePage == 50222 && iSO2022Modes == ISO2022Modes.ModeHalfwidthKatakana)
403 {
404 if (encodingByteBuffer.AddByte(15))
405 {
406 iSO2022Modes = iSO2022Modes2;
407 }
408 else
409 {
410 encodingByteBuffer.GetNextChar();
411 }
412 }
413 if (iSO2022Modes != ISO2022Modes.ModeASCII && (CodePage != 50222 || iSO2022Modes != 0))
414 {
415 if (encodingByteBuffer.AddByte((byte)27, (byte)40, (byte)66))
416 {
417 iSO2022Modes = ISO2022Modes.ModeASCII;
418 }
419 else
420 {
421 encodingByteBuffer.GetNextChar();
422 }
423 }
424 }
425 if (bytes != null && encoder != null)
426 {
427 encoder.currentMode = iSO2022Modes;
428 encoder.shiftInOutMode = iSO2022Modes2;
429 if (!encodingByteBuffer.fallbackBufferHelper.bUsedEncoder)
430 {
431 encoder.charLeftOver = '\0';
432 }
433 encoder.m_charsUsed = encodingByteBuffer.CharsUsed;
434 }
435 return encodingByteBuffer.Count;
436 }
437
438 private unsafe int GetBytesCP50225KR(char* chars, int charCount, byte* bytes, int byteCount, ISO2022Encoder encoder)
439 {
440 EncodingByteBuffer encodingByteBuffer = new EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
441 ISO2022Modes iSO2022Modes = ISO2022Modes.ModeASCII;
442 ISO2022Modes iSO2022Modes2 = ISO2022Modes.ModeASCII;
443 if (encoder != null)
444 {
445 char charLeftOver = encoder.charLeftOver;
446 iSO2022Modes = encoder.currentMode;
447 iSO2022Modes2 = encoder.shiftInOutMode;
448 if (charLeftOver > '\0')
449 {
450 encodingByteBuffer.Fallback(charLeftOver);
451 }
452 }
453 while (encodingByteBuffer.MoreData)
454 {
455 char nextChar = encodingByteBuffer.GetNextChar();
456 ushort num = mapUnicodeToBytes[(int)nextChar];
457 byte b = (byte)(num >> 8);
458 byte b2 = (byte)(num & 0xFFu);
459 if (b != 0)
460 {
461 if (iSO2022Modes2 != ISO2022Modes.ModeKR)
462 {
463 if (!encodingByteBuffer.AddByte((byte)27, (byte)36, (byte)41, (byte)67))
464 {
465 break;
466 }
467 iSO2022Modes2 = ISO2022Modes.ModeKR;
468 }
469 if (iSO2022Modes != ISO2022Modes.ModeKR)
470 {
471 if (!encodingByteBuffer.AddByte(14))
472 {
473 break;
474 }
475 iSO2022Modes = ISO2022Modes.ModeKR;
476 }
477 if (!encodingByteBuffer.AddByte(b, b2))
478 {
479 break;
480 }
481 }
482 else if (num != 0 || nextChar == '\0')
483 {
484 if (iSO2022Modes != ISO2022Modes.ModeASCII)
485 {
486 if (!encodingByteBuffer.AddByte(15))
487 {
488 break;
489 }
490 iSO2022Modes = ISO2022Modes.ModeASCII;
491 }
492 if (!encodingByteBuffer.AddByte(b2))
493 {
494 break;
495 }
496 }
497 else
498 {
499 encodingByteBuffer.Fallback(nextChar);
500 }
501 }
502 if (iSO2022Modes != ISO2022Modes.ModeASCII && (encoder == null || encoder.MustFlush))
503 {
504 if (encodingByteBuffer.AddByte(15))
505 {
506 iSO2022Modes = ISO2022Modes.ModeASCII;
507 }
508 else
509 {
510 encodingByteBuffer.GetNextChar();
511 }
512 }
513 if (bytes != null && encoder != null)
514 {
515 if (!encodingByteBuffer.fallbackBufferHelper.bUsedEncoder)
516 {
517 encoder.charLeftOver = '\0';
518 }
519 encoder.currentMode = iSO2022Modes;
520 if (!encoder.MustFlush || encoder.charLeftOver != 0)
521 {
522 encoder.shiftInOutMode = iSO2022Modes2;
523 }
524 else
525 {
526 encoder.shiftInOutMode = ISO2022Modes.ModeASCII;
527 }
528 encoder.m_charsUsed = encodingByteBuffer.CharsUsed;
529 }
530 return encodingByteBuffer.Count;
531 }
532
533 private unsafe int GetBytesCP52936(char* chars, int charCount, byte* bytes, int byteCount, ISO2022Encoder encoder)
534 {
535 EncodingByteBuffer encodingByteBuffer = new EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
536 ISO2022Modes iSO2022Modes = ISO2022Modes.ModeASCII;
537 if (encoder != null)
538 {
539 char charLeftOver = encoder.charLeftOver;
540 iSO2022Modes = encoder.currentMode;
541 if (charLeftOver > '\0')
542 {
543 encodingByteBuffer.Fallback(charLeftOver);
544 }
545 }
546 while (encodingByteBuffer.MoreData)
547 {
548 char nextChar = encodingByteBuffer.GetNextChar();
549 ushort num = mapUnicodeToBytes[(int)nextChar];
550 if (num == 0 && nextChar != 0)
551 {
552 encodingByteBuffer.Fallback(nextChar);
553 continue;
554 }
555 byte b = (byte)(num >> 8);
556 byte b2 = (byte)(num & 0xFFu);
557 if ((b != 0 && (b < 161 || b > 247 || b2 < 161 || b2 > 254)) || (b == 0 && b2 > 128 && b2 != byte.MaxValue))
558 {
559 encodingByteBuffer.Fallback(nextChar);
560 continue;
561 }
562 if (b != 0)
563 {
564 if (iSO2022Modes != ISO2022Modes.ModeHZ)
565 {
566 if (!encodingByteBuffer.AddByte(126, 123, 2))
567 {
568 break;
569 }
570 iSO2022Modes = ISO2022Modes.ModeHZ;
571 }
572 if (encodingByteBuffer.AddByte((byte)(b & 0x7Fu), (byte)(b2 & 0x7Fu)))
573 {
574 continue;
575 }
576 break;
577 }
578 if (iSO2022Modes != ISO2022Modes.ModeASCII)
579 {
580 if (!encodingByteBuffer.AddByte(126, 125, (b2 != 126) ? 1 : 2))
581 {
582 break;
583 }
584 iSO2022Modes = ISO2022Modes.ModeASCII;
585 }
586 if ((b2 == 126 && !encodingByteBuffer.AddByte(126, 1)) || !encodingByteBuffer.AddByte(b2))
587 {
588 break;
589 }
590 }
591 if (iSO2022Modes != ISO2022Modes.ModeASCII && (encoder == null || encoder.MustFlush))
592 {
593 if (encodingByteBuffer.AddByte((byte)126, (byte)125))
594 {
595 iSO2022Modes = ISO2022Modes.ModeASCII;
596 }
597 else
598 {
599 encodingByteBuffer.GetNextChar();
600 }
601 }
602 if (encoder != null && bytes != null)
603 {
604 encoder.currentMode = iSO2022Modes;
605 if (!encodingByteBuffer.fallbackBufferHelper.bUsedEncoder)
606 {
607 encoder.charLeftOver = '\0';
608 }
609 encoder.m_charsUsed = encodingByteBuffer.CharsUsed;
610 }
611 return encodingByteBuffer.Count;
612 }
613
614 private unsafe int GetCharsCP5022xJP(byte* bytes, int byteCount, char* chars, int charCount, ISO2022Decoder decoder)
615 {
616 EncodingCharBuffer encodingCharBuffer = new EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
617 ISO2022Modes iSO2022Modes = ISO2022Modes.ModeASCII;
618 ISO2022Modes iSO2022Modes2 = ISO2022Modes.ModeASCII;
619 byte[] bytes2 = new byte[4];
620 int count = 0;
621 if (decoder != null)
622 {
623 iSO2022Modes = decoder.currentMode;
624 iSO2022Modes2 = decoder.shiftInOutMode;
625 count = decoder.bytesLeftOverCount;
626 for (int i = 0; i < count; i++)
627 {
628 bytes2[i] = decoder.bytesLeftOver[i];
629 }
630 }
631 while (encodingCharBuffer.MoreData || count > 0)
632 {
633 byte b;
634 if (count > 0)
635 {
636 if (bytes2[0] == 27)
637 {
638 if (!encodingCharBuffer.MoreData)
639 {
640 if (decoder != null && !decoder.MustFlush)
641 {
642 break;
643 }
644 }
645 else
646 {
647 bytes2[count++] = encodingCharBuffer.GetNextByte();
648 ISO2022Modes iSO2022Modes3 = CheckEscapeSequenceJP(bytes2, count);
649 switch (iSO2022Modes3)
650 {
651 default:
652 count = 0;
653 iSO2022Modes = (iSO2022Modes2 = iSO2022Modes3);
654 continue;
655 case ISO2022Modes.ModeInvalidEscape:
656 break;
657 case ISO2022Modes.ModeIncompleteEscape:
658 continue;
659 }
660 }
661 }
662 b = DecrementEscapeBytes(ref bytes2, ref count);
663 }
664 else
665 {
666 b = encodingCharBuffer.GetNextByte();
667 if (b == 27)
668 {
669 if (count == 0)
670 {
671 bytes2[0] = b;
672 count = 1;
673 continue;
674 }
675 encodingCharBuffer.AdjustBytes(-1);
676 }
677 }
678 switch (b)
679 {
680 case 14:
681 iSO2022Modes2 = iSO2022Modes;
682 iSO2022Modes = ISO2022Modes.ModeHalfwidthKatakana;
683 continue;
684 case 15:
685 iSO2022Modes = iSO2022Modes2;
686 continue;
687 }
688 ushort num = b;
689 bool flag = false;
690 if (iSO2022Modes == ISO2022Modes.ModeJIS0208)
691 {
692 if (count > 0)
693 {
694 if (bytes2[0] != 27)
695 {
696 num <<= 8;
697 num |= DecrementEscapeBytes(ref bytes2, ref count);
698 flag = true;
699 }
700 }
701 else
702 {
703 if (!encodingCharBuffer.MoreData)
704 {
705 if (decoder == null || decoder.MustFlush)
706 {
707 encodingCharBuffer.Fallback(b);
708 }
709 else if (chars != null)
710 {
711 bytes2[0] = b;
712 count = 1;
713 }
714 break;
715 }
716 num <<= 8;
717 num |= encodingCharBuffer.GetNextByte();
718 flag = true;
719 }
720 if (flag && (num & 0xFF00) == 10752)
721 {
722 num = (ushort)(num & 0xFFu);
723 num = (ushort)(num | 0x1000u);
724 }
725 }
726 else if (num >= 161 && num <= 223)
727 {
728 num = (ushort)(num | 0x1000u);
729 num = (ushort)(num & 0xFF7Fu);
730 }
731 else if (iSO2022Modes == ISO2022Modes.ModeHalfwidthKatakana)
732 {
733 num = (ushort)(num | 0x1000u);
734 }
735 char c = mapBytesToUnicode[(int)num];
736 if (c == '\0' && num != 0)
737 {
738 if (flag)
739 {
740 if (!encodingCharBuffer.Fallback((byte)(num >> 8), (byte)num))
741 {
742 break;
743 }
744 }
745 else if (!encodingCharBuffer.Fallback(b))
746 {
747 break;
748 }
749 }
750 else if (!encodingCharBuffer.AddChar(c, (!flag) ? 1 : 2))
751 {
752 break;
753 }
754 }
755 if (chars != null && decoder != null)
756 {
757 if (!decoder.MustFlush || count != 0)
758 {
759 decoder.currentMode = iSO2022Modes;
760 decoder.shiftInOutMode = iSO2022Modes2;
761 decoder.bytesLeftOverCount = count;
762 decoder.bytesLeftOver = bytes2;
763 }
764 else
765 {
766 decoder.currentMode = ISO2022Modes.ModeASCII;
767 decoder.shiftInOutMode = ISO2022Modes.ModeASCII;
768 decoder.bytesLeftOverCount = 0;
769 }
770 decoder.m_bytesUsed = encodingCharBuffer.BytesUsed;
771 }
772 return encodingCharBuffer.Count;
773 }
774
775 private ISO2022Modes CheckEscapeSequenceJP(byte[] bytes, int escapeCount)
776 {
777 if (bytes[0] != 27)
778 {
779 return ISO2022Modes.ModeInvalidEscape;
780 }
781 if (escapeCount < 3)
782 {
783 return ISO2022Modes.ModeIncompleteEscape;
784 }
785 if (bytes[1] == 40)
786 {
787 if (bytes[2] == 66)
788 {
789 return ISO2022Modes.ModeASCII;
790 }
791 if (bytes[2] == 72)
792 {
793 return ISO2022Modes.ModeASCII;
794 }
795 if (bytes[2] == 74)
796 {
797 return ISO2022Modes.ModeASCII;
798 }
799 if (bytes[2] == 73)
800 {
801 return ISO2022Modes.ModeHalfwidthKatakana;
802 }
803 }
804 else if (bytes[1] == 36)
805 {
806 if (bytes[2] == 64 || bytes[2] == 66)
807 {
808 return ISO2022Modes.ModeJIS0208;
809 }
810 if (escapeCount < 4)
811 {
812 return ISO2022Modes.ModeIncompleteEscape;
813 }
814 if (bytes[2] == 40 && bytes[3] == 68)
815 {
816 return ISO2022Modes.ModeJIS0208;
817 }
818 }
819 else if (bytes[1] == 38 && bytes[2] == 64)
820 {
821 return ISO2022Modes.ModeNOOP;
822 }
823 return ISO2022Modes.ModeInvalidEscape;
824 }
825
826 private byte DecrementEscapeBytes(ref byte[] bytes, ref int count)
827 {
828 count--;
829 byte result = bytes[0];
830 for (int i = 0; i < count; i++)
831 {
832 bytes[i] = bytes[i + 1];
833 }
834 bytes[count] = 0;
835 return result;
836 }
837
838 private unsafe int GetCharsCP50225KR(byte* bytes, int byteCount, char* chars, int charCount, ISO2022Decoder decoder)
839 {
840 EncodingCharBuffer encodingCharBuffer = new EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
841 ISO2022Modes iSO2022Modes = ISO2022Modes.ModeASCII;
842 byte[] bytes2 = new byte[4];
843 int count = 0;
844 if (decoder != null)
845 {
846 iSO2022Modes = decoder.currentMode;
847 count = decoder.bytesLeftOverCount;
848 for (int i = 0; i < count; i++)
849 {
850 bytes2[i] = decoder.bytesLeftOver[i];
851 }
852 }
853 while (encodingCharBuffer.MoreData || count > 0)
854 {
855 byte b;
856 if (count > 0)
857 {
858 if (bytes2[0] == 27)
859 {
860 if (!encodingCharBuffer.MoreData)
861 {
862 if (decoder != null && !decoder.MustFlush)
863 {
864 break;
865 }
866 }
867 else
868 {
869 bytes2[count++] = encodingCharBuffer.GetNextByte();
870 switch (CheckEscapeSequenceKR(bytes2, count))
871 {
872 default:
873 count = 0;
874 continue;
875 case ISO2022Modes.ModeInvalidEscape:
876 break;
877 case ISO2022Modes.ModeIncompleteEscape:
878 continue;
879 }
880 }
881 }
882 b = DecrementEscapeBytes(ref bytes2, ref count);
883 }
884 else
885 {
886 b = encodingCharBuffer.GetNextByte();
887 if (b == 27)
888 {
889 if (count == 0)
890 {
891 bytes2[0] = b;
892 count = 1;
893 continue;
894 }
895 encodingCharBuffer.AdjustBytes(-1);
896 }
897 }
898 switch (b)
899 {
900 case 14:
901 iSO2022Modes = ISO2022Modes.ModeKR;
902 continue;
903 case 15:
904 iSO2022Modes = ISO2022Modes.ModeASCII;
905 continue;
906 }
907 ushort num = b;
908 bool flag = false;
909 if (iSO2022Modes == ISO2022Modes.ModeKR && b != 32 && b != 9 && b != 10)
910 {
911 if (count > 0)
912 {
913 if (bytes2[0] != 27)
914 {
915 num <<= 8;
916 num |= DecrementEscapeBytes(ref bytes2, ref count);
917 flag = true;
918 }
919 }
920 else
921 {
922 if (!encodingCharBuffer.MoreData)
923 {
924 if (decoder == null || decoder.MustFlush)
925 {
926 encodingCharBuffer.Fallback(b);
927 }
928 else if (chars != null)
929 {
930 bytes2[0] = b;
931 count = 1;
932 }
933 break;
934 }
935 num <<= 8;
936 num |= encodingCharBuffer.GetNextByte();
937 flag = true;
938 }
939 }
940 char c = mapBytesToUnicode[(int)num];
941 if (c == '\0' && num != 0)
942 {
943 if (flag)
944 {
945 if (!encodingCharBuffer.Fallback((byte)(num >> 8), (byte)num))
946 {
947 break;
948 }
949 }
950 else if (!encodingCharBuffer.Fallback(b))
951 {
952 break;
953 }
954 }
955 else if (!encodingCharBuffer.AddChar(c, (!flag) ? 1 : 2))
956 {
957 break;
958 }
959 }
960 if (chars != null && decoder != null)
961 {
962 if (!decoder.MustFlush || count != 0)
963 {
964 decoder.currentMode = iSO2022Modes;
965 decoder.bytesLeftOverCount = count;
966 decoder.bytesLeftOver = bytes2;
967 }
968 else
969 {
970 decoder.currentMode = ISO2022Modes.ModeASCII;
971 decoder.shiftInOutMode = ISO2022Modes.ModeASCII;
972 decoder.bytesLeftOverCount = 0;
973 }
974 decoder.m_bytesUsed = encodingCharBuffer.BytesUsed;
975 }
976 return encodingCharBuffer.Count;
977 }
978
979 private ISO2022Modes CheckEscapeSequenceKR(byte[] bytes, int escapeCount)
980 {
981 if (bytes[0] != 27)
982 {
983 return ISO2022Modes.ModeInvalidEscape;
984 }
985 if (escapeCount < 4)
986 {
987 return ISO2022Modes.ModeIncompleteEscape;
988 }
989 if (bytes[1] == 36 && bytes[2] == 41 && bytes[3] == 67)
990 {
991 return ISO2022Modes.ModeKR;
992 }
993 return ISO2022Modes.ModeInvalidEscape;
994 }
995
996 private unsafe int GetCharsCP52936(byte* bytes, int byteCount, char* chars, int charCount, ISO2022Decoder decoder)
997 {
998 EncodingCharBuffer encodingCharBuffer = new EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
999 ISO2022Modes iSO2022Modes = ISO2022Modes.ModeASCII;
1000 int num = -1;
1001 bool flag = false;
1002 if (decoder != null)
1003 {
1004 iSO2022Modes = decoder.currentMode;
1005 if (decoder.bytesLeftOverCount != 0)
1006 {
1007 num = decoder.bytesLeftOver[0];
1008 }
1009 }
1010 while (encodingCharBuffer.MoreData || num >= 0)
1011 {
1012 byte b;
1013 if (num >= 0)
1014 {
1015 b = (byte)num;
1016 num = -1;
1017 }
1018 else
1019 {
1020 b = encodingCharBuffer.GetNextByte();
1021 }
1022 if (b == 126)
1023 {
1024 if (!encodingCharBuffer.MoreData)
1025 {
1026 if (decoder == null || decoder.MustFlush)
1027 {
1028 encodingCharBuffer.Fallback(b);
1029 break;
1030 }
1031 decoder.ClearMustFlush();
1032 if (chars != null)
1033 {
1034 decoder.bytesLeftOverCount = 1;
1035 decoder.bytesLeftOver[0] = 126;
1036 flag = true;
1037 }
1038 break;
1039 }
1040 b = encodingCharBuffer.GetNextByte();
1041 if (b == 126 && iSO2022Modes == ISO2022Modes.ModeASCII)
1042 {
1043 if (!encodingCharBuffer.AddChar((char)b, 2))
1044 {
1045 break;
1046 }
1047 continue;
1048 }
1049 if (b == 123)
1050 {
1051 iSO2022Modes = ISO2022Modes.ModeHZ;
1052 continue;
1053 }
1054 if (b == 125)
1055 {
1056 iSO2022Modes = ISO2022Modes.ModeASCII;
1057 continue;
1058 }
1059 if (b == 10)
1060 {
1061 continue;
1062 }
1063 encodingCharBuffer.AdjustBytes(-1);
1064 b = 126;
1065 }
1066 if (iSO2022Modes != ISO2022Modes.ModeASCII && b >= 32)
1067 {
1068 if (!encodingCharBuffer.MoreData)
1069 {
1070 if (decoder == null || decoder.MustFlush)
1071 {
1072 encodingCharBuffer.Fallback(b);
1073 break;
1074 }
1075 decoder.ClearMustFlush();
1076 if (chars != null)
1077 {
1078 decoder.bytesLeftOverCount = 1;
1079 decoder.bytesLeftOver[0] = b;
1080 flag = true;
1081 }
1082 break;
1083 }
1084 byte nextByte = encodingCharBuffer.GetNextByte();
1085 ushort num2 = (ushort)((b << 8) | nextByte);
1086 char c;
1087 if (b == 32 && nextByte != 0)
1088 {
1089 c = (char)nextByte;
1090 }
1091 else
1092 {
1093 if ((b < 33 || b > 119 || nextByte < 33 || nextByte > 126) && (b < 161 || b > 247 || nextByte < 161 || nextByte > 254))
1094 {
1095 if (nextByte != 32 || 33 > b || b > 125)
1096 {
1097 if (!encodingCharBuffer.Fallback((byte)(num2 >> 8), (byte)num2))
1098 {
1099 break;
1100 }
1101 continue;
1102 }
1103 num2 = 8481;
1104 }
1105 num2 = (ushort)(num2 | 0x8080u);
1106 c = mapBytesToUnicode[(int)num2];
1107 }
1108 if (c == '\0' && num2 != 0)
1109 {
1110 if (!encodingCharBuffer.Fallback((byte)(num2 >> 8), (byte)num2))
1111 {
1112 break;
1113 }
1114 }
1115 else if (!encodingCharBuffer.AddChar(c, 2))
1116 {
1117 break;
1118 }
1119 continue;
1120 }
1121 char c2 = mapBytesToUnicode[(int)b];
1122 if ((c2 == '\0' || c2 == '\0') && b != 0)
1123 {
1124 if (!encodingCharBuffer.Fallback(b))
1125 {
1126 break;
1127 }
1128 }
1129 else if (!encodingCharBuffer.AddChar(c2))
1130 {
1131 break;
1132 }
1133 }
1134 if (chars != null && decoder != null)
1135 {
1136 if (!flag)
1137 {
1138 decoder.bytesLeftOverCount = 0;
1139 }
1140 if (decoder.MustFlush && decoder.bytesLeftOverCount == 0)
1141 {
1142 decoder.currentMode = ISO2022Modes.ModeASCII;
1143 }
1144 else
1145 {
1146 decoder.currentMode = iSO2022Modes;
1147 }
1148 decoder.m_bytesUsed = encodingCharBuffer.BytesUsed;
1149 }
1150 return encodingCharBuffer.Count;
1151 }
1152
1153 public override int GetMaxByteCount(int charCount)
1154 {
1155 if (charCount < 0)
1156 {
1158 }
1159 long num = (long)charCount + 1L;
1160 if (base.EncoderFallback.MaxCharCount > 1)
1161 {
1162 num *= base.EncoderFallback.MaxCharCount;
1163 }
1164 int num2 = 2;
1165 int num3 = 0;
1166 int num4 = 0;
1167 switch (CodePage)
1168 {
1169 case 50220:
1170 case 50221:
1171 num2 = 5;
1172 num4 = 3;
1173 break;
1174 case 50222:
1175 num2 = 5;
1176 num4 = 4;
1177 break;
1178 case 50225:
1179 num2 = 3;
1180 num3 = 4;
1181 num4 = 1;
1182 break;
1183 case 52936:
1184 num2 = 4;
1185 num4 = 2;
1186 break;
1187 }
1188 num *= num2;
1189 num += num3 + num4;
1190 if (num > int.MaxValue)
1191 {
1193 }
1194 return (int)num;
1195 }
1196
1197 public override int GetMaxCharCount(int byteCount)
1198 {
1199 if (byteCount < 0)
1200 {
1202 }
1203 int num = 1;
1204 int num2 = 1;
1205 switch (CodePage)
1206 {
1207 case 50220:
1208 case 50221:
1209 case 50222:
1210 case 50225:
1211 num = 1;
1212 num2 = 3;
1213 break;
1214 case 52936:
1215 num = 1;
1216 num2 = 1;
1217 break;
1218 }
1219 long num3 = (long)byteCount * (long)num + num2;
1220 if (base.DecoderFallback.MaxCharCount > 1)
1221 {
1222 num3 *= base.DecoderFallback.MaxCharCount;
1223 }
1224 if (num3 > int.MaxValue)
1225 {
1227 }
1228 return (int)num3;
1229 }
1230
1231 public override Encoder GetEncoder()
1232 {
1233 return new ISO2022Encoder(this);
1234 }
1235
1236 public override Decoder GetDecoder()
1237 {
1238 return new ISO2022Decoder(this);
1239 }
1240}
static string ArgumentOutOfRange_GetCharCountOverflow
Definition SR.cs:90
static string ArgumentOutOfRange_GetByteCountOverflow
Definition SR.cs:88
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
DecoderFallbackBuffer m_fallbackBuffer
Definition DecoderNLS.cs:17
EncoderFallbackBuffer m_fallbackBuffer
Definition EncoderNLS.cs:19
unsafe bool AddByte(byte b, int moreBytesExpected)
Definition Encoding.cs:271
unsafe bool AddChar(char ch, int numBytes)
Definition Encoding.cs:145
bool Fallback(byte fallbackByte)
Definition Encoding.cs:180
unsafe void AdjustBytes(int count)
Definition Encoding.cs:166
virtual int CodePage
Definition Encoding.cs:515
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, System.Text.EncoderNLS baseEncoder)
ISO2022Modes CheckEscapeSequenceKR(byte[] bytes, int escapeCount)
override bool CleanUpBytes(ref int bytes)
static readonly ushort[] s_HalfToFullWidthKanaTable
unsafe int GetBytesCP50225KR(char *chars, int charCount, byte *bytes, int byteCount, ISO2022Encoder encoder)
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, System.Text.DecoderNLS baseDecoder)
unsafe int GetBytesCP52936(char *chars, int charCount, byte *bytes, int byteCount, ISO2022Encoder encoder)
unsafe override int GetCharCount(byte *bytes, int count, System.Text.DecoderNLS baseDecoder)
unsafe int GetCharsCP50225KR(byte *bytes, int byteCount, char *chars, int charCount, ISO2022Decoder decoder)
override int GetMaxByteCount(int charCount)
unsafe int GetCharsCP52936(byte *bytes, int byteCount, char *chars, int charCount, ISO2022Decoder decoder)
override int GetMaxCharCount(int byteCount)
unsafe int GetCharsCP5022xJP(byte *bytes, int byteCount, char *chars, int charCount, ISO2022Decoder decoder)
ISO2022Modes CheckEscapeSequenceJP(byte[] bytes, int escapeCount)
unsafe override int GetByteCount(char *chars, int count, System.Text.EncoderNLS baseEncoder)
unsafe int GetBytesCP5022xJP(char *chars, int charCount, byte *bytes, int byteCount, ISO2022Encoder encoder)
byte DecrementEscapeBytes(ref byte[] bytes, ref int count)
static readonly int[] s_tableBaseCodePages