-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.hh
32 lines (26 loc) · 1006 Bytes
/
base64.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <span>
#include <vector>
#include "ryml.hpp"
#include "ryml_init.hh"
namespace MetaModule
{
struct Base64 {
// Patch file yaml -> decode -> raw bytes for module
static std::vector<uint8_t> decode(std::string_view base64_string) {
RymlInit::init_once();
auto needed_size = c4::base64_decode({base64_string.data(), base64_string.size()}, {});
std::vector<uint8_t> decoded_data(needed_size);
c4::base64_decode({base64_string.data(), base64_string.size()}, {decoded_data.data(), decoded_data.size()});
return decoded_data;
}
// Raw bytes from module -> encode -> patch file yaml
static std::string encode(std::span<const uint8_t> raw_data) {
RymlInit::init_once();
// Encode bytes into base64
auto needed_size = c4::base64_encode({}, {raw_data.data(), raw_data.size()});
std::string encoded_data(needed_size, '\0');
c4::base64_encode({encoded_data.data(), encoded_data.size()}, {raw_data.data(), raw_data.size()});
return encoded_data;
}
};
} // namespace MetaModule