Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UnicodeEncoding.cs
Go to the documentation of this file.
4
5namespace System.Text;
6
8{
9 private sealed class Decoder : DecoderNLS
10 {
11 internal int lastByte = -1;
12
13 internal char lastChar;
14
15 internal override bool HasState
16 {
17 get
18 {
19 if (lastByte == -1)
20 {
21 return lastChar != '\0';
22 }
23 return true;
24 }
25 }
26
27 public Decoder(UnicodeEncoding encoding)
28 : base(encoding)
29 {
30 }
31
32 public override void Reset()
33 {
34 lastByte = -1;
35 lastChar = '\0';
36 if (_fallbackBuffer != null)
37 {
39 }
40 }
41 }
42
43 internal static readonly UnicodeEncoding s_bigEndianDefault = new UnicodeEncoding(bigEndian: true, byteOrderMark: true);
44
45 internal static readonly UnicodeEncoding s_littleEndianDefault = new UnicodeEncoding(bigEndian: false, byteOrderMark: true);
46
47 private readonly bool isThrowException;
48
49 private readonly bool bigEndian;
50
51 private readonly bool byteOrderMark;
52
53 public const int CharSize = 2;
54
56 {
57 get
58 {
59 if (!(GetType() != typeof(UnicodeEncoding)))
60 {
61 if (byteOrderMark)
62 {
63 if (bigEndian)
64 {
65 return new byte[2] { 254, 255 };
66 }
67 return new byte[2] { 255, 254 };
68 }
69 return default(ReadOnlySpan<byte>);
70 }
71 return new ReadOnlySpan<byte>(GetPreamble());
72 }
73 }
74
76 : this(bigEndian: false, byteOrderMark: true)
77 {
78 }
79
81 : base(bigEndian ? 1201 : 1200)
82 {
83 this.bigEndian = bigEndian;
84 this.byteOrderMark = byteOrderMark;
85 }
86
87 public UnicodeEncoding(bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes)
89 {
90 isThrowException = throwOnInvalidBytes;
92 {
94 }
95 }
96
97 internal sealed override void SetDefaultFallbacks()
98 {
100 {
103 }
104 else
105 {
108 }
109 }
110
111 public unsafe override int GetByteCount(char[] chars, int index, int count)
112 {
113 if (chars == null)
114 {
115 throw new ArgumentNullException("chars", SR.ArgumentNull_Array);
116 }
117 if (index < 0 || count < 0)
118 {
119 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
120 }
121 if (chars.Length - index < count)
122 {
124 }
125 if (count == 0)
126 {
127 return 0;
128 }
129 fixed (char* ptr = chars)
130 {
131 return GetByteCount(ptr + index, count, null);
132 }
133 }
134
135 public unsafe override int GetByteCount(string s)
136 {
137 if (s == null)
138 {
140 }
141 fixed (char* pChars = s)
142 {
143 return GetByteCount(pChars, s.Length, null);
144 }
145 }
146
147 [CLSCompliant(false)]
148 public unsafe override int GetByteCount(char* chars, int count)
149 {
150 if (chars == null)
151 {
152 throw new ArgumentNullException("chars", SR.ArgumentNull_Array);
153 }
154 if (count < 0)
155 {
157 }
158 return GetByteCount(chars, count, null);
159 }
160
161 public unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
162 {
163 if (s == null || bytes == null)
164 {
165 throw new ArgumentNullException((s == null) ? "s" : "bytes", SR.ArgumentNull_Array);
166 }
167 if (charIndex < 0 || charCount < 0)
168 {
169 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
170 }
171 if (s.Length - charIndex < charCount)
172 {
174 }
175 if (byteIndex < 0 || byteIndex > bytes.Length)
176 {
178 }
179 int byteCount = bytes.Length - byteIndex;
180 fixed (char* ptr = s)
181 {
182 fixed (byte* ptr2 = &MemoryMarshal.GetReference<byte>(bytes))
183 {
184 return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
185 }
186 }
187 }
188
189 public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
190 {
191 if (chars == null || bytes == null)
192 {
193 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", SR.ArgumentNull_Array);
194 }
195 if (charIndex < 0 || charCount < 0)
196 {
197 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
198 }
199 if (chars.Length - charIndex < charCount)
200 {
202 }
203 if (byteIndex < 0 || byteIndex > bytes.Length)
204 {
206 }
207 if (charCount == 0)
208 {
209 return 0;
210 }
211 int byteCount = bytes.Length - byteIndex;
212 fixed (char* ptr = chars)
213 {
214 fixed (byte* ptr2 = &MemoryMarshal.GetReference<byte>(bytes))
215 {
216 return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
217 }
218 }
219 }
220
221 [CLSCompliant(false)]
222 public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
223 {
224 if (bytes == null || chars == null)
225 {
226 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
227 }
228 if (charCount < 0 || byteCount < 0)
229 {
230 throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
231 }
232 return GetBytes(chars, charCount, bytes, byteCount, null);
233 }
234
235 public unsafe override int GetCharCount(byte[] bytes, int index, int count)
236 {
237 if (bytes == null)
238 {
239 throw new ArgumentNullException("bytes", SR.ArgumentNull_Array);
240 }
241 if (index < 0 || count < 0)
242 {
243 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
244 }
245 if (bytes.Length - index < count)
246 {
248 }
249 if (count == 0)
250 {
251 return 0;
252 }
253 fixed (byte* ptr = bytes)
254 {
255 return GetCharCount(ptr + index, count, null);
256 }
257 }
258
259 [CLSCompliant(false)]
260 public unsafe override int GetCharCount(byte* bytes, int count)
261 {
262 if (bytes == null)
263 {
264 throw new ArgumentNullException("bytes", SR.ArgumentNull_Array);
265 }
266 if (count < 0)
267 {
269 }
270 return GetCharCount(bytes, count, null);
271 }
272
273 public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
274 {
275 if (bytes == null || chars == null)
276 {
277 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
278 }
279 if (byteIndex < 0 || byteCount < 0)
280 {
281 throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
282 }
283 if (bytes.Length - byteIndex < byteCount)
284 {
286 }
287 if (charIndex < 0 || charIndex > chars.Length)
288 {
290 }
291 if (byteCount == 0)
292 {
293 return 0;
294 }
295 int charCount = chars.Length - charIndex;
296 fixed (byte* ptr = bytes)
297 {
298 fixed (char* ptr2 = &MemoryMarshal.GetReference<char>(chars))
299 {
300 return GetChars(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount, null);
301 }
302 }
303 }
304
305 [CLSCompliant(false)]
306 public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
307 {
308 if (bytes == null || chars == null)
309 {
310 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
311 }
312 if (charCount < 0 || byteCount < 0)
313 {
314 throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
315 }
316 return GetChars(bytes, byteCount, chars, charCount, null);
317 }
318
319 public unsafe override string GetString(byte[] bytes, int index, int count)
320 {
321 if (bytes == null)
322 {
323 throw new ArgumentNullException("bytes", SR.ArgumentNull_Array);
324 }
325 if (index < 0 || count < 0)
326 {
327 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
328 }
329 if (bytes.Length - index < count)
330 {
332 }
333 if (count == 0)
334 {
335 return string.Empty;
336 }
337 fixed (byte* ptr = bytes)
338 {
339 return string.CreateStringFromEncoding(ptr + index, count, this);
340 }
341 }
342
343 internal unsafe sealed override int GetByteCount(char* chars, int count, EncoderNLS encoder)
344 {
345 int num = count << 1;
346 if (num < 0)
347 {
349 }
350 char* charStart = chars;
351 char* ptr = chars + count;
352 char c = '\0';
353 bool flag = false;
354 EncoderFallbackBuffer encoderFallbackBuffer = null;
355 if (encoder != null)
356 {
357 c = encoder._charLeftOver;
358 if (c > '\0')
359 {
360 num += 2;
361 }
362 if (encoder.InternalHasFallbackBuffer)
363 {
364 encoderFallbackBuffer = encoder.FallbackBuffer;
365 if (encoderFallbackBuffer.Remaining > 0)
366 {
368 }
369 encoderFallbackBuffer.InternalInitialize(charStart, ptr, encoder, setEncoder: false);
370 }
371 }
372 while (true)
373 {
374 char num2 = encoderFallbackBuffer?.InternalGetNextChar() ?? '\0';
375 char c2 = num2;
376 char* chars2;
377 if (num2 != 0 || chars < ptr)
378 {
379 if (c2 == '\0')
380 {
381 if ((bigEndian ^ BitConverter.IsLittleEndian) && ((ulong)chars & 7uL) == 0L && c == '\0')
382 {
383 ulong* ptr2 = (ulong*)(ptr - 3);
384 ulong* ptr3;
385 for (ptr3 = (ulong*)chars; ptr3 < ptr2; ptr3++)
386 {
387 if ((0x8000800080008000uL & *ptr3) == 0L)
388 {
389 continue;
390 }
391 ulong num3 = (0xF800F800F800F800uL & *ptr3) ^ 0xD800D800D800D800uL;
392 if ((num3 & 0xFFFF000000000000uL) == 0L || (num3 & 0xFFFF00000000L) == 0L || (num3 & 0xFFFF0000u) == 0L || (num3 & 0xFFFF) == 0L)
393 {
394 long num4 = -287953294993589248L & (long)(*ptr3);
396 {
397 }
398 if (num4 != -2593835887162763264L)
399 {
400 break;
401 }
402 }
403 }
404 chars = (char*)ptr3;
405 if (chars >= ptr)
406 {
407 goto IL_0295;
408 }
409 }
410 c2 = *chars;
411 chars++;
412 }
413 else
414 {
415 num += 2;
416 }
417 if (c2 >= '\ud800' && c2 <= '\udfff')
418 {
419 if (c2 <= '\udbff')
420 {
421 if (c > '\0')
422 {
423 chars--;
424 num -= 2;
425 if (encoderFallbackBuffer == null)
426 {
427 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
428 encoderFallbackBuffer.InternalInitialize(charStart, ptr, encoder, setEncoder: false);
429 }
430 chars2 = chars;
431 encoderFallbackBuffer.InternalFallback(c, ref chars2);
432 chars = chars2;
433 c = '\0';
434 }
435 else
436 {
437 c = c2;
438 }
439 }
440 else if (c == '\0')
441 {
442 num -= 2;
443 if (encoderFallbackBuffer == null)
444 {
445 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
446 encoderFallbackBuffer.InternalInitialize(charStart, ptr, encoder, setEncoder: false);
447 }
448 chars2 = chars;
449 encoderFallbackBuffer.InternalFallback(c2, ref chars2);
450 chars = chars2;
451 }
452 else
453 {
454 c = '\0';
455 }
456 }
457 else if (c > '\0')
458 {
459 chars--;
460 if (encoderFallbackBuffer == null)
461 {
462 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
463 encoderFallbackBuffer.InternalInitialize(charStart, ptr, encoder, setEncoder: false);
464 }
465 chars2 = chars;
466 encoderFallbackBuffer.InternalFallback(c, ref chars2);
467 chars = chars2;
468 num -= 2;
469 c = '\0';
470 }
471 continue;
472 }
473 goto IL_0295;
474 IL_0295:
475 if (c <= '\0')
476 {
477 break;
478 }
479 num -= 2;
480 if (encoder != null && !encoder.MustFlush)
481 {
482 break;
483 }
484 if (flag)
485 {
487 }
488 if (encoderFallbackBuffer == null)
489 {
490 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
491 encoderFallbackBuffer.InternalInitialize(charStart, ptr, encoder, setEncoder: false);
492 }
493 chars2 = chars;
494 encoderFallbackBuffer.InternalFallback(c, ref chars2);
495 chars = chars2;
496 c = '\0';
497 flag = true;
498 }
499 return num;
500 }
501
502 internal unsafe sealed override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS encoder)
503 {
504 char c = '\0';
505 bool flag = false;
506 byte* ptr = bytes + byteCount;
507 char* ptr2 = chars + charCount;
508 byte* ptr3 = bytes;
509 char* ptr4 = chars;
510 EncoderFallbackBuffer encoderFallbackBuffer = null;
511 if (encoder != null)
512 {
513 c = encoder._charLeftOver;
514 if (encoder.InternalHasFallbackBuffer)
515 {
516 encoderFallbackBuffer = encoder.FallbackBuffer;
517 if (encoderFallbackBuffer.Remaining > 0 && encoder._throwOnOverflow)
518 {
520 }
521 encoderFallbackBuffer.InternalInitialize(ptr4, ptr2, encoder, setEncoder: false);
522 }
523 }
524 while (true)
525 {
526 char num = encoderFallbackBuffer?.InternalGetNextChar() ?? '\0';
527 char c2 = num;
528 char* chars2;
529 if (num != 0 || chars < ptr2)
530 {
531 if (c2 == '\0')
532 {
533 if ((bigEndian ^ BitConverter.IsLittleEndian) && ((ulong)chars & 7uL) == 0L && c == '\0')
534 {
535 ulong* ptr5 = (ulong*)(chars - 3 + ((ptr - bytes >> 1 < ptr2 - chars) ? (ptr - bytes >> 1) : (ptr2 - chars)));
536 ulong* ptr6 = (ulong*)chars;
537 ulong* ptr7 = (ulong*)bytes;
538 while (ptr6 < ptr5)
539 {
540 if ((0x8000800080008000uL & *ptr6) != 0L)
541 {
542 ulong num2 = (0xF800F800F800F800uL & *ptr6) ^ 0xD800D800D800D800uL;
543 if ((num2 & 0xFFFF000000000000uL) == 0L || (num2 & 0xFFFF00000000L) == 0L || (num2 & 0xFFFF0000u) == 0L || (num2 & 0xFFFF) == 0L)
544 {
545 long num3 = -287953294993589248L & (long)(*ptr6);
547 {
548 }
549 if (num3 != -2593835887162763264L)
550 {
551 break;
552 }
553 }
554 }
555 Unsafe.WriteUnaligned(ptr7, *ptr6);
556 ptr6++;
557 ptr7++;
558 }
559 chars = (char*)ptr6;
560 bytes = (byte*)ptr7;
561 if (chars >= ptr2)
562 {
563 goto IL_039f;
564 }
565 }
566 c2 = *chars;
567 chars++;
568 }
569 if (c2 >= '\ud800' && c2 <= '\udfff')
570 {
571 if (c2 <= '\udbff')
572 {
573 if (c > '\0')
574 {
575 chars--;
576 if (encoderFallbackBuffer == null)
577 {
578 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
579 encoderFallbackBuffer.InternalInitialize(ptr4, ptr2, encoder, setEncoder: true);
580 }
581 chars2 = chars;
582 encoderFallbackBuffer.InternalFallback(c, ref chars2);
583 chars = chars2;
584 c = '\0';
585 }
586 else
587 {
588 c = c2;
589 }
590 continue;
591 }
592 if (c == '\0')
593 {
594 if (encoderFallbackBuffer == null)
595 {
596 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
597 encoderFallbackBuffer.InternalInitialize(ptr4, ptr2, encoder, setEncoder: true);
598 }
599 chars2 = chars;
600 encoderFallbackBuffer.InternalFallback(c2, ref chars2);
601 chars = chars2;
602 continue;
603 }
604 if (bytes + 3 >= ptr)
605 {
606 if (encoderFallbackBuffer != null && encoderFallbackBuffer.bFallingBack)
607 {
608 encoderFallbackBuffer.MovePrevious();
609 encoderFallbackBuffer.MovePrevious();
610 }
611 else
612 {
613 chars -= 2;
614 }
615 ThrowBytesOverflow(encoder, bytes == ptr3);
616 c = '\0';
617 goto IL_039f;
618 }
619 if (bigEndian)
620 {
621 *(bytes++) = (byte)((int)c >> 8);
622 *(bytes++) = (byte)c;
623 }
624 else
625 {
626 *(bytes++) = (byte)c;
627 *(bytes++) = (byte)((int)c >> 8);
628 }
629 c = '\0';
630 }
631 else if (c > '\0')
632 {
633 chars--;
634 if (encoderFallbackBuffer == null)
635 {
636 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
637 encoderFallbackBuffer.InternalInitialize(ptr4, ptr2, encoder, setEncoder: true);
638 }
639 chars2 = chars;
640 encoderFallbackBuffer.InternalFallback(c, ref chars2);
641 chars = chars2;
642 c = '\0';
643 continue;
644 }
645 if (bytes + 1 < ptr)
646 {
647 if (bigEndian)
648 {
649 *(bytes++) = (byte)((int)c2 >> 8);
650 *(bytes++) = (byte)c2;
651 }
652 else
653 {
654 *(bytes++) = (byte)c2;
655 *(bytes++) = (byte)((int)c2 >> 8);
656 }
657 continue;
658 }
659 if (encoderFallbackBuffer != null && encoderFallbackBuffer.bFallingBack)
660 {
661 encoderFallbackBuffer.MovePrevious();
662 }
663 else
664 {
665 chars--;
666 }
667 ThrowBytesOverflow(encoder, bytes == ptr3);
668 }
669 goto IL_039f;
670 IL_039f:
671 if (c <= '\0' || (encoder != null && !encoder.MustFlush))
672 {
673 break;
674 }
675 if (flag)
676 {
678 }
679 if (encoderFallbackBuffer == null)
680 {
681 encoderFallbackBuffer = ((encoder != null) ? encoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
682 encoderFallbackBuffer.InternalInitialize(ptr4, ptr2, encoder, setEncoder: true);
683 }
684 chars2 = chars;
685 encoderFallbackBuffer.InternalFallback(c, ref chars2);
686 chars = chars2;
687 c = '\0';
688 flag = true;
689 }
690 if (encoder != null)
691 {
692 encoder._charLeftOver = c;
693 encoder._charsUsed = (int)(chars - ptr4);
694 }
695 return (int)(bytes - ptr3);
696 }
697
698 internal unsafe sealed override int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
699 {
700 Decoder decoder = (Decoder)baseDecoder;
701 byte* ptr = bytes + count;
702 byte* byteStart = bytes;
703 int num = -1;
704 char c = '\0';
705 int num2 = count >> 1;
706 DecoderFallbackBuffer decoderFallbackBuffer = null;
707 if (decoder != null)
708 {
709 num = decoder.lastByte;
710 c = decoder.lastChar;
711 if (c > '\0')
712 {
713 num2++;
714 }
715 if (num >= 0 && (count & 1) == 1)
716 {
717 num2++;
718 }
719 }
720 while (bytes < ptr)
721 {
722 if ((bigEndian ^ BitConverter.IsLittleEndian) && ((ulong)bytes & 7uL) == 0L && num == -1 && c == '\0')
723 {
724 ulong* ptr2 = (ulong*)(ptr - 7);
725 ulong* ptr3;
726 for (ptr3 = (ulong*)bytes; ptr3 < ptr2; ptr3++)
727 {
728 if ((0x8000800080008000uL & *ptr3) == 0L)
729 {
730 continue;
731 }
732 ulong num3 = (0xF800F800F800F800uL & *ptr3) ^ 0xD800D800D800D800uL;
733 if ((num3 & 0xFFFF000000000000uL) == 0L || (num3 & 0xFFFF00000000L) == 0L || (num3 & 0xFFFF0000u) == 0L || (num3 & 0xFFFF) == 0L)
734 {
735 long num4 = -287953294993589248L & (long)(*ptr3);
737 {
738 }
739 if (num4 != -2593835887162763264L)
740 {
741 break;
742 }
743 }
744 }
745 bytes = (byte*)ptr3;
746 if (bytes >= ptr)
747 {
748 break;
749 }
750 }
751 if (num < 0)
752 {
753 num = *(bytes++);
754 if (bytes >= ptr)
755 {
756 break;
757 }
758 }
759 char c2 = ((!bigEndian) ? ((char)((*(bytes++) << 8) | num)) : ((char)((num << 8) | *(bytes++))));
760 num = -1;
761 if (c2 >= '\ud800' && c2 <= '\udfff')
762 {
763 if (c2 <= '\udbff')
764 {
765 if (c > '\0')
766 {
767 num2--;
768 byte[] array = null;
769 array = ((!bigEndian) ? new byte[2]
770 {
771 (byte)c,
772 (byte)((int)c >> 8)
773 } : new byte[2]
774 {
775 (byte)((int)c >> 8),
776 (byte)c
777 });
778 if (decoderFallbackBuffer == null)
779 {
780 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
781 decoderFallbackBuffer.InternalInitialize(byteStart, null);
782 }
783 num2 += decoderFallbackBuffer.InternalFallback(array, bytes);
784 }
785 c = c2;
786 }
787 else if (c == '\0')
788 {
789 num2--;
790 byte[] array2 = null;
791 array2 = ((!bigEndian) ? new byte[2]
792 {
793 (byte)c2,
794 (byte)((int)c2 >> 8)
795 } : new byte[2]
796 {
797 (byte)((int)c2 >> 8),
798 (byte)c2
799 });
800 if (decoderFallbackBuffer == null)
801 {
802 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
803 decoderFallbackBuffer.InternalInitialize(byteStart, null);
804 }
805 num2 += decoderFallbackBuffer.InternalFallback(array2, bytes);
806 }
807 else
808 {
809 c = '\0';
810 }
811 }
812 else if (c > '\0')
813 {
814 num2--;
815 byte[] array3 = null;
816 array3 = ((!bigEndian) ? new byte[2]
817 {
818 (byte)c,
819 (byte)((int)c >> 8)
820 } : new byte[2]
821 {
822 (byte)((int)c >> 8),
823 (byte)c
824 });
825 if (decoderFallbackBuffer == null)
826 {
827 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
828 decoderFallbackBuffer.InternalInitialize(byteStart, null);
829 }
830 num2 += decoderFallbackBuffer.InternalFallback(array3, bytes);
831 c = '\0';
832 }
833 }
834 if (decoder == null || decoder.MustFlush)
835 {
836 if (c > '\0')
837 {
838 num2--;
839 byte[] array4 = null;
840 array4 = ((!bigEndian) ? new byte[2]
841 {
842 (byte)c,
843 (byte)((int)c >> 8)
844 } : new byte[2]
845 {
846 (byte)((int)c >> 8),
847 (byte)c
848 });
849 if (decoderFallbackBuffer == null)
850 {
851 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
852 decoderFallbackBuffer.InternalInitialize(byteStart, null);
853 }
854 num2 += decoderFallbackBuffer.InternalFallback(array4, bytes);
855 c = '\0';
856 }
857 if (num >= 0)
858 {
859 if (decoderFallbackBuffer == null)
860 {
861 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
862 decoderFallbackBuffer.InternalInitialize(byteStart, null);
863 }
864 num2 += decoderFallbackBuffer.InternalFallback(new byte[1] { (byte)num }, bytes);
865 num = -1;
866 }
867 }
868 if (c > '\0')
869 {
870 num2--;
871 }
872 return num2;
873 }
874
875 internal unsafe sealed override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
876 {
877 Decoder decoder = (Decoder)baseDecoder;
878 int num = -1;
879 char c = '\0';
880 if (decoder != null)
881 {
882 num = decoder.lastByte;
883 c = decoder.lastChar;
884 }
885 DecoderFallbackBuffer decoderFallbackBuffer = null;
886 byte* ptr = bytes + byteCount;
887 char* ptr2 = chars + charCount;
888 byte* ptr3 = bytes;
889 char* ptr4 = chars;
890 while (bytes < ptr)
891 {
892 if ((bigEndian ^ BitConverter.IsLittleEndian) && ((ulong)chars & 7uL) == 0L && num == -1 && c == '\0')
893 {
894 ulong* ptr5 = (ulong*)(bytes - 7 + ((ptr - bytes >> 1 < ptr2 - chars) ? (ptr - bytes) : (ptr2 - chars << 1)));
895 ulong* ptr6 = (ulong*)bytes;
896 ulong* ptr7 = (ulong*)chars;
897 while (ptr6 < ptr5)
898 {
899 if ((0x8000800080008000uL & *ptr6) != 0L)
900 {
901 ulong num2 = (0xF800F800F800F800uL & *ptr6) ^ 0xD800D800D800D800uL;
902 if ((num2 & 0xFFFF000000000000uL) == 0L || (num2 & 0xFFFF00000000L) == 0L || (num2 & 0xFFFF0000u) == 0L || (num2 & 0xFFFF) == 0L)
903 {
904 long num3 = -287953294993589248L & (long)(*ptr6);
906 {
907 }
908 if (num3 != -2593835887162763264L)
909 {
910 break;
911 }
912 }
913 }
914 Unsafe.WriteUnaligned(ptr7, *ptr6);
915 ptr6++;
916 ptr7++;
917 }
918 chars = (char*)ptr7;
919 bytes = (byte*)ptr6;
920 if (bytes >= ptr)
921 {
922 break;
923 }
924 }
925 if (num < 0)
926 {
927 num = *(bytes++);
928 continue;
929 }
930 char c2 = ((!bigEndian) ? ((char)((*(bytes++) << 8) | num)) : ((char)((num << 8) | *(bytes++))));
931 num = -1;
932 if (c2 >= '\ud800' && c2 <= '\udfff')
933 {
934 if (c2 <= '\udbff')
935 {
936 if (c > '\0')
937 {
938 byte[] array = null;
939 array = ((!bigEndian) ? new byte[2]
940 {
941 (byte)c,
942 (byte)((int)c >> 8)
943 } : new byte[2]
944 {
945 (byte)((int)c >> 8),
946 (byte)c
947 });
948 if (decoderFallbackBuffer == null)
949 {
950 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
951 decoderFallbackBuffer.InternalInitialize(ptr3, ptr2);
952 }
953 char* chars2 = chars;
954 bool flag = decoderFallbackBuffer.InternalFallback(array, bytes, ref chars2);
955 chars = chars2;
956 if (!flag)
957 {
958 bytes -= 2;
959 decoderFallbackBuffer.InternalReset();
960 ThrowCharsOverflow(decoder, chars == ptr4);
961 break;
962 }
963 }
964 c = c2;
965 continue;
966 }
967 if (c == '\0')
968 {
969 byte[] array2 = null;
970 array2 = ((!bigEndian) ? new byte[2]
971 {
972 (byte)c2,
973 (byte)((int)c2 >> 8)
974 } : new byte[2]
975 {
976 (byte)((int)c2 >> 8),
977 (byte)c2
978 });
979 if (decoderFallbackBuffer == null)
980 {
981 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
982 decoderFallbackBuffer.InternalInitialize(ptr3, ptr2);
983 }
984 char* chars2 = chars;
985 bool flag2 = decoderFallbackBuffer.InternalFallback(array2, bytes, ref chars2);
986 chars = chars2;
987 if (!flag2)
988 {
989 bytes -= 2;
990 decoderFallbackBuffer.InternalReset();
991 ThrowCharsOverflow(decoder, chars == ptr4);
992 break;
993 }
994 continue;
995 }
996 if (chars >= ptr2 - 1)
997 {
998 bytes -= 2;
999 ThrowCharsOverflow(decoder, chars == ptr4);
1000 break;
1001 }
1002 *(chars++) = c;
1003 c = '\0';
1004 }
1005 else if (c > '\0')
1006 {
1007 byte[] array3 = null;
1008 array3 = ((!bigEndian) ? new byte[2]
1009 {
1010 (byte)c,
1011 (byte)((int)c >> 8)
1012 } : new byte[2]
1013 {
1014 (byte)((int)c >> 8),
1015 (byte)c
1016 });
1017 if (decoderFallbackBuffer == null)
1018 {
1019 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
1020 decoderFallbackBuffer.InternalInitialize(ptr3, ptr2);
1021 }
1022 char* chars2 = chars;
1023 bool flag3 = decoderFallbackBuffer.InternalFallback(array3, bytes, ref chars2);
1024 chars = chars2;
1025 if (!flag3)
1026 {
1027 bytes -= 2;
1028 decoderFallbackBuffer.InternalReset();
1029 ThrowCharsOverflow(decoder, chars == ptr4);
1030 break;
1031 }
1032 c = '\0';
1033 }
1034 if (chars >= ptr2)
1035 {
1036 bytes -= 2;
1037 ThrowCharsOverflow(decoder, chars == ptr4);
1038 break;
1039 }
1040 *(chars++) = c2;
1041 }
1042 if (decoder == null || decoder.MustFlush)
1043 {
1044 if (c > '\0')
1045 {
1046 byte[] array4 = null;
1047 array4 = ((!bigEndian) ? new byte[2]
1048 {
1049 (byte)c,
1050 (byte)((int)c >> 8)
1051 } : new byte[2]
1052 {
1053 (byte)((int)c >> 8),
1054 (byte)c
1055 });
1056 if (decoderFallbackBuffer == null)
1057 {
1058 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
1059 decoderFallbackBuffer.InternalInitialize(ptr3, ptr2);
1060 }
1061 char* chars2 = chars;
1062 bool flag4 = decoderFallbackBuffer.InternalFallback(array4, bytes, ref chars2);
1063 chars = chars2;
1064 if (!flag4)
1065 {
1066 bytes -= 2;
1067 if (num >= 0)
1068 {
1069 bytes--;
1070 }
1071 decoderFallbackBuffer.InternalReset();
1072 ThrowCharsOverflow(decoder, chars == ptr4);
1073 bytes += 2;
1074 if (num >= 0)
1075 {
1076 bytes++;
1077 }
1078 goto IL_04df;
1079 }
1080 c = '\0';
1081 }
1082 if (num >= 0)
1083 {
1084 if (decoderFallbackBuffer == null)
1085 {
1086 decoderFallbackBuffer = ((decoder != null) ? decoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
1087 decoderFallbackBuffer.InternalInitialize(ptr3, ptr2);
1088 }
1089 char* chars2 = chars;
1090 bool flag5 = decoderFallbackBuffer.InternalFallback(new byte[1] { (byte)num }, bytes, ref chars2);
1091 chars = chars2;
1092 if (!flag5)
1093 {
1094 bytes--;
1095 decoderFallbackBuffer.InternalReset();
1096 ThrowCharsOverflow(decoder, chars == ptr4);
1097 bytes++;
1098 }
1099 else
1100 {
1101 num = -1;
1102 }
1103 }
1104 }
1105 goto IL_04df;
1106 IL_04df:
1107 if (decoder != null)
1108 {
1109 decoder._bytesUsed = (int)(bytes - ptr3);
1110 decoder.lastChar = c;
1111 decoder.lastByte = num;
1112 }
1113 return (int)(chars - ptr4);
1114 }
1115
1116 public override Encoder GetEncoder()
1117 {
1118 return new EncoderNLS(this);
1119 }
1120
1122 {
1123 return new Decoder(this);
1124 }
1125
1126 public override byte[] GetPreamble()
1127 {
1128 if (byteOrderMark)
1129 {
1130 if (!bigEndian)
1131 {
1132 return new byte[2] { 255, 254 };
1133 }
1134 return new byte[2] { 254, 255 };
1135 }
1136 return Array.Empty<byte>();
1137 }
1138
1139 public override int GetMaxByteCount(int charCount)
1140 {
1141 if (charCount < 0)
1142 {
1144 }
1145 long num = (long)charCount + 1L;
1146 if (base.EncoderFallback.MaxCharCount > 1)
1147 {
1148 num *= base.EncoderFallback.MaxCharCount;
1149 }
1150 num <<= 1;
1151 if (num > int.MaxValue)
1152 {
1154 }
1155 return (int)num;
1156 }
1157
1158 public override int GetMaxCharCount(int byteCount)
1159 {
1160 if (byteCount < 0)
1161 {
1163 }
1164 long num = (long)(byteCount >> 1) + (long)(byteCount & 1) + 1;
1165 if (base.DecoderFallback.MaxCharCount > 1)
1166 {
1167 num *= base.DecoderFallback.MaxCharCount;
1168 }
1169 if (num > int.MaxValue)
1170 {
1172 }
1173 return (int)num;
1174 }
1175
1176 public override bool Equals([NotNullWhen(true)] object? value)
1177 {
1178 if (value is UnicodeEncoding unicodeEncoding)
1179 {
1180 if (CodePage == unicodeEncoding.CodePage && byteOrderMark == unicodeEncoding.byteOrderMark && bigEndian == unicodeEncoding.bigEndian && base.EncoderFallback.Equals(unicodeEncoding.EncoderFallback))
1181 {
1182 return base.DecoderFallback.Equals(unicodeEncoding.DecoderFallback);
1183 }
1184 return false;
1185 }
1186 return false;
1187 }
1188
1189 public override int GetHashCode()
1190 {
1191 return CodePage + base.EncoderFallback.GetHashCode() + base.DecoderFallback.GetHashCode() + (byteOrderMark ? 4 : 0) + (bigEndian ? 8 : 0);
1192 }
1193}
static readonly bool IsLittleEndian
static string ArgumentOutOfRange_Index
Definition SR.cs:30
static string Argument_EncoderFallbackNotEmpty
Definition SR.cs:590
static string ArgumentOutOfRange_IndexCount
Definition SR.cs:80
static string Argument_RecursiveFallback
Definition SR.cs:844
static string ArgumentOutOfRange_GetCharCountOverflow
Definition SR.cs:90
static string ArgumentOutOfRange_GetByteCountOverflow
Definition SR.cs:88
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ArgumentOutOfRange_IndexCountBuffer
Definition SR.cs:78
static string ArgumentNull_Array
Definition SR.cs:24
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
unsafe void InternalInitialize(byte *byteStart, char *charEnd)
virtual unsafe bool InternalFallback(byte[] bytes, byte *pBytes, ref char *chars)
static DecoderFallback ExceptionFallback
DecoderFallbackBuffer CreateFallbackBuffer()
new DecoderFallbackBuffer FallbackBuffer
Definition DecoderNLS.cs:24
DecoderFallbackBuffer _fallbackBuffer
Definition Decoder.cs:9
bool InternalFallback(ReadOnlySpan< char > chars, out int charsConsumed)
unsafe void InternalInitialize(char *charStart, char *charEnd, EncoderNLS encoder, bool setEncoder)
EncoderFallbackBuffer CreateFallbackBuffer()
static EncoderFallback ExceptionFallback
new EncoderFallback Fallback
Definition EncoderNLS.cs:21
new EncoderFallbackBuffer FallbackBuffer
Definition EncoderNLS.cs:26
EncoderFallback encoderFallback
Definition Encoding.cs:341
DecoderFallback decoderFallback
Definition Encoding.cs:343
virtual string EncodingName
Definition Encoding.cs:362
virtual int CodePage
Definition Encoding.cs:515
Decoder(UnicodeEncoding encoding)
override void SetDefaultFallbacks()
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
unsafe override int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder)
UnicodeEncoding(bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes)
unsafe override int GetByteCount(char *chars, int count, EncoderNLS encoder)
unsafe override int GetByteCount(char *chars, int count)
override ReadOnlySpan< byte > Preamble
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount)
static readonly UnicodeEncoding s_littleEndianDefault
unsafe override int GetCharCount(byte *bytes, int count)
static readonly UnicodeEncoding s_bigEndianDefault
UnicodeEncoding(bool bigEndian, bool byteOrderMark)
override int GetMaxCharCount(int byteCount)
override int GetMaxByteCount(int charCount)
override bool Equals([NotNullWhen(true)] object? value)
unsafe override int GetByteCount(string s)
unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
unsafe override int GetCharCount(byte[] bytes, int index, int count)
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
override System.Text.Decoder GetDecoder()
unsafe override string GetString(byte[] bytes, int index, int count)
unsafe override int GetByteCount(char[] chars, int index, int count)
static void ThrowArgumentNullException(string name)