Botcraft 26.3
Loading...
Searching...
No Matches
StringUtilities.cpp
Go to the documentation of this file.
3
4
5#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
6#include <array>
7#include <ctime>
8#include <iomanip>
9#endif
10
11namespace Botcraft::Utilities
12{
13 bool StartsWith(const std::string& mainStr, const std::string& toMatch)
14 {
15 return mainStr.find(toMatch) == 0;
16 }
17
18 bool EndsWith(const std::string& mainStr, const std::string& toMatch)
19 {
20 if (toMatch.size() > mainStr.size())
21 {
22 return false;
23 }
24 return std::equal(toMatch.rbegin(), toMatch.rend(), mainStr.rbegin());
25 }
26
27 bool Contains(const std::string& mainStr, const std::string& toFind)
28 {
29 return mainStr.find(toFind) != std::string::npos;
30 }
31
32 std::vector<std::string> SplitString(const std::string& s, const char delimiter)
33 {
34 std::vector<std::string> tokens;
35 std::string token;
36 std::istringstream tokenStream(s);
37 while (std::getline(tokenStream, token, delimiter))
38 {
39 tokens.push_back(token);
40 }
41 return tokens;
42 }
43
44 std::vector<std::string> SplitString(const std::string& s, const std::string& delimiter)
45 {
46 const size_t delimiter_size = delimiter.size();
47 size_t start = 0;
48 size_t end = 0;
49 std::vector<std::string> tokens;
50
51 while ((end = s.find(delimiter, start)) != std::string::npos)
52 {
53 tokens.push_back(s.substr(start, end - start));
54 start = end + delimiter_size;
55 }
56
57 tokens.push_back(s.substr(start));
58
59 return tokens;
60 }
61
62#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
63 std::vector<unsigned char> DecodeBase64(const std::string& s)
64 {
65 static std::array<unsigned char, 128> constexpr decode_table{
66 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
67 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
68 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
69 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
70 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
71 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
72 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
73 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64
74 };
75
76 if (s.size() == 0)
77 {
78 return {};
79 }
80
81 if (s.size() % 4 != 0)
82 {
83 LOG_ERROR("Base64 string size not a multiple of 4");
84 return {};
85 }
86
87 const size_t output_size = s.size() / 4 * 3;
88
89 std::vector<unsigned char> output;
90 output.reserve(output_size);
91
92 for (int i = 0; i < s.size(); i += 4)
93 {
94 const unsigned char c1 = s[i + 0] == '=' ? 0 : decode_table[static_cast<unsigned char>(s[i + 0])];
95 const unsigned char c2 = s[i + 1] == '=' ? 0 : decode_table[static_cast<unsigned char>(s[i + 1])];
96 const unsigned char c3 = s[i + 2] == '=' ? 0 : decode_table[static_cast<unsigned char>(s[i + 2])];
97 const unsigned char c4 = s[i + 3] == '=' ? 0 : decode_table[static_cast<unsigned char>(s[i + 3])];
98
99 const unsigned int bytes = (c1 << 18) | (c2 << 12) | (c3 << 6) | (c4 << 0);
100
101 output.push_back((bytes >> 16) & 0xFF);
102 output.push_back((bytes >> 8) & 0xFF);
103 output.push_back((bytes >> 0) & 0xFF);
104 }
105
106 // If last character is a padding, we'll have one less byte
107 if (s.back() == '=')
108 {
109 output.pop_back();
110 }
111 // If second last character is a padding, we'll have two less bytes
112 if (s[s.size() - 2] == '=')
113 {
114 output.pop_back();
115 }
116
117 return output;
118 }
119
120 std::vector<unsigned char> RSAToBytes(const std::string& s)
121 {
122 const std::vector<std::string> splitted = SplitString(s, '\n');
123 std::string base64 = "";
124 for (int i = 1; i < splitted.size() - 1; ++i)
125 {
126 base64 += splitted[i];
127 }
128 return DecodeBase64(base64);
129 }
130#endif
131} // Botcraft::Utilities
#define LOG_ERROR(osstream)
Definition Logger.hpp:45
bool Contains(const std::string &mainStr, const std::string &toFind)
bool StartsWith(const std::string &mainStr, const std::string &toMatch)
std::vector< unsigned char > RSAToBytes(const std::string &s)
std::vector< unsigned char > DecodeBase64(const std::string &s)
std::vector< std::string > SplitString(const std::string &s, const char delimiter)
bool EndsWith(const std::string &mainStr, const std::string &toMatch)