JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
Loading...
Searching...
No Matches
base64.h File Reference

Compile-time Base64 and Base64URL codec for the jh::meta subsystem. More...

#include <cstdint>
#include "jh/pods/array.h"
#include "jh/metax/t_str.h"
#include "jh/detail/base64_common.h"

Go to the source code of this file.

Namespaces

namespace  jh::meta
 Aggregated entry point for compile-time metaprogramming utilities.

Functions

template<TStr S>
constexpr auto jh::meta::decode_base64 ()
 Decode a Base64-encoded TStr literal at compile time.
template<TStr S>
constexpr auto jh::meta::decode_base64url ()
 Decode a Base64URL-encoded TStr literal at compile time.
template<std::uint16_t N>
constexpr auto jh::meta::encode_base64 (const jh::pod::array< std::uint8_t, N > &raw)
 Encode a byte buffer into a Base64 literal at compile time.
template<std::uint16_t N, class PadT = std::false_type>
constexpr auto jh::meta::encode_base64url (const jh::pod::array< std::uint8_t, N > &raw, PadT={})
 Encode a byte buffer into a Base64URL literal at compile time.

Detailed Description

Compile-time Base64 and Base64URL codec for the jh::meta subsystem.

Author
JeongHan-Bae <mastropseudo@gmail.com>

This header provides a fully constexpr-capable Base64 and Base64URL codec that operates entirely at compile time. Encoded text is supplied as a non-type template parameter (NTTP) through the TStr compile-time string type.
The jh::meta subsystem focuses on compile-time reflection, static string manipulation, and deterministic constexpr evaluation. Its Base64 facilities allow encoded data to be decoded or generated during compilation, enabling static asset embedding, protocol constant generation, and zero-runtime preprocessing.

Design overview

  • Implements Base64 (RFC 4648 §4) with padding.
  • Implements Base64URL (RFC 4648 §5) with optional padding.
  • All constraints (is_base64, is_base64url) are validated at compile time.
  • Decode functions return jh::pod::array<std::uint8_t, N>.
  • Encode functions return t_str<M> with a built-in null terminator.
  • Supports round-trip transformations between t_str and byte buffers.

Usage

Compile-time decoding using a literal NTTP:

constexpr auto decoded = jh::meta::decode_base64<"Qm9i">(); // "Bob"
constexpr auto decode_base64()
Decode a Base64-encoded TStr literal at compile time.
Definition base64.h:199

Compile-time decoding using a macro-defined literal:

#ifndef BASE64_STR_DEMO
#define BASE64_STR_DEMO "SGkh"
#endif
constexpr auto decoded = jh::meta::decode_base64<BASE64_STR_DEMO>();
// "Hi!"

Full round-trip example using t_str and byte buffers:

// original "Hello"
constexpr jh::meta::t_str str{"Hello"};
// string → bytes
constexpr auto bytes = str.to_bytes();
// bytes → base64 literal
constexpr auto encoded = jh::meta::encode_base64(bytes);
// base64 → bytes
constexpr auto decoded = jh::meta::decode_base64<encoded>();
// bytes → t_str
constexpr auto restored =
jh::meta::t_str<decoded.size() + 1>::from_bytes(decoded);
constexpr auto encode_base64(const jh::pod::array< std::uint8_t, N > &raw)
Encode a byte buffer into a Base64 literal at compile time.
Definition base64.h:292
Compile-time string wrapper for use as a non-type template parameter (NTTP).
Definition t_str.h:109
constexpr std::uint64_t size() const noexcept
Get the length of the string (excluding null terminator).
Definition t_str.h:210
constexpr jh::pod::array< std::uint8_t, N - 1 > to_bytes() const noexcept
Convert to an immutable byte buffer.
Definition t_str.h:867

Subsystem role

Within the jh::meta subsystem, this file provides the compile-time counterpart to the runtime Base64 codec used in the serialization module. While the runtime interface focuses on interoperability and safety, the compile-time interface focuses on deterministic evaluation, static transformation of encoded resources, and NTTP-based metaprogramming.

Version
1.4.x
Date
2025