#124 Base64 encoding implementation

master
Tomatix 3 years ago
parent 8bae52c9e4
commit 7d955b7a8f
  1. 53
      IRCClient/Utilities.cpp
  2. 1
      IRCClient/Utilities.h

@ -6,9 +6,10 @@
*/
#include "Utilities.h"
#include <fmt/format.h>
#include <cstdint>
#include <sstream>
#include <vector>
#include <fmt/format.h>
std::pair<std::string,std::string> FormatCTCPLine(std::string line)
{
@ -142,3 +143,53 @@ bool strEquals(const std::string& l, const std::string& r)
}
);
}
std::string toBase64(const std::string& input)
{
static const char b64map[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };
std::string ret;
for (auto i = 0; i < input.size(); i += 3) {
std::int_fast32_t seq{}; // Need only 24 bits of this
std::int_fast32_t a{}, b{}, c{}, d{};
bool cBlank{}, dBlank{};
seq |= input[i];
seq <<= 8;
if (i + 1 < input.size())
seq |= input[i + 1];
else
cBlank = true;
seq <<= 8;
if (i + 2 < input.size())
seq |= input[i + 2];
else
dBlank = true;
a = (seq & 0xFC0000) >> 18;
b = (seq & 0x3F000) >> 12;
c = (seq & 0xFC0) >> 6;
d = seq & 0x3F;
ret.push_back(b64map[a]);
ret.push_back(b64map[b]);
if (cBlank)
ret.push_back('=');
else
ret.push_back(b64map[c]);
if (dBlank)
ret.push_back('=');
else
ret.push_back(b64map[d]);
}
return ret;
}

@ -20,5 +20,6 @@ std::string longIpToNormal(const std::string& longip);
std::string normalIpToLong(const std::string& normalip);
std::string concatenateModes(const std::unordered_map<char, std::string>& modes);
bool strEquals(const std::string& l, const std::string& r);
std::string toBase64(const std::string& input);
#endif //IRCUTILITIES_H

Loading…
Cancel
Save