7 [__DynamicallyInvokable]
11 internal class UTF32Decoder : DecoderNLS
15 internal int readByteCount;
17 internal override bool HasState => readByteCount != 0;
24 public override void Reset()
28 if (m_fallbackBuffer !=
null)
30 m_fallbackBuffer.Reset();
35 private bool emitUTF32ByteOrderMark;
37 private bool isThrowException;
39 private bool bigEndian;
42 [__DynamicallyInvokable]
44 : this(bigEndian: false, byteOrderMark: true, throwOnInvalidCharacters: false)
53 [__DynamicallyInvokable]
55 : this(bigEndian, byteOrderMark, throwOnInvalidCharacters: false)
66 [__DynamicallyInvokable]
67 public UTF32Encoding(
bool bigEndian,
bool byteOrderMark,
bool throwOnInvalidCharacters)
68 : base(bigEndian ? 12001 : 12000)
70 this.bigEndian = bigEndian;
71 emitUTF32ByteOrderMark = byteOrderMark;
72 isThrowException = throwOnInvalidCharacters;
75 SetDefaultFallbacks();
79 internal override void SetDefaultFallbacks()
88 encoderFallback =
new EncoderReplacementFallback(
"�");
89 decoderFallback =
new DecoderReplacementFallback(
"�");
106 [SecuritySafeCritical]
107 [__DynamicallyInvokable]
108 public unsafe
override int GetByteCount(
char[] chars,
int index,
int count)
114 if (index < 0 || count < 0)
118 if (chars.Length - index < count)
122 if (chars.Length == 0)
126 fixed (
char* ptr = chars)
141 [SecuritySafeCritical]
142 [__DynamicallyInvokable]
149 fixed (
char* chars = s)
167 [CLSCompliant(
false)]
199 [SecuritySafeCritical]
200 [__DynamicallyInvokable]
201 public unsafe
override int GetBytes(
string s,
int charIndex,
int charCount,
byte[] bytes,
int byteIndex)
203 if (s ==
null || bytes ==
null)
207 if (charIndex < 0 || charCount < 0)
211 if (s.Length - charIndex < charCount)
215 if (byteIndex < 0 || byteIndex > bytes.Length)
219 int byteCount = bytes.Length - byteIndex;
220 if (bytes.Length == 0)
224 fixed (
char* ptr = s)
226 byte[] array = bytes;
227 fixed (
byte* ptr2 = array)
229 return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount,
null);
252 [SecuritySafeCritical]
253 [__DynamicallyInvokable]
254 public unsafe
override int GetBytes(
char[] chars,
int charIndex,
int charCount,
byte[] bytes,
int byteIndex)
256 if (chars ==
null || bytes ==
null)
260 if (charIndex < 0 || charCount < 0)
264 if (chars.Length - charIndex < charCount)
268 if (byteIndex < 0 || byteIndex > bytes.Length)
272 if (chars.Length == 0)
276 int byteCount = bytes.Length - byteIndex;
277 if (bytes.Length == 0)
281 fixed (
char* ptr = chars)
283 byte[] array = bytes;
284 fixed (
byte* ptr2 = array)
286 return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount,
null);
307 [CLSCompliant(
false)]
308 public unsafe
override int GetBytes(
char* chars,
int charCount,
byte* bytes,
int byteCount)
310 if (bytes ==
null || chars ==
null)
314 if (charCount < 0 || byteCount < 0)
318 return GetBytes(chars, charCount, bytes, byteCount,
null);
334 [SecuritySafeCritical]
335 [__DynamicallyInvokable]
336 public unsafe
override int GetCharCount(
byte[] bytes,
int index,
int count)
342 if (index < 0 || count < 0)
346 if (bytes.Length - index < count)
350 if (bytes.Length == 0)
354 fixed (
byte* ptr = bytes)
372 [CLSCompliant(
false)]
404 [SecuritySafeCritical]
405 [__DynamicallyInvokable]
406 public unsafe
override int GetChars(
byte[] bytes,
int byteIndex,
int byteCount,
char[] chars,
int charIndex)
408 if (bytes ==
null || chars ==
null)
412 if (byteIndex < 0 || byteCount < 0)
416 if (bytes.Length - byteIndex < byteCount)
420 if (charIndex < 0 || charIndex > chars.Length)
424 if (bytes.Length == 0)
428 int charCount = chars.Length - charIndex;
429 if (chars.Length == 0)
433 fixed (
byte* ptr = bytes)
435 char[] array = chars;
436 fixed (
char* ptr2 = array)
438 return GetChars(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount,
null);
459 [CLSCompliant(
false)]
460 public unsafe
override int GetChars(
byte* bytes,
int byteCount,
char* chars,
int charCount)
462 if (bytes ==
null || chars ==
null)
466 if (charCount < 0 || byteCount < 0)
470 return GetChars(bytes, byteCount, chars, charCount,
null);
486 [SecuritySafeCritical]
487 [__DynamicallyInvokable]
488 public unsafe
override string GetString(
byte[] bytes,
int index,
int count)
494 if (index < 0 || count < 0)
498 if (bytes.Length - index < count)
502 if (bytes.Length == 0)
506 fixed (
byte* ptr = bytes)
508 return string.CreateStringFromEncoding(ptr + index, count,
this);
513 internal unsafe
override int GetByteCount(
char* chars,
int count, EncoderNLS encoder)
515 char* ptr = chars + count;
516 char* charStart = chars;
522 c = encoder.charLeftOver;
523 encoderFallbackBuffer = encoder.FallbackBuffer;
531 encoderFallbackBuffer = encoderFallback.CreateFallbackBuffer();
533 encoderFallbackBuffer.InternalInitialize(charStart, ptr, encoder, setEncoder:
false);
537 if ((c2 = encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < ptr)
546 if (
char.IsLowSurrogate(c2))
554 encoderFallbackBuffer.InternalFallback(c, ref chars);
558 else if (
char.IsHighSurrogate(c2))
562 else if (
char.IsLowSurrogate(c2))
564 encoderFallbackBuffer.InternalFallback(c2, ref chars);
573 if ((encoder !=
null && !encoder.MustFlush) || c <=
'\0')
577 encoderFallbackBuffer.InternalFallback(c, ref chars);
583 throw new ArgumentOutOfRangeException(
"count", Environment.GetResourceString(
"ArgumentOutOfRange_GetByteCountOverflow"));
589 internal unsafe
override int GetBytes(
char* chars,
int charCount,
byte* bytes,
int byteCount, EncoderNLS encoder)
592 char* ptr2 = chars + charCount;
594 byte* ptr4 = bytes + byteCount;
596 EncoderFallbackBuffer encoderFallbackBuffer =
null;
599 c = encoder.charLeftOver;
600 encoderFallbackBuffer = encoder.FallbackBuffer;
601 if (encoder.m_throwOnOverflow && encoderFallbackBuffer.Remaining > 0)
603 throw new ArgumentException(Environment.GetResourceString(
"Argument_EncoderFallbackNotEmpty",
EncodingName, encoder.Fallback.GetType()));
608 encoderFallbackBuffer = encoderFallback.CreateFallbackBuffer();
610 encoderFallbackBuffer.InternalInitialize(ptr, ptr2, encoder, setEncoder:
true);
614 if ((c2 = encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < ptr2)
623 if (!
char.IsLowSurrogate(c2))
626 encoderFallbackBuffer.InternalFallback(c, ref chars);
630 uint surrogate = GetSurrogate(c, c2);
632 if (bytes + 3 < ptr4)
636 byte* intPtr = bytes;
639 byte* intPtr2 = bytes;
641 *intPtr2 = (byte)(surrogate >> 16);
642 byte* intPtr3 = bytes;
644 *intPtr3 = (byte)(surrogate >> 8);
645 byte* intPtr4 = bytes;
647 *intPtr4 = (byte)surrogate;
651 byte* intPtr5 = bytes;
653 *intPtr5 = (byte)surrogate;
654 byte* intPtr6 = bytes;
656 *intPtr6 = (byte)(surrogate >> 8);
657 byte* intPtr7 = bytes;
659 *intPtr7 = (byte)(surrogate >> 16);
660 byte* intPtr8 = bytes;
666 if (encoderFallbackBuffer.bFallingBack)
668 encoderFallbackBuffer.MovePrevious();
669 encoderFallbackBuffer.MovePrevious();
675 ThrowBytesOverflow(encoder, bytes == ptr3);
680 if (
char.IsHighSurrogate(c2))
685 if (
char.IsLowSurrogate(c2))
687 encoderFallbackBuffer.InternalFallback(c2, ref chars);
690 if (bytes + 3 < ptr4)
694 byte* intPtr9 = bytes;
697 byte* intPtr10 = bytes;
698 bytes = intPtr10 + 1;
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;
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;
718 byte* intPtr16 = bytes;
719 bytes = intPtr16 + 1;
724 if (encoderFallbackBuffer.bFallingBack)
726 encoderFallbackBuffer.MovePrevious();
732 ThrowBytesOverflow(encoder, bytes == ptr3);
735 if ((encoder !=
null && !encoder.MustFlush) || c <=
'\0')
739 encoderFallbackBuffer.InternalFallback(c, ref chars);
744 encoder.charLeftOver = c;
745 encoder.m_charsUsed = (int)(chars - ptr);
747 return (
int)(bytes - ptr3);
751 internal unsafe
override int GetCharCount(
byte* bytes,
int count, DecoderNLS baseDecoder)
753 UTF32Decoder uTF32Decoder = (UTF32Decoder)baseDecoder;
755 byte* ptr = bytes + count;
756 byte* byteStart = bytes;
759 DecoderFallbackBuffer decoderFallbackBuffer =
null;
760 if (uTF32Decoder !=
null)
762 num2 = uTF32Decoder.readByteCount;
763 num3 = (uint)uTF32Decoder.iChar;
764 decoderFallbackBuffer = uTF32Decoder.FallbackBuffer;
768 decoderFallbackBuffer = decoderFallback.CreateFallbackBuffer();
770 decoderFallbackBuffer.InternalInitialize(byteStart,
null);
771 while (bytes < ptr && num >= 0)
777 byte* intPtr = bytes;
779 num3 = num4 + *intPtr;
785 byte* intPtr2 = bytes;
787 num3 = (uint)((
int)num5 + (*intPtr2 << 24));
795 if (num3 > 1114111 || (num3 >= 55296 && num3 <= 57343))
797 byte[] bytes2 = (!bigEndian) ?
new byte[4]
810 num += decoderFallbackBuffer.InternalFallback(bytes2, bytes);
821 if (num2 > 0 && (uTF32Decoder ==
null || uTF32Decoder.MustFlush))
823 byte[] array =
new byte[num2];
828 array[--num2] = (byte)num3;
836 array[--num2] = (byte)(num3 >> 24);
840 num += decoderFallbackBuffer.InternalFallback(array, bytes);
844 throw new ArgumentOutOfRangeException(
"count", Environment.GetResourceString(
"ArgumentOutOfRange_GetByteCountOverflow"));
850 internal unsafe
override int GetChars(
byte* bytes,
int byteCount,
char* chars,
int charCount, DecoderNLS baseDecoder)
852 UTF32Decoder uTF32Decoder = (UTF32Decoder)baseDecoder;
854 char* ptr2 = chars + charCount;
856 byte* ptr4 = bytes + byteCount;
859 DecoderFallbackBuffer decoderFallbackBuffer =
null;
860 if (uTF32Decoder !=
null)
862 num = uTF32Decoder.readByteCount;
863 num2 = (uint)uTF32Decoder.iChar;
864 decoderFallbackBuffer = baseDecoder.FallbackBuffer;
868 decoderFallbackBuffer = decoderFallback.CreateFallbackBuffer();
870 decoderFallbackBuffer.InternalInitialize(bytes, chars + charCount);
877 byte* intPtr = bytes;
879 num2 = num3 + *intPtr;
885 byte* intPtr2 = bytes;
887 num2 = (uint)((
int)num4 + (*intPtr2 << 24));
895 if (num2 > 1114111 || (num2 >= 55296 && num2 <= 57343))
897 byte[] bytes2 = (!bigEndian) ?
new byte[4]
910 if (!decoderFallbackBuffer.InternalFallback(bytes2, bytes, ref chars))
914 decoderFallbackBuffer.InternalReset();
915 ThrowCharsOverflow(uTF32Decoder, chars == ptr);
923 if (chars >= ptr2 - 1)
927 ThrowCharsOverflow(uTF32Decoder, chars == ptr);
930 char* intPtr3 = chars;
932 *intPtr3 = GetHighSurrogate(num2);
933 num2 = GetLowSurrogate(num2);
935 else if (chars >= ptr2)
939 ThrowCharsOverflow(uTF32Decoder, chars == ptr);
942 char* intPtr4 = chars;
944 *intPtr4 = (char)num2;
947 if (num > 0 && (uTF32Decoder ==
null || uTF32Decoder.MustFlush))
949 byte[] array =
new byte[num];
955 array[--num5] = (byte)num2;
963 array[--num5] = (byte)(num2 >> 24);
967 if (!decoderFallbackBuffer.InternalFallback(array, bytes, ref chars))
969 decoderFallbackBuffer.InternalReset();
970 ThrowCharsOverflow(uTF32Decoder, chars == ptr);
978 if (uTF32Decoder !=
null)
980 uTF32Decoder.iChar = (int)num2;
981 uTF32Decoder.readByteCount = num;
982 uTF32Decoder.m_bytesUsed = (int)(bytes - ptr3);
984 return (
int)(chars - ptr);
987 private uint GetSurrogate(
char cHigh,
char cLow)
989 return (uint)((cHigh - 55296) * 1024 + (cLow - 56320) + 65536);
992 private char GetHighSurrogate(uint iChar)
994 return (
char)((iChar - 65536) / 1024u + 55296);
997 private char GetLowSurrogate(uint iChar)
999 return (
char)((iChar - 65536) % 1024u + 56320);
1004 [__DynamicallyInvokable]
1007 return new UTF32Decoder(
this);
1012 [__DynamicallyInvokable]
1015 return new EncoderNLS(
this);
1025 [__DynamicallyInvokable]
1032 long num = (long)charCount + 1
L;
1033 if (base.EncoderFallback.MaxCharCount > 1)
1035 num *= base.EncoderFallback.MaxCharCount;
1038 if (num >
int.MaxValue)
1052 [__DynamicallyInvokable]
1059 int num = byteCount / 2 + 2;
1060 if (base.DecoderFallback.MaxCharCount > 2)
1062 num *= base.DecoderFallback.MaxCharCount;
1065 if (num >
int.MaxValue)
1074 [__DynamicallyInvokable]
1077 if (emitUTF32ByteOrderMark)
1097 return EmptyArray<byte>.Value;
1104 [__DynamicallyInvokable]
1108 if (uTF32Encoding !=
null)
1110 if (emitUTF32ByteOrderMark == uTF32Encoding.emitUTF32ByteOrderMark && bigEndian == uTF32Encoding.bigEndian && base.EncoderFallback.Equals(uTF32Encoding.
EncoderFallback))
1121 [__DynamicallyInvokable]
1124 return base.EncoderFallback.GetHashCode() + base.DecoderFallback.GetHashCode() +
CodePage + (emitUTF32ByteOrderMark ? 4 : 0) + (bigEndian ? 8 : 0);
Represents a character encoding.To browse the .NET Framework source code for this type,...
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.
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 ...
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....
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....
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....
virtual int CodePage
When overridden in a derived class, gets the code page identifier of the current T:System....
Represents a UTF-32 encoding of Unicode characters.
Converts a sequence of encoded bytes into a set of characters.
virtual string EncodingName
When overridden in a derived class, gets the human-readable description of the current encoding.
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.