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