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