pw_base64#
Base64 encoding, decoding, and validating
Stable C C++ Rust
This module provides functions for encoding, decoding, and validating Base64 data as specified by RFC 3548 and RFC 4648.
C++ API reference#
-
namespace base64#
Functions
-
constexpr size_t EncodedSize(size_t binary_size_bytes)#
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.
- Parameters:
binary_size_bytes – [in] The size of the binary data in bytes, before encoding.
- Returns:
The size of
binary_size_bytes
after Base64 encoding.
-
inline void Encode(span<const std::byte> binary, char *output)#
Encodes the provided data in Base64 and writes the result to the buffer.
Note
Encodes to the standard alphabet with
+
and/
for characters62
and63
.Warning
The resulting string in the output is NOT null-terminated!
- Parameters:
binary – [in] The binary data to encode.
The – [out] output buffer where the encoded data is placed. Exactly
EncodedSize(binary_size_bytes)
bytes is written.
- Pre:
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.
-
size_t Encode(span<const std::byte> binary, span<char> output_buffer)#
Encodes the provided data in Base64 if the result fits in the provided buffer.
Warning
The resulting string in the output is NOT null-terminated!
- Parameters:
binary – [in] The binary data to encode.
output_buffer – [out] The output buffer where the encoded data is placed.
- Returns:
The number of bytes written. Returns
0
if the output buffer is too small.
-
void Encode(span<const std::byte> binary, InlineString<> &output)#
Appends Base64 encoded binary data to the provided
pw::InlineString
.If the data does not fit in the string, an assertion fails.
- Parameters:
binary – [in] The binary data that has already been Base64-encoded.
output – [out] The
pw::InlineString
thatbinary
is appended to.
-
template<size_t kMaxBinaryDataSizeBytes>
inline InlineString<EncodedSize(kMaxBinaryDataSizeBytes)> Encode( - span<const std::byte> binary,
Creates a
pw::InlineString<>
large enough to holdkMaxBinaryDataSizeBytes
of binary data when encoded as Base64 and encodes the provided span into it.
-
constexpr size_t MaxDecodedSize(size_t base64_size_bytes)#
Calculates the maximum size of Base64-encoded data after decoding.
- Parameters:
base64_size_bytes – [in] The size of the Base64-encoded data.
- Pre:
base64_size_bytes
must be a multiple of 4, since Base64 encodes 3-byte groups into 4-character strings.- 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 byMaxDecodedSize()
.
-
inline size_t Decode(std::string_view base64, void *output)#
Decodes the provided Base64 data into raw binary.
- Parameters:
base64 – [in] The 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=
.output – [out] The 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.
- Pre:
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.
- Returns:
The number of bytes that were decoded.
-
size_t 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.
-
template<typename T>
inline void DecodeInPlace(InlineBasicString<T> &buffer)# Decodes a
pw::InlineString<>
in place.
-
inline bool IsValid(std::string_view base64)#
- Parameters:
base64 – [in] The 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.
-
inline bool IsValidChar(char base64)#
- Parameters:
base64 – [in] The character to check. Can be encoded with either the standard (
+/
) or URL-safe (-_
) alphabet.- Returns:
true
if the provided character is a valid Base64 character.
-
constexpr size_t EncodedSize(size_t binary_size_bytes)#
Rust API reference#
pw_base64
’s Rust API is documented in the
pw_base64 crate’s docs.