22 std::string in_padded;
25 if (in_padded.length() % 4 == 2)
27 in_padded = in +
"==";
30 else if (in_padded.length() % 4 == 3)
40 std::vector<int> T(256, -1);
41 for (
int i = 0; i < 64; i++)
42 T[
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] =
45 int val = 0, valb = -8;
46 for (
unsigned char c : in_padded)
50 val = (val << 6) + T[c];
54 out.push_back(
char((val >> valb) & 0xFF));
58 return std::vector<unsigned char>(out.begin(), out.end());
68 inline unsigned long getValueAt(std::vector<unsigned char>& bytes,
69 const unsigned int startByteIndex,
70 const unsigned int endByteIndex)
72 unsigned long out = 0;
74 for (
int i = startByteIndex; i <= endByteIndex; i++)
76 if (i >= bytes.size())
77 throw std::out_of_range(
"Out of bounds error while decoding b64.");
78 out = out | ((0x0000000000000000 | bytes[i]) << 8 * (i - startByteIndex));
91 inline long convertValue(
const unsigned long value,
const unsigned int size,
95 return static_cast<unsigned int>(value);
96 unsigned long mask = 0x0000000000000080;
97 mask = mask << (8 * (size - 1));
99 for (
int i = 0; i < 64 - (size * 8); i++)
101 mask = mask | (mask << 1);
135 const unsigned int startByteIndex,
136 const unsigned int endByteIndex,
137 const unsigned int size,
bool isSigned)
140 unsigned long value =
getValueAt(bytes, startByteIndex, endByteIndex);
unsigned long getValueAt(std::vector< unsigned char > &bytes, const unsigned int startByteIndex, const unsigned int endByteIndex)
Concatenates bytes from index a to index b of a byte array.
long convertValue(const unsigned long value, const unsigned int size, bool isSigned)
Converts a 32bit signed/unsigned integer from its bit representation to signed 64bit integer...
std::vector< unsigned char > base64_to_binary(const std::string &in)
Decodes a base64 encoded string to bytes.
long base64_decode(const std::string &in, const unsigned int startByteIndex, const unsigned int endByteIndex, const unsigned int size, bool isSigned)
Function decodes a Base64 string and returns the decoded string (or only parts of it) up to a maximum...