#pragma once #include #include #include #include #include #include #include namespace Void::Encoding::Base64 { namespace Detail { constexpr std::string_view alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; constexpr std::string_view alphabet_url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; [[nodiscard]] constexpr std::array make_decode_table(std::string_view alpha) noexcept { std::array t{}; t.fill(-1); for (std::size_t i = 0; i < 64; ++i) { t[static_cast(alpha[i])] = static_cast(i); } t[static_cast('=')] = -2; // padding sentinel return t; } } // namespace Detail /// Encode bytes to standard Base64 (with padding). [[nodiscard]] inline std::string encode(std::span 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(data[i]); const auto b = static_cast(data[i + 1]); const auto c = static_cast(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(data[i]); const auto b = static_cast(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(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 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(data[i]); const auto b = static_cast(data[i + 1]); const auto c = static_cast(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(data[i]); const auto b = static_cast(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(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 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(encoded); const std::int8_t value = table[character]; if (value < 0) { return std::nullopt; } buffer = (buffer << 6) | static_cast(value); bits += 6; if (bits >= 8) { bits -= 8; out += static_cast((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 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(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(value); ++symbols; bits += 6; if (bits >= 8) { bits -= 8; out += static_cast((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> 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{bytes.begin(), bytes.end()}; } } // namespace Void::Encoding::Base64