Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base64Url support for netstandard 2.0 #103617

Merged
merged 12 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/libraries/Microsoft.Bcl.Memory/Microsoft.Bcl.Memory.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35004.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bcl.Memory", "ref\Microsoft.Bcl.Memory.csproj", "{97D0AA68-5D2E-4E18-9651-19D0143EA98A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bcl.Memory", "src\Microsoft.Bcl.Memory.csproj", "{D4D9D9AC-E482-457A-8047-5267BDF2C2EE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Bcl.Memory.Tests", "tests\Microsoft.Bcl.Memory.Tests.csproj", "{6916DB98-2958-498D-83E7-90080BEA9D39}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{76C22FE9-D65E-40C3-BE2D-98BD628779EC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{76EDDEA8-0E0E-4A94-9D8F-F4DB250E8EB8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{E78D7A3F-384B-4AA3-ABF1-C4464BE9237C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D4D9D9AC-E482-457A-8047-5267BDF2C2EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4D9D9AC-E482-457A-8047-5267BDF2C2EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4D9D9AC-E482-457A-8047-5267BDF2C2EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4D9D9AC-E482-457A-8047-5267BDF2C2EE}.Release|Any CPU.Build.0 = Release|Any CPU
{97D0AA68-5D2E-4E18-9651-19D0143EA98A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97D0AA68-5D2E-4E18-9651-19D0143EA98A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97D0AA68-5D2E-4E18-9651-19D0143EA98A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97D0AA68-5D2E-4E18-9651-19D0143EA98A}.Release|Any CPU.Build.0 = Release|Any CPU
{6916DB98-2958-498D-83E7-90080BEA9D39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6916DB98-2958-498D-83E7-90080BEA9D39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6916DB98-2958-498D-83E7-90080BEA9D39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6916DB98-2958-498D-83E7-90080BEA9D39}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D4D9D9AC-E482-457A-8047-5267BDF2C2EE} = {76C22FE9-D65E-40C3-BE2D-98BD628779EC}
{97D0AA68-5D2E-4E18-9651-19D0143EA98A} = {76EDDEA8-0E0E-4A94-9D8F-F4DB250E8EB8}
{6916DB98-2958-498D-83E7-90080BEA9D39} = {E78D7A3F-384B-4AA3-ABF1-C4464BE9237C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CB4D3234-B365-4A38-B9C6-47B72CE8BC85}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Buffers.Text.Base64Url))]
34 changes: 34 additions & 0 deletions src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Buffers.Text
{
public static class Base64Url
{
public static byte[] DecodeFromChars(System.ReadOnlySpan<char> source) { throw null; }
public static int DecodeFromChars(System.ReadOnlySpan<char> source, System.Span<byte> destination) { throw null; }
public static System.Buffers.OperationStatus DecodeFromChars(System.ReadOnlySpan<char> source, System.Span<byte> destination, out int charsConsumed, out int bytesWritten, bool isFinalBlock = true) { throw null; }
public static byte[] DecodeFromUtf8(System.ReadOnlySpan<byte> source) { throw null; }
public static int DecodeFromUtf8(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static System.Buffers.OperationStatus DecodeFromUtf8(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true) { throw null; }
public static int DecodeFromUtf8InPlace(System.Span<byte> buffer) { throw null; }
public static char[] EncodeToChars(System.ReadOnlySpan<byte> source) { throw null; }
public static int EncodeToChars(System.ReadOnlySpan<byte> source, System.Span<char> destination) { throw null; }
public static System.Buffers.OperationStatus EncodeToChars(System.ReadOnlySpan<byte> source, System.Span<char> destination, out int bytesConsumed, out int charsWritten, bool isFinalBlock = true) { throw null; }
public static string EncodeToString(System.ReadOnlySpan<byte> source) { throw null; }
public static byte[] EncodeToUtf8(System.ReadOnlySpan<byte> source) { throw null; }
public static int EncodeToUtf8(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static System.Buffers.OperationStatus EncodeToUtf8(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true) { throw null; }
public static int GetEncodedLength(int bytesLength) { throw null; }
public static int GetMaxDecodedLength(int base64Length) { throw null; }
public static bool IsValid(System.ReadOnlySpan<char> base64UrlText) { throw null; }
public static bool IsValid(System.ReadOnlySpan<char> base64UrlText, out int decodedLength) { throw null; }
public static bool IsValid(System.ReadOnlySpan<byte> utf8Base64UrlText) { throw null; }
public static bool IsValid(System.ReadOnlySpan<byte> utf8Base64UrlText, out int decodedLength) { throw null; }
public static bool TryDecodeFromChars(System.ReadOnlySpan<char> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
public static bool TryDecodeFromUtf8(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
public static bool TryEncodeToChars(System.ReadOnlySpan<byte> source, System.Span<char> destination, out int charsWritten) { throw null; }
public static bool TryEncodeToUtf8(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
public static bool TryEncodeToUtf8InPlace(System.Span<byte> buffer, int dataLength, out int bytesWritten) { throw null; }
}
}
19 changes: 19 additions & 0 deletions src/libraries/Microsoft.Bcl.Memory/ref/Microsoft.Bcl.Memory.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
ViktorHofer marked this conversation as resolved.
Show resolved Hide resolved

<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="Microsoft.Bcl.Memory.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="Microsoft.Bcl.Memory.Forwards.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions src/libraries/Microsoft.Bcl.Memory/src/Microsoft.Bcl.Memory.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<!-- Disabling baseline validation since this is a brand new package.
Once this package has shipped a stable version, the following line
should be removed in order to re-enable validation. -->
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
<PackageDescription>
Provides Base64Url encoding, decoding and validation APIs support for .NET Framework and .NET Standard.

Commonly Used Types:
System.Buffers.Text.Base64Url
</PackageDescription>
</PropertyGroup>

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
<OmitResources Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">true</OmitResources>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="..\ref\Microsoft.Bcl.Memory.Forwards.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Helper\Base64Helper.cs">
<Link>System\Buffers\Text\Base64Helper\Base64Helper.cs</Link>
</Compile>
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Helper\Base64DecoderHelper.cs">
<Link>System\Buffers\Text\Base64Helper\Base64DecoderHelper.cs</Link>
</Compile>
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Helper\Base64EncoderHelper.cs">
<Link>System\Buffers\Text\Base64Helper\Base64EncoderHelper.cs</Link>
</Compile>
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Helper\Base64ValidatorHelper.cs">
<Link>System\Buffers\Text\Base64Helper\Base64ValidatorHelper.cs</Link>
</Compile>
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Url\Base64UrlDecoder.cs">
<Link>System\Buffers\Text\Base64Url\Base64UrlDecoder.cs</Link>
</Compile>
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Url\Base64UrlEncoder.cs">
<Link>System\Buffers\Text\Base64Url\Base64UrlEncoder.cs</Link>
</Compile>
<Compile Include="..\..\System.Private.CoreLib\src\System\Buffers\Text\Base64Url\Base64UrlValidator.cs">
<Link>System\Buffers\Text\Base64Url\Base64UrlValidator.cs</Link>
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>

</Project>
126 changes: 126 additions & 0 deletions src/libraries/Microsoft.Bcl.Memory/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Argument_DestinationTooShort" xml:space="preserve">
<value>Destination is too short.</value>
</data>
<data name="Format_BadBase64Char" xml:space="preserve">
<value>The input is not a valid Base64Url string as it contains a non-Base64Url character, more than two padding characters, or an illegal character among the padding characters.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetFrameworkMinimum);$(NetCoreAppCurrent);</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\System.Memory\tests\AllocationHelper.cs">
<Link>System\Memory\AllocationHelper.cs</Link>
</Compile>
<Compile Include="..\..\System.Memory\tests\Base64\Base64TestBase.cs">
<Link>System\Memory\Base64\Base64TestBase.cs</Link>
</Compile>
<Compile Include="..\..\System.Memory\tests\Base64\Base64TestHelper.cs">
<Link>System\Memory\Base64\Base64TestHelper.cs</Link>
</Compile>
<Compile Include="..\..\System.Memory\tests\Base64Url\Base64UrlDecoderUnitTests.cs">
<Link>System\Memory\Base64Url\Base64UrlDecoderUnitTests.cs</Link>
</Compile>
<Compile Include="..\..\System.Memory\tests\Base64Url\Base64UrlEncoderUnitTests.cs">
<Link>System\Memory\Base64Url\Base64UrlEncoderUnitTests.cs</Link>
</Compile>
<Compile Include="..\..\System.Memory\tests\Base64Url\Base64UrlValidationUnitTests.cs">
<Link>System\Memory\Base64Url\Base64UrlValidationUnitTests.cs</Link>
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<ProjectReference Include="..\src\Microsoft.Bcl.Memory.csproj" />
</ItemGroup>

</Project>
24 changes: 23 additions & 1 deletion src/libraries/System.Memory/tests/Base64/Base64TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,35 @@ public static IEnumerable<object[]> BasicDecodingWithExtraWhitespaceShouldBeCoun
var r = new Random(42);
for (int i = 0; i < 5; i++)
{
yield return new object[] { "AQ==" + new string(r.GetItems<char>(" \n\t\r", i)), 4 + i, 1 };
yield return new object[] { "AQ==" + new string(GetChars(r, i)), 4 + i, 1 };
}

foreach (string s in new[] { "MTIz", "M TIz", "MT Iz", "MTI z", "MTIz ", "M TI z", "M T I Z " })
{
yield return new object[] { s + s + s + s, s.Length * 4, 12 };
}
}

private static char[] GetChars(Random r, int i)
{
#if NETCOREAPP
return r.GetItems<char>(" \n\t\r", i);
#else
byte[] bytes = new byte[i];
char[] chars = new char[i];
r.NextBytes(bytes);
for (int j = 0; j < bytes.Length; j++)
{
switch (bytes[j] % 4)
{
case 0: chars[j] = ' '; break;
case 1: chars[j] = '\n'; break;
case 2: chars[j] = '\t'; break;
case 3: chars[j] = '\r'; break;
}
}
return chars;
#endif
}
}
}
Loading
Loading