codex
Loading...
Searching...
No Matches
base64.hpp
Go to the documentation of this file.
1#ifndef CODEX_BASE64_BASE64_H
2#define CODEX_BASE64_BASE64_H
3
4#include <algorithm>
5#include <iterator>
6#include <limits>
7#include <string_view>
8
9using namespace std::literals;
10
17consteval char lut_idx(std::string_view lut, char c) {
18 const auto p = lut.data();
19 const auto n = static_cast<char>(lut.size());
20 return static_cast<char>(std::distance(p, std::find(p, p + n, c)));
21}
22
30consteval auto gen_rlut(std::string_view lut) {
31 std::array<char, std::numeric_limits<unsigned char>::max()> ret = {};
32 for(std::size_t i = 0; i < ret.size(); ++i)
33 ret[i] = lut_idx(lut, static_cast<char>(i));
34 ret['='] = 0;
35 return ret;
36}
37
41class Base64 {
45 static constexpr auto lut =
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"sv;
51 static constexpr auto rlut = gen_rlut(lut);
52public:
60 static void encode(std::string_view src, char *dst);
66 static char *decode(std::string_view src, char *dst);
67};
68
69#endif
Encodes and decodes byte streams using base64.
Definition base64.hpp:41
static constexpr auto rlut
Reverse look-up table for encoding, maps every possible character value to a byte or zero.
Definition base64.hpp:51
static char * decode(std::string_view src, char *dst)
Decodes a byte stream.
static constexpr auto lut
Look-up table for encoding, maps input 6-bit values to output characters.
Definition base64.hpp:45
static void encode(std::string_view src, char *dst)
Encodes a byte stream.
#define p()
Definition std2.c:11
Definition mult_inh.c:27
consteval auto gen_rlut(std::string_view lut)
Creates an array that contains the position of every byte in lut.
Definition base64.hpp:30
consteval char lut_idx(std::string_view lut, char c)
Performs a reverse look-up.
Definition base64.hpp:17