C/C++ API Reference
Loading...
Searching...
No Matches
pw_base64

Oveview

Base64 encoding, decoding, and validating. Main docs: https://pigweed.dev/pw_base64.

Functions

constexpr size_t pw::base64::EncodedSize (size_t binary_size_bytes)
 
void pw::base64::Encode (span< const std::byte > binary, char *output)
 
size_t pw::base64::Encode (span< const std::byte > binary, span< char > output_buffer)
 
void pw::base64::Encode (span< const std::byte > binary, InlineString<> &output)
 
template<size_t kMaxBinaryDataSizeBytes>
InlineString< EncodedSize(kMaxBinaryDataSizeBytes)> pw::base64::Encode (span< const std::byte > binary)
 
constexpr size_t pw::base64::MaxDecodedSize (size_t base64_size_bytes)
 
constexpr size_t pw::base64::DecodedSize (std::string_view valid_base64)
 
size_t pw::base64::Decode (std::string_view base64, void *output)
 
size_t pw::base64::Decode (std::string_view base64, span< std::byte > output_buffer)
 
template<typename T >
void pw::base64::DecodeInPlace (InlineBasicString< T > &buffer)
 Decodes a pw::InlineString<> in place.
 
bool pw::base64::IsValid (std::string_view base64)
 
bool pw::base64::IsValidChar (char base64)
 

Function Documentation

◆ Decode() [1/2]

size_t pw::base64::Decode ( std::string_view  base64,
span< std::byte >  output_buffer 
)

Decodes the provided Base64 data, if the data is valid and fits in the output buffer.

Returns
The number of bytes written, which will be 0 if the data is invalid or doesn't fit.

◆ Decode() [2/2]

size_t pw::base64::Decode ( std::string_view  base64,
void *  output 
)
inline

Decodes the provided Base64 data into raw binary.

Precondition
  • The output buffer MUST be at least MaxDecodedSize() bytes large.
  • This function does NOT check that the input is valid! Use IsValid() or the four-argument overload to check the input formatting.
Parameters
[in]base64The Base64 data that should be decoded. Can be encoded with either the standard (+/) or URL-safe (-_) alphabet. The data must be padded to 4-character blocks with =.
[out]outputThe output buffer where the raw binary will be placed. The output buffer may be the same as the input buffer; decoding can occur in place.
Returns
The number of bytes that were decoded.

◆ DecodedSize()

constexpr size_t pw::base64::DecodedSize ( std::string_view  valid_base64)
constexpr

Calculates the exact size of Base64-encoded data after decoding.

Parameters
[in]valid_base64A valid Base64-encoded string
Returns
The size of the Base64-encoded data represented by valid_base64 after decoding. Returns 0 if valid_base64.size() is not a multiple of 4, since Base64 encodes 3-byte groups into 4-character strings.

◆ Encode() [1/4]

template<size_t kMaxBinaryDataSizeBytes>
InlineString< EncodedSize(kMaxBinaryDataSizeBytes)> pw::base64::Encode ( span< const std::byte >  binary)
inline

Creates a pw::InlineString<> large enough to hold kMaxBinaryDataSizeBytes of binary data when encoded as Base64 and encodes the provided span into it.

◆ Encode() [2/4]

void pw::base64::Encode ( span< const std::byte >  binary,
char *  output 
)
inline

Encodes the provided data in Base64 and writes the result to the buffer.

Parameters
[in]binaryThe binary data to encode.
[out]Theoutput buffer where the encoded data is placed. Exactly EncodedSize(binary_size_bytes) bytes is written.
Note
Encodes to the standard alphabet with + and / for characters 62 and 63.
Precondition
  • The output buffer MUST be large enough for the encoded output!
  • The input and output buffers MUST NOT be the same; encoding cannot occur in place.
Warning
The resulting string in the output is NOT null-terminated!

◆ Encode() [3/4]

void pw::base64::Encode ( span< const std::byte >  binary,
InlineString<> &  output 
)

Appends Base64 encoded binary data to the provided pw::InlineString.

Parameters
[in]binaryThe binary data that has already been Base64-encoded.
[out]outputThe pw::InlineString that binary is appended to.

If the data does not fit in the string, an assertion fails.

◆ Encode() [4/4]

size_t pw::base64::Encode ( span< const std::byte >  binary,
span< char >  output_buffer 
)

Encodes the provided data in Base64 if the result fits in the provided buffer.

Parameters
[in]binaryThe binary data to encode.
[out]output_bufferThe output buffer where the encoded data is placed.
Warning
The resulting string in the output is NOT null-terminated!
Returns
The number of bytes written. Returns 0 if the output buffer is too small.

◆ EncodedSize()

constexpr size_t pw::base64::EncodedSize ( size_t  binary_size_bytes)
constexpr
Parameters
[in]binary_size_bytesThe size of the binary data in bytes, before encoding.
Returns
The size of binary_size_bytes after Base64 encoding.
Note
Base64 encodes 3-byte groups into 4-character strings. The final group is padded to be 3 bytes if it only has 1 or 2.

◆ IsValid()

bool pw::base64::IsValid ( std::string_view  base64)
inline
Parameters
[in]base64The string to check. Can be encoded with either the standard (+/) or URL-safe (-_) alphabet.
Returns
true if the provided string is valid Base64-encoded data.

◆ IsValidChar()

bool pw::base64::IsValidChar ( char  base64)
inline
Parameters
[in]base64The character to check. Can be encoded with either the standard (+/) or URL-safe (-_) alphabet.
Returns
true if the character is a valid non-padding Base64 character.

◆ MaxDecodedSize()

constexpr size_t pw::base64::MaxDecodedSize ( size_t  base64_size_bytes)
constexpr

Calculates the maximum size of Base64-encoded data after decoding.

Parameters
[in]base64_size_bytesThe size of the Base64-encoded data.
Returns
The maximum size of the Base64-encoded data represented by base64_bytes_size after decoding. If the last 3-byte group has padding, the actual decoded size will be 1 or 2 bytes less than the value returned by MaxDecodedSize(). Returns 0 if base64_size_bytes is not a multiple of 4, since Base64 encodes 3-byte groups into 4-character strings.