2025-01-22 20:33:17 -08:00
|
|
|
/**
|
|
|
|
* @file mybase64.hpp
|
|
|
|
* @author Eric Mertens (emertens@gmail.com)
|
|
|
|
* @brief Base64 encoding and decoding
|
2025-02-05 09:24:47 -08:00
|
|
|
*
|
2025-01-22 20:33:17 -08:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string_view>
|
|
|
|
|
2025-02-05 09:24:47 -08:00
|
|
|
namespace mybase64 {
|
2025-01-22 20:33:17 -08:00
|
|
|
|
|
|
|
inline constexpr auto encoded_size(std::size_t len) -> std::size_t
|
|
|
|
{
|
|
|
|
return (len + 2) / 3 * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr auto decoded_size(std::size_t len) -> std::size_t
|
|
|
|
{
|
|
|
|
return (len + 3) / 4 * 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Encode a string into base64
|
2025-02-05 09:24:47 -08:00
|
|
|
*
|
2025-01-22 20:33:17 -08:00
|
|
|
* @param input input text
|
|
|
|
* @param output Target buffer for encoded value
|
|
|
|
*/
|
|
|
|
auto encode(std::string_view input, char* output) -> void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Decode a base64 encoded string
|
2025-02-05 09:24:47 -08:00
|
|
|
*
|
2025-01-22 20:33:17 -08:00
|
|
|
* @param input Base64 input text
|
|
|
|
* @param output Target buffer for decoded value
|
2025-02-05 09:24:47 -08:00
|
|
|
* @return pointer to end of output on success
|
2025-01-22 20:33:17 -08:00
|
|
|
*/
|
2025-02-05 09:24:47 -08:00
|
|
|
auto decode(std::string_view input, char* output) -> char*;
|
2025-01-22 20:33:17 -08:00
|
|
|
|
|
|
|
} // namespace
|