Base64
Convert between binary data and UTF-8 encoded text that is represented in base 64.
public static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Span<byte> bytes, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true)
Decode the span of UTF-8 encoded text represented as base 64 into binary data.
If the input is not a multiple of 4, it will decode as much as it can, to the closest multiple of 4.
The input span which contains UTF-8 encoded text in base 64 that needs to be decoded.The output span which contains the result of the operation, i.e. the decoded binary data.The number of input bytes consumed during the operation. This can be used to slice the input for subsequent calls, if necessary.The number of bytes written into the output span. This can be used to slice the output for subsequent calls, if necessary.True (default) when the input span contains the entire data to decode.
Set to false only if it is known that the input span contains partial data with more data to follow.It returns the OperationStatus enum values:
- Done - on successful processing of the entire input span
- DestinationTooSmall - if there is not enough space in the output span to fit the decoded input
- NeedMoreData - only if isFinalBlock is false and the input is not a multiple of 4, otherwise the partial input would be considered as InvalidData
- InvalidData - if the input contains bytes outside of the expected base 64 range, or if it contains invalid/more than two padding characters,
or if the input is incomplete (i.e. not a multiple of 4) and isFinalBlock is true.
Decode the span of UTF-8 encoded text in base 64 (in-place) into binary data.
The decoded binary output is smaller than the text data contained in the input (the operation deflates the data).
If the input is not a multiple of 4, it will not decode any.
The input span which contains the base 64 text data that needs to be decoded.The number of bytes written into the buffer.It returns the OperationStatus enum values:
- Done - on successful processing of the entire input span
- InvalidData - if the input contains bytes outside of the expected base 64 range, or if it contains invalid/more than two padding characters,
or if the input is incomplete (i.e. not a multiple of 4).
It does not return DestinationTooSmall since that is not possible for base 64 decoding.
It does not return NeedMoreData since this method tramples the data in the buffer and
hence can only be called once with all the data in the buffer.
public static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> bytes, Span<byte> utf8, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true)
Encode the span of binary data into UTF-8 encoded text represented as base 64.
The input span which contains binary data that needs to be encoded.The output span which contains the result of the operation, i.e. the UTF-8 encoded text in base 64.The number of input bytes consumed during the operation. This can be used to slice the input for subsequent calls, if necessary.The number of bytes written into the output span. This can be used to slice the output for subsequent calls, if necessary.True (default) when the input span contains the entire data to decode.
Set to false only if it is known that the input span contains partial data with more data to follow.It returns the OperationStatus enum values:
- Done - on successful processing of the entire input span
- DestinationTooSmall - if there is not enough space in the output span to fit the encoded input
- NeedMoreData - only if isFinalBlock is false, otherwise the output is padded if the input is not a multiple of 3
It does not return InvalidData since that is not possible for base 64 encoding.
public static OperationStatus EncodeToUtf8InPlace(Span<byte> buffer, int dataLength, out int bytesWritten)
Encode the span of binary data (in-place) into UTF-8 encoded text represented as base 64.
The encoded text output is larger than the binary data contained in the input (the operation inflates the data).
The input span which contains binary data that needs to be encoded.
It needs to be large enough to fit the result of the operation.The amount of binary data contained within the buffer that needs to be encoded
(and needs to be smaller than the buffer length).The number of bytes written into the buffer.It returns the OperationStatus enum values:
- Done - on successful processing of the entire buffer
- DestinationTooSmall - if there is not enough space in the buffer beyond dataLength to fit the result of encoding the input
It does not return NeedMoreData since this method tramples the data in the buffer and hence can only be called once with all the data in the buffer.
It does not return InvalidData since that is not possible for base 64 encoding.
Returns the maximum length (in bytes) of the result if you were to deocde base 64 encoded text within a byte span of size "length".
Returns the maximum length (in bytes) of the result if you were to encode binary data within a byte span of size "length".