提交 3b39ca9e authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

wip: C++ Base64 (working but must expose a more C++ API)

上级 7077a0dc
...@@ -295,9 +295,10 @@ namespace Utils ...@@ -295,9 +295,10 @@ namespace Utils
{ {
public: public:
static void ToLowerCase(std::string& str); static void ToLowerCase(std::string& str);
static const std::string Base64Encode(const uint8_t* data, size_t len); static unsigned char* Base64Encode(const unsigned char* data, size_t len, size_t* outLen);
static const std::string Base64Encode(std::string& data); static unsigned char* Base64Encode(const std::string& data, size_t* outLen);
static const uint8_t* Base64Decode(const std::string& str); static unsigned char* Base64Decode(const unsigned char* data, size_t len, size_t* outLen);
static unsigned char* Base64Decode(const std::string& data, size_t* outLen);
}; };
inline void String::ToLowerCase(std::string& str) inline void String::ToLowerCase(std::string& str)
......
/*
* Code from http://web.mit.edu/freebsd/head/contrib/wpa/src/utils/base64.c.
*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#define MS_CLASS "Utils::String" #define MS_CLASS "Utils::String"
// #define MS_LOG_DEV_LEVEL 3 // #define MS_LOG_DEV_LEVEL 3
#include "Logger.hpp" #include "Logger.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include <cstring> // std::memset()
static const char* B64Chars = /* Static. */
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// clang-format off static constexpr size_t BufferOutSize{ 65536 };
static const int B64Index[256] = static unsigned char BufferOut[BufferOutSize];
{ static const unsigned char Base64Table[65] =
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 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, 0, 0, 0, 0, 63,
0, 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
};
// clang-format on
namespace Utils namespace Utils
{ {
const std::string Utils::String::Base64Encode(const uint8_t* data, size_t len) /**
* @param data - Data to be encoded.
* @param len - Length of the data to be encoded.
* @param outLen - Pointer to output length variable.
*
* @returns Buffer of outLen bytes of encoded data, or nulllptr on failure.
*
* Returned buffer is nul terminated to make it easier to use as a C string.
* The nul terminator is not included in outLen.
*/
unsigned char* Utils::String::Base64Encode(const unsigned char* data, size_t len, size_t* outLen)
{ {
MS_TRACE(); MS_TRACE();
std::string result((len + 2) / 3 * 4, '='); unsigned char* out = BufferOut;
const char* p = reinterpret_cast<const char*>(data); unsigned char* pos;
char* str = &result[0]; const unsigned char* end;
size_t j{ 0 }; const unsigned char* in;
size_t pad = len % 3; size_t olen;
const size_t last = len - pad; int lineLen;
for (size_t i{ 0 }; i < last; i += 3) olen = len * 4 / 3 + 4; // 3-byte blocks to 4-byte.
{ olen += olen / 72; // line feeds.
int n = static_cast<int>(p[i]) << 16 | static_cast<int>(p[i + 1]) << 8 | p[i + 2]; olen++; // Nul termination.
if (olen < len)
return nullptr; // Integer overflow.
str[j++] = B64Chars[n >> 18]; end = data + len;
str[j++] = B64Chars[n >> 12 & 0x3F]; in = data;
str[j++] = B64Chars[n >> 6 & 0x3F]; pos = out;
str[j++] = B64Chars[n & 0x3F]; lineLen = 0;
while (end - in >= 3)
{
*pos++ = Base64Table[in[0] >> 2];
*pos++ = Base64Table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = Base64Table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
*pos++ = Base64Table[in[2] & 0x3f];
in += 3;
lineLen += 4;
if (lineLen >= 72)
{
*pos++ = '\n';
lineLen = 0;
}
} }
if (pad) if (end - in)
{ {
int n = --pad ? static_cast<int>(p[last]) << 8 | p[last + 1] : p[last]; *pos++ = Base64Table[in[0] >> 2];
str[j++] = B64Chars[pad ? n >> 10 & 0x3F : n >> 2]; if (end - in == 1)
str[j++] = B64Chars[pad ? n >> 4 & 0x03F : n << 4 & 0x3F]; {
str[j++] = pad ? B64Chars[n << 2 & 0x3F] : '='; *pos++ = Base64Table[(in[0] & 0x03) << 4];
*pos++ = '=';
}
else
{
*pos++ = Base64Table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = Base64Table[(in[1] & 0x0f) << 2];
}
*pos++ = '=';
lineLen += 4;
} }
return result; if (lineLen)
*pos++ = '\n';
*pos = '\0';
if (outLen)
*outLen = pos - out;
return out;
} }
const std::string Utils::String::Base64Encode(std::string& data) unsigned char* Utils::String::Base64Encode(const std::string& data, size_t* outLen)
{ {
MS_TRACE(); MS_TRACE();
return Base64Encode(reinterpret_cast<const uint8_t*>(data.c_str()), data.size()); return Base64Encode(reinterpret_cast<const unsigned char*>(data.c_str()), data.size(), outLen);
} }
const uint8_t* Utils::String::Base64Decode(const std::string& str) /**
* @param data - Data to be decoded.
* @param len - Length of the data to be decoded.
* @param outLen - Pointer to output length variable.
*
* @returns Buffer of outLen bytes of decoded data, or nullptr on failure.
*/
unsigned char* Utils::String::Base64Decode(const unsigned char* data, size_t len, size_t* outLen)
{ {
MS_TRACE(); MS_TRACE();
const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str()); unsigned char dtable[256];
const size_t len = str.size(); unsigned char* out = BufferOut;
unsigned char* pos;
if (len == 0) unsigned char block[4];
return nullptr; unsigned char tmp;
size_t i;
size_t count;
size_t olen;
int pad{ 0 };
const unsigned char* p = static_cast<const unsigned char*>(data); std::memset(dtable, 0x80, 256);
size_t j{ 0 };
size_t pad1 = len % 4 || p[len - 1] == '=';
size_t pad2 = pad1 && (len % 4 > 2 || p[len - 2] != '=');
const size_t last = (len - pad1) / 4 << 2;
std::string result(last / 4 * 3 + pad1 + pad2, '\0');
// const unsigned char* out = static_cast<const unsigned char*>(&result[0]);
char* out = &result[0];
for (size_t i = 0; i < last; i += 4) for (i = 0; i < sizeof(Base64Table) - 1; ++i)
{ {
int n = B64Index[p[i]] << 18 | B64Index[p[i + 1]] << 12 | B64Index[p[i + 2]] << 6 | B64Index[p[i + 3]]; dtable[Base64Table[i]] = static_cast<unsigned char>(i);
}
dtable[static_cast<unsigned char>('=')] = 0;
out[j++] = n >> 16; count = 0;
out[j++] = n >> 8 & 0xFF; for (i = 0; i < len; ++i)
out[j++] = n & 0xFF; {
if (dtable[data[i]] != 0x80)
count++;
} }
if (pad1) if (count == 0 || count % 4)
return nullptr;
olen = count / 4 * 3;
pos = out;
count = 0;
for (i = 0; i < len; ++i)
{ {
int n = B64Index[p[last]] << 18 | B64Index[p[last + 1]] << 12; tmp = dtable[data[i]];
if (tmp == 0x80)
continue;
out[j++] = n >> 16; if (data[i] == '=')
pad++;
if (pad2) block[count] = tmp;
count++;
if (count == 4)
{
*pos++ = (block[0] << 2) | (block[1] >> 4);
*pos++ = (block[1] << 4) | (block[2] >> 2);
*pos++ = (block[2] << 6) | block[3];
count = 0;
if (pad)
{
if (pad == 1)
pos--;
else if (pad == 2)
pos -= 2;
else
{ {
n |= B64Index[p[last + 2]] << 6; // Invalid padding.
out[j++] = n >> 8 & 0xFF; return nullptr;
}
break;
}
}
} }
*outLen = pos - out;
return out;
} }
return reinterpret_cast<const uint8_t*>(result.c_str()); unsigned char* Utils::String::Base64Decode(const std::string& data, size_t* outLen)
{
MS_TRACE();
return Base64Decode(reinterpret_cast<const unsigned char*>(data.c_str()), data.size(), outLen);
} }
} // namespace Utils } // namespace Utils
...@@ -20,17 +20,27 @@ SCENARIO("String::ToLowerCase()") ...@@ -20,17 +20,27 @@ SCENARIO("String::ToLowerCase()")
SCENARIO("String::Base64Encode()") SCENARIO("String::Base64Encode()")
{ {
std::string data; std::string data;
std::string base64; size_t outLen;
unsigned char* encodedPtr;
unsigned char* decodedPtr;
std::string encoded;
std::string decoded;
data = "abcd"; data = "abcd";
base64 = String::Base64Encode(data); encodedPtr = String::Base64Encode(data, &outLen);
REQUIRE(base64 == "YWJjZA=="); encoded = std::string(reinterpret_cast<char*>(encodedPtr), outLen - 1); // TODO
REQUIRE(std::string(reinterpret_cast<const char*>(String::Base64Decode(base64))) == data); decodedPtr = String::Base64Decode(encoded, &outLen);
decoded = std::string(reinterpret_cast<char*>(decodedPtr), outLen);
REQUIRE(encoded == "YWJjZA==");
REQUIRE(decoded == data);
// TODO: This fails even in the original implementation:
// http://cpp.sh/3qj7c
data = "Iñaki"; data = "Iñaki";
base64 = String::Base64Encode(data); encodedPtr = String::Base64Encode(data, &outLen);
REQUIRE(base64 == "ScOxYWtp"); encoded = std::string(reinterpret_cast<char*>(encodedPtr), outLen - 1); // TODO
REQUIRE(std::string(reinterpret_cast<const char*>(String::Base64Decode(base64))) == data); decodedPtr = String::Base64Decode(encoded, &outLen);
decoded = std::string(reinterpret_cast<char*>(decodedPtr), outLen);
REQUIRE(encoded == "ScOxYWtp");
REQUIRE(decoded == data);
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论