A single header C++17 compatible base64 library. It should compile with any C++17 compliant compiler without any additional source or binary dependencies. SSSE3 and AVX2 codepaths can be used to accelerate processing.
The library provides two low-level functions for handling encoding and decoding of base64 buffers. They accept raw buffers, including lengths, and decode directly into them. Utility methods are provided for calculating the sizes of buffers.
// Input data
const uint8_t* data = ...;
size_t data_length = ...;
// Encoded buffer
size_t buf_length = base64::get_encoded_length(data_length);
auto buf = std::make_unique<uint8_t[]>(buf_length);
base64::encode(
data, data_length,
buf.get(), buf_length
);// Encoded data
const uint8_t* data = ...;
size_t data_length = ...;
// Decoded buffer
size_t buf_length = base64::get_decoded_length(data, data_length);
auto buf = std::make_unique<uint8_t[]>(buf_length);
base64::decode(
data, data_length,
buf.get(), buf_length
);Base64 padding bytes are encoded by default but can be optionally controlled by setting the padding parameter on the get_encoded_length and encode methods.
// Padded encoding
size_t padded_length = base64::get_encoded_length(data_length);
base64::encode(data, data_length, buf, padded_length);
// Unpadded encoding
size_t unpadded_length = base64::get_encoded_length(data_length, false);
base64::encode(data, data_length, buf, unpadded_length, false);Decoding will automatically handle padding bytes if they are present but will still function correctly without them. The get_decoded_length method requires the full encoded buffer so padding bytes can be detected.
// Padded data
assert(base64::get_decoded_length("YWJjZA==", 8) == 4);
// Unpadded data
assert(base64::get_decoded_length("YWJjZA", 6) == 4);By default the fastest codepath is chosen at runtime (AVX2 > SSSE3 > Basic), though this can be overriden by providing a specific codepath to the encode and decode methods. The Basic implementation will work on any architecture but will not be optimal. If your target architecture supports AVX2 or SSSE3 instructions then an alternative implementation can be used instead.
The alternative implementations are based on work by Wojciech Muła: encoding, decoding.