diff --git a/src/mscorlib/mscorlib.shared.sources.props b/src/mscorlib/mscorlib.shared.sources.props index fc5153f06234..6f3ea268af72 100644 --- a/src/mscorlib/mscorlib.shared.sources.props +++ b/src/mscorlib/mscorlib.shared.sources.props @@ -991,7 +991,6 @@ - diff --git a/src/mscorlib/src/System/Text/ASCIIEncoding.cs b/src/mscorlib/src/System/Text/ASCIIEncoding.cs index 9d1a773f8df2..825a917b607f 100644 --- a/src/mscorlib/src/System/Text/ASCIIEncoding.cs +++ b/src/mscorlib/src/System/Text/ASCIIEncoding.cs @@ -59,9 +59,29 @@ internal override void SetDefaultFallbacks() // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override int GetByteCount(char[] chars, int index, int count) + public override unsafe int GetByteCount(char[] chars, int index, int count) { - return EncodingForwarder.GetByteCount(this, chars, index, count); + // Validate input parameters + if (chars == null) + throw new ArgumentNullException("chars", + Environment.GetResourceString("ArgumentNull_Array")); + + if (index < 0 || count < 0) + throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + + if (chars.Length - index < count) + throw new ArgumentOutOfRangeException("chars", + Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); + Contract.EndContractBlock(); + + // If no input, return 0, avoid fixed empty array problem + if (count == 0) + return 0; + + // Just call the pointer version + fixed (char* pChars = chars) + return GetByteCount(pChars + index, count, null); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/EncodingForwarder.cs b/src/mscorlib/src/System/Text/EncodingForwarder.cs deleted file mode 100644 index 75f8343607f2..000000000000 --- a/src/mscorlib/src/System/Text/EncodingForwarder.cs +++ /dev/null @@ -1,53 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Diagnostics.Contracts; - -namespace System.Text -{ - // Shared implementations for commonly overriden Encoding methods - - internal static class EncodingForwarder - { - // We normally have to duplicate a lot of code between UTF8Encoding, - // UTF7Encoding, EncodingNLS, etc. because we want to override many - // of the methods in all of those classes to just forward to the unsafe - // version. (e.g. GetBytes(char[])) - // Ideally, everything would just derive from EncodingNLS, but that's - // not exposed in the public API, and C# prohibits a public class from - // inheriting from an internal one. So, we have to override each of the - // methods in question and repeat the argument validation/logic. - - // These set of methods exist so instead of duplicating code, we can - // simply have those overriden methods call here to do the actual work. - - public unsafe static int GetByteCount(Encoding encoding, char[] chars, int index, int count) - { - // Validate parameters - - Contract.Assert(encoding != null); // this parameter should only be affected internally, so just do a debug check here - if (chars == null) - { - throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array")); - } - if (index < 0 || count < 0) - { - throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - } - if (chars.Length - index < count) - { - throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); - } - Contract.EndContractBlock(); - - // If no input, return 0, avoid fixed empty array problem - if (count == 0) - return 0; - - // Just call the pointer version - fixed (char* pChars = chars) - return encoding.GetByteCount(pChars + index, count, null); - } - } -} diff --git a/src/mscorlib/src/System/Text/EncodingNLS.cs b/src/mscorlib/src/System/Text/EncodingNLS.cs index 91625ae36793..73aba3df9533 100644 --- a/src/mscorlib/src/System/Text/EncodingNLS.cs +++ b/src/mscorlib/src/System/Text/EncodingNLS.cs @@ -42,9 +42,29 @@ protected EncodingNLS(int codePage) : base(codePage) // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding // parent method is safe [System.Security.SecuritySafeCritical] // overrides public transparent member - public override int GetByteCount(char[] chars, int index, int count) + public override unsafe int GetByteCount(char[] chars, int index, int count) { - return EncodingForwarder.GetByteCount(this, chars, index, count); + // Validate input parameters + if (chars == null) + throw new ArgumentNullException("chars", + Environment.GetResourceString("ArgumentNull_Array")); + + if (index < 0 || count < 0) + throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + + if (chars.Length - index < count) + throw new ArgumentOutOfRangeException("chars", + Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); + Contract.EndContractBlock(); + + // If no input, return 0, avoid fixed empty array problem + if (count == 0) + return 0; + + // Just call the pointer version + fixed (char* pChars = chars) + return GetByteCount(pChars + index, count, null); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UTF32Encoding.cs b/src/mscorlib/src/System/Text/UTF32Encoding.cs index d0df749a75ca..fcf30b4d937c 100644 --- a/src/mscorlib/src/System/Text/UTF32Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF32Encoding.cs @@ -99,9 +99,29 @@ internal override void SetDefaultFallbacks() // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override int GetByteCount(char[] chars, int index, int count) + public override unsafe int GetByteCount(char[] chars, int index, int count) { - return EncodingForwarder.GetByteCount(this, chars, index, count); + // Validate input parameters + if (chars == null) + throw new ArgumentNullException("chars", + Environment.GetResourceString("ArgumentNull_Array")); + + if (index < 0 || count < 0) + throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + + if (chars.Length - index < count) + throw new ArgumentOutOfRangeException("chars", + Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); + Contract.EndContractBlock(); + + // If no input, return 0, avoid fixed empty array problem + if (count == 0) + return 0; + + // Just call the pointer version + fixed (char* pChars = chars) + return GetByteCount(pChars + index, count, null); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UTF7Encoding.cs b/src/mscorlib/src/System/Text/UTF7Encoding.cs index 75d162557349..63e07feb7d8e 100644 --- a/src/mscorlib/src/System/Text/UTF7Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF7Encoding.cs @@ -163,9 +163,29 @@ public override int GetHashCode() // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override int GetByteCount(char[] chars, int index, int count) + public override unsafe int GetByteCount(char[] chars, int index, int count) { - return EncodingForwarder.GetByteCount(this, chars, index, count); + // Validate input parameters + if (chars == null) + throw new ArgumentNullException("chars", + Environment.GetResourceString("ArgumentNull_Array")); + + if (index < 0 || count < 0) + throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + + if (chars.Length - index < count) + throw new ArgumentOutOfRangeException("chars", + Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); + Contract.EndContractBlock(); + + // If no input, return 0, avoid fixed empty array problem + if (count == 0) + return 0; + + // Just call the pointer version + fixed (char* pChars = chars) + return GetByteCount(pChars + index, count, null); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UTF8Encoding.cs b/src/mscorlib/src/System/Text/UTF8Encoding.cs index 6f919df5e9bc..f6a7909ccc7c 100644 --- a/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF8Encoding.cs @@ -123,9 +123,29 @@ internal override void SetDefaultFallbacks() // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override int GetByteCount(char[] chars, int index, int count) + public override unsafe int GetByteCount(char[] chars, int index, int count) { - return EncodingForwarder.GetByteCount(this, chars, index, count); + // Validate input parameters + if (chars == null) + throw new ArgumentNullException("chars", + Environment.GetResourceString("ArgumentNull_Array")); + + if (index < 0 || count < 0) + throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + + if (chars.Length - index < count) + throw new ArgumentOutOfRangeException("chars", + Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); + Contract.EndContractBlock(); + + // If no input, return 0, avoid fixed empty array problem + if (count == 0) + return 0; + + // Just call the pointer version + fixed (char* pChars = chars) + return GetByteCount(pChars + index, count, null); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UnicodeEncoding.cs b/src/mscorlib/src/System/Text/UnicodeEncoding.cs index 71ad1b16b99d..7465d200223a 100644 --- a/src/mscorlib/src/System/Text/UnicodeEncoding.cs +++ b/src/mscorlib/src/System/Text/UnicodeEncoding.cs @@ -98,9 +98,29 @@ internal override void SetDefaultFallbacks() // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override int GetByteCount(char[] chars, int index, int count) + public override unsafe int GetByteCount(char[] chars, int index, int count) { - return EncodingForwarder.GetByteCount(this, chars, index, count); + // Validate input parameters + if (chars == null) + throw new ArgumentNullException("chars", + Environment.GetResourceString("ArgumentNull_Array")); + + if (index < 0 || count < 0) + throw new ArgumentOutOfRangeException((index<0 ? "index" : "count"), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + + if (chars.Length - index < count) + throw new ArgumentOutOfRangeException("chars", + Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); + Contract.EndContractBlock(); + + // If no input, return 0, avoid fixed empty array problem + if (count == 0) + return 0; + + // Just call the pointer version + fixed (char* pChars = chars) + return GetByteCount(pChars + index, count, null); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)