Files
Library/include/Void/Text/Base64.hpp
T
faumaray 3851aeb703
CI / GCC 16 / C++26 / werror (push) Failing after 3m9s
CI / GCC 16 / C++26 / sanitizers (push) Failing after 10m15s
CI / GCC 16 / C++26 / default (push) Failing after 13m1s
Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-20 01:18:53 +03:00

222 lines
6.2 KiB
C++

#pragma once
#include <array>
#include <cstdint>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Encoding::Base64
{
namespace Detail
{
constexpr std::string_view alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
constexpr std::string_view alphabet_url =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
[[nodiscard]] constexpr std::array<std::int8_t, 256>
make_decode_table(std::string_view alpha) noexcept
{
std::array<std::int8_t, 256> t{};
t.fill(-1);
for (std::size_t i = 0; i < 64; ++i) {
t[static_cast<unsigned char>(alpha[i])] =
static_cast<std::int8_t>(i);
}
t[static_cast<unsigned char>('=')] = -2; // padding sentinel
return t;
}
} // namespace Detail
/// Encode bytes to standard Base64 (with padding).
[[nodiscard]] inline std::string encode(std::span<const std::byte> data)
{
const auto& alpha = Detail::alphabet;
std::string out;
out.reserve(((data.size() + 2) / 3) * 4);
std::size_t i = 0;
for (; i + 2 < data.size(); i += 3) {
const auto a = static_cast<std::uint8_t>(data[i]);
const auto b = static_cast<std::uint8_t>(data[i + 1]);
const auto c = static_cast<std::uint8_t>(data[i + 2]);
out += alpha[a >> 2];
out += alpha[((a & 3) << 4) | (b >> 4)];
out += alpha[((b & 15) << 2) | (c >> 6)];
out += alpha[c & 63];
}
if (data.size() - i == 2) {
const auto a = static_cast<std::uint8_t>(data[i]);
const auto b = static_cast<std::uint8_t>(data[i + 1]);
out += alpha[a >> 2];
out += alpha[((a & 3) << 4) | (b >> 4)];
out += alpha[(b & 15) << 2];
out += '=';
} else if (data.size() - i == 1) {
const auto a = static_cast<std::uint8_t>(data[i]);
out += alpha[a >> 2];
out += alpha[(a & 3) << 4];
out += '=';
out += '=';
}
return out;
}
/// Encode an unsigned-byte view without forcing callers to reinterpret
/// their binary buffers as text.
[[nodiscard]] inline std::string encode(std::span<const std::uint8_t> data)
{
return encode(std::as_bytes(data));
}
/// Encode a string_view to standard Base64.
[[nodiscard]] inline std::string encode(std::string_view s)
{
return encode(std::as_bytes(std::span{s.data(), s.size()}));
}
/// Encode to URL-safe Base64 (no padding).
[[nodiscard]] inline std::string encode_url(std::string_view s)
{
const auto& alpha = Detail::alphabet_url;
auto data = std::as_bytes(std::span{s.data(), s.size()});
std::string out;
out.reserve(((data.size() + 2) / 3) * 4);
std::size_t i = 0;
for (; i + 2 < data.size(); i += 3) {
const auto a = static_cast<std::uint8_t>(data[i]);
const auto b = static_cast<std::uint8_t>(data[i + 1]);
const auto c = static_cast<std::uint8_t>(data[i + 2]);
out += alpha[a >> 2];
out += alpha[((a & 3) << 4) | (b >> 4)];
out += alpha[((b & 15) << 2) | (c >> 6)];
out += alpha[c & 63];
}
if (data.size() - i == 2) {
const auto a = static_cast<std::uint8_t>(data[i]);
const auto b = static_cast<std::uint8_t>(data[i + 1]);
out += alpha[a >> 2];
out += alpha[((a & 3) << 4) | (b >> 4)];
out += alpha[(b & 15) << 2];
} else if (data.size() - i == 1) {
const auto a = static_cast<std::uint8_t>(data[i]);
out += alpha[a >> 2];
out += alpha[(a & 3) << 4];
}
return out;
}
/// Decode canonical URL-safe Base64 without padding. Returns nullopt for
/// padding, invalid characters, impossible lengths, or non-zero trailing
/// bits.
[[nodiscard]] inline std::optional<std::string>
decode_url(std::string_view s)
{
static constexpr auto table =
Detail::make_decode_table(Detail::alphabet_url);
if (s.size() % 4 == 1 || s.find('=') != std::string_view::npos) {
return std::nullopt;
}
std::string out;
out.reserve((s.size() * 3) / 4);
std::uint32_t buffer = 0;
int bits = 0;
for (const char encoded : s) {
const auto character = static_cast<unsigned char>(encoded);
const std::int8_t value = table[character];
if (value < 0) {
return std::nullopt;
}
buffer = (buffer << 6) | static_cast<std::uint32_t>(value);
bits += 6;
if (bits >= 8) {
bits -= 8;
out += static_cast<char>((buffer >> bits) & 0xFFU);
buffer &= bits == 0 ? 0U : ((1U << bits) - 1U);
}
}
if (bits != 0 && buffer != 0) {
return std::nullopt;
}
return out;
}
/// Decode standard Base64 (with or without padding). Returns nullopt on
/// error.
[[nodiscard]] inline std::optional<std::string> decode(std::string_view s)
{
static constexpr auto table =
Detail::make_decode_table(Detail::alphabet);
std::string out;
out.reserve((s.size() / 4) * 3);
std::uint32_t buffer = 0;
int bits = 0;
std::size_t symbols = 0;
std::size_t padding = 0;
bool saw_padding = false;
for (const char encoded : s) {
const auto character = static_cast<unsigned char>(encoded);
if (character == ' ' || character == '\n' || character == '\r' ||
character == '\t') {
continue;
}
if (character == '=') {
saw_padding = true;
if (++padding > 2) {
return std::nullopt;
}
continue;
}
if (saw_padding) {
return std::nullopt;
}
const std::int8_t value = table[character];
if (value < 0) {
return std::nullopt;
}
buffer = (buffer << 6) | static_cast<std::uint32_t>(value);
++symbols;
bits += 6;
if (bits >= 8) {
bits -= 8;
out += static_cast<char>((buffer >> bits) & 0xFFU);
buffer &= bits == 0 ? 0U : ((1U << bits) - 1U);
}
}
const std::size_t remainder = symbols % 4;
if (remainder == 1) {
return std::nullopt;
}
const std::size_t expected_padding = remainder == 0 ? 0 : 4 - remainder;
if (padding != 0 && padding != expected_padding) {
return std::nullopt;
}
if (bits != 0 && buffer != 0) {
return std::nullopt;
}
return out;
}
/// Decode standard Base64 into an explicitly binary buffer. This is the
/// preferred overload for protocol, cryptographic, and file-format data.
[[nodiscard]] inline std::optional<std::vector<std::byte>>
decode_bytes(std::string_view s)
{
const auto decoded = decode(s);
if (!decoded) {
return std::nullopt;
}
const auto bytes =
std::as_bytes(std::span{decoded->data(), decoded->size()});
return std::vector<std::byte>{bytes.begin(), bytes.end()};
}
} // namespace Void::Encoding::Base64