Loading...
Searching...
No Matches
Base64.h
1/*
2 * Base64.h
3 *
4 * Created on: Jan 26, 2026
5 * Author: mpeura
6 */
7
8#ifndef DRAIN_UTIL_BASE64
9#define DRAIN_UTIL_BASE64
10
11#include <cstdint>
12#include <cstring>
13
14#include <drain/Enum.h>
15//#include <drain/Sprinter.h>
16
17namespace drain {
18
19
20
21class Base64 : public std::vector<uint8_t> {
22
23public:
24
25 enum NumType {
26 Int8,
27 Int16,
28 Int32,
29 Float16,
30 Float32,
31 Float64,
32 };
33
34 NumType getType(){
35 return type;
36 }
37
38
39protected:
40 NumType type;
41 std::vector<uint8_t> bytes;
42
43public:
44
45
46 // static
47 // void convert(const std::vector<float> & data, std::vector<uint8_t> & bytes);
48 template <typename T>
49 static
50 void convert(const std::vector<T> & data, std::vector<uint8_t> & bytes){
51
52 static const size_t N = sizeof(T);
53
54 bytes.reserve(data.size() * N); // float
55 for (const auto & x: data) {
56 append_value_le(bytes, x);
57 //append_float32_le(bytes, x);
58 }
59 }
60
61 inline
62 void convertFrom(const std::vector<float> & data){
63 type = Float32;
64 convert(data, *this);
65 }
66
67 inline
68 void convertFrom(const std::vector<uint16_t> & data){
69 type = Int16; // uint!
70 convert(data, *this);
71 }
72
73 /*
74 static
75 inline void append_float32_le(std::vector<uint8_t>& out, float v) {
76 uint32_t u;
77 static_assert(sizeof(float) == 4, "float must be 32-bit");
78 std::memcpy(&u, &v, 4);
79
80 // Write little-endian explicitly:
81 out.push_back(uint8_t((u >> 0) & 0xFF));
82 out.push_back(uint8_t((u >> 8) & 0xFF));
83 out.push_back(uint8_t((u >> 16) & 0xFF));
84 out.push_back(uint8_t((u >> 24) & 0xFF));
85 }
86 */
87
88 template <typename T>
89 static
90 inline void append_value_le(std::vector<uint8_t>& out, T v) {
91
92 static
93 const size_t N = sizeof(T);
94
95 uint32_t u;
96 std::memcpy(&u, &v, N);
97
98 for (size_t i=0; i<N; ++i){
99 // Write little-endian explicitly:
100 out.push_back(uint8_t(u & 0xFF));
101 u = (u>>8);
102 }
103 // Write little-endian explicitly:
104 /*
105 out.push_back(uint8_t((u >> 0) & 0xFF));
106 out.push_back(uint8_t((u >> 8) & 0xFF));
107 out.push_back(uint8_t((u >> 16) & 0xFF));
108 out.push_back(uint8_t((u >> 24) & 0xFF));
109 */
110 }
111
112
113 static
114 void base64_encode(const std::vector<uint8_t>& data, std::string & out);
115
116 /*
117 template <typename T>
118 static
119 void createArray(const Outlet & code, const std::string & variableName, NumType type, const T & sequence);
120 */
121
122protected:
123
124 static
125 void assign();
126
127};
128
129DRAIN_ENUM_DICT(Base64::NumType);
130DRAIN_ENUM_OSTREAM(Base64::NumType);
131
132/*
133template <typename T> // std::string & code // std::ostream & ostr
134void Base64::createArray(const Outlet & code, const std::string & variableName, NumType type, const T & sequence){
135 code << "const " << variableName << " = new " << type << "Array(";
136 Sprinter::toStream(code.getOStream(), sequence, Sprinter::jsLayout);
137 code << ");";
138}
139*/
140
141
142} // drain::
143
144#endif // SRC_DRAIN_UTIL_Base64_H_
Definition Base64.h:21
Definition DataSelector.cpp:1277