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
jh::serio::base64 Namespace Reference

Implements standard Base64 (RFC 4648 §4) encoding and decoding. More...

Functions

std::string encode (const std::uint8_t *data, std::size_t len)
 Encode raw binary data into a Base64 string.
std::vector< std::uint8_t > decode (const std::string &input)
 Decode a Base64 string into a byte vector.
jh::pod::bytes_view decode (const std::string &input, std::vector< std::uint8_t > &output_buffer)
 Decode a Base64-encoded string into raw bytes.
jh::pod::string_view decode (const std::string &input, std::string &output_buffer)
 Decode a Base64-encoded string into textual data.

Detailed Description

Implements standard Base64 (RFC 4648 §4) encoding and decoding.

The Base64 codec is designed for binary-to-text serialization. It ensures cross-language interoperability and safe transmission of binary payloads through text-based channels such as JSON, HTTP, XML, or protocol buffers.

This namespace provides safe, high-level wrappers over the constexpr-verified low-level primitives in jh::detail::base64_common.

Function Documentation

◆ decode() [1/3]

std::vector< std::uint8_t > jh::serio::base64::decode ( const std::string & input)
nodiscard

Decode a Base64 string into a byte vector.

Parameters
inputThe Base64-encoded input string.
Returns
A vector of decoded bytes.
Exceptions
std::runtime_errorif input is not a valid Base64 string.

◆ decode() [2/3]

jh::pod::string_view jh::serio::base64::decode ( const std::string & input,
std::string & output_buffer )

Decode a Base64-encoded string into textual data.

This overload operates in string mode.

Parameters
inputBase64-encoded input string.
output_bufferOutput buffer storing the decoded string.
Returns
A jh::pod::string_view referencing the decoded text.
Note
  • The returned view is non-owning and bound to output_buffer.
  • The view represents a lightweight, read-only window over the decoded text.
  • Available operations in string_view:
    • operator[](std::uint64_t index) — direct character access (no bounds checking).
    • sub(std::uint64_t offset, std::uint64_t length = 0) — create a substring view.
    • compare(const string_view &rhs) — perform lexical comparison (similar to strcmp).
    • starts_with(const string_view &prefix) — check if the view starts with the specified prefix.
    • ends_with(const string_view &suffix) — check if the view ends with the specified suffix.
    • find(char ch) — locate the first occurrence of a character; returns -1 if not found.
    • hash(jh::meta::c_hash hash_method = jh::meta::c_hash::fnv1a64) — compute a stable 64-bit hash of the contents.
    • operator std::string_view() or to_std() — explicit conversion to std::string_view for interoperability.
    • operator<=>(const string_view &rhs) — perform lexicographical three-way comparison (returns std::strong_ordering).
  • All string operations are constexpr and noexcept, providing zero-overhead interoperability with the standard library and compile-time evaluation where applicable.

◆ decode() [3/3]

jh::pod::bytes_view jh::serio::base64::decode ( const std::string & input,
std::vector< std::uint8_t > & output_buffer )

Decode a Base64-encoded string into raw bytes.

This overload operates in byte mode.

Parameters
inputBase64-encoded input string.
output_bufferOutput buffer storing the decoded bytes.
Returns
A jh::pod::bytes_view referencing the decoded data.
Note
  • The returned view is non-owning and bound to output_buffer.
  • The view represents a contiguous, POD-safe region of memory and provides access utilities for direct reinterpretation, inspection, and hashing.
  • Available operations in bytes_view:
    • at<T>(offset = 0) — reinterpret a subregion as a reference to T (no bounds checking).
    • fetch<T>(offset = 0) — safely fetch a pointer to T; returns nullptr if out of range.
    • clone<T>() — clone the entire view into a value of type T; returns T{} on size mismatch.
    • hash(jh::meta::c_hash hash_method = jh::meta::c_hash::fnv1a64) — compute a 64-bit hash of the byte content.
  • These operations ensure POD-safe reinterpretation and provide zero-overhead access to binary data.

◆ encode()

std::string jh::serio::base64::encode ( const std::uint8_t * data,
std::size_t len )
nodiscard

Encode raw binary data into a Base64 string.

Parameters
dataPointer to the binary buffer to encode.
lenNumber of bytes to encode.
Returns
A Base64-encoded string with padding ('=').
Exceptions
std::invalid_argumentif data is null and len > 0.
Note
This function always produces a padded Base64 output.