diff options
author | madmaxoft <github@xoft.cz> | 2014-01-07 12:36:36 +0100 |
---|---|---|
committer | madmaxoft <github@xoft.cz> | 2014-01-07 12:36:36 +0100 |
commit | e3bb82d95a0bf270f9d43d0504bf155b52dc516b (patch) | |
tree | b669419ae906a3ad08f695e598f661aebd36bc0d /src | |
parent | Disabled the type conversion MSVC warning. (diff) | |
download | cuberite-e3bb82d95a0bf270f9d43d0504bf155b52dc516b.tar cuberite-e3bb82d95a0bf270f9d43d0504bf155b52dc516b.tar.gz cuberite-e3bb82d95a0bf270f9d43d0504bf155b52dc516b.tar.bz2 cuberite-e3bb82d95a0bf270f9d43d0504bf155b52dc516b.tar.lz cuberite-e3bb82d95a0bf270f9d43d0504bf155b52dc516b.tar.xz cuberite-e3bb82d95a0bf270f9d43d0504bf155b52dc516b.tar.zst cuberite-e3bb82d95a0bf270f9d43d0504bf155b52dc516b.zip |
Diffstat (limited to '')
-rw-r--r-- | src/StringUtils.cpp | 48 | ||||
-rw-r--r-- | src/StringUtils.h | 3 |
2 files changed, 51 insertions, 0 deletions
diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index 5c6b99d88..8d2352331 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -765,6 +765,54 @@ AString Base64Decode(const AString & a_Base64String) +AString Base64Encode(const AString & a_Input) +{ + static const char BASE64[64] = { + '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 output; + output.resize(((a_Input.size() + 2) / 3) * 4); + + size_t output_index = 0; + size_t size_full24 = (a_Input.size() / 3) * 3; + + for (size_t i = 0; i < size_full24; i += 3) + { + output[output_index++] = BASE64[(unsigned char)a_Input[i] >> 2]; + output[output_index++] = BASE64[((unsigned char)a_Input[i] << 4 | (unsigned char)a_Input[i + 1] >> 4) & 63]; + output[output_index++] = BASE64[((unsigned char)a_Input[i + 1] << 2 | (unsigned char)a_Input[i + 2] >> 6) & 63]; + output[output_index++] = BASE64[(unsigned char)a_Input[i + 2] & 63]; + } + + if (size_full24 < a_Input.size()) + { + output[output_index++] = BASE64[(unsigned char)a_Input[size_full24] >> 2]; + if (size_full24 + 1 == a_Input.size()) + { + output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4) & 63]; + output[output_index++] = '='; + } + else + { + output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4 | (unsigned char)a_Input[size_full24 + 1] >> 4) & 63]; + output[output_index++] = BASE64[((unsigned char)a_Input[size_full24 + 1] << 2) & 63]; + } + + output[output_index++] = '='; + } + assert(output_index == output.size()); + + return output; +} + + + + + short GetBEShort(const char * a_Mem) { return (((short)a_Mem[0]) << 8) | a_Mem[1]; diff --git a/src/StringUtils.h b/src/StringUtils.h index 471e843e4..2373f3843 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -81,6 +81,9 @@ extern AString ReplaceAllCharOccurrences(const AString & a_String, char a_From, /// Decodes a Base64-encoded string into the raw data extern AString Base64Decode(const AString & a_Base64String); +/// Encodes a string into Base64 +extern AString Base64Encode(const AString & a_Input); + /// Reads two bytes from the specified memory location and interprets them as BigEndian short extern short GetBEShort(const char * a_Mem); |