diff options
author | madmaxoft <github@xoft.cz> | 2013-10-04 20:28:30 +0200 |
---|---|---|
committer | madmaxoft <github@xoft.cz> | 2013-10-04 20:28:30 +0200 |
commit | db3d83b38dd61b90466a0721fa9104e742f3fb8b (patch) | |
tree | ba45a5a87eb7a688cbbe05587abaf1bea667371f /source/StringUtils.cpp | |
parent | HTTP Server can now parse multipart/form-data forms; better architecture. (diff) | |
download | cuberite-db3d83b38dd61b90466a0721fa9104e742f3fb8b.tar cuberite-db3d83b38dd61b90466a0721fa9104e742f3fb8b.tar.gz cuberite-db3d83b38dd61b90466a0721fa9104e742f3fb8b.tar.bz2 cuberite-db3d83b38dd61b90466a0721fa9104e742f3fb8b.tar.lz cuberite-db3d83b38dd61b90466a0721fa9104e742f3fb8b.tar.xz cuberite-db3d83b38dd61b90466a0721fa9104e742f3fb8b.tar.zst cuberite-db3d83b38dd61b90466a0721fa9104e742f3fb8b.zip |
Diffstat (limited to 'source/StringUtils.cpp')
-rw-r--r-- | source/StringUtils.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp index c62bb3acb..530eda086 100644 --- a/source/StringUtils.cpp +++ b/source/StringUtils.cpp @@ -745,3 +745,71 @@ AString ReplaceAllCharOccurrences(const AString & a_String, char a_From, char a_ + +/// Converts one Hex character in a Base64 encoding into the data value +static inline int UnBase64(char c) +{ + if (c >='A' && c <= 'Z') + { + return c - 'A'; + } + if (c >='a' && c <= 'z') + { + return c - 'a' + 26; + } + if (c >= '0' && c <= '9') + { + return c - '0' + 52; + } + if (c == '+') + { + return 62; + } + if (c == '/') + { + return 63; + } + if (c == '=') + { + return -1; + } + return -2; +} + + + + + +AString Base64Decode(const AString & a_Base64String) +{ + AString res; + size_t i, len = a_Base64String.size(); + int o, c; + res.resize((len * 4) / 3 + 5, 0); // Approximate the upper bound on the result length + for (o = 0, i = 0; i < len; i++) + { + c = UnBase64(a_Base64String[i]); + if (c >= 0) + { + switch (o & 7) + { + case 0: res[o >> 3] |= (c << 2); break; + case 6: res[o >> 3] |= (c >> 4); res[(o >> 3) + 1] |= (c << 4); break; + case 4: res[o >> 3] |= (c >> 2); res[(o >> 3) + 1] |= (c << 6); break; + case 2: res[o >> 3] |= c; break; + } + o += 6; + } + if (c == -1) + { + // Error while decoding, invalid input. Return as much as we've decoded: + res.resize(o >> 3); + return ERROR_SUCCESS; + } + } + res.resize(o >> 3); + return res;} + + + + |