diff options
Diffstat (limited to 'src/mscorlib/src/System/Text/UTF8Encoding.cs')
-rw-r--r-- | src/mscorlib/src/System/Text/UTF8Encoding.cs | 96 |
1 files changed, 41 insertions, 55 deletions
diff --git a/src/mscorlib/src/System/Text/UTF8Encoding.cs b/src/mscorlib/src/System/Text/UTF8Encoding.cs index a527de7b61..ba19649b56 100644 --- a/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF8Encoding.cs @@ -21,6 +21,7 @@ namespace System.Text using System.Globalization; using System.Runtime.Serialization; using System.Security.Permissions; + using System.Diagnostics; using System.Diagnostics.Contracts; // Encodes text into and out of UTF-8. UTF-8 is a way of writing @@ -129,7 +130,6 @@ namespace System.Text return EncodingForwarder.GetByteCount(this, chars); } - [System.Security.SecurityCritical] // auto-generated [CLSCompliant(false)] [System.Runtime.InteropServices.ComVisible(false)] public override unsafe int GetByteCount(char* chars, int count) @@ -158,7 +158,6 @@ namespace System.Text return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex); } - [System.Security.SecurityCritical] // auto-generated [CLSCompliant(false)] [System.Runtime.InteropServices.ComVisible(false)] public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount) @@ -174,7 +173,6 @@ namespace System.Text return EncodingForwarder.GetCharCount(this, bytes, index, count); } - [System.Security.SecurityCritical] // auto-generated [CLSCompliant(false)] [System.Runtime.InteropServices.ComVisible(false)] public override unsafe int GetCharCount(byte* bytes, int count) @@ -188,7 +186,6 @@ namespace System.Text return EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex); } - [System.Security.SecurityCritical] // auto-generated [CLSCompliant(false)] [System.Runtime.InteropServices.ComVisible(false)] public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount) @@ -209,7 +206,6 @@ namespace System.Text // To simplify maintenance, the structure of GetByteCount and GetBytes should be // kept the same as much as possible - [System.Security.SecurityCritical] // auto-generated internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS baseEncoder) { // For fallback we may need a fallback buffer. @@ -254,7 +250,7 @@ namespace System.Text } else { // Case of surrogates in the fallback. if (fallbackBuffer != null && fallbackBuffer.bFallingBack) { - Contract.Assert(ch >= 0xD800 && ch <= 0xDBFF, + Debug.Assert(ch >= 0xD800 && ch <= 0xDBFF, "[UTF8Encoding.GetBytes]expected high surrogate, not 0x" + ((int)ch).ToString("X4", CultureInfo.InvariantCulture)); ch = fallbackBuffer.InternalGetNextChar(); @@ -286,7 +282,7 @@ namespace System.Text } if (ch > 0) { - Contract.Assert(ch >= 0xD800 && ch <= 0xDBFF, + Debug.Assert(ch >= 0xD800 && ch <= 0xDBFF, "[UTF8Encoding.GetBytes]expected high surrogate, not 0x" + ((int)ch).ToString("X4", CultureInfo.InvariantCulture)); // use separate helper variables for local contexts so that the jit optimizations @@ -557,7 +553,7 @@ namespace System.Text } #endif - Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, + Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[UTF8Encoding.GetByteCount]Expected Empty fallback buffer"); return byteCount; @@ -566,14 +562,12 @@ namespace System.Text // diffs two char pointers using unsigned arithmetic. The unsigned arithmetic // is good enough for us, and it tends to generate better code than the signed // arithmetic generated by default - [System.Security.SecurityCritical] // auto-generated unsafe private static int PtrDiff(char *a, char* b) { return (int)(((uint)((byte*)a - (byte*)b)) >> 1); } // byte* flavor just for parity - [System.Security.SecurityCritical] // auto-generated unsafe private static int PtrDiff(byte* a, byte* b) { return (int)(a - b); @@ -586,14 +580,13 @@ namespace System.Text // Our workhorse // Note: We ignore mismatched surrogates, unless the exception flag is set in which case we throw - [System.Security.SecurityCritical] // auto-generated internal override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS baseEncoder) { - Contract.Assert(chars!=null, "[UTF8Encoding.GetBytes]chars!=null"); - Contract.Assert(byteCount >=0, "[UTF8Encoding.GetBytes]byteCount >=0"); - Contract.Assert(charCount >=0, "[UTF8Encoding.GetBytes]charCount >=0"); - Contract.Assert(bytes!=null, "[UTF8Encoding.GetBytes]bytes!=null"); + Debug.Assert(chars!=null, "[UTF8Encoding.GetBytes]chars!=null"); + Debug.Assert(byteCount >=0, "[UTF8Encoding.GetBytes]byteCount >=0"); + Debug.Assert(charCount >=0, "[UTF8Encoding.GetBytes]charCount >=0"); + Debug.Assert(bytes!=null, "[UTF8Encoding.GetBytes]bytes!=null"); UTF8Encoder encoder = null; @@ -642,7 +635,7 @@ namespace System.Text } else { // Case of leftover surrogates in the fallback buffer if (fallbackBuffer != null && fallbackBuffer.bFallingBack) { - Contract.Assert(ch >= 0xD800 && ch <= 0xDBFF, + Debug.Assert(ch >= 0xD800 && ch <= 0xDBFF, "[UTF8Encoding.GetBytes]expected high surrogate, not 0x" + ((int)ch).ToString("X4", CultureInfo.InvariantCulture)); int cha = ch; @@ -670,7 +663,7 @@ namespace System.Text if (ch > 0) { // We have a high surrogate left over from a previous loop. - Contract.Assert(ch >= 0xD800 && ch <= 0xDBFF, + Debug.Assert(ch >= 0xD800 && ch <= 0xDBFF, "[UTF8Encoding.GetBytes]expected high surrogate, not 0x" + ((int)ch).ToString("X4", CultureInfo.InvariantCulture)); // use separate helper variables for local contexts so that the jit optimizations @@ -767,7 +760,7 @@ namespace System.Text if (ch > 0xFFFF) pSrc--; // Was surrogate, didn't use 2nd part either } - Contract.Assert(pSrc >= chars || pTarget == bytes, + Debug.Assert(pSrc >= chars || pTarget == bytes, "[UTF8Encoding.GetBytes]Expected pSrc to be within buffer or to throw with insufficient room."); ThrowBytesOverflow(encoder, pTarget == bytes); // Throw if we must ch = 0; // Nothing left over (we backed up to start of pair if supplimentary) @@ -989,7 +982,7 @@ namespace System.Text pTarget++; } - Contract.Assert(pTarget <= pAllocatedBufferEnd, "[UTF8Encoding.GetBytes]pTarget <= pAllocatedBufferEnd"); + Debug.Assert(pTarget <= pAllocatedBufferEnd, "[UTF8Encoding.GetBytes]pTarget <= pAllocatedBufferEnd"); #endif // FASTLOOP @@ -1000,14 +993,14 @@ namespace System.Text // Do we have to set the encoder bytes? if (encoder != null) { - Contract.Assert(!encoder.MustFlush || ch == 0, + Debug.Assert(!encoder.MustFlush || ch == 0, "[UTF8Encoding.GetBytes] Expected no mustflush or 0 leftover ch " + ch.ToString("X2", CultureInfo.InvariantCulture)); encoder.surrogateChar = ch; encoder.m_charsUsed = (int)(pSrc - chars); } - Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 || + Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 || baseEncoder == null || !baseEncoder.m_throwOnOverflow, "[UTF8Encoding.GetBytes]Expected empty fallback buffer if not converting"); @@ -1029,11 +1022,10 @@ namespace System.Text // // To simplify maintenance, the structure of GetCharCount and GetChars should be // kept the same as much as possible - [System.Security.SecurityCritical] // auto-generated internal override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder) { - Contract.Assert(count >=0, "[UTF8Encoding.GetCharCount]count >=0"); - Contract.Assert(bytes!=null, "[UTF8Encoding.GetCharCount]bytes!=null"); + Debug.Assert(count >=0, "[UTF8Encoding.GetCharCount]count >=0"); + Debug.Assert(bytes!=null, "[UTF8Encoding.GetCharCount]bytes!=null"); // Initialize stuff byte *pSrc = bytes; @@ -1052,7 +1044,7 @@ namespace System.Text // Shouldn't have anything in fallback buffer for GetCharCount // (don't have to check m_throwOnOverflow for count) - Contract.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, + Debug.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[UTF8Encoding.GetCharCount]Expected empty fallback buffer at start"); } @@ -1087,7 +1079,7 @@ namespace System.Text ch = (ch << 6) | (cha & 0x3F); if ((ch & FinalByte) == 0) { - Contract.Assert( (ch & (SupplimentarySeq | ThreeByteSeq)) != 0, + Debug.Assert( (ch & (SupplimentarySeq | ThreeByteSeq)) != 0, "[UTF8Encoding.GetChars]Invariant volation"); if ((ch & SupplimentarySeq) != 0) { @@ -1408,7 +1400,7 @@ namespace System.Text // Shouldn't have anything in fallback buffer for GetCharCount // (don't have to check m_throwOnOverflow for count) - Contract.Assert(fallback == null || fallback.Remaining == 0, + Debug.Assert(fallback == null || fallback.Remaining == 0, "[UTF8Encoding.GetCharCount]Expected empty fallback buffer at end"); return charCount; @@ -1424,14 +1416,13 @@ namespace System.Text // // To simplify maintenance, the structure of GetCharCount and GetChars should be // kept the same as much as possible - [System.Security.SecurityCritical] // auto-generated internal override unsafe int GetChars(byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder) { - Contract.Assert(chars!=null, "[UTF8Encoding.GetChars]chars!=null"); - Contract.Assert(byteCount >=0, "[UTF8Encoding.GetChars]count >=0"); - Contract.Assert(charCount >=0, "[UTF8Encoding.GetChars]charCount >=0"); - Contract.Assert(bytes!=null, "[UTF8Encoding.GetChars]bytes!=null"); + Debug.Assert(chars!=null, "[UTF8Encoding.GetChars]chars!=null"); + Debug.Assert(byteCount >=0, "[UTF8Encoding.GetChars]count >=0"); + Debug.Assert(charCount >=0, "[UTF8Encoding.GetChars]charCount >=0"); + Debug.Assert(bytes!=null, "[UTF8Encoding.GetChars]bytes!=null"); byte *pSrc = bytes; char *pTarget = chars; @@ -1448,7 +1439,7 @@ namespace System.Text // Shouldn't have anything in fallback buffer for GetChars // (don't have to check m_throwOnOverflow for chars, we always use all or none so always should be empty) - Contract.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, + Debug.Assert(!decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[UTF8Encoding.GetChars]Expected empty fallback buffer at start"); } @@ -1483,7 +1474,7 @@ namespace System.Text if ((ch & FinalByte) == 0) { // Not at last byte yet - Contract.Assert( (ch & (SupplimentarySeq | ThreeByteSeq)) != 0, + Debug.Assert( (ch & (SupplimentarySeq | ThreeByteSeq)) != 0, "[UTF8Encoding.GetChars]Invariant volation"); if ((ch & SupplimentarySeq) != 0) { @@ -1546,14 +1537,14 @@ namespace System.Text { // Ran out of buffer space // Need to throw an exception? - Contract.Assert(pSrc >= bytes || pTarget == chars, + Debug.Assert(pSrc >= bytes || pTarget == chars, "[UTF8Encoding.GetChars]Expected to throw or remain in byte buffer after fallback"); fallback.InternalReset(); ThrowCharsOverflow(baseDecoder, pTarget == chars); ch = 0; break; } - Contract.Assert(pSrc >= bytes, + Debug.Assert(pSrc >= bytes, "[UTF8Encoding.GetChars]Expected invalid byte sequence to have remained within the byte array"); ch = 0; continue; @@ -1639,7 +1630,7 @@ namespace System.Text // Throw that we don't have enough room (pSrc could be < chars if we had started to process // a 4 byte sequence alredy) - Contract.Assert(pSrc >= bytes || pTarget == chars, + Debug.Assert(pSrc >= bytes || pTarget == chars, "[UTF8Encoding.GetChars]Expected pSrc to be within input buffer or throw due to no output]"); ThrowCharsOverflow(baseDecoder, pTarget == chars); @@ -1893,7 +1884,7 @@ namespace System.Text } #endif // FASTLOOP - Contract.Assert(pTarget <= pAllocatedBufferEnd, "[UTF8Encoding.GetChars]pTarget <= pAllocatedBufferEnd"); + Debug.Assert(pTarget <= pAllocatedBufferEnd, "[UTF8Encoding.GetChars]pTarget <= pAllocatedBufferEnd"); // no pending bits at this point ch = 0; @@ -1920,7 +1911,7 @@ namespace System.Text // This'll back us up the appropriate # of bytes if we didn't get anywhere if (!FallbackInvalidByteSequence(ref pSrc, ch, fallback, ref pTarget)) { - Contract.Assert(pSrc >= bytes || pTarget == chars, + Debug.Assert(pSrc >= bytes || pTarget == chars, "[UTF8Encoding.GetChars]Expected to throw or remain in byte buffer while flushing"); // Ran out of buffer space @@ -1928,7 +1919,7 @@ namespace System.Text fallback.InternalReset(); ThrowCharsOverflow(baseDecoder, pTarget == chars); } - Contract.Assert(pSrc >= bytes, + Debug.Assert(pSrc >= bytes, "[UTF8Encoding.GetChars]Expected flushing invalid byte sequence to have remained within the byte array"); ch = 0; } @@ -1939,7 +1930,7 @@ namespace System.Text // If we're storing flush data we expect all bits to be used or else // we're stuck in the middle of a conversion - Contract.Assert(!baseDecoder.MustFlush || ch == 0 || !baseDecoder.m_throwOnOverflow, + Debug.Assert(!baseDecoder.MustFlush || ch == 0 || !baseDecoder.m_throwOnOverflow, "[UTF8Encoding.GetChars]Expected no must flush or no left over bits or no throw on overflow."); // Remember our leftover bits. @@ -1950,7 +1941,7 @@ namespace System.Text // Shouldn't have anything in fallback buffer for GetChars // (don't have to check m_throwOnOverflow for chars) - Contract.Assert(fallback == null || fallback.Remaining == 0, + Debug.Assert(fallback == null || fallback.Remaining == 0, "[UTF8Encoding.GetChars]Expected empty fallback buffer at end"); return PtrDiff(pTarget, chars); @@ -1959,7 +1950,6 @@ namespace System.Text // During GetChars we had an invalid byte sequence // pSrc is backed up to the start of the bad sequence if we didn't have room to // fall it back. Otherwise pSrc remains wher it is. - [System.Security.SecurityCritical] // auto-generated private unsafe bool FallbackInvalidByteSequence( ref byte* pSrc, int ch, DecoderFallbackBuffer fallback, ref char* pTarget) { @@ -1982,7 +1972,6 @@ namespace System.Text // During GetCharCount we had an invalid byte sequence // pSrc is used to find the index that points to the invalid bytes, // however the byte[] contains the fallback bytes (in case the index is -1) - [System.Security.SecurityCritical] // auto-generated private unsafe int FallbackInvalidByteSequence( byte* pSrc, int ch, DecoderFallbackBuffer fallback) { @@ -2001,7 +1990,6 @@ namespace System.Text // Note that some of these bytes may have come from a previous fallback, so we cannot // just decrement the pointer and use the values we read. In those cases we have // to regenerate the original values. - [System.Security.SecurityCritical] // auto-generated private unsafe byte[] GetBytesUnknown(ref byte* pSrc, int ch) { // Get our byte[] @@ -2085,7 +2073,7 @@ namespace System.Text public override int GetMaxByteCount(int charCount) { if (charCount < 0) - throw new ArgumentOutOfRangeException("charCount", + throw new ArgumentOutOfRangeException(nameof(charCount), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); Contract.EndContractBlock(); @@ -2099,7 +2087,7 @@ namespace System.Text byteCount *= 3; if (byteCount > 0x7fffffff) - throw new ArgumentOutOfRangeException("charCount", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow")); + throw new ArgumentOutOfRangeException(nameof(charCount), Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow")); return (int)byteCount; } @@ -2108,7 +2096,7 @@ namespace System.Text public override int GetMaxCharCount(int byteCount) { if (byteCount < 0) - throw new ArgumentOutOfRangeException("byteCount", + throw new ArgumentOutOfRangeException(nameof(byteCount), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); Contract.EndContractBlock(); @@ -2123,7 +2111,7 @@ namespace System.Text } if (charCount > 0x7fffffff) - throw new ArgumentOutOfRangeException("byteCount", Environment.GetResourceString("ArgumentOutOfRange_GetCharCountOverflow")); + throw new ArgumentOutOfRangeException(nameof(byteCount), Environment.GetResourceString("ArgumentOutOfRange_GetCharCountOverflow")); return (int)charCount; } @@ -2174,7 +2162,7 @@ namespace System.Text internal UTF8Encoder(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException("info"); + if (info==null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // Get common info @@ -2194,11 +2182,10 @@ namespace System.Text } // ISerializable implementation, get data for this object - [System.Security.SecurityCritical] // auto-generated_required void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException("info"); + if (info==null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // Save Whidbey data @@ -2247,7 +2234,7 @@ namespace System.Text internal UTF8Decoder(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException("info"); + if (info==null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // Get common info @@ -2268,11 +2255,10 @@ namespace System.Text } // ISerializable implementation, get data for this object - [System.Security.SecurityCritical] // auto-generated_required void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException("info"); + if (info==null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // Save new Whidbey data |