From 209457816e13bb70aecf822a64010e5315e06957 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Thu, 13 Dec 2018 11:41:24 -0800 Subject: [PATCH] Remove Microsoft.Extensions.WebEncoders.Sources Commit migrated from https://github.com/dotnet/Extensions/commit/e7bb41b9bfaba1219dfba3dfd80f3a1452311a72 --- .../Properties/EncoderResources.cs | 38 -- .../src/Extensions/WebEncoders/WebEncoders.cs | 388 ------------------ .../tests/Extensions/WebEncodersTests.cs | 113 ----- 3 files changed, 539 deletions(-) delete mode 100644 src/libraries/Common/src/Extensions/WebEncoders/Properties/EncoderResources.cs delete mode 100644 src/libraries/Common/src/Extensions/WebEncoders/WebEncoders.cs delete mode 100644 src/libraries/Common/tests/Extensions/WebEncodersTests.cs diff --git a/src/libraries/Common/src/Extensions/WebEncoders/Properties/EncoderResources.cs b/src/libraries/Common/src/Extensions/WebEncoders/Properties/EncoderResources.cs deleted file mode 100644 index 3474ae82c5b7c..0000000000000 --- a/src/libraries/Common/src/Extensions/WebEncoders/Properties/EncoderResources.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Globalization; - -namespace Microsoft.Extensions.WebEncoders.Sources -{ - // TODO using a resx file. project.json, unfortunately, fails to embed resx files when there are also compile items - // in the contentFiles section. Revisit once we convert repos to MSBuild - internal static class EncoderResources - { - /// - /// Invalid {0}, {1} or {2} length. - /// - internal static readonly string WebEncoders_InvalidCountOffsetOrLength = "Invalid {0}, {1} or {2} length."; - - /// - /// Malformed input: {0} is an invalid input length. - /// - internal static readonly string WebEncoders_MalformedInput = "Malformed input: {0} is an invalid input length."; - - /// - /// Invalid {0}, {1} or {2} length. - /// - internal static string FormatWebEncoders_InvalidCountOffsetOrLength(object p0, object p1, object p2) - { - return string.Format(CultureInfo.CurrentCulture, WebEncoders_InvalidCountOffsetOrLength, p0, p1, p2); - } - - /// - /// Malformed input: {0} is an invalid input length. - /// - internal static string FormatWebEncoders_MalformedInput(object p0) - { - return string.Format(CultureInfo.CurrentCulture, WebEncoders_MalformedInput, p0); - } - } -} diff --git a/src/libraries/Common/src/Extensions/WebEncoders/WebEncoders.cs b/src/libraries/Common/src/Extensions/WebEncoders/WebEncoders.cs deleted file mode 100644 index 17068ae67a55f..0000000000000 --- a/src/libraries/Common/src/Extensions/WebEncoders/WebEncoders.cs +++ /dev/null @@ -1,388 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Diagnostics; -using System.Globalization; -using Microsoft.Extensions.WebEncoders.Sources; - -#if WebEncoders_In_WebUtilities -namespace Microsoft.AspNetCore.WebUtilities -#else -namespace Microsoft.Extensions.Internal -#endif -{ - /// - /// Contains utility APIs to assist with common encoding and decoding operations. - /// -#if WebEncoders_In_WebUtilities - public -#else - internal -#endif - static class WebEncoders - { - private static readonly byte[] EmptyBytes = new byte[0]; - - /// - /// Decodes a base64url-encoded string. - /// - /// The base64url-encoded input to decode. - /// The base64url-decoded form of the input. - /// - /// The input must not contain any whitespace or padding characters. - /// Throws if the input is malformed. - /// - public static byte[] Base64UrlDecode(string input) - { - if (input == null) - { - throw new ArgumentNullException(nameof(input)); - } - - return Base64UrlDecode(input, offset: 0, count: input.Length); - } - - /// - /// Decodes a base64url-encoded substring of a given string. - /// - /// A string containing the base64url-encoded input to decode. - /// The position in at which decoding should begin. - /// The number of characters in to decode. - /// The base64url-decoded form of the input. - /// - /// The input must not contain any whitespace or padding characters. - /// Throws if the input is malformed. - /// - public static byte[] Base64UrlDecode(string input, int offset, int count) - { - if (input == null) - { - throw new ArgumentNullException(nameof(input)); - } - - ValidateParameters(input.Length, nameof(input), offset, count); - - // Special-case empty input - if (count == 0) - { - return EmptyBytes; - } - - // Create array large enough for the Base64 characters, not just shorter Base64-URL-encoded form. - var buffer = new char[GetArraySizeRequiredToDecode(count)]; - - return Base64UrlDecode(input, offset, buffer, bufferOffset: 0, count: count); - } - - /// - /// Decodes a base64url-encoded into a byte[]. - /// - /// A string containing the base64url-encoded input to decode. - /// The position in at which decoding should begin. - /// - /// Scratch buffer to hold the s to decode. Array must be large enough to hold - /// and characters as well as Base64 padding - /// characters. Content is not preserved. - /// - /// - /// The offset into at which to begin writing the s to decode. - /// - /// The number of characters in to decode. - /// The base64url-decoded form of the . - /// - /// The input must not contain any whitespace or padding characters. - /// Throws if the input is malformed. - /// - public static byte[] Base64UrlDecode(string input, int offset, char[] buffer, int bufferOffset, int count) - { - if (input == null) - { - throw new ArgumentNullException(nameof(input)); - } - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } - - ValidateParameters(input.Length, nameof(input), offset, count); - if (bufferOffset < 0) - { - throw new ArgumentOutOfRangeException(nameof(bufferOffset)); - } - - if (count == 0) - { - return EmptyBytes; - } - - // Assumption: input is base64url encoded without padding and contains no whitespace. - - var paddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(count); - var arraySizeRequired = checked(count + paddingCharsToAdd); - Debug.Assert(arraySizeRequired % 4 == 0, "Invariant: Array length must be a multiple of 4."); - - if (buffer.Length - bufferOffset < arraySizeRequired) - { - throw new ArgumentException( - string.Format( - CultureInfo.CurrentCulture, - EncoderResources.WebEncoders_InvalidCountOffsetOrLength, - nameof(count), - nameof(bufferOffset), - nameof(input)), - nameof(count)); - } - - // Copy input into buffer, fixing up '-' -> '+' and '_' -> '/'. - var i = bufferOffset; - for (var j = offset; i - bufferOffset < count; i++, j++) - { - var ch = input[j]; - if (ch == '-') - { - buffer[i] = '+'; - } - else if (ch == '_') - { - buffer[i] = '/'; - } - else - { - buffer[i] = ch; - } - } - - // Add the padding characters back. - for (; paddingCharsToAdd > 0; i++, paddingCharsToAdd--) - { - buffer[i] = '='; - } - - // Decode. - // If the caller provided invalid base64 chars, they'll be caught here. - return Convert.FromBase64CharArray(buffer, bufferOffset, arraySizeRequired); - } - - /// - /// Gets the minimum char[] size required for decoding of characters - /// with the method. - /// - /// The number of characters to decode. - /// - /// The minimum char[] size required for decoding of characters. - /// - public static int GetArraySizeRequiredToDecode(int count) - { - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - - if (count == 0) - { - return 0; - } - - var numPaddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(count); - - return checked(count + numPaddingCharsToAdd); - } - - /// - /// Encodes using base64url encoding. - /// - /// The binary input to encode. - /// The base64url-encoded form of . - public static string Base64UrlEncode(byte[] input) - { - if (input == null) - { - throw new ArgumentNullException(nameof(input)); - } - - return Base64UrlEncode(input, offset: 0, count: input.Length); - } - - /// - /// Encodes using base64url encoding. - /// - /// The binary input to encode. - /// The offset into at which to begin encoding. - /// The number of bytes from to encode. - /// The base64url-encoded form of . - public static string Base64UrlEncode(byte[] input, int offset, int count) - { - if (input == null) - { - throw new ArgumentNullException(nameof(input)); - } - - ValidateParameters(input.Length, nameof(input), offset, count); - - // Special-case empty input - if (count == 0) - { - return string.Empty; - } - - var buffer = new char[GetArraySizeRequiredToEncode(count)]; - var numBase64Chars = Base64UrlEncode(input, offset, buffer, outputOffset: 0, count: count); - - return new String(buffer, startIndex: 0, length: numBase64Chars); - } - - /// - /// Encodes using base64url encoding. - /// - /// The binary input to encode. - /// The offset into at which to begin encoding. - /// - /// Buffer to receive the base64url-encoded form of . Array must be large enough to - /// hold characters and the full base64-encoded form of - /// , including padding characters. - /// - /// - /// The offset into at which to begin writing the base64url-encoded form of - /// . - /// - /// The number of bytes from to encode. - /// - /// The number of characters written to , less any padding characters. - /// - public static int Base64UrlEncode(byte[] input, int offset, char[] output, int outputOffset, int count) - { - if (input == null) - { - throw new ArgumentNullException(nameof(input)); - } - if (output == null) - { - throw new ArgumentNullException(nameof(output)); - } - - ValidateParameters(input.Length, nameof(input), offset, count); - if (outputOffset < 0) - { - throw new ArgumentOutOfRangeException(nameof(outputOffset)); - } - - var arraySizeRequired = GetArraySizeRequiredToEncode(count); - if (output.Length - outputOffset < arraySizeRequired) - { - throw new ArgumentException( - string.Format( - CultureInfo.CurrentCulture, - EncoderResources.WebEncoders_InvalidCountOffsetOrLength, - nameof(count), - nameof(outputOffset), - nameof(output)), - nameof(count)); - } - - // Special-case empty input. - if (count == 0) - { - return 0; - } - - // Use base64url encoding with no padding characters. See RFC 4648, Sec. 5. - - // Start with default Base64 encoding. - var numBase64Chars = Convert.ToBase64CharArray(input, offset, count, output, outputOffset); - - // Fix up '+' -> '-' and '/' -> '_'. Drop padding characters. - for (var i = outputOffset; i - outputOffset < numBase64Chars; i++) - { - var ch = output[i]; - if (ch == '+') - { - output[i] = '-'; - } - else if (ch == '/') - { - output[i] = '_'; - } - else if (ch == '=') - { - // We've reached a padding character; truncate the remainder. - return i - outputOffset; - } - } - - return numBase64Chars; - } - - /// - /// Get the minimum output char[] size required for encoding - /// s with the method. - /// - /// The number of characters to encode. - /// - /// The minimum output char[] size required for encoding s. - /// - public static int GetArraySizeRequiredToEncode(int count) - { - var numWholeOrPartialInputBlocks = checked(count + 2) / 3; - return checked(numWholeOrPartialInputBlocks * 4); - } - - private static int GetNumBase64PaddingCharsInString(string str) - { - // Assumption: input contains a well-formed base64 string with no whitespace. - - // base64 guaranteed have 0 - 2 padding characters. - if (str[str.Length - 1] == '=') - { - if (str[str.Length - 2] == '=') - { - return 2; - } - return 1; - } - return 0; - } - - private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength) - { - switch (inputLength % 4) - { - case 0: - return 0; - case 2: - return 2; - case 3: - return 1; - default: - throw new FormatException( - string.Format( - CultureInfo.CurrentCulture, - EncoderResources.WebEncoders_MalformedInput, - inputLength)); - } - } - - private static void ValidateParameters(int bufferLength, string inputName, int offset, int count) - { - if (offset < 0) - { - throw new ArgumentOutOfRangeException(nameof(offset)); - } - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - if (bufferLength - offset < count) - { - throw new ArgumentException( - string.Format( - CultureInfo.CurrentCulture, - EncoderResources.WebEncoders_InvalidCountOffsetOrLength, - nameof(count), - nameof(offset), - inputName), - nameof(count)); - } - } - } -} diff --git a/src/libraries/Common/tests/Extensions/WebEncodersTests.cs b/src/libraries/Common/tests/Extensions/WebEncodersTests.cs deleted file mode 100644 index 5c71403fd6531..0000000000000 --- a/src/libraries/Common/tests/Extensions/WebEncodersTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Linq; -using Xunit; - -namespace Microsoft.Extensions.Internal -{ - public class WebEncodersTests - { - [Theory] - [InlineData("", 1, 0)] - [InlineData("", 0, 1)] - [InlineData("0123456789", 9, 2)] - [InlineData("0123456789", Int32.MaxValue, 2)] - [InlineData("0123456789", 9, -1)] - public void Base64UrlDecode_BadOffsets(string input, int offset, int count) - { - // Act & assert - Assert.ThrowsAny(() => - { - var retVal = WebEncoders.Base64UrlDecode(input, offset, count); - }); - } - - [Theory] - [InlineData("x")] - [InlineData("(x)")] - public void Base64UrlDecode_MalformedInput(string input) - { - // Act & assert - Assert.Throws(() => - { - var retVal = WebEncoders.Base64UrlDecode(input); - }); - } - - [Theory] - [InlineData("", "")] - [InlineData("123456qwerty++//X+/x", "123456qwerty--__X-_x")] - [InlineData("123456qwerty++//X+/xxw==", "123456qwerty--__X-_xxw")] - [InlineData("123456qwerty++//X+/xxw0=", "123456qwerty--__X-_xxw0")] - public void Base64UrlEncode_And_Decode(string base64Input, string expectedBase64Url) - { - // Arrange - byte[] input = new byte[3].Concat(Convert.FromBase64String(base64Input)).Concat(new byte[2]).ToArray(); - - // Act & assert - 1 - string actualBase64Url = WebEncoders.Base64UrlEncode(input, 3, input.Length - 5); // also helps test offsets - Assert.Equal(expectedBase64Url, actualBase64Url); - - // Act & assert - 2 - // Verify that values round-trip - byte[] roundTripped = WebEncoders.Base64UrlDecode("xx" + actualBase64Url + "yyy", 2, actualBase64Url.Length); // also helps test offsets - string roundTrippedAsBase64 = Convert.ToBase64String(roundTripped); - Assert.Equal(roundTrippedAsBase64, base64Input); - } - - [Theory] - [InlineData("", "")] - [InlineData("123456qwerty++//X+/x", "123456qwerty--__X-_x")] - [InlineData("123456qwerty++//X+/xxw==", "123456qwerty--__X-_xxw")] - [InlineData("123456qwerty++//X+/xxw0=", "123456qwerty--__X-_xxw0")] - public void Base64UrlEncode_And_Decode_WithBufferOffsets(string base64Input, string expectedBase64Url) - { - // Arrange - var input = new byte[3].Concat(Convert.FromBase64String(base64Input)).Concat(new byte[2]).ToArray(); - var buffer = new char[30]; - var output = new char[30]; - for (var i = 0; i < buffer.Length; i++) - { - buffer[i] = '^'; - output[i] = '^'; - } - - // Act 1 - var numEncodedChars = - WebEncoders.Base64UrlEncode(input, offset: 3, output: output, outputOffset: 4, count: input.Length - 5); - - // Assert 1 - var encodedString = new string(output, startIndex: 4, length: numEncodedChars); - Assert.Equal(expectedBase64Url, encodedString); - - // Act 2 - var roundTripInput = new string(output); - var roundTripped = - WebEncoders.Base64UrlDecode(roundTripInput, offset: 4, buffer: buffer, bufferOffset: 5, count: numEncodedChars); - - // Assert 2, verify that values round-trip - var roundTrippedAsBase64 = Convert.ToBase64String(roundTripped); - Assert.Equal(roundTrippedAsBase64, base64Input); - } - - [Theory] - [InlineData(0, 1, 0)] - [InlineData(0, 0, 1)] - [InlineData(10, 9, 2)] - [InlineData(10, Int32.MaxValue, 2)] - [InlineData(10, 9, -1)] - public void Base64UrlEncode_BadOffsets(int inputLength, int offset, int count) - { - // Arrange - byte[] input = new byte[inputLength]; - - // Act & assert - Assert.ThrowsAny(() => - { - var retVal = WebEncoders.Base64UrlEncode(input, offset, count); - }); - } - } -}