mscorlib(4.0.0.0) API with additions
UTF32Encoding.cs
1 using System.Security;
2 
3 namespace System.Text
4 {
7  [__DynamicallyInvokable]
8  public sealed class UTF32Encoding : Encoding
9  {
10  [Serializable]
11  internal class UTF32Decoder : DecoderNLS
12  {
13  internal int iChar;
14 
15  internal int readByteCount;
16 
17  internal override bool HasState => readByteCount != 0;
18 
19  public UTF32Decoder(UTF32Encoding encoding)
20  : base(encoding)
21  {
22  }
23 
24  public override void Reset()
25  {
26  iChar = 0;
27  readByteCount = 0;
28  if (m_fallbackBuffer != null)
29  {
30  m_fallbackBuffer.Reset();
31  }
32  }
33  }
34 
35  private bool emitUTF32ByteOrderMark;
36 
37  private bool isThrowException;
38 
39  private bool bigEndian;
40 
42  [__DynamicallyInvokable]
43  public UTF32Encoding()
44  : this(bigEndian: false, byteOrderMark: true, throwOnInvalidCharacters: false)
45  {
46  }
47 
53  [__DynamicallyInvokable]
54  public UTF32Encoding(bool bigEndian, bool byteOrderMark)
55  : this(bigEndian, byteOrderMark, throwOnInvalidCharacters: false)
56  {
57  }
58 
66  [__DynamicallyInvokable]
67  public UTF32Encoding(bool bigEndian, bool byteOrderMark, bool throwOnInvalidCharacters)
68  : base(bigEndian ? 12001 : 12000)
69  {
70  this.bigEndian = bigEndian;
71  emitUTF32ByteOrderMark = byteOrderMark;
72  isThrowException = throwOnInvalidCharacters;
73  if (isThrowException)
74  {
75  SetDefaultFallbacks();
76  }
77  }
78 
79  internal override void SetDefaultFallbacks()
80  {
81  if (isThrowException)
82  {
83  encoderFallback = EncoderFallback.ExceptionFallback;
84  decoderFallback = DecoderFallback.ExceptionFallback;
85  }
86  else
87  {
88  encoderFallback = new EncoderReplacementFallback("�");
89  decoderFallback = new DecoderReplacementFallback("�");
90  }
91  }
92 
106  [SecuritySafeCritical]
107  [__DynamicallyInvokable]
108  public unsafe override int GetByteCount(char[] chars, int index, int count)
109  {
110  if (chars == null)
111  {
112  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
113  }
114  if (index < 0 || count < 0)
115  {
116  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
117  }
118  if (chars.Length - index < count)
119  {
120  throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
121  }
122  if (chars.Length == 0)
123  {
124  return 0;
125  }
126  fixed (char* ptr = chars)
127  {
128  return GetByteCount(ptr + index, count, null);
129  }
130  }
131 
141  [SecuritySafeCritical]
142  [__DynamicallyInvokable]
143  public unsafe override int GetByteCount(string s)
144  {
145  if (s == null)
146  {
147  throw new ArgumentNullException("s");
148  }
149  fixed (char* chars = s)
150  {
151  return GetByteCount(chars, s.Length, null);
152  }
153  }
154 
166  [SecurityCritical]
167  [CLSCompliant(false)]
168  public unsafe override int GetByteCount(char* chars, int count)
169  {
170  if (chars == null)
171  {
172  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
173  }
174  if (count < 0)
175  {
176  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
177  }
178  return GetByteCount(chars, count, null);
179  }
180 
199  [SecuritySafeCritical]
200  [__DynamicallyInvokable]
201  public unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
202  {
203  if (s == null || bytes == null)
204  {
205  throw new ArgumentNullException((s == null) ? "s" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
206  }
207  if (charIndex < 0 || charCount < 0)
208  {
209  throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
210  }
211  if (s.Length - charIndex < charCount)
212  {
213  throw new ArgumentOutOfRangeException("s", Environment.GetResourceString("ArgumentOutOfRange_IndexCount"));
214  }
215  if (byteIndex < 0 || byteIndex > bytes.Length)
216  {
217  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
218  }
219  int byteCount = bytes.Length - byteIndex;
220  if (bytes.Length == 0)
221  {
222  bytes = new byte[1];
223  }
224  fixed (char* ptr = s)
225  {
226  byte[] array = bytes;
227  fixed (byte* ptr2 = array)
228  {
229  return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
230  }
231  }
232  }
233 
252  [SecuritySafeCritical]
253  [__DynamicallyInvokable]
254  public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
255  {
256  if (chars == null || bytes == null)
257  {
258  throw new ArgumentNullException((chars == null) ? "chars" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
259  }
260  if (charIndex < 0 || charCount < 0)
261  {
262  throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
263  }
264  if (chars.Length - charIndex < charCount)
265  {
266  throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
267  }
268  if (byteIndex < 0 || byteIndex > bytes.Length)
269  {
270  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
271  }
272  if (chars.Length == 0)
273  {
274  return 0;
275  }
276  int byteCount = bytes.Length - byteIndex;
277  if (bytes.Length == 0)
278  {
279  bytes = new byte[1];
280  }
281  fixed (char* ptr = chars)
282  {
283  byte[] array = bytes;
284  fixed (byte* ptr2 = array)
285  {
286  return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
287  }
288  }
289  }
290 
306  [SecurityCritical]
307  [CLSCompliant(false)]
308  public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
309  {
310  if (bytes == null || chars == null)
311  {
312  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
313  }
314  if (charCount < 0 || byteCount < 0)
315  {
316  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
317  }
318  return GetBytes(chars, charCount, bytes, byteCount, null);
319  }
320 
334  [SecuritySafeCritical]
335  [__DynamicallyInvokable]
336  public unsafe override int GetCharCount(byte[] bytes, int index, int count)
337  {
338  if (bytes == null)
339  {
340  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
341  }
342  if (index < 0 || count < 0)
343  {
344  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
345  }
346  if (bytes.Length - index < count)
347  {
348  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
349  }
350  if (bytes.Length == 0)
351  {
352  return 0;
353  }
354  fixed (byte* ptr = bytes)
355  {
356  return GetCharCount(ptr + index, count, null);
357  }
358  }
359 
371  [SecurityCritical]
372  [CLSCompliant(false)]
373  public unsafe override int GetCharCount(byte* bytes, int count)
374  {
375  if (bytes == null)
376  {
377  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
378  }
379  if (count < 0)
380  {
381  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
382  }
383  return GetCharCount(bytes, count, null);
384  }
385 
404  [SecuritySafeCritical]
405  [__DynamicallyInvokable]
406  public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
407  {
408  if (bytes == null || chars == null)
409  {
410  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
411  }
412  if (byteIndex < 0 || byteCount < 0)
413  {
414  throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
415  }
416  if (bytes.Length - byteIndex < byteCount)
417  {
418  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
419  }
420  if (charIndex < 0 || charIndex > chars.Length)
421  {
422  throw new ArgumentOutOfRangeException("charIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
423  }
424  if (bytes.Length == 0)
425  {
426  return 0;
427  }
428  int charCount = chars.Length - charIndex;
429  if (chars.Length == 0)
430  {
431  chars = new char[1];
432  }
433  fixed (byte* ptr = bytes)
434  {
435  char[] array = chars;
436  fixed (char* ptr2 = array)
437  {
438  return GetChars(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount, null);
439  }
440  }
441  }
442 
458  [SecurityCritical]
459  [CLSCompliant(false)]
460  public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
461  {
462  if (bytes == null || chars == null)
463  {
464  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
465  }
466  if (charCount < 0 || byteCount < 0)
467  {
468  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
469  }
470  return GetChars(bytes, byteCount, chars, charCount, null);
471  }
472 
486  [SecuritySafeCritical]
487  [__DynamicallyInvokable]
488  public unsafe override string GetString(byte[] bytes, int index, int count)
489  {
490  if (bytes == null)
491  {
492  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
493  }
494  if (index < 0 || count < 0)
495  {
496  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
497  }
498  if (bytes.Length - index < count)
499  {
500  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
501  }
502  if (bytes.Length == 0)
503  {
504  return string.Empty;
505  }
506  fixed (byte* ptr = bytes)
507  {
508  return string.CreateStringFromEncoding(ptr + index, count, this);
509  }
510  }
511 
512  [SecurityCritical]
513  internal unsafe override int GetByteCount(char* chars, int count, EncoderNLS encoder)
514  {
515  char* ptr = chars + count;
516  char* charStart = chars;
517  int num = 0;
518  char c = '\0';
519  EncoderFallbackBuffer encoderFallbackBuffer = null;
520  if (encoder != null)
521  {
522  c = encoder.charLeftOver;
523  encoderFallbackBuffer = encoder.FallbackBuffer;
524  if (encoderFallbackBuffer.Remaining > 0)
525  {
526  throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", EncodingName, encoder.Fallback.GetType()));
527  }
528  }
529  else
530  {
531  encoderFallbackBuffer = encoderFallback.CreateFallbackBuffer();
532  }
533  encoderFallbackBuffer.InternalInitialize(charStart, ptr, encoder, setEncoder: false);
534  while (true)
535  {
536  char c2;
537  if ((c2 = encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < ptr)
538  {
539  if (c2 == '\0')
540  {
541  c2 = *chars;
542  chars++;
543  }
544  if (c != 0)
545  {
546  if (char.IsLowSurrogate(c2))
547  {
548  c = '\0';
549  num += 4;
550  }
551  else
552  {
553  chars--;
554  encoderFallbackBuffer.InternalFallback(c, ref chars);
555  c = '\0';
556  }
557  }
558  else if (char.IsHighSurrogate(c2))
559  {
560  c = c2;
561  }
562  else if (char.IsLowSurrogate(c2))
563  {
564  encoderFallbackBuffer.InternalFallback(c2, ref chars);
565  }
566  else
567  {
568  num += 4;
569  }
570  }
571  else
572  {
573  if ((encoder != null && !encoder.MustFlush) || c <= '\0')
574  {
575  break;
576  }
577  encoderFallbackBuffer.InternalFallback(c, ref chars);
578  c = '\0';
579  }
580  }
581  if (num < 0)
582  {
583  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
584  }
585  return num;
586  }
587 
588  [SecurityCritical]
589  internal unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS encoder)
590  {
591  char* ptr = chars;
592  char* ptr2 = chars + charCount;
593  byte* ptr3 = bytes;
594  byte* ptr4 = bytes + byteCount;
595  char c = '\0';
596  EncoderFallbackBuffer encoderFallbackBuffer = null;
597  if (encoder != null)
598  {
599  c = encoder.charLeftOver;
600  encoderFallbackBuffer = encoder.FallbackBuffer;
601  if (encoder.m_throwOnOverflow && encoderFallbackBuffer.Remaining > 0)
602  {
603  throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", EncodingName, encoder.Fallback.GetType()));
604  }
605  }
606  else
607  {
608  encoderFallbackBuffer = encoderFallback.CreateFallbackBuffer();
609  }
610  encoderFallbackBuffer.InternalInitialize(ptr, ptr2, encoder, setEncoder: true);
611  while (true)
612  {
613  char c2;
614  if ((c2 = encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < ptr2)
615  {
616  if (c2 == '\0')
617  {
618  c2 = *chars;
619  chars++;
620  }
621  if (c != 0)
622  {
623  if (!char.IsLowSurrogate(c2))
624  {
625  chars--;
626  encoderFallbackBuffer.InternalFallback(c, ref chars);
627  c = '\0';
628  continue;
629  }
630  uint surrogate = GetSurrogate(c, c2);
631  c = '\0';
632  if (bytes + 3 < ptr4)
633  {
634  if (bigEndian)
635  {
636  byte* intPtr = bytes;
637  bytes = intPtr + 1;
638  *intPtr = 0;
639  byte* intPtr2 = bytes;
640  bytes = intPtr2 + 1;
641  *intPtr2 = (byte)(surrogate >> 16);
642  byte* intPtr3 = bytes;
643  bytes = intPtr3 + 1;
644  *intPtr3 = (byte)(surrogate >> 8);
645  byte* intPtr4 = bytes;
646  bytes = intPtr4 + 1;
647  *intPtr4 = (byte)surrogate;
648  }
649  else
650  {
651  byte* intPtr5 = bytes;
652  bytes = intPtr5 + 1;
653  *intPtr5 = (byte)surrogate;
654  byte* intPtr6 = bytes;
655  bytes = intPtr6 + 1;
656  *intPtr6 = (byte)(surrogate >> 8);
657  byte* intPtr7 = bytes;
658  bytes = intPtr7 + 1;
659  *intPtr7 = (byte)(surrogate >> 16);
660  byte* intPtr8 = bytes;
661  bytes = intPtr8 + 1;
662  *intPtr8 = 0;
663  }
664  continue;
665  }
666  if (encoderFallbackBuffer.bFallingBack)
667  {
668  encoderFallbackBuffer.MovePrevious();
669  encoderFallbackBuffer.MovePrevious();
670  }
671  else
672  {
673  chars -= 2;
674  }
675  ThrowBytesOverflow(encoder, bytes == ptr3);
676  c = '\0';
677  }
678  else
679  {
680  if (char.IsHighSurrogate(c2))
681  {
682  c = c2;
683  continue;
684  }
685  if (char.IsLowSurrogate(c2))
686  {
687  encoderFallbackBuffer.InternalFallback(c2, ref chars);
688  continue;
689  }
690  if (bytes + 3 < ptr4)
691  {
692  if (bigEndian)
693  {
694  byte* intPtr9 = bytes;
695  bytes = intPtr9 + 1;
696  *intPtr9 = 0;
697  byte* intPtr10 = bytes;
698  bytes = intPtr10 + 1;
699  *intPtr10 = 0;
700  byte* intPtr11 = bytes;
701  bytes = intPtr11 + 1;
702  *intPtr11 = (byte)((uint)c2 >> 8);
703  byte* intPtr12 = bytes;
704  bytes = intPtr12 + 1;
705  *intPtr12 = (byte)c2;
706  }
707  else
708  {
709  byte* intPtr13 = bytes;
710  bytes = intPtr13 + 1;
711  *intPtr13 = (byte)c2;
712  byte* intPtr14 = bytes;
713  bytes = intPtr14 + 1;
714  *intPtr14 = (byte)((uint)c2 >> 8);
715  byte* intPtr15 = bytes;
716  bytes = intPtr15 + 1;
717  *intPtr15 = 0;
718  byte* intPtr16 = bytes;
719  bytes = intPtr16 + 1;
720  *intPtr16 = 0;
721  }
722  continue;
723  }
724  if (encoderFallbackBuffer.bFallingBack)
725  {
726  encoderFallbackBuffer.MovePrevious();
727  }
728  else
729  {
730  chars--;
731  }
732  ThrowBytesOverflow(encoder, bytes == ptr3);
733  }
734  }
735  if ((encoder != null && !encoder.MustFlush) || c <= '\0')
736  {
737  break;
738  }
739  encoderFallbackBuffer.InternalFallback(c, ref chars);
740  c = '\0';
741  }
742  if (encoder != null)
743  {
744  encoder.charLeftOver = c;
745  encoder.m_charsUsed = (int)(chars - ptr);
746  }
747  return (int)(bytes - ptr3);
748  }
749 
750  [SecurityCritical]
751  internal unsafe override int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
752  {
753  UTF32Decoder uTF32Decoder = (UTF32Decoder)baseDecoder;
754  int num = 0;
755  byte* ptr = bytes + count;
756  byte* byteStart = bytes;
757  int num2 = 0;
758  uint num3 = 0u;
759  DecoderFallbackBuffer decoderFallbackBuffer = null;
760  if (uTF32Decoder != null)
761  {
762  num2 = uTF32Decoder.readByteCount;
763  num3 = (uint)uTF32Decoder.iChar;
764  decoderFallbackBuffer = uTF32Decoder.FallbackBuffer;
765  }
766  else
767  {
768  decoderFallbackBuffer = decoderFallback.CreateFallbackBuffer();
769  }
770  decoderFallbackBuffer.InternalInitialize(byteStart, null);
771  while (bytes < ptr && num >= 0)
772  {
773  if (bigEndian)
774  {
775  num3 <<= 8;
776  uint num4 = num3;
777  byte* intPtr = bytes;
778  bytes = intPtr + 1;
779  num3 = num4 + *intPtr;
780  }
781  else
782  {
783  num3 >>= 8;
784  uint num5 = num3;
785  byte* intPtr2 = bytes;
786  bytes = intPtr2 + 1;
787  num3 = (uint)((int)num5 + (*intPtr2 << 24));
788  }
789  num2++;
790  if (num2 < 4)
791  {
792  continue;
793  }
794  num2 = 0;
795  if (num3 > 1114111 || (num3 >= 55296 && num3 <= 57343))
796  {
797  byte[] bytes2 = (!bigEndian) ? new byte[4]
798  {
799  (byte)num3,
800  (byte)(num3 >> 8),
801  (byte)(num3 >> 16),
802  (byte)(num3 >> 24)
803  } : new byte[4]
804  {
805  (byte)(num3 >> 24),
806  (byte)(num3 >> 16),
807  (byte)(num3 >> 8),
808  (byte)num3
809  };
810  num += decoderFallbackBuffer.InternalFallback(bytes2, bytes);
811  num3 = 0u;
812  continue;
813  }
814  if (num3 >= 65536)
815  {
816  num++;
817  }
818  num++;
819  num3 = 0u;
820  }
821  if (num2 > 0 && (uTF32Decoder == null || uTF32Decoder.MustFlush))
822  {
823  byte[] array = new byte[num2];
824  if (bigEndian)
825  {
826  while (num2 > 0)
827  {
828  array[--num2] = (byte)num3;
829  num3 >>= 8;
830  }
831  }
832  else
833  {
834  while (num2 > 0)
835  {
836  array[--num2] = (byte)(num3 >> 24);
837  num3 <<= 8;
838  }
839  }
840  num += decoderFallbackBuffer.InternalFallback(array, bytes);
841  }
842  if (num < 0)
843  {
844  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
845  }
846  return num;
847  }
848 
849  [SecurityCritical]
850  internal unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
851  {
852  UTF32Decoder uTF32Decoder = (UTF32Decoder)baseDecoder;
853  char* ptr = chars;
854  char* ptr2 = chars + charCount;
855  byte* ptr3 = bytes;
856  byte* ptr4 = bytes + byteCount;
857  int num = 0;
858  uint num2 = 0u;
859  DecoderFallbackBuffer decoderFallbackBuffer = null;
860  if (uTF32Decoder != null)
861  {
862  num = uTF32Decoder.readByteCount;
863  num2 = (uint)uTF32Decoder.iChar;
864  decoderFallbackBuffer = baseDecoder.FallbackBuffer;
865  }
866  else
867  {
868  decoderFallbackBuffer = decoderFallback.CreateFallbackBuffer();
869  }
870  decoderFallbackBuffer.InternalInitialize(bytes, chars + charCount);
871  while (bytes < ptr4)
872  {
873  if (bigEndian)
874  {
875  num2 <<= 8;
876  uint num3 = num2;
877  byte* intPtr = bytes;
878  bytes = intPtr + 1;
879  num2 = num3 + *intPtr;
880  }
881  else
882  {
883  num2 >>= 8;
884  uint num4 = num2;
885  byte* intPtr2 = bytes;
886  bytes = intPtr2 + 1;
887  num2 = (uint)((int)num4 + (*intPtr2 << 24));
888  }
889  num++;
890  if (num < 4)
891  {
892  continue;
893  }
894  num = 0;
895  if (num2 > 1114111 || (num2 >= 55296 && num2 <= 57343))
896  {
897  byte[] bytes2 = (!bigEndian) ? new byte[4]
898  {
899  (byte)num2,
900  (byte)(num2 >> 8),
901  (byte)(num2 >> 16),
902  (byte)(num2 >> 24)
903  } : new byte[4]
904  {
905  (byte)(num2 >> 24),
906  (byte)(num2 >> 16),
907  (byte)(num2 >> 8),
908  (byte)num2
909  };
910  if (!decoderFallbackBuffer.InternalFallback(bytes2, bytes, ref chars))
911  {
912  bytes -= 4;
913  num2 = 0u;
914  decoderFallbackBuffer.InternalReset();
915  ThrowCharsOverflow(uTF32Decoder, chars == ptr);
916  break;
917  }
918  num2 = 0u;
919  continue;
920  }
921  if (num2 >= 65536)
922  {
923  if (chars >= ptr2 - 1)
924  {
925  bytes -= 4;
926  num2 = 0u;
927  ThrowCharsOverflow(uTF32Decoder, chars == ptr);
928  break;
929  }
930  char* intPtr3 = chars;
931  chars = intPtr3 + 1;
932  *intPtr3 = GetHighSurrogate(num2);
933  num2 = GetLowSurrogate(num2);
934  }
935  else if (chars >= ptr2)
936  {
937  bytes -= 4;
938  num2 = 0u;
939  ThrowCharsOverflow(uTF32Decoder, chars == ptr);
940  break;
941  }
942  char* intPtr4 = chars;
943  chars = intPtr4 + 1;
944  *intPtr4 = (char)num2;
945  num2 = 0u;
946  }
947  if (num > 0 && (uTF32Decoder == null || uTF32Decoder.MustFlush))
948  {
949  byte[] array = new byte[num];
950  int num5 = num;
951  if (bigEndian)
952  {
953  while (num5 > 0)
954  {
955  array[--num5] = (byte)num2;
956  num2 >>= 8;
957  }
958  }
959  else
960  {
961  while (num5 > 0)
962  {
963  array[--num5] = (byte)(num2 >> 24);
964  num2 <<= 8;
965  }
966  }
967  if (!decoderFallbackBuffer.InternalFallback(array, bytes, ref chars))
968  {
969  decoderFallbackBuffer.InternalReset();
970  ThrowCharsOverflow(uTF32Decoder, chars == ptr);
971  }
972  else
973  {
974  num = 0;
975  num2 = 0u;
976  }
977  }
978  if (uTF32Decoder != null)
979  {
980  uTF32Decoder.iChar = (int)num2;
981  uTF32Decoder.readByteCount = num;
982  uTF32Decoder.m_bytesUsed = (int)(bytes - ptr3);
983  }
984  return (int)(chars - ptr);
985  }
986 
987  private uint GetSurrogate(char cHigh, char cLow)
988  {
989  return (uint)((cHigh - 55296) * 1024 + (cLow - 56320) + 65536);
990  }
991 
992  private char GetHighSurrogate(uint iChar)
993  {
994  return (char)((iChar - 65536) / 1024u + 55296);
995  }
996 
997  private char GetLowSurrogate(uint iChar)
998  {
999  return (char)((iChar - 65536) % 1024u + 56320);
1000  }
1001 
1004  [__DynamicallyInvokable]
1005  public override Decoder GetDecoder()
1006  {
1007  return new UTF32Decoder(this);
1008  }
1009 
1012  [__DynamicallyInvokable]
1013  public override Encoder GetEncoder()
1014  {
1015  return new EncoderNLS(this);
1016  }
1017 
1025  [__DynamicallyInvokable]
1026  public override int GetMaxByteCount(int charCount)
1027  {
1028  if (charCount < 0)
1029  {
1030  throw new ArgumentOutOfRangeException("charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1031  }
1032  long num = (long)charCount + 1L;
1033  if (base.EncoderFallback.MaxCharCount > 1)
1034  {
1035  num *= base.EncoderFallback.MaxCharCount;
1036  }
1037  num *= 4;
1038  if (num > int.MaxValue)
1039  {
1040  throw new ArgumentOutOfRangeException("charCount", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
1041  }
1042  return (int)num;
1043  }
1044 
1052  [__DynamicallyInvokable]
1053  public override int GetMaxCharCount(int byteCount)
1054  {
1055  if (byteCount < 0)
1056  {
1057  throw new ArgumentOutOfRangeException("byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1058  }
1059  int num = byteCount / 2 + 2;
1060  if (base.DecoderFallback.MaxCharCount > 2)
1061  {
1062  num *= base.DecoderFallback.MaxCharCount;
1063  num /= 2;
1064  }
1065  if (num > int.MaxValue)
1066  {
1067  throw new ArgumentOutOfRangeException("byteCount", Environment.GetResourceString("ArgumentOutOfRange_GetCharCountOverflow"));
1068  }
1069  return num;
1070  }
1071 
1074  [__DynamicallyInvokable]
1075  public override byte[] GetPreamble()
1076  {
1077  if (emitUTF32ByteOrderMark)
1078  {
1079  if (!bigEndian)
1080  {
1081  return new byte[4]
1082  {
1083  255,
1084  254,
1085  0,
1086  0
1087  };
1088  }
1089  return new byte[4]
1090  {
1091  0,
1092  0,
1093  254,
1094  255
1095  };
1096  }
1097  return EmptyArray<byte>.Value;
1098  }
1099 
1104  [__DynamicallyInvokable]
1105  public override bool Equals(object value)
1106  {
1107  UTF32Encoding uTF32Encoding = value as UTF32Encoding;
1108  if (uTF32Encoding != null)
1109  {
1110  if (emitUTF32ByteOrderMark == uTF32Encoding.emitUTF32ByteOrderMark && bigEndian == uTF32Encoding.bigEndian && base.EncoderFallback.Equals(uTF32Encoding.EncoderFallback))
1111  {
1112  return base.DecoderFallback.Equals(uTF32Encoding.DecoderFallback);
1113  }
1114  return false;
1115  }
1116  return false;
1117  }
1118 
1121  [__DynamicallyInvokable]
1122  public override int GetHashCode()
1123  {
1124  return base.EncoderFallback.GetHashCode() + base.DecoderFallback.GetHashCode() + CodePage + (emitUTF32ByteOrderMark ? 4 : 0) + (bigEndian ? 8 : 0);
1125  }
1126  }
1127 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
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 GetCharCount(byte[] bytes, int index, int count)
Calculates the number of characters produced by decoding a sequence of bytes from the specified byte ...
unsafe override int GetByteCount(string s)
Calculates the number of bytes produced by encoding the characters in the specified T:System....
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 ...
Definition: __Canon.cs:3
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 ...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
override Decoder GetDecoder()
Obtains a decoder that converts a UTF-32 encoded sequence of bytes into a sequence of Unicode charact...
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
override Encoder GetEncoder()
Obtains an encoder that converts a sequence of Unicode characters into a UTF-32 encoded sequence of b...
unsafe override string GetString(byte[] bytes, int index, int count)
Decodes a range of bytes from a byte array into a string.
static DecoderFallback ExceptionFallback
Gets an object that throws an exception when an input byte sequence cannot be decoded.
EncoderFallback EncoderFallback
Gets or sets the T:System.Text.EncoderFallback object for the current T:System.Text....
Definition: Encoding.cs:857
override int GetHashCode()
Returns the hash code for the current instance.
unsafe override int GetCharCount(byte *bytes, int count)
Calculates the number of characters produced by decoding a sequence of bytes starting at the specifie...
Provides a failure-handling mechanism, called a fallback, for an input character that cannot be conve...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual int CodePage
When overridden in a derived class, gets the code page identifier of the current T:System....
Definition: Encoding.cs:948
Represents a UTF-32 encoding of Unicode characters.
Definition: UTF32Encoding.cs:8
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
UTF32Encoding(bool bigEndian, bool byteOrderMark)
Initializes a new instance of the T:System.Text.UTF32Encoding class. Parameters specify whether to us...
Provides a failure-handling mechanism, called a fallback, for an encoded input byte sequence that can...
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.
override int GetMaxByteCount(int charCount)
Calculates the maximum number of bytes produced by encoding the specified number of characters.
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.
override int GetMaxCharCount(int byteCount)
Calculates the maximum number of characters produced by decoding the specified number of bytes.
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.
override bool Equals(object value)
Determines whether the specified T:System.Object is equal to the current T:System....
static EncoderFallback ExceptionFallback
Gets an object that throws an exception when an input character cannot be encoded.
override byte [] GetPreamble()
Returns a Unicode byte order mark encoded in UTF-32 format, if the T:System.Text.UTF32Encoding object...
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...
UTF32Encoding()
Initializes a new instance of the T:System.Text.UTF32Encoding class.
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...
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.