FastLED 3.9.15
Loading...
Searching...
No Matches
base64.cpp.hpp
Go to the documentation of this file.
2#include "fl/stl/cstring.h"
3
4namespace fl {
5
6namespace {
7
8static const char kBase64Chars[] =
9 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
10
11static int base64_char_index(char c) {
12 if (c >= 'A' && c <= 'Z') return c - 'A';
13 if (c >= 'a' && c <= 'z') return c - 'a' + 26;
14 if (c >= '0' && c <= '9') return c - '0' + 52;
15 if (c == '+') return 62;
16 if (c == '/') return 63;
17 return -1;
18}
19
20} // namespace
21
23 fl::string out;
24 if (data.empty()) return out;
25
26 fl::size encoded_len = 4 * ((data.size() + 2) / 3);
27 out.reserve(encoded_len);
28
29 fl::size i = 0;
30 fl::size len = data.size();
31
32 while (i + 2 < len) {
33 fl::u32 triple = (fl::u32(data[i]) << 16) |
34 (fl::u32(data[i + 1]) << 8) |
35 fl::u32(data[i + 2]);
36 out += kBase64Chars[(triple >> 18) & 0x3F];
37 out += kBase64Chars[(triple >> 12) & 0x3F];
38 out += kBase64Chars[(triple >> 6) & 0x3F];
39 out += kBase64Chars[triple & 0x3F];
40 i += 3;
41 }
42
43 // Handle remaining bytes
44 if (i < len) {
45 fl::u32 triple = fl::u32(data[i]) << 16;
46 if (i + 1 < len) {
47 triple |= fl::u32(data[i + 1]) << 8;
48 }
49 out += kBase64Chars[(triple >> 18) & 0x3F];
50 out += kBase64Chars[(triple >> 12) & 0x3F];
51 if (i + 1 < len) {
52 out += kBase64Chars[(triple >> 6) & 0x3F];
53 } else {
54 out += '=';
55 }
56 out += '=';
57 }
58
59 return out;
60}
61
64 if (encoded.empty()) return out;
65
66 fl::size len = encoded.size();
67
68 // Validate length is multiple of 4
69 if (len % 4 != 0) return out;
70
71 fl::size out_len = (len / 4) * 3;
72 if (len >= 1 && encoded[len - 1] == '=') out_len--;
73 if (len >= 2 && encoded[len - 2] == '=') out_len--;
74 out.reserve(out_len);
75
76 for (fl::size i = 0; i < len; i += 4) {
77 int a = base64_char_index(encoded[i]);
78 int b = base64_char_index(encoded[i + 1]);
79 int c = (encoded[i + 2] == '=') ? 0 : base64_char_index(encoded[i + 2]);
80 int d = (encoded[i + 3] == '=') ? 0 : base64_char_index(encoded[i + 3]);
81
82 if (a < 0 || b < 0 || c < 0 || d < 0) {
83 return fl::vector<fl::u8>(); // invalid character
84 }
85
86 fl::u32 triple = (fl::u32(a) << 18) | (fl::u32(b) << 12) |
87 (fl::u32(c) << 6) | fl::u32(d);
88
89 out.push_back((triple >> 16) & 0xFF);
90 if (encoded[i + 2] != '=') {
91 out.push_back((triple >> 8) & 0xFF);
92 }
93 if (encoded[i + 3] != '=') {
94 out.push_back(triple & 0xFF);
95 }
96 }
97
98 return out;
99}
100
101} // namespace fl
void reserve(fl::size newCapacity) FL_NOEXCEPT
bool empty() const FL_NOEXCEPT
fl::size size() const FL_NOEXCEPT
constexpr bool empty() const FL_NOEXCEPT
Definition span.h:510
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
void reserve(fl::size n) FL_NOEXCEPT
Definition vector.h:591
void push_back(const T &value) FL_NOEXCEPT
Definition vector.h:624
fl::vector< fl::u8 > base64_decode(const fl::string &encoded)
fl::string base64_encode(fl::span< const fl::u8 > data)
Base definition for an LED controller.
Definition crgb.hpp:179