< Summary

Line coverage
81%
Covered lines: 75
Uncovered lines: 17
Coverable lines: 92
Total lines: 538
Line coverage: 81.5%
Branch coverage
54%
Covered branches: 23
Total branches: 42
Branch coverage: 54.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\h\w\B7B10A05\w\A2F5091A\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Base64Decoder.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7using static System.Buffers.Text.Base64Helper;
 8
 9namespace System.Buffers.Text
 10{
 11    public static partial class Base64
 12    {
 13        private const int MaxStackallocThreshold = 256;
 14
 15        /// <summary>
 16        /// Returns the maximum length (in bytes) of the result if you were to decode base 64 encoded text from a span o
 17        /// </summary>
 18        /// <param name="base64Length">The length of the base64-encoded input.</param>
 19        /// <returns>The maximum number of bytes that decoding could produce.</returns>
 20        /// <exception cref="ArgumentOutOfRangeException">
 21        /// <paramref name="base64Length"/> is less than 0.
 22        /// </exception>
 23        /// <remarks>
 24        /// This method is equivalent to <see cref="GetMaxDecodedFromUtf8Length(int)"/>.
 25        /// </remarks>
 26        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 27        public static int GetMaxDecodedLength(int base64Length)
 28        {
 133120029            ArgumentOutOfRangeException.ThrowIfNegative(base64Length);
 30
 133120031            return (base64Length >> 2) * 3;
 32        }
 33
 34        /// <summary>
 35        /// Returns the maximum length (in bytes) of the result if you were to decode base 64 encoded text within a byte
 36        /// </summary>
 37        /// <exception cref="ArgumentOutOfRangeException">
 38        /// Thrown when the specified <paramref name="length"/> is less than 0.
 39        /// </exception>
 40        /// <remarks>
 41        /// This method is equivalent to <see cref="GetMaxDecodedLength(int)"/>.
 42        /// </remarks>
 43        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 123585644        public static int GetMaxDecodedFromUtf8Length(int length) => GetMaxDecodedLength(length);
 45
 46        /// <summary>
 47        /// Decode the span of UTF-8 encoded text represented as base64 into binary data.
 48        /// If the input is not a multiple of 4, it will decode as much as it can, to the closest multiple of 4.
 49        /// </summary>
 50        /// <param name="utf8">The input span which contains UTF-8 encoded text in base64 that needs to be decoded.</par
 51        /// <param name="bytes">The output span which contains the result of the operation, i.e. the decoded binary data
 52        /// <param name="bytesConsumed">The number of input bytes consumed during the operation. This can be used to sli
 53        /// <param name="bytesWritten">The number of bytes written into the output span. This can be used to slice the o
 54        /// <param name="isFinalBlock"><see langword="true"/> (default) when the input span contains the entire data to 
 55        /// Set to <see langword="true"/> when the source buffer contains the entirety of the data to decode.
 56        /// Set to <see langword="false"/> if this method is being called in a loop and if more input data may follow.
 57        /// At the end of the loop, call this (potentially with an empty source buffer) passing <see langword="true"/>.<
 58        /// <returns>It returns the OperationStatus enum values:
 59        /// - Done - on successful processing of the entire input span
 60        /// - DestinationTooSmall - if there is not enough space in the output span to fit the decoded input
 61        /// - NeedMoreData - only if <paramref name="isFinalBlock"/> is false and the input is not a multiple of 4, othe
 62        /// - InvalidData - if the input contains bytes outside of the expected base64 range, or if it contains invalid/
 63        ///   or if the input is incomplete (i.e. not a multiple of 4) and <paramref name="isFinalBlock"/> is <see langw
 64        /// </returns>
 65        public static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Span<byte> bytes, out int bytesConsumed, o
 7150866            DecodeFrom(default(Base64DecoderByte), utf8, bytes, out bytesConsumed, out bytesWritten, isFinalBlock, ignor
 67
 68        /// <summary>
 69        /// Decodes the span of UTF-8 encoded text represented as Base64 into binary data.
 70        /// </summary>
 71        /// <param name="source">The input span which contains UTF-8 encoded text in Base64 that needs to be decoded.</p
 72        /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar
 73        /// <returns>The number of bytes written into <paramref name="destination"/>. This can be used to slice the outp
 74        /// <exception cref="ArgumentException">The buffer in <paramref name="destination"/> is too small to hold the en
 75        /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64 character,
 76        /// more than two padding characters, or a non white space character among the padding characters.</exception>
 77        public static int DecodeFromUtf8(ReadOnlySpan<byte> source, Span<byte> destination)
 78        {
 1191879            OperationStatus status = DecodeFromUtf8(source, destination, out _, out int bytesWritten);
 80
 1191881            if (status == OperationStatus.Done)
 82            {
 1191883                return bytesWritten;
 84            }
 85
 086            if (status == OperationStatus.DestinationTooSmall)
 87            {
 088                throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
 89            }
 90
 091            Debug.Assert(status == OperationStatus.InvalidData);
 092            throw new FormatException(SR.Format_BadBase64Char);
 93        }
 94
 95        /// <summary>
 96        /// Decodes the span of UTF-8 encoded text represented as Base64 into binary data.
 97        /// </summary>
 98        /// <param name="source">The input span which contains UTF-8 encoded text in Base64 that needs to be decoded.</p
 99        /// <returns>A byte array which contains the result of the decoding operation.</returns>
 100        /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64 character,
 101        /// more than two padding characters, or a non white space character among the padding characters.</exception>
 102        public static byte[] DecodeFromUtf8(ReadOnlySpan<byte> source)
 103        {
 11918104            int upperBound = GetMaxDecodedLength(source.Length);
 11918105            byte[]? rented = null;
 106
 11918107            Span<byte> destination = (uint)upperBound <= MaxStackallocThreshold
 11918108                ? stackalloc byte[MaxStackallocThreshold]
 11918109                : (rented = ArrayPool<byte>.Shared.Rent(upperBound));
 110
 11918111            OperationStatus status = DecodeFromUtf8(source, destination, out _, out int bytesWritten);
 11918112            Debug.Assert(status is OperationStatus.Done or OperationStatus.InvalidData);
 11918113            byte[] result = destination.Slice(0, bytesWritten).ToArray();
 114
 11918115            if (rented is not null)
 116            {
 6494117                ArrayPool<byte>.Shared.Return(rented);
 118            }
 119
 11918120            return status == OperationStatus.Done ? result : throw new FormatException(SR.Format_BadBase64Char);
 121        }
 122
 123        /// <summary>
 124        /// Decodes the span of UTF-8 encoded text represented as Base64 into binary data.
 125        /// </summary>
 126        /// <param name="source">The input span which contains UTF-8 encoded text in Base64 that needs to be decoded.</p
 127        /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar
 128        /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp
 129        /// <returns><see langword="true"/> if bytes decoded successfully, otherwise <see langword="false"/>.</returns>
 130        /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64 character,
 131        /// more than two padding characters, or a non white space character among the padding characters.</exception>
 132        public static bool TryDecodeFromUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
 133        {
 11918134            OperationStatus status = DecodeFromUtf8(source, destination, out _, out bytesWritten);
 135
 11918136            if (status == OperationStatus.InvalidData)
 137            {
 0138                throw new FormatException(SR.Format_BadBase64Char);
 139            }
 140
 11918141            Debug.Assert(status is OperationStatus.Done or OperationStatus.DestinationTooSmall);
 11918142            return status == OperationStatus.Done;
 143        }
 144
 145        /// <summary>
 146        /// Decode the span of UTF-8 encoded text in base 64 (in-place) into binary data.
 147        /// The decoded binary output is smaller than the text data contained in the input (the operation deflates the d
 148        /// If the input is not a multiple of 4, it will not decode any.
 149        /// </summary>
 150        /// <param name="buffer">The input span which contains the base 64 text data that needs to be decoded.</param>
 151        /// <param name="bytesWritten">The number of bytes written into the buffer.</param>
 152        /// <returns>It returns the OperationStatus enum values:
 153        /// - Done - on successful processing of the entire input span
 154        /// - InvalidData - if the input contains bytes outside of the expected base 64 range, or if it contains invalid
 155        ///   or if the input is incomplete (i.e. not a multiple of 4).
 156        /// It does not return DestinationTooSmall since that is not possible for base 64 decoding.
 157        /// It does not return NeedMoreData since this method tramples the data in the buffer and
 158        /// hence can only be called once with all the data in the buffer.
 159        /// </returns>
 160        public static OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, out int bytesWritten) =>
 23836161            Base64Helper.DecodeFromUtf8InPlace(default(Base64DecoderByte), buffer, out bytesWritten, ignoreWhiteSpace: t
 162
 163        /// <summary>
 164        /// Decodes the span of unicode ASCII chars represented as Base64 into binary data.
 165        /// </summary>
 166        /// <param name="source">The input span which contains unicode ASCII chars in Base64 that needs to be decoded.</
 167        /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar
 168        /// <param name="charsConsumed">When this method returns, contains the number of input chars consumed during the
 169        /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp
 170        /// <param name="isFinalBlock"><see langword="true"/> when the input span contains the entirety of data to encod
 171        /// such as when calling in a loop. Calls with <see langword="false"/> should be followed up with another call w
 172        /// <returns>One of the enumeration values that indicates the success or failure of the operation.</returns>
 173        public static OperationStatus DecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination,
 174            out int charsConsumed, out int bytesWritten, bool isFinalBlock = true) =>
 119170175            DecodeFrom(default(Base64DecoderChar), MemoryMarshal.Cast<char, ushort>(source), destination,
 119170176                out charsConsumed, out bytesWritten, isFinalBlock, ignoreWhiteSpace: true);
 177
 178        /// <summary>
 179        /// Decodes the span of unicode ASCII chars represented as Base64 into binary data.
 180        /// </summary>
 181        /// <param name="source">The input span which contains ASCII chars in Base64 that needs to be decoded.</param>
 182        /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar
 183        /// <returns>The number of bytes written into the output span. This can be used to slice the output for subseque
 184        /// <exception cref="ArgumentException">The buffer in <paramref name="destination"/> is too small to hold the en
 185        /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64 character,
 186        /// more than two padding characters, or a non white space character among the padding characters.</exception>
 187        public static int DecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination)
 188        {
 11918189            OperationStatus status = DecodeFromChars(source, destination, out _, out int bytesWritten);
 190
 11918191            if (status == OperationStatus.Done)
 192            {
 11918193                return bytesWritten;
 194            }
 195
 0196            if (status == OperationStatus.DestinationTooSmall)
 197            {
 0198                throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
 199            }
 200
 0201            Debug.Assert(status == OperationStatus.InvalidData);
 0202            throw new FormatException(SR.Format_BadBase64Char);
 203        }
 204
 205        /// <summary>
 206        /// Decodes the span of unicode ASCII chars represented as Base64 into binary data.
 207        /// </summary>
 208        /// <param name="source">The input span which contains ASCII chars in Base64 that needs to be decoded.</param>
 209        /// <param name="destination">The output span which contains the result of the operation, i.e. the decoded binar
 210        /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp
 211        /// <returns><see langword="true"/> if bytes decoded successfully, otherwise <see langword="false"/>.</returns>
 212        /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64 character,
 213        /// more than two padding characters, or a non white space character among the padding characters.</exception>
 214        public static bool TryDecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination, out int bytesWritten)
 215        {
 11918216            OperationStatus status = DecodeFromChars(source, destination, out _, out bytesWritten);
 217
 11918218            if (status == OperationStatus.InvalidData)
 219            {
 0220                throw new FormatException(SR.Format_BadBase64Char);
 221            }
 222
 11918223            Debug.Assert(status is OperationStatus.Done or OperationStatus.DestinationTooSmall);
 11918224            return status == OperationStatus.Done;
 225        }
 226
 227        /// <summary>
 228        /// Decodes the span of unicode ASCII chars represented as Base64 into binary data.
 229        /// </summary>
 230        /// <param name="source">The input span which contains ASCII chars in Base64 that needs to be decoded.</param>
 231        /// <returns>A byte array which contains the result of the decoding operation.</returns>
 232        /// <exception cref="FormatException"><paramref name="source"/> contains an invalid Base64 character,
 233        /// more than two padding characters, or a non white space character among the padding characters.</exception>
 234        public static byte[] DecodeFromChars(ReadOnlySpan<char> source)
 235        {
 59590236            int upperBound = GetMaxDecodedLength(source.Length);
 59590237            byte[]? rented = null;
 238
 59590239            Span<byte> destination = (uint)upperBound <= MaxStackallocThreshold
 59590240                ? stackalloc byte[MaxStackallocThreshold]
 59590241                : (rented = ArrayPool<byte>.Shared.Rent(upperBound));
 242
 59590243            OperationStatus status = DecodeFromChars(source, destination, out _, out int bytesWritten);
 59590244            Debug.Assert(status is OperationStatus.Done or OperationStatus.InvalidData);
 59590245            byte[] result = destination.Slice(0, bytesWritten).ToArray();
 246
 59590247            if (rented is not null)
 248            {
 32686249                ArrayPool<byte>.Shared.Return(rented);
 250            }
 251
 59590252            return status == OperationStatus.Done ? result : throw new FormatException(SR.Format_BadBase64Char);
 253        }
 254    }
 255}

C:\h\w\B7B10A05\w\A2F5091A\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Base64Encoder.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7using static System.Buffers.Text.Base64Helper;
 8
 9namespace System.Buffers.Text
 10{
 11    /// <summary>
 12    /// Convert between binary data and UTF-8 encoded text that is represented in base 64.
 13    /// </summary>
 14    public static partial class Base64
 15    {
 16        /// <summary>
 17        /// Returns the length (in bytes) of the result if you were to encode binary data within a byte span of size <pa
 18        /// </summary>
 19        /// <param name="bytesLength">The number of bytes to encode.</param>
 20        /// <returns>The number of bytes that encoding will produce.</returns>
 21        /// <exception cref="ArgumentOutOfRangeException">
 22        /// <paramref name="bytesLength"/> is less than 0 or greater than 1610612733.
 23        /// </exception>
 24        /// <remarks>
 25        /// This method is equivalent to <see cref="GetMaxEncodedToUtf8Length(int)"/>. The encoded length for base64 is 
 26        /// </remarks>
 27        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 28        public static int GetEncodedLength(int bytesLength)
 29        {
 51249030            ArgumentOutOfRangeException.ThrowIfGreaterThan<uint>((uint)bytesLength, MaximumEncodeLength);
 31
 51249032            return ((bytesLength + 2) / 3) * 4;
 33        }
 34
 35        /// <summary>
 36        /// Returns the maximum length (in bytes) of the result if you were to encode binary data within a byte span of 
 37        /// </summary>
 38        /// <exception cref="ArgumentOutOfRangeException">
 39        /// Thrown when the specified <paramref name="length"/> is less than 0 or larger than 1610612733 (since encode i
 40        /// </exception>
 41        /// <remarks>
 42        /// This method is equivalent to <see cref="GetEncodedLength(int)"/>. The encoded length for base64 is exactly c
 43        /// </remarks>
 44        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 44098245        public static int GetMaxEncodedToUtf8Length(int length) => GetEncodedLength(length);
 46
 47        /// <summary>
 48        /// Encode the span of binary data into UTF-8 encoded text represented as base64.
 49        /// </summary>
 50        /// <param name="bytes">The input span which contains binary data that needs to be encoded.</param>
 51        /// <param name="utf8">The output span which contains the result of the operation, i.e. the UTF-8 encoded text i
 52        /// <param name="bytesConsumed">The number of input bytes consumed during the operation. This can be used to sli
 53        /// <param name="bytesWritten">The number of bytes written into the output span. This can be used to slice the o
 54        /// <param name="isFinalBlock"><see langword="true"/> (default) when the input span contains the entire data to 
 55        /// Set to <see langword="true"/> when the source buffer contains the entirety of the data to encode.
 56        /// Set to <see langword="false"/> if this method is being called in a loop and if more input data may follow.
 57        /// At the end of the loop, call this (potentially with an empty source buffer) passing <see langword="true"/>.<
 58        /// <returns>It returns the <see cref="OperationStatus"/> enum values:
 59        /// - Done - on successful processing of the entire input span
 60        /// - DestinationTooSmall - if there is not enough space in the output span to fit the encoded input
 61        /// - NeedMoreData - only if <paramref name="isFinalBlock"/> is <see langword="false"/>, otherwise the output is
 62        /// It does not return InvalidData since that is not possible for base64 encoding.
 63        /// </returns>
 64        public static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> bytes, Span<byte> utf8, out int bytesConsumed, out
 5959065            EncodeTo(default(Base64EncoderByte), bytes, utf8, out bytesConsumed, out bytesWritten, isFinalBlock);
 66
 67        /// <summary>
 68        /// Encodes the span of binary data into UTF-8 encoded text represented as Base64.
 69        /// </summary>
 70        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 71        /// <param name="destination">The output span which contains the result of the operation, i.e. the UTF-8 encoded
 72        /// <returns>The number of bytes written into the destination span. This can be used to slice the output for sub
 73        /// <exception cref="ArgumentException">The buffer in <paramref name="destination"/> is too small to hold the en
 74        public static int EncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination)
 75        {
 1191876            OperationStatus status = EncodeToUtf8(source, destination, out _, out int bytesWritten);
 77
 1191878            if (status == OperationStatus.Done)
 79            {
 1191880                return bytesWritten;
 81            }
 82
 083            Debug.Assert(status == OperationStatus.DestinationTooSmall);
 084            throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
 85        }
 86
 87        /// <summary>
 88        /// Encodes the span of binary data into UTF-8 encoded text represented as Base64.
 89        /// </summary>
 90        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 91        /// <returns>The output byte array which contains the result of the operation, i.e. the UTF-8 encoded text in Ba
 92        public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source)
 93        {
 1191894            byte[] destination = new byte[GetEncodedLength(source.Length)];
 1191895            EncodeToUtf8(source, destination, out _, out int bytesWritten);
 1191896            Debug.Assert(destination.Length == bytesWritten);
 97
 1191898            return destination;
 99        }
 100
 101        /// <summary>
 102        /// Encodes the span of binary data into UTF-8 encoded text represented as Base64.
 103        /// </summary>
 104        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 105        /// <param name="destination">The output span which contains the result of the operation, i.e. the UTF-8 encoded
 106        /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the output sp
 107        /// <returns><see langword="true"/> if bytes encoded successfully, otherwise <see langword="false"/>.</returns>
 108        public static bool TryEncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
 109        {
 11918110            OperationStatus status = EncodeToUtf8(source, destination, out _, out bytesWritten);
 111
 11918112            return status == OperationStatus.Done;
 113        }
 114
 115        /// <summary>
 116        /// Encode the span of binary data (in-place) into UTF-8 encoded text represented as base 64.
 117        /// The encoded text output is larger than the binary data contained in the input (the operation inflates the da
 118        /// </summary>
 119        /// <param name="buffer">The input span which contains binary data that needs to be encoded.
 120        /// It needs to be large enough to fit the result of the operation.</param>
 121        /// <param name="dataLength">The amount of binary data contained within the buffer that needs to be encoded
 122        /// (and needs to be smaller than the buffer length).</param>
 123        /// <param name="bytesWritten">The number of bytes written into the buffer.</param>
 124        /// <returns>It returns the OperationStatus enum values:
 125        /// - Done - on successful processing of the entire buffer
 126        /// - DestinationTooSmall - if there is not enough space in the buffer beyond dataLength to fit the result of en
 127        /// It does not return NeedMoreData since this method tramples the data in the buffer and hence can only be call
 128        /// It does not return InvalidData since that is not possible for base 64 encoding.
 129        /// </returns>
 130        public static OperationStatus EncodeToUtf8InPlace(Span<byte> buffer, int dataLength, out int bytesWritten) =>
 23836131            Base64Helper.EncodeToUtf8InPlace(default(Base64EncoderByte), buffer, dataLength, out bytesWritten);
 132
 133        /// <summary>
 134        /// Encodes the span of binary data (in-place) into UTF-8 encoded text represented as base 64.
 135        /// The encoded text output is larger than the binary data contained in the input (the operation inflates the da
 136        /// </summary>
 137        /// <param name="buffer">The input span which contains binary data that needs to be encoded.
 138        /// It needs to be large enough to fit the result of the operation.</param>
 139        /// <param name="dataLength">The amount of binary data contained within the buffer that needs to be encoded
 140        /// (and needs to be smaller than the buffer length).</param>
 141        /// <param name="bytesWritten">When this method returns, contains the number of bytes written into the buffer. T
 142        /// <returns><see langword="true"/> if bytes encoded successfully, otherwise <see langword="false"/>.</returns>
 143        public static bool TryEncodeToUtf8InPlace(Span<byte> buffer, int dataLength, out int bytesWritten)
 144        {
 11918145            OperationStatus status = EncodeToUtf8InPlace(buffer, dataLength, out bytesWritten);
 146
 11918147            return status == OperationStatus.Done;
 148        }
 149
 150        /// <summary>
 151        /// Encodes the span of binary data into unicode ASCII chars represented as Base64.
 152        /// </summary>
 153        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 154        /// <param name="destination">The output span which contains the result of the operation, i.e. the ASCII chars i
 155        /// <param name="bytesConsumed">When this method returns, contains the number of input bytes consumed during the
 156        /// <param name="charsWritten">When this method returns, contains the number of chars written into the output sp
 157        /// <param name="isFinalBlock"><see langword="true"/> when the input span contains the entirety of data to encod
 158        /// such as when calling in a loop, subsequent calls with <see langword="false"/> should end with <see langword=
 159        /// <returns>One of the enumeration values that indicates the success or failure of the operation.</returns>
 160        public static OperationStatus EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination,
 161            out int bytesConsumed, out int charsWritten, bool isFinalBlock = true) =>
 333720162            EncodeTo(default(Base64EncoderChar), source, MemoryMarshal.Cast<char, ushort>(destination), out bytesConsume
 163
 164        /// <summary>
 165        /// Encodes the span of binary data into unicode ASCII chars represented as Base64.
 166        /// </summary>
 167        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 168        /// <param name="destination">The output span which contains the result of the operation, i.e. the ASCII chars i
 169        /// <returns>The number of chars written into the destination span. This can be used to slice the output for sub
 170        /// <exception cref="ArgumentException">The buffer in <paramref name="destination"/> is too small to hold the en
 171        public static int EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination)
 172        {
 23836173            OperationStatus status = EncodeToChars(source, destination, out _, out int charsWritten);
 174
 23836175            if (status == OperationStatus.Done)
 176            {
 23836177                return charsWritten;
 178            }
 179
 0180            Debug.Assert(status == OperationStatus.DestinationTooSmall);
 0181            throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
 182        }
 183
 184        /// <summary>
 185        /// Encodes the span of binary data into unicode ASCII chars represented as Base64.
 186        /// </summary>
 187        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 188        /// <returns>A char array which contains the result of the operation, i.e. the ASCII chars in Base64.</returns>
 189        public static char[] EncodeToChars(ReadOnlySpan<byte> source)
 190        {
 11918191            char[] destination = new char[GetEncodedLength(source.Length)];
 11918192            EncodeToChars(source, destination, out _, out int charsWritten);
 11918193            Debug.Assert(destination.Length == charsWritten);
 194
 11918195            return destination;
 196        }
 197
 198        /// <summary>
 199        /// Encodes the span of binary data into unicode string represented as Base64 ASCII chars.
 200        /// </summary>
 201        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 202        /// <returns>A string which contains the result of the operation, i.e. the ASCII string in Base64.</returns>
 203        public static string EncodeToString(ReadOnlySpan<byte> source) =>
 23836204            string.Create(GetEncodedLength(source.Length), source, static (buffer, source) =>
 23836205            {
 23836206                EncodeToChars(source, buffer, out _, out int charsWritten);
 23836207                Debug.Assert(buffer.Length == charsWritten, $"The source length: {source.Length}, chars written: {charsW
 47672208            });
 209
 210        /// <summary>
 211        /// Encodes the span of binary data into unicode ASCII chars represented as Base64.
 212        /// </summary>
 213        /// <param name="source">The input span which contains binary data that needs to be encoded.</param>
 214        /// <param name="destination">The output span which contains the result of the operation, i.e. the ASCII chars i
 215        /// <param name="charsWritten">When this method returns, contains the number of chars written into the output sp
 216        /// <returns><see langword="true"/> if chars encoded successfully, otherwise <see langword="false"/>.</returns>
 217        public static bool TryEncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)
 218        {
 11918219            OperationStatus status = EncodeToChars(source, destination, out _, out charsWritten);
 220
 11918221            return status == OperationStatus.Done;
 222        }
 223    }
 224}

C:\h\w\B7B10A05\w\A2F5091A\e\runtime-utils\Runner\runtime\src\libraries\System.Private.CoreLib\src\System\Buffers\Text\Base64Validator.cs

#LineLine coverage
 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
 4using static System.Buffers.Text.Base64Helper;
 5
 6namespace System.Buffers.Text
 7{
 8    public static partial class Base64
 9    {
 10        /// <summary>Validates that the specified span of text is comprised of valid base-64 encoded data.</summary>
 11        /// <param name="base64Text">A span of text to validate.</param>
 12        /// <returns><see langword="true"/> if <paramref name="base64Text"/> contains a valid, decodable sequence of bas
 13        /// <remarks>
 14        /// If the method returns <see langword="true"/>, the same text passed to <see cref="Convert.FromBase64String(st
 15        /// <see cref="Convert.TryFromBase64Chars"/> would successfully decode (in the case
 16        /// of <see cref="Convert.TryFromBase64Chars"/> assuming sufficient output space). Any amount of whitespace is a
 17        /// where whitespace is defined as the characters ' ', '\t', '\r', or '\n'.
 18        /// </remarks>
 19        public static bool IsValid(ReadOnlySpan<char> base64Text) =>
 020            Base64Helper.IsValid(default(Base64CharValidatable), base64Text, out _);
 21
 22        /// <summary>Validates that the specified span of text is comprised of valid base-64 encoded data.</summary>
 23        /// <param name="base64Text">A span of text to validate.</param>
 24        /// <param name="decodedLength">If the method returns true, the number of decoded bytes that will result from de
 25        /// <returns><see langword="true"/> if <paramref name="base64Text"/> contains a valid, decodable sequence of bas
 26        /// <remarks>
 27        /// If the method returns <see langword="true"/>, the same text passed to <see cref="Convert.FromBase64String(st
 28        /// <see cref="Convert.TryFromBase64Chars"/> would successfully decode (in the case
 29        /// of <see cref="Convert.TryFromBase64Chars"/> assuming sufficient output space). Any amount of whitespace is a
 30        /// where whitespace is defined as the characters ' ', '\t', '\r', or '\n'.
 31        /// </remarks>
 32        public static bool IsValid(ReadOnlySpan<char> base64Text, out int decodedLength) =>
 033            Base64Helper.IsValid(default(Base64CharValidatable), base64Text, out decodedLength);
 34
 35        /// <summary>Validates that the specified span of UTF-8 text is comprised of valid base-64 encoded data.</summar
 36        /// <param name="base64TextUtf8">A span of UTF-8 text to validate.</param>
 37        /// <returns><see langword="true"/> if <paramref name="base64TextUtf8"/> contains a valid, decodable sequence of
 38        /// <remarks>
 39        /// If the method returns <see langword="true"/>, the same text passed to <see cref="DecodeFromUtf8"/> and
 40        /// <see cref="DecodeFromUtf8InPlace"/> would successfully decode. Any amount of whitespace is allowed anywhere 
 41        /// where whitespace is defined as the characters ' ', '\t', '\r', or '\n' (as bytes).
 42        /// </remarks>
 43        public static bool IsValid(ReadOnlySpan<byte> base64TextUtf8) =>
 044            Base64Helper.IsValid(default(Base64ByteValidatable), base64TextUtf8, out _);
 45
 46        /// <summary>Validates that the specified span of UTF-8 text is comprised of valid base-64 encoded data.</summar
 47        /// <param name="base64TextUtf8">A span of UTF-8 text to validate.</param>
 48        /// <param name="decodedLength">If the method returns true, the number of decoded bytes that will result from de
 49        /// <returns><see langword="true"/> if <paramref name="base64TextUtf8"/> contains a valid, decodable sequence of
 50        /// <remarks>
 51        /// If the method returns <see langword="true"/>, the same text passed to <see cref="DecodeFromUtf8"/> and
 52        /// <see cref="DecodeFromUtf8InPlace"/> would successfully decode. Any amount of whitespace is allowed anywhere 
 53        /// where whitespace is defined as the characters ' ', '\t', '\r', or '\n' (as bytes).
 54        /// </remarks>
 55        public static bool IsValid(ReadOnlySpan<byte> base64TextUtf8, out int decodedLength) =>
 1191856            Base64Helper.IsValid(default(Base64ByteValidatable), base64TextUtf8, out decodedLength);
 57
 58    }
 59}

Methods/Properties

GetMaxDecodedLength(System.Int32)
GetMaxDecodedFromUtf8Length(System.Int32)
DecodeFromUtf8(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Byte>,System.Int32&,System.Int32&,System.Boolean)
DecodeFromUtf8(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Byte>)
DecodeFromUtf8(System.ReadOnlySpan`1<System.Byte>)
TryDecodeFromUtf8(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Byte>,System.Int32&)
DecodeFromUtf8InPlace(System.Span`1<System.Byte>,System.Int32&)
DecodeFromChars(System.ReadOnlySpan`1<System.Char>,System.Span`1<System.Byte>,System.Int32&,System.Int32&,System.Boolean)
DecodeFromChars(System.ReadOnlySpan`1<System.Char>,System.Span`1<System.Byte>)
TryDecodeFromChars(System.ReadOnlySpan`1<System.Char>,System.Span`1<System.Byte>,System.Int32&)
DecodeFromChars(System.ReadOnlySpan`1<System.Char>)
GetEncodedLength(System.Int32)
GetMaxEncodedToUtf8Length(System.Int32)
EncodeToUtf8(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Byte>,System.Int32&,System.Int32&,System.Boolean)
EncodeToUtf8(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Byte>)
EncodeToUtf8(System.ReadOnlySpan`1<System.Byte>)
TryEncodeToUtf8(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Byte>,System.Int32&)
EncodeToUtf8InPlace(System.Span`1<System.Byte>,System.Int32,System.Int32&)
TryEncodeToUtf8InPlace(System.Span`1<System.Byte>,System.Int32,System.Int32&)
EncodeToChars(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Char>,System.Int32&,System.Int32&,System.Boolean)
EncodeToChars(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Char>)
EncodeToChars(System.ReadOnlySpan`1<System.Byte>)
EncodeToString(System.ReadOnlySpan`1<System.Byte>)
TryEncodeToChars(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Char>,System.Int32&)
IsValid(System.ReadOnlySpan`1<System.Char>)
IsValid(System.ReadOnlySpan`1<System.Char>,System.Int32&)
IsValid(System.ReadOnlySpan`1<System.Byte>)
IsValid(System.ReadOnlySpan`1<System.Byte>,System.Int32&)