diff options
Diffstat (limited to 'Swiften/StringCodecs/Base64.cpp')
-rw-r--r-- | Swiften/StringCodecs/Base64.cpp | 180 |
1 files changed, 76 insertions, 104 deletions
diff --git a/Swiften/StringCodecs/Base64.cpp b/Swiften/StringCodecs/Base64.cpp index e4eaa4e..e1d70a0 100644 --- a/Swiften/StringCodecs/Base64.cpp +++ b/Swiften/StringCodecs/Base64.cpp @@ -1,129 +1,101 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ -#include <boost/numeric/conversion/cast.hpp> - -#include <algorithm> - #include <Swiften/StringCodecs/Base64.h> -#include <Swiften/Base/Algorithm.h> -namespace Swift { +#pragma clang diagnostic ignored "-Wconversion" -#pragma GCC diagnostic ignored "-Wold-style-cast" +using namespace Swift; namespace { - template<typename TargetType, typename SourceType> - TargetType base64Encode(const SourceType& s) { - int i; - int len = s.size(); - char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - int a, b, c; - - TargetType p; - p.resize((len+2)/3*4); - int at = 0; - for( i = 0; i < len; i += 3 ) { - a = ((unsigned char) (s[i]) & 3) << 4; - if(i + 1 < len) { - a += (unsigned char) (s[i + 1]) >> 4; - b = ((unsigned char) (s[i + 1]) & 0xF) << 2; - if(i + 2 < len) { - b += (unsigned char) (s[i + 2]) >> 6; - c = (unsigned char) (s[i + 2]) & 0x3F; - } - else - c = 64; - } - else { - b = c = 64; - } + const char* encodeMap = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + const unsigned char decodeMap[255] = { + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 62, 255, 255, 255, 63, + 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 255, 255, 255, 255, 255, 255, + 255, 0, 1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, + 23, 24, 25, 255, 255, 255, 255, 255, + 255, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 255, 255, 255, 255, 255 + }; - p[at++] = tbl[(unsigned char) (s[i]) >> 2]; - p[at++] = tbl[a]; - p[at++] = tbl[b]; - p[at++] = tbl[c]; + template<typename ResultType, typename InputType> + ResultType encodeDetail(const InputType& input) { + ResultType result; + size_t i = 0; + for (; i < (input.size()/3)*3; i += 3) { + unsigned int c = input[i+2] | (input[i+1]<<8) | (input[i]<<16); + result.push_back(encodeMap[(c&0xFC0000)>>18]); + result.push_back(encodeMap[(c&0x3F000)>>12]); + result.push_back(encodeMap[(c&0xFC0)>>6]); + result.push_back(encodeMap[c&0x3F]); + } + if (input.size() % 3 == 2) { + unsigned int c = (input[i+1]<<8) | (input[i]<<16); + result.push_back(encodeMap[(c&0xFC0000)>>18]); + result.push_back(encodeMap[(c&0x3F000)>>12]); + result.push_back(encodeMap[(c&0xFC0)>>6]); + result.push_back('='); + } + else if (input.size() % 3 == 1) { + unsigned int c = input[i]<<16; + result.push_back(encodeMap[(c&0xFC0000)>>18]); + result.push_back(encodeMap[(c&0x3F000)>>12]); + result.push_back('='); + result.push_back('='); } - return p; + return result; } } -std::string Base64::encode(const ByteArray &s) { - return base64Encode<std::string, ByteArray>(s); + +std::string Base64::encode(const ByteArray& s) { + return encodeDetail<std::string>(s); } -SafeByteArray Base64::encode(const SafeByteArray &s) { - return base64Encode<SafeByteArray, SafeByteArray>(s); +SafeByteArray Base64::encode(const SafeByteArray& s) { + return encodeDetail<SafeByteArray>(s); } ByteArray Base64::decode(const std::string& input) { - std::string inputWithoutNewlines(input); - erase(inputWithoutNewlines, '\n'); - - const std::string& s = inputWithoutNewlines; - ByteArray p; - - // -1 specifies invalid - // 64 specifies eof - // everything else specifies data - - char tbl[] = { - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, - 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,64,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, - 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, - -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, - 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - }; + ByteArray result; - // this should be a multiple of 4 - int len = s.size(); - - if(len % 4) { - return p; + if (input.size() % 4) { + return ByteArray(); } - - p.resize(len / 4 * 3); - - int i; - int at = 0; - - int a, b, c, d; - c = d = 0; - - for( i = 0; i < len; i += 4 ) { - a = tbl[boost::numeric_cast<int>(s[i])]; - b = tbl[boost::numeric_cast<int>(s[i + 1])]; - c = tbl[boost::numeric_cast<int>(s[i + 2])]; - d = tbl[boost::numeric_cast<int>(s[i + 3])]; - if((a == 64 || b == 64) || (a < 0 || b < 0 || c < 0 || d < 0)) { - p.resize(0); - return p; + for (size_t i = 0; i < input.size(); i += 4) { + unsigned char c1 = input[i+0]; + unsigned char c2 = input[i+1]; + unsigned char c3 = input[i+2]; + unsigned char c4 = input[i+3]; + if (c3 == '=') { + unsigned int c = (((decodeMap[c1]<<6)|decodeMap[c2])&0xFF0)>>4; + result.push_back(c); + } + else if (c4 == '=') { + unsigned int c = (((decodeMap[c1]<<12)|(decodeMap[c2]<<6)|decodeMap[c3])&0x3FFFC)>>2; + result.push_back((c&0xFF00) >> 8); + result.push_back(c&0xFF); + } + else { + unsigned int c = (decodeMap[c1]<<18) | (decodeMap[c2]<<12) | (decodeMap[c3]<<6) | decodeMap[c4]; + result.push_back((c&0xFF0000) >> 16); + result.push_back((c&0xFF00) >> 8); + result.push_back(c&0xFF); } - p[at++] = ((a & 0x3F) << 2) | ((b >> 4) & 0x03); - p[at++] = ((b & 0x0F) << 4) | ((c >> 2) & 0x0F); - p[at++] = ((c & 0x03) << 6) | ((d >> 0) & 0x3F); } - - if(c & 64) - p.resize(at - 2); - else if(d & 64) - p.resize(at - 1); - - return p; -} - + return result; } |