| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Runtime; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | #if NET |
| | | 10 | | using System.Runtime.Intrinsics; |
| | | 11 | | using System.Runtime.Intrinsics.Arm; |
| | | 12 | | using System.Runtime.Intrinsics.Wasm; |
| | | 13 | | using System.Runtime.Intrinsics.X86; |
| | | 14 | | #endif |
| | | 15 | | using static System.Buffers.Text.Base64Helper; |
| | | 16 | | |
| | | 17 | | namespace System.Buffers.Text |
| | | 18 | | { |
| | | 19 | | // AVX2 and Vector128 version based on https://github.com/gfoidl/Base64/blob/5383320e28cac6c7ac6f86502fb05d23a048a21 |
| | | 20 | | #pragma warning disable 1591 // TODO: Document this API. https://github.com/dotnet/runtime/issues/105974 |
| | | 21 | | public static partial class Base64Url |
| | | 22 | | #pragma warning restore 1591 |
| | | 23 | | { |
| | | 24 | | private const int MaxStackallocThreshold = 256; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Returns the maximum length (in bytes) of the result if you were to decode Base64Url-encoded text from a span |
| | | 28 | | /// </summary> |
| | | 29 | | /// <exception cref="ArgumentOutOfRangeException">The specified <paramref name="base64Length"/> is less than 0. |
| | | 30 | | /// </exception> |
| | | 31 | | public static int GetMaxDecodedLength(int base64Length) |
| | | 32 | | { |
| | | 33 | | #if NET |
| | 69537 | 34 | | ArgumentOutOfRangeException.ThrowIfNegative(base64Length); |
| | | 35 | | |
| | 69537 | 36 | | (uint whole, uint remainder) = uint.DivRem((uint)base64Length, 4); |
| | | 37 | | |
| | 69537 | 38 | | return (int)(whole * 3 + (remainder > 0 ? remainder - 1 : 0)); |
| | | 39 | | #else |
| | | 40 | | if (base64Length < 0) |
| | | 41 | | { |
| | | 42 | | throw new ArgumentOutOfRangeException(nameof(base64Length)); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | int remainder = (int)((uint)base64Length % 4); |
| | | 46 | | |
| | | 47 | | return (base64Length >> 2) * 3 + (remainder > 0 ? remainder - 1 : 0); |
| | | 48 | | #endif |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Decodes the span of UTF-8 encoded text represented as Base64Url into binary data. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="source">The input span which contains UTF-8 encoded text in Base64Url that needs to be decoded. |
| | | 55 | | /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar |
| | | 56 | | /// <param name="bytesConsumed">When this method returns, contains the number of input bytes consumed during the |
| | | 57 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp |
| | | 58 | | /// <param name="isFinalBlock"><see langword="true"/> when the input span contains the entirety of data to decod |
| | | 59 | | /// such as when calling in a loop. Calls with <see langword="false"/> should be followed up with another call w |
| | | 60 | | /// <returns>One of the enumeration values that indicates the success or failure of the operation.</returns> |
| | | 61 | | /// <remarks> |
| | | 62 | | /// As padding is optional for Base64Url the <paramref name="source"/> length not required to be a multiple of 4 |
| | | 63 | | /// If the <paramref name="source"/> length is not a multiple of 4 and <paramref name="isFinalBlock"/> is <see l |
| | | 64 | | /// - Remainder of 3 bytes - decoded into 2 bytes data, decoding succeeds. |
| | | 65 | | /// - Remainder of 2 bytes - decoded into 1 byte data. decoding succeeds. |
| | | 66 | | /// - Remainder of 1 byte - will cause OperationStatus.InvalidData result. |
| | | 67 | | /// </remarks> |
| | | 68 | | public static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesCon |
| | 2113 | 69 | | DecodeFrom(default(Base64UrlDecoderByte), source, destination, out bytesConsumed, out bytesWritten, isFinalB |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Decodes the span of UTF-8 encoded text in Base64Url into binary data, in-place. |
| | | 73 | | /// The decoded binary output is smaller than the text data contained in the input (the operation deflates the d |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="buffer">The input span which contains the Base64Url text data that needs to be decoded.</param> |
| | | 76 | | /// <returns>The number of bytes written into <paramref name="buffer"/>. This can be used to slice the output fo |
| | | 77 | | /// <exception cref="FormatException"><paramref name="buffer"/> contains an invalid Base64Url character, |
| | | 78 | | /// more than two padding characters, or a non white space character among the padding characters.</exception> |
| | | 79 | | /// <remarks> |
| | | 80 | | /// As padding is optional for Base64Url the <paramref name="buffer"/> length not required to be a multiple of 4 |
| | | 81 | | /// If the <paramref name="buffer"/> length is not a multiple of 4 the remainders decoded accordingly: |
| | | 82 | | /// - Remainder of 3 bytes - decoded into 2 bytes data, decoding succeeds. |
| | | 83 | | /// - Remainder of 2 bytes - decoded into 1 byte data. decoding succeeds. |
| | | 84 | | /// - Remainder of 1 byte - is invalid input, causes FormatException. |
| | | 85 | | /// </remarks> |
| | | 86 | | public static int DecodeFromUtf8InPlace(Span<byte> buffer) |
| | | 87 | | { |
| | 2870 | 88 | | OperationStatus status = DecodeFromUtf8InPlace<Base64UrlDecoderByte>(default, buffer, out int bytesWritten, |
| | | 89 | | |
| | | 90 | | // Base64.DecodeFromUtf8InPlace returns OperationStatus, therefore doesn't throw. |
| | | 91 | | // For Base64Url, this is not an OperationStatus API and thus throws. |
| | 2870 | 92 | | if (status == OperationStatus.InvalidData) |
| | | 93 | | { |
| | 757 | 94 | | throw new FormatException(SR.Format_BadBase64Char); |
| | | 95 | | } |
| | | 96 | | |
| | 2113 | 97 | | Debug.Assert(status is OperationStatus.Done); |
| | 2113 | 98 | | return bytesWritten; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Decodes the span of UTF-8 encoded text represented as Base64Url into binary data. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="source">The input span which contains UTF-8 encoded text in Base64Url that needs to be decoded. |
| | | 105 | | /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar |
| | | 106 | | /// <returns>The number of bytes written into <paramref name="destination"/>. This can be used to slice the outp |
| | | 107 | | /// <exception cref="ArgumentException">The buffer in <paramref name="destination"/> is too small to hold the de |
| | | 108 | | /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64Url character, |
| | | 109 | | /// more than two padding characters, or a non white space character among the padding characters.</exception> |
| | | 110 | | /// <remarks> |
| | | 111 | | /// As padding is optional for Base64Url the <paramref name="source"/> length not required to be a multiple of 4 |
| | | 112 | | /// If the <paramref name="source"/> length is not a multiple of 4 the remainders decoded accordingly: |
| | | 113 | | /// - Remainder of 3 bytes - decoded into 2 bytes data, decoding succeeds. |
| | | 114 | | /// - Remainder of 2 bytes - decoded into 1 byte data. decoding succeeds. |
| | | 115 | | /// - Remainder of 1 byte - is invalid input, causes FormatException. |
| | | 116 | | /// </remarks> |
| | | 117 | | public static int DecodeFromUtf8(ReadOnlySpan<byte> source, Span<byte> destination) |
| | | 118 | | { |
| | 0 | 119 | | OperationStatus status = DecodeFromUtf8(source, destination, out _, out int bytesWritten); |
| | | 120 | | |
| | 0 | 121 | | if (status == OperationStatus.Done) |
| | | 122 | | { |
| | 0 | 123 | | return bytesWritten; |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (status == OperationStatus.DestinationTooSmall) |
| | | 127 | | { |
| | 0 | 128 | | throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination)); |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | Debug.Assert(status is OperationStatus.InvalidData); |
| | 0 | 132 | | throw new FormatException(SR.Format_BadBase64Char); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Decodes the span of UTF-8 encoded text represented as Base64Url into binary data. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="source">The input span which contains UTF-8 encoded text in Base64Url that needs to be decoded. |
| | | 139 | | /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar |
| | | 140 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp |
| | | 141 | | /// <returns><see langword="true"/> if bytes decoded successfully, otherwise <see langword="false"/>.</returns> |
| | | 142 | | /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64Url character, |
| | | 143 | | /// more than two padding characters, or a non white space character among the padding characters.</exception> |
| | | 144 | | public static bool TryDecodeFromUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) |
| | | 145 | | { |
| | 678 | 146 | | OperationStatus status = DecodeFromUtf8(source, destination, out _, out bytesWritten); |
| | | 147 | | |
| | 678 | 148 | | if (status == OperationStatus.InvalidData) |
| | | 149 | | { |
| | 0 | 150 | | throw new FormatException(SR.Format_BadBase64Char); |
| | | 151 | | } |
| | | 152 | | |
| | 678 | 153 | | Debug.Assert(status is OperationStatus.Done or OperationStatus.DestinationTooSmall); |
| | 678 | 154 | | return status == OperationStatus.Done; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Decodes the span of UTF-8 encoded text represented as Base64Url into binary data. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="source">The input span which contains UTF-8 encoded text in Base64Url that needs to be decoded. |
| | | 161 | | /// <returns>A byte array which contains the result of the decoding operation.</returns> |
| | | 162 | | /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64Url character, |
| | | 163 | | /// more than two padding characters, or a non white space character among the padding characters.</exception> |
| | | 164 | | public static unsafe byte[] DecodeFromUtf8(ReadOnlySpan<byte> source) |
| | | 165 | | { |
| | 0 | 166 | | int upperBound = GetMaxDecodedLength(source.Length); |
| | 0 | 167 | | byte[]? rented = null; |
| | | 168 | | |
| | 0 | 169 | | Span<byte> destination = (uint)upperBound <= MaxStackallocThreshold |
| | 0 | 170 | | ? stackalloc byte[MaxStackallocThreshold] |
| | 0 | 171 | | : (rented = ArrayPool<byte>.Shared.Rent(upperBound)); |
| | | 172 | | |
| | 0 | 173 | | OperationStatus status = DecodeFromUtf8(source, destination, out _, out int bytesWritten); |
| | 0 | 174 | | Debug.Assert(status is OperationStatus.Done or OperationStatus.InvalidData); |
| | 0 | 175 | | byte[] ret = destination.Slice(0, bytesWritten).ToArray(); |
| | | 176 | | |
| | 0 | 177 | | if (rented is not null) |
| | | 178 | | { |
| | 0 | 179 | | ArrayPool<byte>.Shared.Return(rented); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | return status == OperationStatus.Done ? ret : throw new FormatException(SR.Format_BadBase64Char); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Decodes the span of unicode ASCII chars represented as Base64Url into binary data. |
| | | 187 | | /// </summary> |
| | | 188 | | /// <param name="source">The input span which contains unicode ASCII chars in Base64Url that needs to be decoded |
| | | 189 | | /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar |
| | | 190 | | /// <param name="charsConsumed">When this method returns, contains the number of input chars consumed during the |
| | | 191 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp |
| | | 192 | | /// <param name="isFinalBlock"><see langword="true"/> when the input span contains the entirety of data to decod |
| | | 193 | | /// such as when calling in a loop. Calls with <see langword="false"/> should be followed up with another call w |
| | | 194 | | /// <returns>One of the enumeration values that indicates the success or failure of the operation.</returns> |
| | | 195 | | /// <remarks> |
| | | 196 | | /// As padding is optional for Base64Url the <paramref name="source"/> length not required to be a multiple of 4 |
| | | 197 | | /// If the <paramref name="source"/> length is not a multiple of 4 and <paramref name="isFinalBlock"/> is <see l |
| | | 198 | | /// - Remainder of 3 chars - decoded into 2 bytes data, decoding succeeds. |
| | | 199 | | /// - Remainder of 2 chars - decoded into 1 byte data. decoding succeeds. |
| | | 200 | | /// - Remainder of 1 char - will cause OperationStatus.InvalidData result. |
| | | 201 | | /// </remarks> |
| | | 202 | | public static OperationStatus DecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination, |
| | | 203 | | out int charsConsumed, out int bytesWritten, bool isFinalBlock = true) => |
| | 2870 | 204 | | DecodeFrom(default(Base64UrlDecoderChar), MemoryMarshal.Cast<char, ushort>(source), destination, |
| | 2870 | 205 | | out charsConsumed, out bytesWritten, isFinalBlock, ignoreWhiteSpace: true); |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Decodes the span of unicode ASCII chars represented as Base64Url into binary data. |
| | | 209 | | /// </summary> |
| | | 210 | | /// <param name="source">The input span which contains ASCII chars in Base64Url that needs to be decoded.</param |
| | | 211 | | /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar |
| | | 212 | | /// <returns>The number of bytes written into the output span. This can be used to slice the output for subseque |
| | | 213 | | /// <exception cref="ArgumentException">The buffer in <paramref name="destination"/> is too small to hold the de |
| | | 214 | | /// <exception cref="FormatException"><paramref name="source"/> contains a invalid Base64Url character, |
| | | 215 | | /// more than two padding characters, or a non white space character among the padding characters.</exception> |
| | | 216 | | public static int DecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination) |
| | | 217 | | { |
| | 0 | 218 | | OperationStatus status = DecodeFromChars(source, destination, out _, out int bytesWritten); |
| | | 219 | | |
| | 0 | 220 | | if (status == OperationStatus.Done) |
| | | 221 | | { |
| | 0 | 222 | | return bytesWritten; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | if (status == OperationStatus.DestinationTooSmall) |
| | | 226 | | { |
| | 0 | 227 | | throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination)); |
| | | 228 | | } |
| | | 229 | | |
| | 0 | 230 | | Debug.Assert(status == OperationStatus.InvalidData); |
| | 0 | 231 | | throw new FormatException(SR.Format_BadBase64Char); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Decodes the span of unicode ASCII chars represented as Base64Url into binary data. |
| | | 236 | | /// </summary> |
| | | 237 | | /// <param name="source">The input span which contains ASCII chars in Base64Url that needs to be decoded.</param |
| | | 238 | | /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar |
| | | 239 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp |
| | | 240 | | /// <returns><see langword="true"/> if bytes decoded successfully, otherwise <see langword="false"/>.</returns> |
| | | 241 | | /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64Url character, |
| | | 242 | | /// more than two padding characters, or a non white space character among the padding characters.</exception> |
| | | 243 | | public static bool TryDecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination, out int bytesWritten) |
| | | 244 | | { |
| | 0 | 245 | | OperationStatus status = DecodeFromChars(source, destination, out _, out bytesWritten); |
| | | 246 | | |
| | 0 | 247 | | if (status == OperationStatus.InvalidData) |
| | | 248 | | { |
| | 0 | 249 | | throw new FormatException(SR.Format_BadBase64Char); |
| | | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | return status == OperationStatus.Done; |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Decodes the span of unicode ASCII chars represented as Base64Url into binary data. |
| | | 257 | | /// </summary> |
| | | 258 | | /// <param name="source">The input span which contains ASCII chars in Base64Url that needs to be decoded.</param |
| | | 259 | | /// <returns>A byte array which contains the result of the decoding operation.</returns> |
| | | 260 | | /// <exception cref="FormatException"><paramref name="source"/> contains a invalid Base64Url character, |
| | | 261 | | /// more than two padding characters, or a non white space character among the padding characters.</exception> |
| | | 262 | | public static unsafe byte[] DecodeFromChars(ReadOnlySpan<char> source) |
| | | 263 | | { |
| | 0 | 264 | | int upperBound = GetMaxDecodedLength(source.Length); |
| | 0 | 265 | | byte[]? rented = null; |
| | | 266 | | |
| | 0 | 267 | | Span<byte> destination = (uint)upperBound <= MaxStackallocThreshold |
| | 0 | 268 | | ? stackalloc byte[MaxStackallocThreshold] |
| | 0 | 269 | | : (rented = ArrayPool<byte>.Shared.Rent(upperBound)); |
| | | 270 | | |
| | 0 | 271 | | OperationStatus status = DecodeFromChars(source, destination, out _, out int bytesWritten); |
| | 0 | 272 | | byte[] ret = destination.Slice(0, bytesWritten).ToArray(); |
| | | 273 | | |
| | 0 | 274 | | if (rented is not null) |
| | | 275 | | { |
| | 0 | 276 | | ArrayPool<byte>.Shared.Return(rented); |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | return status == OperationStatus.Done ? ret : throw new FormatException(SR.Format_BadBase64Char); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | private readonly struct Base64UrlDecoderByte : IBase64Decoder<byte> |
| | | 283 | | { |
| | | 284 | | public ReadOnlySpan<sbyte> DecodingMap => |
| | 127331 | 285 | | [ |
| | 127331 | 286 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 287 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 288 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, //62 is placed at index 45 ( |
| | 127331 | 289 | | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, //52-61 are placed at index |
| | 127331 | 290 | | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| | 127331 | 291 | | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, //0-25 are placed at index 6 |
| | 127331 | 292 | | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| | 127331 | 293 | | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, //26-51 are placed at index |
| | 127331 | 294 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // Bytes over 122 ('z') are |
| | 127331 | 295 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // Hence, padding the map wi |
| | 127331 | 296 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 297 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 298 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 299 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 300 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 301 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| | 127331 | 302 | | ]; |
| | | 303 | | |
| | | 304 | | public ReadOnlySpan<uint> VbmiLookup0 => |
| | 0 | 305 | | [ |
| | 0 | 306 | | 0x80808080, 0x80808080, 0x80808080, 0x80808080, |
| | 0 | 307 | | 0x80808080, 0x80808080, 0x80808080, 0x80808080, |
| | 0 | 308 | | 0x80808080, 0x80808080, 0x80808080, 0x80803e80, |
| | 0 | 309 | | 0x37363534, 0x3b3a3938, 0x80803d3c, 0x80808080 |
| | 0 | 310 | | ]; |
| | | 311 | | |
| | | 312 | | public ReadOnlySpan<uint> VbmiLookup1 => |
| | 0 | 313 | | [ |
| | 0 | 314 | | 0x02010080, 0x06050403, 0x0a090807, 0x0e0d0c0b, |
| | 0 | 315 | | 0x1211100f, 0x16151413, 0x80191817, 0x3f808080, |
| | 0 | 316 | | 0x1c1b1a80, 0x201f1e1d, 0x24232221, 0x28272625, |
| | 0 | 317 | | 0x2c2b2a29, 0x302f2e2d, 0x80333231, 0x80808080 |
| | 0 | 318 | | ]; |
| | | 319 | | |
| | | 320 | | public ReadOnlySpan<sbyte> Avx2LutHigh => |
| | 39801 | 321 | | [ |
| | 39801 | 322 | | 0x00, 0x00, 0x2d, 0x39, |
| | 39801 | 323 | | 0x4f, 0x5a, 0x6f, 0x7a, |
| | 39801 | 324 | | 0x00, 0x00, 0x00, 0x00, |
| | 39801 | 325 | | 0x00, 0x00, 0x00, 0x00, |
| | 39801 | 326 | | 0x00, 0x00, 0x2d, 0x39, |
| | 39801 | 327 | | 0x4f, 0x5a, 0x6f, 0x7a, |
| | 39801 | 328 | | 0x00, 0x00, 0x00, 0x00, |
| | 39801 | 329 | | 0x00, 0x00, 0x00, 0x00 |
| | 39801 | 330 | | ]; |
| | | 331 | | |
| | | 332 | | public ReadOnlySpan<sbyte> Avx2LutLow => |
| | 39801 | 333 | | [ |
| | 39801 | 334 | | 0x01, 0x01, 0x2d, 0x30, |
| | 39801 | 335 | | 0x41, 0x50, 0x61, 0x70, |
| | 39801 | 336 | | 0x01, 0x01, 0x01, 0x01, |
| | 39801 | 337 | | 0x01, 0x01, 0x01, 0x01, |
| | 39801 | 338 | | 0x01, 0x01, 0x2d, 0x30, |
| | 39801 | 339 | | 0x41, 0x50, 0x61, 0x70, |
| | 39801 | 340 | | 0x01, 0x01, 0x01, 0x01, |
| | 39801 | 341 | | 0x01, 0x01, 0x01, 0x01 |
| | 39801 | 342 | | ]; |
| | | 343 | | |
| | | 344 | | public ReadOnlySpan<sbyte> Avx2LutShift => |
| | 39801 | 345 | | [ |
| | 39801 | 346 | | 0, 0, 17, 4, |
| | 39801 | 347 | | -65, -65, -71, -71, |
| | 39801 | 348 | | 0, 0, 0, 0, |
| | 39801 | 349 | | 0, 0, 0, 0, |
| | 39801 | 350 | | 0, 0, 17, 4, |
| | 39801 | 351 | | -65, -65, -71, -71, |
| | 39801 | 352 | | 0, 0, 0, 0, |
| | 39801 | 353 | | 0, 0, 0, 0 |
| | 39801 | 354 | | ]; |
| | | 355 | | |
| | 80868 | 356 | | public byte MaskSlashOrUnderscore => (byte)'_'; // underscore |
| | | 357 | | |
| | 41067 | 358 | | public ReadOnlySpan<int> Vector128LutHigh => [0x392d0000, 0x7a6f5a4f, 0x00000000, 0x00000000]; |
| | | 359 | | |
| | 41067 | 360 | | public ReadOnlySpan<int> Vector128LutLow => [0x302d0101, 0x70615041, 0x01010101, 0x01010101]; |
| | | 361 | | |
| | 41067 | 362 | | public ReadOnlySpan<uint> Vector128LutShift => [0x04110000, 0xb9b9bfbf, 0x00000000, 0x00000000]; |
| | | 363 | | |
| | 0 | 364 | | public ReadOnlySpan<uint> AdvSimdLutOne3 => [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFF3EFF]; |
| | | 365 | | |
| | 0 | 366 | | public uint AdvSimdLutTwo3Uint1 => 0x1B1AFF3F; |
| | | 367 | | |
| | 67424 | 368 | | public int GetMaxDecodedLength(int sourceLength) => Base64Url.GetMaxDecodedLength(sourceLength); |
| | | 369 | | |
| | 59727 | 370 | | public bool IsInvalidLength(int bufferLength) => (bufferLength & 3) == 1; // One byte cannot be decoded comp |
| | | 371 | | |
| | 111251 | 372 | | public bool IsValidPadding(uint padChar) => padChar is EncodingPad or UrlEncodingPad; |
| | | 373 | | |
| | 67424 | 374 | | public int SrcLength(bool isFinalBlock, int sourceLength) => isFinalBlock ? sourceLength : sourceLength & ~0 |
| | | 375 | | |
| | | 376 | | #if NET |
| | | 377 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 378 | | [CompExactlyDependsOn(typeof(AdvSimd.Arm64))] |
| | | 379 | | [CompExactlyDependsOn(typeof(Ssse3))] |
| | | 380 | | [CompExactlyDependsOn(typeof(PackedSimd))] |
| | | 381 | | public bool TryDecode128Core( |
| | | 382 | | Vector128<byte> str, |
| | | 383 | | Vector128<byte> hiNibbles, |
| | | 384 | | Vector128<byte> maskSlashOrUnderscore, |
| | | 385 | | Vector128<byte> mask8F, |
| | | 386 | | Vector128<byte> lutLow, |
| | | 387 | | Vector128<byte> lutHigh, |
| | | 388 | | Vector128<sbyte> lutShift, |
| | | 389 | | Vector128<byte> shiftForUnderscore, |
| | | 390 | | out Vector128<byte> result) |
| | | 391 | | { |
| | 44250 | 392 | | Vector128<byte> lowerBound = SimdShuffle(lutLow, hiNibbles, mask8F); |
| | 44250 | 393 | | Vector128<byte> upperBound = SimdShuffle(lutHigh, hiNibbles, mask8F); |
| | | 394 | | |
| | 44250 | 395 | | Vector128<byte> below = Vector128.LessThan(str, lowerBound); |
| | 44250 | 396 | | Vector128<byte> above = Vector128.GreaterThan(str, upperBound); |
| | 44250 | 397 | | Vector128<byte> eq5F = Vector128.Equals(str, maskSlashOrUnderscore); |
| | | 398 | | |
| | | 399 | | // Take care as arguments are flipped in order! |
| | 44250 | 400 | | Vector128<byte> outside = Vector128.AndNot(below | above, eq5F); |
| | | 401 | | |
| | 44250 | 402 | | if (outside != Vector128<byte>.Zero) |
| | | 403 | | { |
| | 38827 | 404 | | result = default; |
| | 38827 | 405 | | return false; |
| | | 406 | | } |
| | | 407 | | |
| | 5423 | 408 | | Vector128<byte> shift = SimdShuffle(lutShift.AsByte(), hiNibbles, mask8F); |
| | 5423 | 409 | | str += shift; |
| | | 410 | | |
| | 5423 | 411 | | result = str + (eq5F & shiftForUnderscore); |
| | 5423 | 412 | | return true; |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 416 | | [CompExactlyDependsOn(typeof(Avx2))] |
| | | 417 | | public bool TryDecode256Core( |
| | | 418 | | Vector256<sbyte> str, |
| | | 419 | | Vector256<sbyte> hiNibbles, |
| | | 420 | | Vector256<sbyte> maskSlashOrUnderscore, |
| | | 421 | | Vector256<sbyte> lutLow, |
| | | 422 | | Vector256<sbyte> lutHigh, |
| | | 423 | | Vector256<sbyte> lutShift, |
| | | 424 | | Vector256<sbyte> shiftForUnderscore, |
| | | 425 | | out Vector256<sbyte> result) |
| | | 426 | | { |
| | 85453 | 427 | | Vector256<sbyte> lowerBound = Avx2.Shuffle(lutLow, hiNibbles); |
| | 85453 | 428 | | Vector256<sbyte> upperBound = Avx2.Shuffle(lutHigh, hiNibbles); |
| | | 429 | | |
| | 85453 | 430 | | Vector256<sbyte> below = Vector256.LessThan(str, lowerBound); |
| | 85453 | 431 | | Vector256<sbyte> above = Vector256.GreaterThan(str, upperBound); |
| | 85453 | 432 | | Vector256<sbyte> eq5F = Vector256.Equals(str, maskSlashOrUnderscore); |
| | | 433 | | |
| | | 434 | | // Take care as arguments are flipped in order! |
| | 85453 | 435 | | Vector256<sbyte> outside = Vector256.AndNot(below | above, eq5F); |
| | | 436 | | |
| | 85453 | 437 | | if (outside != Vector256<sbyte>.Zero) |
| | | 438 | | { |
| | 37197 | 439 | | result = default; |
| | 37197 | 440 | | return false; |
| | | 441 | | } |
| | | 442 | | |
| | 48256 | 443 | | Vector256<sbyte> shift = Avx2.Shuffle(lutShift, hiNibbles); |
| | 48256 | 444 | | str += shift; |
| | | 445 | | |
| | 48256 | 446 | | result = str + (eq5F & shiftForUnderscore); |
| | 48256 | 447 | | return true; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 451 | | public unsafe bool TryLoadVector512(byte* src, byte* srcStart, int sourceLength, out Vector512<sbyte> str) = |
| | 0 | 452 | | default(Base64DecoderByte).TryLoadVector512(src, srcStart, sourceLength, out str); |
| | | 453 | | |
| | | 454 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 455 | | [CompExactlyDependsOn(typeof(Avx2))] |
| | | 456 | | public unsafe bool TryLoadAvxVector256(byte* src, byte* srcStart, int sourceLength, out Vector256<sbyte> str |
| | 55135 | 457 | | default(Base64DecoderByte).TryLoadAvxVector256(src, srcStart, sourceLength, out str); |
| | | 458 | | |
| | | 459 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 460 | | public unsafe bool TryLoadVector128(byte* src, byte* srcStart, int sourceLength, out Vector128<byte> str) => |
| | 42266 | 461 | | default(Base64DecoderByte).TryLoadVector128(src, srcStart, sourceLength, out str); |
| | | 462 | | |
| | | 463 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 464 | | [CompExactlyDependsOn(typeof(AdvSimd.Arm64))] |
| | | 465 | | public unsafe bool TryLoadArmVector128x4(byte* src, byte* srcStart, int sourceLength, |
| | | 466 | | out Vector128<byte> str1, out Vector128<byte> str2, out Vector128<byte> str3, out Vector128<byte> str4) |
| | 0 | 467 | | default(Base64DecoderByte).TryLoadArmVector128x4(src, srcStart, sourceLength, out str1, out str2, out st |
| | | 468 | | #endif // NET |
| | | 469 | | |
| | | 470 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 471 | | public unsafe int DecodeFourElements(byte* source, ref sbyte decodingMap) => |
| | 106420 | 472 | | default(Base64DecoderByte).DecodeFourElements(source, ref decodingMap); |
| | | 473 | | |
| | | 474 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 475 | | public unsafe int DecodeRemaining(byte* srcEnd, ref sbyte decodingMap, long remaining, out uint t2, out uint |
| | 1986 | 476 | | default(Base64DecoderByte).DecodeRemaining(srcEnd, ref decodingMap, remaining, out t2, out t3); |
| | | 477 | | |
| | | 478 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 39833 | 479 | | public int IndexOfAnyExceptWhiteSpace(ReadOnlySpan<byte> span) => default(Base64DecoderByte).IndexOfAnyExcep |
| | | 480 | | |
| | | 481 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 482 | | [SuppressReadyToRunCanonicalRooting] |
| | | 483 | | public OperationStatus DecodeWithWhiteSpaceBlockwiseWrapper<TBase64Decoder>(TBase64Decoder decoder, ReadOnly |
| | | 484 | | ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true) where TBase64Decoder : IBase64Dec |
| | 1717 | 485 | | DecodeWithWhiteSpaceBlockwise(decoder, utf8, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock); |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | private readonly struct Base64UrlDecoderChar : IBase64Decoder<ushort> |
| | | 489 | | { |
| | 2832 | 490 | | public ReadOnlySpan<sbyte> DecodingMap => default(Base64UrlDecoderByte).DecodingMap; |
| | | 491 | | |
| | 0 | 492 | | public ReadOnlySpan<uint> VbmiLookup0 => default(Base64UrlDecoderByte).VbmiLookup0; |
| | | 493 | | |
| | 0 | 494 | | public ReadOnlySpan<uint> VbmiLookup1 => default(Base64UrlDecoderByte).VbmiLookup1; |
| | | 495 | | |
| | 1632 | 496 | | public ReadOnlySpan<sbyte> Avx2LutHigh => default(Base64UrlDecoderByte).Avx2LutHigh; |
| | | 497 | | |
| | 1632 | 498 | | public ReadOnlySpan<sbyte> Avx2LutLow => default(Base64UrlDecoderByte).Avx2LutLow; |
| | | 499 | | |
| | 1632 | 500 | | public ReadOnlySpan<sbyte> Avx2LutShift => default(Base64UrlDecoderByte).Avx2LutShift; |
| | | 501 | | |
| | 3078 | 502 | | public byte MaskSlashOrUnderscore => default(Base64UrlDecoderByte).MaskSlashOrUnderscore; |
| | | 503 | | |
| | 1446 | 504 | | public ReadOnlySpan<int> Vector128LutHigh => default(Base64UrlDecoderByte).Vector128LutHigh; |
| | | 505 | | |
| | 1446 | 506 | | public ReadOnlySpan<int> Vector128LutLow => default(Base64UrlDecoderByte).Vector128LutLow; |
| | | 507 | | |
| | 1446 | 508 | | public ReadOnlySpan<uint> Vector128LutShift => default(Base64UrlDecoderByte).Vector128LutShift; |
| | | 509 | | |
| | 0 | 510 | | public ReadOnlySpan<uint> AdvSimdLutOne3 => default(Base64UrlDecoderByte).AdvSimdLutOne3; |
| | | 511 | | |
| | 0 | 512 | | public uint AdvSimdLutTwo3Uint1 => default(Base64UrlDecoderByte).AdvSimdLutTwo3Uint1; |
| | | 513 | | |
| | 2832 | 514 | | public int GetMaxDecodedLength(int sourceLength) => default(Base64UrlDecoderByte).GetMaxDecodedLength(source |
| | | 515 | | |
| | 0 | 516 | | public bool IsInvalidLength(int bufferLength) => default(Base64UrlDecoderByte).IsInvalidLength(bufferLength) |
| | | 517 | | |
| | 2321 | 518 | | public bool IsValidPadding(uint padChar) => default(Base64UrlDecoderByte).IsValidPadding(padChar); |
| | | 519 | | |
| | 2832 | 520 | | public int SrcLength(bool isFinalBlock, int sourceLength) => default(Base64UrlDecoderByte).SrcLength(isFinal |
| | | 521 | | |
| | | 522 | | #if NET |
| | | 523 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 524 | | [CompExactlyDependsOn(typeof(AdvSimd.Arm64))] |
| | | 525 | | [CompExactlyDependsOn(typeof(Ssse3))] |
| | | 526 | | [CompExactlyDependsOn(typeof(PackedSimd))] |
| | | 527 | | public bool TryDecode128Core(Vector128<byte> str, Vector128<byte> hiNibbles, Vector128<byte> maskSlashOrUnde |
| | | 528 | | Vector128<byte> lutLow, Vector128<byte> lutHigh, Vector128<sbyte> lutShift, Vector128<byte> shiftForUnde |
| | 1984 | 529 | | default(Base64UrlDecoderByte).TryDecode128Core(str, hiNibbles, maskSlashOrUnderscore, mask8F, lutLow, lu |
| | | 530 | | |
| | | 531 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 532 | | [CompExactlyDependsOn(typeof(Avx2))] |
| | | 533 | | public bool TryDecode256Core(Vector256<sbyte> str, Vector256<sbyte> hiNibbles, Vector256<sbyte> maskSlashOrU |
| | | 534 | | Vector256<sbyte> lutHigh, Vector256<sbyte> lutShift, Vector256<sbyte> shiftForUnderscore, out Vector256< |
| | 30318 | 535 | | default(Base64UrlDecoderByte).TryDecode256Core(str, hiNibbles, maskSlashOrUnderscore, lutLow, lutHigh, l |
| | | 536 | | |
| | | 537 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 538 | | public unsafe bool TryLoadVector512(ushort* src, ushort* srcStart, int sourceLength, out Vector512<sbyte> st |
| | 0 | 539 | | default(Base64DecoderChar).TryLoadVector512(src, srcStart, sourceLength, out str); |
| | | 540 | | |
| | | 541 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 542 | | [CompExactlyDependsOn(typeof(Avx2))] |
| | | 543 | | public unsafe bool TryLoadAvxVector256(ushort* src, ushort* srcStart, int sourceLength, out Vector256<sbyte> |
| | 30318 | 544 | | default(Base64DecoderChar).TryLoadAvxVector256(src, srcStart, sourceLength, out str); |
| | | 545 | | |
| | | 546 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 547 | | public unsafe bool TryLoadVector128(ushort* src, ushort* srcStart, int sourceLength, out Vector128<byte> str |
| | 1984 | 548 | | default(Base64DecoderChar).TryLoadVector128(src, srcStart, sourceLength, out str); |
| | | 549 | | |
| | | 550 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 551 | | [CompExactlyDependsOn(typeof(AdvSimd.Arm64))] |
| | | 552 | | public unsafe bool TryLoadArmVector128x4(ushort* src, ushort* srcStart, int sourceLength, |
| | | 553 | | out Vector128<byte> str1, out Vector128<byte> str2, out Vector128<byte> str3, out Vector128<byte> str4) |
| | 0 | 554 | | default(Base64DecoderChar).TryLoadArmVector128x4(src, srcStart, sourceLength, out str1, out str2, out st |
| | | 555 | | #endif // NET |
| | | 556 | | |
| | | 557 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 558 | | public unsafe int DecodeFourElements(ushort* source, ref sbyte decodingMap) => |
| | 7959 | 559 | | default(Base64DecoderChar).DecodeFourElements(source, ref decodingMap); |
| | | 560 | | |
| | | 561 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 562 | | public unsafe int DecodeRemaining(ushort* srcEnd, ref sbyte decodingMap, long remaining, out uint t2, out ui |
| | 1435 | 563 | | default(Base64DecoderChar).DecodeRemaining(srcEnd, ref decodingMap, remaining, out t2, out t3); |
| | | 564 | | |
| | | 565 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 566 | | public int IndexOfAnyExceptWhiteSpace(ReadOnlySpan<ushort> span) => default(Base64DecoderChar).IndexOfAnyExc |
| | | 567 | | |
| | | 568 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 569 | | [SuppressReadyToRunCanonicalRooting] |
| | | 570 | | public OperationStatus DecodeWithWhiteSpaceBlockwiseWrapper<TBase64Decoder>(TBase64Decoder decoder, ReadOnly |
| | | 571 | | ref int bytesConsumed, ref int bytesWritten, bool isFinalBlock = true) where TBase64Decoder : IBase64Dec |
| | 0 | 572 | | DecodeWithWhiteSpaceBlockwise(decoder, source, bytes, ref bytesConsumed, ref bytesWritten, isFinalBlock) |
| | | 573 | | } |
| | | 574 | | } |
| | | 575 | | } |