Skip to content

Commit f66a310

Browse files
Move base64_encode to Utilities.h
1 parent 1f0003a commit f66a310

4 files changed

Lines changed: 41 additions & 31 deletions

File tree

CSAPI/CSAPI.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
<ClInclude Include="SystemsAPI.h" />
198198
<ClInclude Include="Util\JsonUtils.h" />
199199
<ClInclude Include="Util\TimeUtils.h" />
200+
<ClInclude Include="Util\Utilities.h" />
200201
</ItemGroup>
201202
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
202203
<ImportGroup Label="ExtensionTargets">

CSAPI/CSAPI.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,8 @@
222222
<ClInclude Include="Util\TimeUtils.h">
223223
<Filter>Header Files\Util</Filter>
224224
</ClInclude>
225+
<ClInclude Include="Util\Utilities.h">
226+
<Filter>Header Files\Util</Filter>
227+
</ClInclude>
225228
</ItemGroup>
226229
</Project>

CSAPI/ConnectedSystemsAPI.h

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "ObservationsAPI.h"
77
#include "SystemsAPI.h"
88
#include "ControlStreamsAPI.h"
9+
#include "Util/Utilities.h"
910

1011
namespace ConnectedSystemsAPI {
1112
/// <summary>
@@ -43,7 +44,7 @@ namespace ConnectedSystemsAPI {
4344
/// <param name="username">Username for Basic authentication</param>
4445
/// <param name="password">Password for Basic authentication</param>
4546
ConSysAPI(const std::string& apiRoot, const std::string& username, const std::string& password)
46-
: ConSysAPI(apiRoot, base64_encode(username + ":" + password), true) {
47+
: ConSysAPI(apiRoot, Utilities::base64_encode(username + ":" + password), true) {
4748
}
4849

4950
// Overload to accept C-style string literals to avoid list-initialization narrowing issues
@@ -63,35 +64,5 @@ namespace ConnectedSystemsAPI {
6364
DataStreamsAPI& getDataStreamsAPI() { return dataStreamsAPI; }
6465
ObservationsAPI& getObservationsAPI() { return observationsAPI; }
6566
ControlStreamsAPI& getControlStreamsAPI() { return controlStreamsAPI; }
66-
private:
67-
static std::string base64_encode(const std::string& in) {
68-
static const char base64_chars[] =
69-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
70-
"abcdefghijklmnopqrstuvwxyz"
71-
"0123456789+/";
72-
73-
std::string out;
74-
out.reserve(((in.size() + 2) / 3) * 4);
75-
76-
unsigned int val = 0;
77-
int valb = -6;
78-
79-
for (unsigned char c : in) {
80-
val = (val << 8) + c;
81-
valb += 8;
82-
while (valb >= 0) {
83-
out.push_back(base64_chars[(val >> valb) & 0x3F]);
84-
valb -= 6;
85-
}
86-
}
87-
88-
if (valb > -6)
89-
out.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
90-
91-
while (out.size() % 4)
92-
out.push_back('=');
93-
94-
return out;
95-
}
9667
};
9768
}

CSAPI/Util/Utilities.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace ConnectedSystemsAPI::Utilities {
6+
inline std::string base64_encode(const std::string& in) {
7+
static const char base64_chars[] =
8+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9+
"abcdefghijklmnopqrstuvwxyz"
10+
"0123456789+/";
11+
12+
std::string out;
13+
out.reserve(((in.size() + 2) / 3) * 4);
14+
15+
unsigned int val = 0;
16+
int valb = -6;
17+
18+
for (unsigned char c : in) {
19+
val = (val << 8) + c;
20+
valb += 8;
21+
while (valb >= 0) {
22+
out.push_back(base64_chars[(val >> valb) & 0x3F]);
23+
valb -= 6;
24+
}
25+
}
26+
27+
if (valb > -6)
28+
out.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
29+
30+
while (out.size() % 4)
31+
out.push_back('=');
32+
33+
return out;
34+
}
35+
}

0 commit comments

Comments
 (0)