mscorlib(4.0.0.0) API with additions
UTF8Encoding.cs
3 using System.Security;
4 
5 namespace System.Text
6 {
9  [ComVisible(true)]
10  [__DynamicallyInvokable]
11  public class UTF8Encoding : Encoding
12  {
13  [Serializable]
14  internal class UTF8Encoder : EncoderNLS, ISerializable
15  {
16  internal int surrogateChar;
17 
18  internal override bool HasState => surrogateChar != 0;
19 
20  public UTF8Encoder(UTF8Encoding encoding)
21  : base(encoding)
22  {
23  }
24 
25  internal UTF8Encoder(SerializationInfo info, StreamingContext context)
26  {
27  if (info == null)
28  {
29  throw new ArgumentNullException("info");
30  }
31  m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding));
32  surrogateChar = (int)info.GetValue("surrogateChar", typeof(int));
33  try
34  {
35  m_fallback = (EncoderFallback)info.GetValue("m_fallback", typeof(EncoderFallback));
36  }
38  {
39  m_fallback = null;
40  }
41  }
42 
43  [SecurityCritical]
45  {
46  if (info == null)
47  {
48  throw new ArgumentNullException("info");
49  }
50  info.AddValue("encoding", m_encoding);
51  info.AddValue("surrogateChar", surrogateChar);
52  info.AddValue("m_fallback", m_fallback);
53  info.AddValue("storedSurrogate", (surrogateChar > 0) ? true : false);
54  info.AddValue("mustFlush", value: false);
55  }
56 
57  public override void Reset()
58  {
59  surrogateChar = 0;
60  if (m_fallbackBuffer != null)
61  {
62  m_fallbackBuffer.Reset();
63  }
64  }
65  }
66 
67  [Serializable]
68  internal class UTF8Decoder : DecoderNLS, ISerializable
69  {
70  internal int bits;
71 
72  internal override bool HasState => bits != 0;
73 
74  public UTF8Decoder(UTF8Encoding encoding)
75  : base(encoding)
76  {
77  }
78 
79  internal UTF8Decoder(SerializationInfo info, StreamingContext context)
80  {
81  if (info == null)
82  {
83  throw new ArgumentNullException("info");
84  }
85  m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding));
86  try
87  {
88  bits = (int)info.GetValue("wbits", typeof(int));
89  m_fallback = (DecoderFallback)info.GetValue("m_fallback", typeof(DecoderFallback));
90  }
92  {
93  bits = 0;
94  m_fallback = null;
95  }
96  }
97 
98  [SecurityCritical]
100  {
101  if (info == null)
102  {
103  throw new ArgumentNullException("info");
104  }
105  info.AddValue("encoding", m_encoding);
106  info.AddValue("wbits", bits);
107  info.AddValue("m_fallback", m_fallback);
108  info.AddValue("bits", 0);
109  info.AddValue("trailCount", 0);
110  info.AddValue("isSurrogate", value: false);
111  info.AddValue("byteSequence", 0);
112  }
113 
114  public override void Reset()
115  {
116  bits = 0;
117  if (m_fallbackBuffer != null)
118  {
119  m_fallbackBuffer.Reset();
120  }
121  }
122  }
123 
124  private const int UTF8_CODEPAGE = 65001;
125 
126  private bool emitUTF8Identifier;
127 
128  private bool isThrowException;
129 
130  private const int FinalByte = 536870912;
131 
132  private const int SupplimentarySeq = 268435456;
133 
134  private const int ThreeByteSeq = 134217728;
135 
137  [__DynamicallyInvokable]
138  public UTF8Encoding()
139  : this(encoderShouldEmitUTF8Identifier: false)
140  {
141  }
142 
146  [__DynamicallyInvokable]
147  public UTF8Encoding(bool encoderShouldEmitUTF8Identifier)
148  : this(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes: false)
149  {
150  }
151 
157  [__DynamicallyInvokable]
158  public UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes)
159  : base(65001)
160  {
161  emitUTF8Identifier = encoderShouldEmitUTF8Identifier;
162  isThrowException = throwOnInvalidBytes;
163  if (isThrowException)
164  {
165  SetDefaultFallbacks();
166  }
167  }
168 
169  internal override void SetDefaultFallbacks()
170  {
171  if (isThrowException)
172  {
173  encoderFallback = EncoderFallback.ExceptionFallback;
174  decoderFallback = DecoderFallback.ExceptionFallback;
175  }
176  else
177  {
178  encoderFallback = new EncoderReplacementFallback("�");
179  decoderFallback = new DecoderReplacementFallback("�");
180  }
181  }
182 
195  [SecuritySafeCritical]
196  [__DynamicallyInvokable]
197  public unsafe override int GetByteCount(char[] chars, int index, int count)
198  {
199  if (chars == null)
200  {
201  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
202  }
203  if (index < 0 || count < 0)
204  {
205  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
206  }
207  if (chars.Length - index < count)
208  {
209  throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
210  }
211  if (chars.Length == 0)
212  {
213  return 0;
214  }
215  fixed (char* ptr = chars)
216  {
217  return GetByteCount(ptr + index, count, null);
218  }
219  }
220 
230  [SecuritySafeCritical]
231  [__DynamicallyInvokable]
232  public unsafe override int GetByteCount(string chars)
233  {
234  if (chars == null)
235  {
236  throw new ArgumentNullException("s");
237  }
238  fixed (char* chars2 = chars)
239  {
240  return GetByteCount(chars2, chars.Length, null);
241  }
242  }
243 
255  [SecurityCritical]
256  [CLSCompliant(false)]
257  [ComVisible(false)]
258  public unsafe override int GetByteCount(char* chars, int count)
259  {
260  if (chars == null)
261  {
262  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
263  }
264  if (count < 0)
265  {
266  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
267  }
268  return GetByteCount(chars, count, null);
269  }
270 
289  [SecuritySafeCritical]
290  [__DynamicallyInvokable]
291  public unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
292  {
293  if (s == null || bytes == null)
294  {
295  throw new ArgumentNullException((s == null) ? "s" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
296  }
297  if (charIndex < 0 || charCount < 0)
298  {
299  throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
300  }
301  if (s.Length - charIndex < charCount)
302  {
303  throw new ArgumentOutOfRangeException("s", Environment.GetResourceString("ArgumentOutOfRange_IndexCount"));
304  }
305  if (byteIndex < 0 || byteIndex > bytes.Length)
306  {
307  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
308  }
309  int byteCount = bytes.Length - byteIndex;
310  if (bytes.Length == 0)
311  {
312  bytes = new byte[1];
313  }
314  fixed (char* ptr = s)
315  {
316  byte[] array = bytes;
317  fixed (byte* ptr2 = array)
318  {
319  return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
320  }
321  }
322  }
323 
342  [SecuritySafeCritical]
343  [__DynamicallyInvokable]
344  public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
345  {
346  if (chars == null || bytes == null)
347  {
348  throw new ArgumentNullException((chars == null) ? "chars" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
349  }
350  if (charIndex < 0 || charCount < 0)
351  {
352  throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
353  }
354  if (chars.Length - charIndex < charCount)
355  {
356  throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
357  }
358  if (byteIndex < 0 || byteIndex > bytes.Length)
359  {
360  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
361  }
362  if (chars.Length == 0)
363  {
364  return 0;
365  }
366  int byteCount = bytes.Length - byteIndex;
367  if (bytes.Length == 0)
368  {
369  bytes = new byte[1];
370  }
371  fixed (char* ptr = chars)
372  {
373  byte[] array = bytes;
374  fixed (byte* ptr2 = array)
375  {
376  return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
377  }
378  }
379  }
380 
396  [SecurityCritical]
397  [CLSCompliant(false)]
398  [ComVisible(false)]
399  public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
400  {
401  if (bytes == null || chars == null)
402  {
403  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
404  }
405  if (charCount < 0 || byteCount < 0)
406  {
407  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
408  }
409  return GetBytes(chars, charCount, bytes, byteCount, null);
410  }
411 
425  [SecuritySafeCritical]
426  [__DynamicallyInvokable]
427  public unsafe override int GetCharCount(byte[] bytes, int index, int count)
428  {
429  if (bytes == null)
430  {
431  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
432  }
433  if (index < 0 || count < 0)
434  {
435  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
436  }
437  if (bytes.Length - index < count)
438  {
439  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
440  }
441  if (bytes.Length == 0)
442  {
443  return 0;
444  }
445  fixed (byte* ptr = bytes)
446  {
447  return GetCharCount(ptr + index, count, null);
448  }
449  }
450 
462  [SecurityCritical]
463  [CLSCompliant(false)]
464  [ComVisible(false)]
465  public unsafe override int GetCharCount(byte* bytes, int count)
466  {
467  if (bytes == null)
468  {
469  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
470  }
471  if (count < 0)
472  {
473  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
474  }
475  return GetCharCount(bytes, count, null);
476  }
477 
496  [SecuritySafeCritical]
497  [__DynamicallyInvokable]
498  public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
499  {
500  if (bytes == null || chars == null)
501  {
502  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
503  }
504  if (byteIndex < 0 || byteCount < 0)
505  {
506  throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
507  }
508  if (bytes.Length - byteIndex < byteCount)
509  {
510  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
511  }
512  if (charIndex < 0 || charIndex > chars.Length)
513  {
514  throw new ArgumentOutOfRangeException("charIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
515  }
516  if (bytes.Length == 0)
517  {
518  return 0;
519  }
520  int charCount = chars.Length - charIndex;
521  if (chars.Length == 0)
522  {
523  chars = new char[1];
524  }
525  fixed (byte* ptr = bytes)
526  {
527  char[] array = chars;
528  fixed (char* ptr2 = array)
529  {
530  return GetChars(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount, null);
531  }
532  }
533  }
534 
550  [SecurityCritical]
551  [CLSCompliant(false)]
552  [ComVisible(false)]
553  public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
554  {
555  if (bytes == null || chars == null)
556  {
557  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
558  }
559  if (charCount < 0 || byteCount < 0)
560  {
561  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
562  }
563  return GetChars(bytes, byteCount, chars, charCount, null);
564  }
565 
579  [SecuritySafeCritical]
580  [ComVisible(false)]
581  [__DynamicallyInvokable]
582  public unsafe override string GetString(byte[] bytes, int index, int count)
583  {
584  if (bytes == null)
585  {
586  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
587  }
588  if (index < 0 || count < 0)
589  {
590  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
591  }
592  if (bytes.Length - index < count)
593  {
594  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
595  }
596  if (bytes.Length == 0)
597  {
598  return string.Empty;
599  }
600  fixed (byte* ptr = bytes)
601  {
602  return string.CreateStringFromEncoding(ptr + index, count, this);
603  }
604  }
605 
606  [SecurityCritical]
607  internal unsafe override int GetByteCount(char* chars, int count, EncoderNLS baseEncoder)
608  {
609  EncoderFallbackBuffer encoderFallbackBuffer = null;
610  char* chars2 = chars;
611  char* ptr = chars2 + count;
612  int num = count;
613  int num2 = 0;
614  if (baseEncoder != null)
615  {
616  UTF8Encoder uTF8Encoder = (UTF8Encoder)baseEncoder;
617  num2 = uTF8Encoder.surrogateChar;
618  if (uTF8Encoder.InternalHasFallbackBuffer)
619  {
620  encoderFallbackBuffer = uTF8Encoder.FallbackBuffer;
621  if (encoderFallbackBuffer.Remaining > 0)
622  {
623  throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", EncodingName, uTF8Encoder.Fallback.GetType()));
624  }
625  encoderFallbackBuffer.InternalInitialize(chars, ptr, uTF8Encoder, setEncoder: false);
626  }
627  }
628  while (true)
629  {
630  if (chars2 >= ptr)
631  {
632  if (num2 == 0)
633  {
634  num2 = (encoderFallbackBuffer?.InternalGetNextChar() ?? '\0');
635  if (num2 <= 0)
636  {
637  goto IL_00e6;
638  }
639  num++;
640  }
641  else
642  {
643  if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
644  {
645  goto IL_00e6;
646  }
647  num2 = encoderFallbackBuffer.InternalGetNextChar();
648  num++;
649  if (InRange(num2, 56320, 57343))
650  {
651  num2 = 65533;
652  num++;
653  goto IL_0169;
654  }
655  if (num2 <= 0)
656  {
657  num--;
658  break;
659  }
660  }
661  }
662  else
663  {
664  if (num2 > 0)
665  {
666  int ch = *chars2;
667  num++;
668  if (InRange(ch, 56320, 57343))
669  {
670  num2 = 65533;
671  chars2++;
672  }
673  goto IL_0169;
674  }
675  if (encoderFallbackBuffer != null)
676  {
677  num2 = encoderFallbackBuffer.InternalGetNextChar();
678  if (num2 > 0)
679  {
680  num++;
681  goto IL_014d;
682  }
683  }
684  num2 = *chars2;
685  chars2++;
686  }
687  goto IL_014d;
688  IL_014d:
689  if (InRange(num2, 55296, 56319))
690  {
691  num--;
692  continue;
693  }
694  goto IL_0169;
695  IL_0169:
696  if (InRange(num2, 55296, 57343))
697  {
698  if (encoderFallbackBuffer == null)
699  {
700  encoderFallbackBuffer = ((baseEncoder != null) ? baseEncoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
701  encoderFallbackBuffer.InternalInitialize(chars, chars + count, baseEncoder, setEncoder: false);
702  }
703  encoderFallbackBuffer.InternalFallback((char)num2, ref chars2);
704  num--;
705  num2 = 0;
706  continue;
707  }
708  if (num2 > 127)
709  {
710  if (num2 > 2047)
711  {
712  num++;
713  }
714  num++;
715  }
716  if (num < 0)
717  {
718  break;
719  }
720  if (encoderFallbackBuffer != null && (num2 = encoderFallbackBuffer.InternalGetNextChar()) != 0)
721  {
722  num++;
723  goto IL_014d;
724  }
725  int num3 = PtrDiff(ptr, chars2);
726  if (num3 <= 13)
727  {
728  char* ptr2 = ptr;
729  while (chars2 < ptr2)
730  {
731  num2 = *chars2;
732  chars2++;
733  if (num2 <= 127)
734  {
735  continue;
736  }
737  goto IL_014d;
738  }
739  break;
740  }
741  num3 &= 0xFFFFFFF;
742  char* ptr3 = chars2 + num3 - 7;
743  while (chars2 < ptr3)
744  {
745  num2 = *chars2;
746  chars2++;
747  if (num2 > 127)
748  {
749  if (num2 > 2047)
750  {
751  if ((num2 & 0xF800) == 55296)
752  {
753  goto IL_039a;
754  }
755  num++;
756  }
757  num++;
758  }
759  if (((int)chars2 & 2) != 0)
760  {
761  num2 = *chars2;
762  chars2++;
763  if (num2 > 127)
764  {
765  if (num2 > 2047)
766  {
767  if ((num2 & 0xF800) == 55296)
768  {
769  goto IL_039a;
770  }
771  num++;
772  }
773  num++;
774  }
775  }
776  for (; chars2 < ptr3; chars2 += 4)
777  {
778  num2 = *(int*)chars2;
779  int num4 = *(int*)(chars2 + 2);
780  if (((num2 | num4) & -8323200) != 0)
781  {
782  if (((num2 | num4) & -134154240) != 0)
783  {
784  goto IL_038b;
785  }
786  if ((num2 & -8388608) != 0)
787  {
788  num++;
789  }
790  if ((num2 & 0xFF80) != 0)
791  {
792  num++;
793  }
794  if ((num4 & -8388608) != 0)
795  {
796  num++;
797  }
798  if ((num4 & 0xFF80) != 0)
799  {
800  num++;
801  }
802  }
803  chars2 += 4;
804  num2 = *(int*)chars2;
805  num4 = *(int*)(chars2 + 2);
806  if (((num2 | num4) & -8323200) == 0)
807  {
808  continue;
809  }
810  if (((num2 | num4) & -134154240) == 0)
811  {
812  if ((num2 & -8388608) != 0)
813  {
814  num++;
815  }
816  if ((num2 & 0xFF80) != 0)
817  {
818  num++;
819  }
820  if ((num4 & -8388608) != 0)
821  {
822  num++;
823  }
824  if ((num4 & 0xFF80) != 0)
825  {
826  num++;
827  }
828  continue;
829  }
830  goto IL_038b;
831  }
832  break;
833  IL_039a:
834  if (num2 > 2047)
835  {
836  if (InRange(num2, 55296, 57343))
837  {
838  int ch2 = *chars2;
839  if (num2 > 56319 || !InRange(ch2, 56320, 57343))
840  {
841  chars2--;
842  break;
843  }
844  chars2++;
845  }
846  num++;
847  }
848  num++;
849  continue;
850  IL_038b:
851  num2 = (ushort)num2;
852  chars2++;
853  if (num2 <= 127)
854  {
855  continue;
856  }
857  goto IL_039a;
858  }
859  num2 = 0;
860  continue;
861  IL_00e6:
862  if (num2 <= 0 || (baseEncoder != null && !baseEncoder.MustFlush))
863  {
864  break;
865  }
866  num++;
867  goto IL_0169;
868  }
869  if (num < 0)
870  {
871  throw new ArgumentException(Environment.GetResourceString("Argument_ConversionOverflow"));
872  }
873  return num;
874  }
875 
876  [SecurityCritical]
877  private unsafe static int PtrDiff(char* a, char* b)
878  {
879  return (int)((uint)((byte*)a - (byte*)b) >> 1);
880  }
881 
882  [SecurityCritical]
883  private unsafe static int PtrDiff(byte* a, byte* b)
884  {
885  return (int)(a - b);
886  }
887 
888  private static bool InRange(int ch, int start, int end)
889  {
890  return (uint)(ch - start) <= (uint)(end - start);
891  }
892 
893  [SecurityCritical]
894  internal unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS baseEncoder)
895  {
896  UTF8Encoder uTF8Encoder = null;
897  EncoderFallbackBuffer encoderFallbackBuffer = null;
898  char* chars2 = chars;
899  byte* ptr = bytes;
900  char* ptr2 = chars2 + charCount;
901  byte* ptr3 = ptr + byteCount;
902  int num = 0;
903  if (baseEncoder != null)
904  {
905  uTF8Encoder = (UTF8Encoder)baseEncoder;
906  num = uTF8Encoder.surrogateChar;
907  if (uTF8Encoder.InternalHasFallbackBuffer)
908  {
909  encoderFallbackBuffer = uTF8Encoder.FallbackBuffer;
910  if (encoderFallbackBuffer.Remaining > 0 && uTF8Encoder.m_throwOnOverflow)
911  {
912  throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", EncodingName, uTF8Encoder.Fallback.GetType()));
913  }
914  encoderFallbackBuffer.InternalInitialize(chars, ptr2, uTF8Encoder, setEncoder: true);
915  }
916  }
917  while (true)
918  {
919  if (chars2 >= ptr2)
920  {
921  if (num == 0)
922  {
923  num = (encoderFallbackBuffer?.InternalGetNextChar() ?? '\0');
924  if (num <= 0)
925  {
926  goto IL_00ef;
927  }
928  }
929  else
930  {
931  if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack)
932  {
933  goto IL_00ef;
934  }
935  int num2 = num;
936  num = encoderFallbackBuffer.InternalGetNextChar();
937  if (InRange(num, 56320, 57343))
938  {
939  num = num + (num2 << 10) + -56613888;
940  goto IL_0167;
941  }
942  if (num <= 0)
943  {
944  break;
945  }
946  }
947  }
948  else
949  {
950  if (num > 0)
951  {
952  int num3 = *chars2;
953  if (InRange(num3, 56320, 57343))
954  {
955  num = num3 + (num << 10) + -56613888;
956  chars2++;
957  }
958  goto IL_0167;
959  }
960  if (encoderFallbackBuffer != null)
961  {
962  num = encoderFallbackBuffer.InternalGetNextChar();
963  if (num > 0)
964  {
965  goto IL_0151;
966  }
967  }
968  num = *chars2;
969  chars2++;
970  }
971  goto IL_0151;
972  IL_0167:
973  if (InRange(num, 55296, 57343))
974  {
975  if (encoderFallbackBuffer == null)
976  {
977  encoderFallbackBuffer = ((baseEncoder != null) ? baseEncoder.FallbackBuffer : encoderFallback.CreateFallbackBuffer());
978  encoderFallbackBuffer.InternalInitialize(chars, ptr2, baseEncoder, setEncoder: true);
979  }
980  encoderFallbackBuffer.InternalFallback((char)num, ref chars2);
981  num = 0;
982  continue;
983  }
984  int num4 = 1;
985  if (num > 127)
986  {
987  if (num > 2047)
988  {
989  if (num > 65535)
990  {
991  num4++;
992  }
993  num4++;
994  }
995  num4++;
996  }
997  if (ptr > ptr3 - num4)
998  {
999  if (encoderFallbackBuffer != null && encoderFallbackBuffer.bFallingBack)
1000  {
1001  encoderFallbackBuffer.MovePrevious();
1002  if (num > 65535)
1003  {
1004  encoderFallbackBuffer.MovePrevious();
1005  }
1006  }
1007  else
1008  {
1009  chars2--;
1010  if (num > 65535)
1011  {
1012  chars2--;
1013  }
1014  }
1015  ThrowBytesOverflow(uTF8Encoder, ptr == bytes);
1016  num = 0;
1017  break;
1018  }
1019  if (num <= 127)
1020  {
1021  *ptr = (byte)num;
1022  }
1023  else
1024  {
1025  int num5;
1026  if (num <= 2047)
1027  {
1028  num5 = (byte)(-64 | (num >> 6));
1029  }
1030  else
1031  {
1032  if (num <= 65535)
1033  {
1034  num5 = (byte)(-32 | (num >> 12));
1035  }
1036  else
1037  {
1038  *ptr = (byte)(-16 | (num >> 18));
1039  ptr++;
1040  num5 = (-128 | ((num >> 12) & 0x3F));
1041  }
1042  *ptr = (byte)num5;
1043  ptr++;
1044  num5 = (-128 | ((num >> 6) & 0x3F));
1045  }
1046  *ptr = (byte)num5;
1047  ptr++;
1048  *ptr = (byte)(-128 | (num & 0x3F));
1049  }
1050  ptr++;
1051  if (encoderFallbackBuffer == null || (num = encoderFallbackBuffer.InternalGetNextChar()) == 0)
1052  {
1053  int num6 = PtrDiff(ptr2, chars2);
1054  int num7 = PtrDiff(ptr3, ptr);
1055  if (num6 <= 13)
1056  {
1057  if (num7 < num6)
1058  {
1059  num = 0;
1060  continue;
1061  }
1062  char* ptr4 = ptr2;
1063  while (chars2 < ptr4)
1064  {
1065  num = *chars2;
1066  chars2++;
1067  if (num <= 127)
1068  {
1069  *ptr = (byte)num;
1070  ptr++;
1071  continue;
1072  }
1073  goto IL_0151;
1074  }
1075  num = 0;
1076  break;
1077  }
1078  if (num7 < num6)
1079  {
1080  num6 = num7;
1081  }
1082  char* ptr5 = chars2 + num6 - 5;
1083  while (chars2 < ptr5)
1084  {
1085  num = *chars2;
1086  chars2++;
1087  if (num <= 127)
1088  {
1089  *ptr = (byte)num;
1090  ptr++;
1091  if (((int)chars2 & 2) != 0)
1092  {
1093  num = *chars2;
1094  chars2++;
1095  if (num > 127)
1096  {
1097  goto IL_03dd;
1098  }
1099  *ptr = (byte)num;
1100  ptr++;
1101  }
1102  while (chars2 < ptr5)
1103  {
1104  num = *(int*)chars2;
1105  int num8 = *(int*)(chars2 + 2);
1106  if (((num | num8) & -8323200) == 0)
1107  {
1108  *ptr = (byte)num;
1109  ptr[1] = (byte)(num >> 16);
1110  chars2 += 4;
1111  ptr[2] = (byte)num8;
1112  ptr[3] = (byte)(num8 >> 16);
1113  ptr += 4;
1114  continue;
1115  }
1116  goto IL_03c0;
1117  }
1118  continue;
1119  }
1120  goto IL_03dd;
1121  IL_03c0:
1122  num = (ushort)num;
1123  chars2++;
1124  if (num <= 127)
1125  {
1126  *ptr = (byte)num;
1127  ptr++;
1128  continue;
1129  }
1130  goto IL_03dd;
1131  IL_03dd:
1132  int num9;
1133  if (num <= 2047)
1134  {
1135  num9 = (-64 | (num >> 6));
1136  }
1137  else
1138  {
1139  if (!InRange(num, 55296, 57343))
1140  {
1141  num9 = (-32 | (num >> 12));
1142  }
1143  else
1144  {
1145  if (num > 56319)
1146  {
1147  chars2--;
1148  break;
1149  }
1150  num9 = *chars2;
1151  chars2++;
1152  if (!InRange(num9, 56320, 57343))
1153  {
1154  chars2 -= 2;
1155  break;
1156  }
1157  num = num9 + (num << 10) + -56613888;
1158  *ptr = (byte)(-16 | (num >> 18));
1159  ptr++;
1160  num9 = (-128 | ((num >> 12) & 0x3F));
1161  }
1162  *ptr = (byte)num9;
1163  ptr5--;
1164  ptr++;
1165  num9 = (-128 | ((num >> 6) & 0x3F));
1166  }
1167  *ptr = (byte)num9;
1168  ptr5--;
1169  ptr++;
1170  *ptr = (byte)(-128 | (num & 0x3F));
1171  ptr++;
1172  }
1173  num = 0;
1174  continue;
1175  }
1176  goto IL_0151;
1177  IL_00ef:
1178  if (num <= 0 || (uTF8Encoder != null && !uTF8Encoder.MustFlush))
1179  {
1180  break;
1181  }
1182  goto IL_0167;
1183  IL_0151:
1184  if (InRange(num, 55296, 56319))
1185  {
1186  continue;
1187  }
1188  goto IL_0167;
1189  }
1190  if (uTF8Encoder != null)
1191  {
1192  uTF8Encoder.surrogateChar = num;
1193  uTF8Encoder.m_charsUsed = (int)(chars2 - chars);
1194  }
1195  return (int)(ptr - bytes);
1196  }
1197 
1198  [SecurityCritical]
1199  internal unsafe override int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
1200  {
1201  byte* ptr = bytes;
1202  byte* ptr2 = ptr + count;
1203  int num = count;
1204  int num2 = 0;
1205  DecoderFallbackBuffer decoderFallbackBuffer = null;
1206  if (baseDecoder != null)
1207  {
1208  UTF8Decoder uTF8Decoder = (UTF8Decoder)baseDecoder;
1209  num2 = uTF8Decoder.bits;
1210  num -= num2 >> 30;
1211  }
1212  while (ptr < ptr2)
1213  {
1214  if (num2 != 0)
1215  {
1216  int num3 = *ptr;
1217  ptr++;
1218  if ((num3 & -64) != 128)
1219  {
1220  ptr--;
1221  num += num2 >> 30;
1222  }
1223  else
1224  {
1225  num2 = ((num2 << 6) | (num3 & 0x3F));
1226  if ((num2 & 0x20000000) != 0)
1227  {
1228  if ((num2 & 0x101F0000) == 268435456)
1229  {
1230  num--;
1231  }
1232  goto IL_0180;
1233  }
1234  if ((num2 & 0x10000000) != 0)
1235  {
1236  if ((num2 & 0x800000) != 0 || InRange(num2 & 0x1F0, 16, 256))
1237  {
1238  continue;
1239  }
1240  }
1241  else if ((num2 & 0x3E0) != 0 && (num2 & 0x3E0) != 864)
1242  {
1243  continue;
1244  }
1245  }
1246  goto IL_00c7;
1247  }
1248  num2 = *ptr;
1249  ptr++;
1250  goto IL_010a;
1251  IL_0180:
1252  int num4 = PtrDiff(ptr2, ptr);
1253  if (num4 <= 13)
1254  {
1255  byte* ptr3 = ptr2;
1256  while (ptr < ptr3)
1257  {
1258  num2 = *ptr;
1259  ptr++;
1260  if (num2 <= 127)
1261  {
1262  continue;
1263  }
1264  goto IL_010a;
1265  }
1266  num2 = 0;
1267  break;
1268  }
1269  byte* ptr4 = ptr + num4 - 7;
1270  while (true)
1271  {
1272  if (ptr < ptr4)
1273  {
1274  num2 = *ptr;
1275  ptr++;
1276  if (num2 > 127)
1277  {
1278  goto IL_024d;
1279  }
1280  if (((int)ptr & 1) != 0)
1281  {
1282  num2 = *ptr;
1283  ptr++;
1284  if (num2 > 127)
1285  {
1286  goto IL_024d;
1287  }
1288  }
1289  if (((int)ptr & 2) != 0)
1290  {
1291  num2 = *(ushort*)ptr;
1292  if ((num2 & 0x8080) != 0)
1293  {
1294  goto IL_0239;
1295  }
1296  ptr += 2;
1297  }
1298  while (ptr < ptr4)
1299  {
1300  num2 = *(int*)ptr;
1301  int num5 = *(int*)(ptr + 4);
1302  if (((num2 | num5) & -2139062144) == 0)
1303  {
1304  ptr += 8;
1305  if (ptr >= ptr4)
1306  {
1307  break;
1308  }
1309  num2 = *(int*)ptr;
1310  num5 = *(int*)(ptr + 4);
1311  if (((num2 | num5) & -2139062144) == 0)
1312  {
1313  ptr += 8;
1314  continue;
1315  }
1316  }
1317  goto IL_0239;
1318  }
1319  }
1320  num2 = 0;
1321  break;
1322  IL_024d:
1323  int num6 = *ptr;
1324  ptr++;
1325  if ((num2 & 0x40) != 0 && (num6 & -64) == 128)
1326  {
1327  num6 &= 0x3F;
1328  if ((num2 & 0x20) != 0)
1329  {
1330  num6 |= (num2 & 0xF) << 6;
1331  if ((num2 & 0x10) != 0)
1332  {
1333  num2 = *ptr;
1334  if (InRange(num6 >> 4, 1, 16) && (num2 & -64) == 128)
1335  {
1336  num6 = ((num6 << 6) | (num2 & 0x3F));
1337  num2 = ptr[1];
1338  if ((num2 & -64) == 128)
1339  {
1340  ptr += 2;
1341  num--;
1342  goto IL_0306;
1343  }
1344  }
1345  }
1346  else
1347  {
1348  num2 = *ptr;
1349  if ((num6 & 0x3E0) != 0 && (num6 & 0x3E0) != 864 && (num2 & -64) == 128)
1350  {
1351  ptr++;
1352  num--;
1353  goto IL_0306;
1354  }
1355  }
1356  }
1357  else if ((num2 & 0x1E) != 0)
1358  {
1359  goto IL_0306;
1360  }
1361  }
1362  ptr -= 2;
1363  num2 = 0;
1364  break;
1365  IL_0306:
1366  num--;
1367  continue;
1368  IL_0239:
1369  num2 &= 0xFF;
1370  ptr++;
1371  if (num2 <= 127)
1372  {
1373  continue;
1374  }
1375  goto IL_024d;
1376  }
1377  continue;
1378  IL_00c7:
1379  if (decoderFallbackBuffer == null)
1380  {
1381  decoderFallbackBuffer = ((baseDecoder != null) ? baseDecoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
1382  decoderFallbackBuffer.InternalInitialize(bytes, null);
1383  }
1384  num += FallbackInvalidByteSequence(ptr, num2, decoderFallbackBuffer);
1385  num2 = 0;
1386  continue;
1387  IL_010a:
1388  if (num2 > 127)
1389  {
1390  num--;
1391  if ((num2 & 0x40) != 0)
1392  {
1393  if ((num2 & 0x20) != 0)
1394  {
1395  if ((num2 & 0x10) == 0)
1396  {
1397  num2 = ((num2 & 0xF) | 0x48228000);
1398  num--;
1399  continue;
1400  }
1401  num2 &= 0xF;
1402  if (num2 <= 4)
1403  {
1404  num2 |= 0x504D0C00;
1405  num--;
1406  continue;
1407  }
1408  num2 |= 0xF0;
1409  }
1410  else
1411  {
1412  num2 &= 0x1F;
1413  if (num2 > 1)
1414  {
1415  num2 |= 0x800000;
1416  continue;
1417  }
1418  num2 |= 0xC0;
1419  }
1420  }
1421  goto IL_00c7;
1422  }
1423  goto IL_0180;
1424  }
1425  if (num2 != 0)
1426  {
1427  num += num2 >> 30;
1428  if (baseDecoder == null || baseDecoder.MustFlush)
1429  {
1430  if (decoderFallbackBuffer == null)
1431  {
1432  decoderFallbackBuffer = ((baseDecoder != null) ? baseDecoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
1433  decoderFallbackBuffer.InternalInitialize(bytes, null);
1434  }
1435  num += FallbackInvalidByteSequence(ptr, num2, decoderFallbackBuffer);
1436  }
1437  }
1438  return num;
1439  }
1440 
1441  [SecurityCritical]
1442  internal unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
1443  {
1444  byte* pSrc = bytes;
1445  char* pTarget = chars;
1446  byte* ptr = pSrc + byteCount;
1447  char* ptr2 = pTarget + charCount;
1448  int num = 0;
1449  DecoderFallbackBuffer decoderFallbackBuffer = null;
1450  if (baseDecoder != null)
1451  {
1452  UTF8Decoder uTF8Decoder = (UTF8Decoder)baseDecoder;
1453  num = uTF8Decoder.bits;
1454  }
1455  while (pSrc < ptr)
1456  {
1457  if (num != 0)
1458  {
1459  int num2 = *pSrc;
1460  pSrc++;
1461  if ((num2 & -64) != 128)
1462  {
1463  pSrc--;
1464  }
1465  else
1466  {
1467  num = ((num << 6) | (num2 & 0x3F));
1468  if ((num & 0x20000000) != 0)
1469  {
1470  if ((num & 0x101F0000) > 268435456 && pTarget < ptr2)
1471  {
1472  *pTarget = (char)(((num >> 10) & 0x7FF) + -10304);
1473  pTarget++;
1474  num = (num & 0x3FF) + 56320;
1475  }
1476  goto IL_01e2;
1477  }
1478  if ((num & 0x10000000) != 0)
1479  {
1480  if ((num & 0x800000) != 0 || InRange(num & 0x1F0, 16, 256))
1481  {
1482  continue;
1483  }
1484  }
1485  else if ((num & 0x3E0) != 0 && (num & 0x3E0) != 864)
1486  {
1487  continue;
1488  }
1489  }
1490  goto IL_00fd;
1491  }
1492  num = *pSrc;
1493  pSrc++;
1494  goto IL_0161;
1495  IL_01e2:
1496  if (pTarget >= ptr2)
1497  {
1498  num &= 0x1FFFFF;
1499  if (num > 127)
1500  {
1501  if (num > 2047)
1502  {
1503  if (num >= 56320 && num <= 57343)
1504  {
1505  pSrc--;
1506  pTarget--;
1507  }
1508  else if (num > 65535)
1509  {
1510  pSrc--;
1511  }
1512  pSrc--;
1513  }
1514  pSrc--;
1515  }
1516  pSrc--;
1517  ThrowCharsOverflow(baseDecoder, pTarget == chars);
1518  num = 0;
1519  break;
1520  }
1521  *pTarget = (char)num;
1522  pTarget++;
1523  int num3 = PtrDiff(ptr2, pTarget);
1524  int num4 = PtrDiff(ptr, pSrc);
1525  if (num4 <= 13)
1526  {
1527  if (num3 < num4)
1528  {
1529  num = 0;
1530  continue;
1531  }
1532  byte* ptr3 = ptr;
1533  while (pSrc < ptr3)
1534  {
1535  num = *pSrc;
1536  pSrc++;
1537  if (num <= 127)
1538  {
1539  *pTarget = (char)num;
1540  pTarget++;
1541  continue;
1542  }
1543  goto IL_0161;
1544  }
1545  num = 0;
1546  break;
1547  }
1548  if (num3 < num4)
1549  {
1550  num4 = num3;
1551  }
1552  char* ptr4 = pTarget + num4 - 7;
1553  while (true)
1554  {
1555  if (pTarget < ptr4)
1556  {
1557  num = *pSrc;
1558  pSrc++;
1559  if (num > 127)
1560  {
1561  goto IL_03fc;
1562  }
1563  *pTarget = (char)num;
1564  pTarget++;
1565  if (((int)pSrc & 1) != 0)
1566  {
1567  num = *pSrc;
1568  pSrc++;
1569  if (num > 127)
1570  {
1571  goto IL_03fc;
1572  }
1573  *pTarget = (char)num;
1574  pTarget++;
1575  }
1576  if (((int)pSrc & 2) != 0)
1577  {
1578  num = *(ushort*)pSrc;
1579  if ((num & 0x8080) != 0)
1580  {
1581  goto IL_03da;
1582  }
1583  *pTarget = (char)(num & 0x7F);
1584  pSrc += 2;
1585  pTarget[1] = (char)((num >> 8) & 0x7F);
1586  pTarget += 2;
1587  }
1588  while (pTarget < ptr4)
1589  {
1590  num = *(int*)pSrc;
1591  int num5 = *(int*)(pSrc + 4);
1592  if (((num | num5) & -2139062144) == 0)
1593  {
1594  *pTarget = (char)(num & 0x7F);
1595  pTarget[1] = (char)((num >> 8) & 0x7F);
1596  pTarget[2] = (char)((num >> 16) & 0x7F);
1597  pTarget[3] = (char)((num >> 24) & 0x7F);
1598  pSrc += 8;
1599  pTarget[4] = (char)(num5 & 0x7F);
1600  pTarget[5] = (char)((num5 >> 8) & 0x7F);
1601  pTarget[6] = (char)((num5 >> 16) & 0x7F);
1602  pTarget[7] = (char)((num5 >> 24) & 0x7F);
1603  pTarget += 8;
1604  continue;
1605  }
1606  goto IL_03da;
1607  }
1608  }
1609  num = 0;
1610  break;
1611  IL_03fc:
1612  int num6 = *pSrc;
1613  pSrc++;
1614  if ((num & 0x40) != 0 && (num6 & -64) == 128)
1615  {
1616  num6 &= 0x3F;
1617  if ((num & 0x20) != 0)
1618  {
1619  num6 |= (num & 0xF) << 6;
1620  if ((num & 0x10) != 0)
1621  {
1622  num = *pSrc;
1623  if (InRange(num6 >> 4, 1, 16) && (num & -64) == 128)
1624  {
1625  num6 = ((num6 << 6) | (num & 0x3F));
1626  num = pSrc[1];
1627  if ((num & -64) == 128)
1628  {
1629  pSrc += 2;
1630  num = ((num6 << 6) | (num & 0x3F));
1631  *pTarget = (char)(((num >> 10) & 0x7FF) + -10304);
1632  pTarget++;
1633  num = (num & 0x3FF) + -9216;
1634  ptr4--;
1635  goto IL_051f;
1636  }
1637  }
1638  }
1639  else
1640  {
1641  num = *pSrc;
1642  if ((num6 & 0x3E0) != 0 && (num6 & 0x3E0) != 864 && (num & -64) == 128)
1643  {
1644  pSrc++;
1645  num = ((num6 << 6) | (num & 0x3F));
1646  ptr4--;
1647  goto IL_051f;
1648  }
1649  }
1650  }
1651  else
1652  {
1653  num &= 0x1F;
1654  if (num > 1)
1655  {
1656  num = ((num << 6) | num6);
1657  goto IL_051f;
1658  }
1659  }
1660  }
1661  pSrc -= 2;
1662  num = 0;
1663  break;
1664  IL_051f:
1665  *pTarget = (char)num;
1666  pTarget++;
1667  ptr4--;
1668  continue;
1669  IL_03da:
1670  num &= 0xFF;
1671  pSrc++;
1672  if (num <= 127)
1673  {
1674  *pTarget = (char)num;
1675  pTarget++;
1676  continue;
1677  }
1678  goto IL_03fc;
1679  }
1680  continue;
1681  IL_0161:
1682  if (num > 127)
1683  {
1684  if ((num & 0x40) != 0)
1685  {
1686  if ((num & 0x20) != 0)
1687  {
1688  if ((num & 0x10) == 0)
1689  {
1690  num = ((num & 0xF) | 0x48228000);
1691  continue;
1692  }
1693  num &= 0xF;
1694  if (num <= 4)
1695  {
1696  num |= 0x504D0C00;
1697  continue;
1698  }
1699  num |= 0xF0;
1700  }
1701  else
1702  {
1703  num &= 0x1F;
1704  if (num > 1)
1705  {
1706  num |= 0x800000;
1707  continue;
1708  }
1709  num |= 0xC0;
1710  }
1711  }
1712  goto IL_00fd;
1713  }
1714  goto IL_01e2;
1715  IL_00fd:
1716  if (decoderFallbackBuffer == null)
1717  {
1718  decoderFallbackBuffer = ((baseDecoder != null) ? baseDecoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
1719  decoderFallbackBuffer.InternalInitialize(bytes, ptr2);
1720  }
1721  if (!FallbackInvalidByteSequence(ref pSrc, num, decoderFallbackBuffer, ref pTarget))
1722  {
1723  decoderFallbackBuffer.InternalReset();
1724  ThrowCharsOverflow(baseDecoder, pTarget == chars);
1725  num = 0;
1726  break;
1727  }
1728  num = 0;
1729  }
1730  if (num != 0 && (baseDecoder == null || baseDecoder.MustFlush))
1731  {
1732  if (decoderFallbackBuffer == null)
1733  {
1734  decoderFallbackBuffer = ((baseDecoder != null) ? baseDecoder.FallbackBuffer : decoderFallback.CreateFallbackBuffer());
1735  decoderFallbackBuffer.InternalInitialize(bytes, ptr2);
1736  }
1737  if (!FallbackInvalidByteSequence(ref pSrc, num, decoderFallbackBuffer, ref pTarget))
1738  {
1739  decoderFallbackBuffer.InternalReset();
1740  ThrowCharsOverflow(baseDecoder, pTarget == chars);
1741  }
1742  num = 0;
1743  }
1744  if (baseDecoder != null)
1745  {
1746  UTF8Decoder uTF8Decoder2 = (UTF8Decoder)baseDecoder;
1747  uTF8Decoder2.bits = num;
1748  baseDecoder.m_bytesUsed = (int)(pSrc - bytes);
1749  }
1750  return PtrDiff(pTarget, chars);
1751  }
1752 
1753  [SecurityCritical]
1754  private unsafe bool FallbackInvalidByteSequence(ref byte* pSrc, int ch, DecoderFallbackBuffer fallback, ref char* pTarget)
1755  {
1756  byte* pSrc2 = pSrc;
1757  byte[] bytesUnknown = GetBytesUnknown(ref pSrc2, ch);
1758  if (!fallback.InternalFallback(bytesUnknown, pSrc, ref pTarget))
1759  {
1760  pSrc = pSrc2;
1761  return false;
1762  }
1763  return true;
1764  }
1765 
1766  [SecurityCritical]
1767  private unsafe int FallbackInvalidByteSequence(byte* pSrc, int ch, DecoderFallbackBuffer fallback)
1768  {
1769  byte[] bytesUnknown = GetBytesUnknown(ref pSrc, ch);
1770  return fallback.InternalFallback(bytesUnknown, pSrc);
1771  }
1772 
1773  [SecurityCritical]
1774  private unsafe byte[] GetBytesUnknown(ref byte* pSrc, int ch)
1775  {
1776  byte[] array = null;
1777  if (ch < 256 && ch >= 0)
1778  {
1779  pSrc--;
1780  return new byte[1]
1781  {
1782  (byte)ch
1783  };
1784  }
1785  if ((ch & 0x18000000) == 0)
1786  {
1787  pSrc--;
1788  return new byte[1]
1789  {
1790  (byte)((ch & 0x1F) | 0xC0)
1791  };
1792  }
1793  if ((ch & 0x10000000) != 0)
1794  {
1795  if ((ch & 0x800000) != 0)
1796  {
1797  pSrc -= 3;
1798  return new byte[3]
1799  {
1800  (byte)(((ch >> 12) & 7) | 0xF0),
1801  (byte)(((ch >> 6) & 0x3F) | 0x80),
1802  (byte)((ch & 0x3F) | 0x80)
1803  };
1804  }
1805  if ((ch & 0x20000) != 0)
1806  {
1807  pSrc -= 2;
1808  return new byte[2]
1809  {
1810  (byte)(((ch >> 6) & 7) | 0xF0),
1811  (byte)((ch & 0x3F) | 0x80)
1812  };
1813  }
1814  pSrc--;
1815  return new byte[1]
1816  {
1817  (byte)((ch & 7) | 0xF0)
1818  };
1819  }
1820  if ((ch & 0x800000) != 0)
1821  {
1822  pSrc -= 2;
1823  return new byte[2]
1824  {
1825  (byte)(((ch >> 6) & 0xF) | 0xE0),
1826  (byte)((ch & 0x3F) | 0x80)
1827  };
1828  }
1829  pSrc--;
1830  return new byte[1]
1831  {
1832  (byte)((ch & 0xF) | 0xE0)
1833  };
1834  }
1835 
1838  [__DynamicallyInvokable]
1839  public override Decoder GetDecoder()
1840  {
1841  return new UTF8Decoder(this);
1842  }
1843 
1846  [__DynamicallyInvokable]
1847  public override Encoder GetEncoder()
1848  {
1849  return new UTF8Encoder(this);
1850  }
1851 
1859  [__DynamicallyInvokable]
1860  public override int GetMaxByteCount(int charCount)
1861  {
1862  if (charCount < 0)
1863  {
1864  throw new ArgumentOutOfRangeException("charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1865  }
1866  long num = (long)charCount + 1L;
1867  if (base.EncoderFallback.MaxCharCount > 1)
1868  {
1869  num *= base.EncoderFallback.MaxCharCount;
1870  }
1871  num *= 3;
1872  if (num > int.MaxValue)
1873  {
1874  throw new ArgumentOutOfRangeException("charCount", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
1875  }
1876  return (int)num;
1877  }
1878 
1886  [__DynamicallyInvokable]
1887  public override int GetMaxCharCount(int byteCount)
1888  {
1889  if (byteCount < 0)
1890  {
1891  throw new ArgumentOutOfRangeException("byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1892  }
1893  long num = (long)byteCount + 1L;
1894  if (base.DecoderFallback.MaxCharCount > 1)
1895  {
1896  num *= base.DecoderFallback.MaxCharCount;
1897  }
1898  if (num > int.MaxValue)
1899  {
1900  throw new ArgumentOutOfRangeException("byteCount", Environment.GetResourceString("ArgumentOutOfRange_GetCharCountOverflow"));
1901  }
1902  return (int)num;
1903  }
1904 
1907  [__DynamicallyInvokable]
1908  public override byte[] GetPreamble()
1909  {
1910  if (emitUTF8Identifier)
1911  {
1912  return new byte[3]
1913  {
1914  239,
1915  187,
1916  191
1917  };
1918  }
1919  return EmptyArray<byte>.Value;
1920  }
1921 
1926  [__DynamicallyInvokable]
1927  public override bool Equals(object value)
1928  {
1929  UTF8Encoding uTF8Encoding = value as UTF8Encoding;
1930  if (uTF8Encoding != null)
1931  {
1932  if (emitUTF8Identifier == uTF8Encoding.emitUTF8Identifier && base.EncoderFallback.Equals(uTF8Encoding.EncoderFallback))
1933  {
1934  return base.DecoderFallback.Equals(uTF8Encoding.DecoderFallback);
1935  }
1936  return false;
1937  }
1938  return false;
1939  }
1940 
1943  [__DynamicallyInvokable]
1944  public override int GetHashCode()
1945  {
1946  return base.EncoderFallback.GetHashCode() + base.DecoderFallback.GetHashCode() + 65001 + (emitUTF8Identifier ? 1 : 0);
1947  }
1948  }
1949 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
override Decoder GetDecoder()
Obtains a decoder that converts a UTF-8 encoded sequence of bytes into a sequence of Unicode characte...
override Encoder GetEncoder()
Obtains an encoder that converts a sequence of Unicode characters into a UTF-8 encoded sequence of by...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Converts a set of characters into a sequence of bytes.
Definition: Encoder.cs:11
unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
Encodes a set of characters from the specified T:System.String into the specified byte array.
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
Encodes a set of characters starting at the specified character pointer into a sequence of bytes that...
override int GetMaxCharCount(int byteCount)
Calculates the maximum number of characters produced by decoding the specified number of bytes.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
UTF8Encoding(bool encoderShouldEmitUTF8Identifier)
Initializes a new instance of the T:System.Text.UTF8Encoding class. A parameter specifies whether to ...
abstract int Remaining
When overridden in a derived class, gets the number of characters in the current T:System....
DecoderFallback DecoderFallback
Gets or sets the T:System.Text.DecoderFallback object for the current T:System.Text....
Definition: Encoding.cs:884
static DecoderFallback ExceptionFallback
Gets an object that throws an exception when an input byte sequence cannot be decoded.
unsafe override int GetByteCount(char *chars, int count)
Calculates the number of bytes produced by encoding a set of characters starting at the specified cha...
EncoderFallback EncoderFallback
Gets or sets the T:System.Text.EncoderFallback object for the current T:System.Text....
Definition: Encoding.cs:857
Describes the source and destination of a given serialized stream, and provides an additional caller-...
override int GetHashCode()
Returns the hash code for the current instance.
unsafe override string GetString(byte[] bytes, int index, int count)
Decodes a range of bytes from a byte array into a string.
Provides a failure-handling mechanism, called a fallback, for an input character that cannot be conve...
unsafe override int GetCharCount(byte[] bytes, int index, int count)
Calculates the number of characters produced by decoding a sequence of bytes from the specified byte ...
override byte [] GetPreamble()
Returns a Unicode byte order mark encoded in UTF-8 format, if the T:System.Text.UTF8Encoding encoding...
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount)
Decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are ...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
unsafe override int GetByteCount(char[] chars, int index, int count)
Calculates the number of bytes produced by encoding a set of characters from the specified character ...
UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes)
Initializes a new instance of the T:System.Text.UTF8Encoding class. Parameters specify whether to pro...
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
Decodes a sequence of bytes from the specified byte array into the specified character array.
Converts a sequence of encoded bytes into a set of characters.
Definition: Decoder.cs:11
virtual string EncodingName
When overridden in a derived class, gets the human-readable description of the current encoding.
Definition: Encoding.cs:724
Provides a failure-handling mechanism, called a fallback, for an encoded input byte sequence that can...
Represents a UTF-8 encoding of Unicode characters.
Definition: UTF8Encoding.cs:11
The exception thrown when an error occurs during serialization or deserialization.
unsafe override int GetByteCount(string chars)
Calculates the number of bytes produced by encoding the characters in the specified T:System....
UTF8Encoding()
Initializes a new instance of the T:System.Text.UTF8Encoding class.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
override int GetMaxByteCount(int charCount)
Calculates the maximum number of bytes produced by encoding the specified number of characters.
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
Encodes a set of characters from the specified character array into the specified byte array.
The exception that is thrown when one of the arguments provided to a method is not valid.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
override bool Equals(object value)
Determines whether the specified object is equal to the current T:System.Text.UTF8Encoding object.
Provides a buffer that allows a fallback handler to return an alternate string to an encoder when it ...
Specifies that the class can be serialized.
Encoding()
Initializes a new instance of the T:System.Text.Encoding class.
Definition: Encoding.cs:1053
static EncoderFallback ExceptionFallback
Gets an object that throws an exception when an input character cannot be encoded.
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
unsafe override int GetCharCount(byte *bytes, int count)
Calculates the number of characters produced by decoding a sequence of bytes starting at the specifie...