-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.cpp
More file actions
113 lines (94 loc) · 3.07 KB
/
Copy pathbase64.cpp
File metadata and controls
113 lines (94 loc) · 3.07 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <brewtils/base64.h>
namespace brewtils {
namespace base64 {
static constexpr const char *BASE64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
} // namespace base64
} // namespace brewtils
std::string brewtils::base64::encode(const std::string &data) noexcept(true) {
std::string encoded;
size_t len = data.size();
size_t i = 0;
while (i < len) {
encoded += brewtils::base64::BASE64_CHARS[(data[i] >> 2) & 0x3F];
if (i + 1 < len) {
encoded += brewtils::base64::BASE64_CHARS[((data[i] & 0x3) << 4) |
((data[i + 1] >> 4) & 0xF)];
if (i + 2 < len) {
encoded += brewtils::base64::BASE64_CHARS[((data[i + 1] & 0xF) << 2) |
((data[i + 2] >> 6) & 0x3)];
encoded += brewtils::base64::BASE64_CHARS[data[i + 2] & 0x3F];
} else {
encoded += brewtils::base64::BASE64_CHARS[(data[i + 1] & 0xF) << 2];
encoded += '=';
}
} else {
encoded += brewtils::base64::BASE64_CHARS[(data[i] & 0x3) << 4];
encoded += "==";
}
i += 3;
}
return encoded;
}
std::string brewtils::base64::decode(const std::string &data) noexcept(false) {
size_t len = data.size();
if (len % 4 != 0) {
logger::error("Input is not valid Base64 encoding: " + data,
"std::string brewtils::base64::decode(const std::string "
"&data) noexcept(false)");
}
size_t padding = 0;
if (len > 0 && data[len - 1] == '=')
padding++;
if (len > 1 && data[len - 2] == '=')
padding++;
size_t decoded_size = len / 4 * 3 - padding;
std::string decoded;
decoded.reserve(decoded_size);
int num = 0;
int countBits = 0;
for (size_t i = 0; i < len; i += 4) {
num = 0, countBits = 0;
for (int j = 0; j < 4; j++) {
num = num << 6;
char currentChar = data[i + j];
if (currentChar == '=')
continue;
const char *pos =
std::strchr(brewtils::base64::BASE64_CHARS, currentChar);
if (pos == nullptr)
logger::error("Invalid Base64 character: " +
std::string(1, currentChar),
"std::string brewtils::base64::decode(const std::string "
"&data) noexcept(false)");
num |= static_cast<int>(pos - brewtils::base64::BASE64_CHARS);
countBits += 6;
}
if (data[i + 2] == '=')
countBits -= 4;
else if (data[i + 3] == '=')
countBits -= 2;
while (countBits >= 8) {
countBits -= 8;
decoded.push_back(static_cast<char>((num >> countBits) & 255));
}
}
return decoded;
}
bool brewtils::base64::isValid(const std::string &data) noexcept(true) {
size_t len = data.size();
if (len % 4 != 0)
return false;
size_t padding = 0;
if (len > 0 && data[len - 1] == '=')
padding++;
if (len > 1 && data[len - 2] == '=')
padding++;
if (len / 4 * 3 - padding == 0)
return false;
for (char c : data) {
if (!std::isalnum(c) && c != '+' && c != '/' && c != '=')
return false;
}
return true;
}