44template <
typename InputIterator>
46 static const std::string kBase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "abcdefghijklmnopqrstuvwxyz"
52 while (first != last) {
53 auto c0 =
static_cast<uint8_t
>(*first++);
55 dst.push_back(kBase64[
static_cast<std::string::size_type
>(c0 >> 2)]);
56 dst.push_back(kBase64[
static_cast<std::string::size_type
>(((c0 & 0x3) << 4))]);
60 auto c1 =
static_cast<uint8_t
>(*first++);
62 dst.push_back(kBase64[
static_cast<std::string::size_type
>(c0 >> 2)]);
63 dst.push_back(kBase64[
static_cast<std::string::size_type
>(((c0 & 0x3) << 4) | (c1 >> 4))]);
64 dst.push_back(kBase64[
static_cast<std::string::size_type
>(((c1 & 0xf) << 2))]);
67 auto c2 =
static_cast<uint8_t
>(*first++);
68 dst.push_back(kBase64[
static_cast<std::string::size_type
>(c0 >> 2)]);
69 dst.push_back(kBase64[
static_cast<std::string::size_type
>(((c0 & 0x3) << 4) | (c1 >> 4))]);
70 dst.push_back(kBase64[
static_cast<std::string::size_type
>(((c1 & 0xf) << 2) | (c2 >> 6))]);
71 dst.push_back(kBase64[
static_cast<std::string::size_type
>(c2 & 0x3f)]);
88template <
typename OutputIterator,
typename Type>
90 static const std::string kBase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
91 "abcdefghijklmnopqrstuvwxyz"
94 std::string src_trimmed;
95 std::copy_if(src.begin(), src.end(), std::back_inserter<std::string>(src_trimmed),
96 [](
char c) { return !std::isspace(c, std::locale::classic()); });
98 if (src_trimmed.size() % 4 != 0)
99 throw FormatError(
"Base64 data must be a multiple of four in size.");
104 for (
char c : src_trimmed) {
106 *result =
static_cast<Type
>(bits24 >> 16);
109 *result =
static_cast<Type
>((bits24 >> 8) & 0xff);
113 *result =
static_cast<Type
>(bits24 & 0xff);
119 std::size_t v = kBase64.find(c);
120 if (v == std::string::npos)
121 throw FormatError(
"Illegal character in base64 stream.");
123 bits24 |=
static_cast<uint32_t
>(v) << (18 - i++ * 6);
125 *result =
static_cast<Type
>(bits24 >> 16);
127 *result =
static_cast<Type
>((bits24 >> 8) & 0xff);
129 *result =
static_cast<Type
>(bits24 & 0xff);
146 base64_decode<std::back_insert_iterator<std::string>,
char>(src, std::back_inserter(dst));
void base64_decode(std::string_view src, OutputIterator result)
Decodes a base64 string into an output sequence.
Definition base64.hh:89
std::string base64_encode(InputIterator first, InputIterator last)
Encodes a range of bytes into a base64 string.
Definition base64.hh:45
Exception types thrown by the library.