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

C++ Base64 works :)

上级 3b39ca9e
......@@ -295,10 +295,10 @@ namespace Utils
{
public:
static void ToLowerCase(std::string& str);
static unsigned char* Base64Encode(const unsigned char* data, size_t len, size_t* outLen);
static unsigned char* Base64Encode(const std::string& data, size_t* outLen);
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);
static std::string Base64Encode(const uint8_t* data, size_t len);
static std::string Base64Encode(const std::string& str);
static uint8_t* Base64Decode(const uint8_t* data, size_t len, size_t* outLen);
static uint8_t* Base64Decode(const std::string& data, size_t* outLen);
};
inline void String::ToLowerCase(std::string& str)
......
......@@ -12,50 +12,37 @@
// #define MS_LOG_DEV_LEVEL 3
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include <cstring> // std::memset()
/* Static. */
static constexpr size_t BufferOutSize{ 65536 };
static unsigned char BufferOut[BufferOutSize];
static const unsigned char Base64Table[65] =
static uint8_t BufferOut[BufferOutSize];
static const uint8_t Base64Table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
namespace Utils
{
/**
* @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)
std::string Utils::String::Base64Encode(const uint8_t* data, size_t len)
{
MS_TRACE();
unsigned char* out = BufferOut;
unsigned char* pos;
const unsigned char* end;
const unsigned char* in;
const uint8_t* out = BufferOut;
uint8_t* pos;
const uint8_t* end;
const uint8_t* in;
size_t olen;
int lineLen;
olen = len * 4 / 3 + 4; // 3-byte blocks to 4-byte.
olen += olen / 72; // line feeds.
olen++; // Nul termination.
if (olen < len)
return nullptr; // Integer overflow.
MS_THROW_TYPE_ERROR("integer overflow");
end = data + len;
in = data;
pos = out;
lineLen = 0;
end = data + len;
in = data;
pos = const_cast<uint8_t*>(out);
while (end - in >= 3)
{
......@@ -64,13 +51,6 @@ namespace Utils
*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 (end - in)
......@@ -89,43 +69,29 @@ namespace Utils
}
*pos++ = '=';
lineLen += 4;
}
if (lineLen)
*pos++ = '\n';
*pos = '\0';
if (outLen)
*outLen = pos - out;
return out;
return std::string(reinterpret_cast<const char*>(out), pos - out);
}
unsigned char* Utils::String::Base64Encode(const std::string& data, size_t* outLen)
std::string Utils::String::Base64Encode(const std::string& str)
{
MS_TRACE();
return Base64Encode(reinterpret_cast<const unsigned char*>(data.c_str()), data.size(), outLen);
auto* data = reinterpret_cast<const uint8_t*>(str.c_str());
return Base64Encode(data, str.size());
}
/**
* @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)
uint8_t* Utils::String::Base64Decode(const uint8_t* data, size_t len, size_t* outLen)
{
MS_TRACE();
unsigned char dtable[256];
unsigned char* out = BufferOut;
unsigned char* pos;
unsigned char block[4];
unsigned char tmp;
uint8_t dtable[256];
uint8_t* out = BufferOut;
uint8_t* pos;
uint8_t block[4];
uint8_t tmp;
size_t i;
size_t count;
size_t olen;
......@@ -135,9 +101,9 @@ namespace Utils
for (i = 0; i < sizeof(Base64Table) - 1; ++i)
{
dtable[Base64Table[i]] = static_cast<unsigned char>(i);
dtable[Base64Table[i]] = static_cast<uint8_t>(i);
}
dtable[static_cast<unsigned char>('=')] = 0;
dtable[uint8_t{ '=' }] = 0;
count = 0;
for (i = 0; i < len; ++i)
......@@ -180,10 +146,7 @@ namespace Utils
else if (pad == 2)
pos -= 2;
else
{
// Invalid padding.
return nullptr;
}
MS_THROW_TYPE_ERROR("integer padding");
break;
}
......@@ -195,10 +158,12 @@ namespace Utils
return out;
}
unsigned char* Utils::String::Base64Decode(const std::string& data, size_t* outLen)
uint8_t* Utils::String::Base64Decode(const std::string& str, size_t* outLen)
{
MS_TRACE();
return Base64Decode(reinterpret_cast<const unsigned char*>(data.c_str()), data.size(), outLen);
auto* data = reinterpret_cast<const uint8_t*>(str.c_str());
return Base64Decode(data, str.size(), outLen);
}
} // namespace Utils
......@@ -20,27 +20,32 @@ SCENARIO("String::ToLowerCase()")
SCENARIO("String::Base64Encode()")
{
std::string data;
size_t outLen;
unsigned char* encodedPtr;
unsigned char* decodedPtr;
std::string encoded;
size_t outLen;
uint8_t* decodedPtr;
std::string decoded;
data = "abcd";
encodedPtr = String::Base64Encode(data, &outLen);
encoded = std::string(reinterpret_cast<char*>(encodedPtr), outLen - 1); // TODO
decodedPtr = String::Base64Decode(encoded, &outLen);
encoded = String::Base64Encode(data);
decodedPtr = String::Base64Decode(encoded, std::addressof(outLen));
decoded = std::string(reinterpret_cast<char*>(decodedPtr), outLen);
REQUIRE(encoded == "YWJjZA==");
REQUIRE(decoded == data);
data = "Iñaki";
encodedPtr = String::Base64Encode(data, &outLen);
encoded = std::string(reinterpret_cast<char*>(encodedPtr), outLen - 1); // TODO
decodedPtr = String::Base64Decode(encoded, &outLen);
encoded = String::Base64Encode(data);
decodedPtr = String::Base64Decode(encoded, std::addressof(outLen));
decoded = std::string(reinterpret_cast<char*>(decodedPtr), outLen);
REQUIRE(encoded == "ScOxYWtp");
REQUIRE(decoded == data);
data =
"kjsh 23 å∫∂ is89 ∫¶ §∂¶ i823y kjahsd 234u asd kasjhdii7682342 asdkjhaskjsahd k jashd kajsdhaksjdh skadhkjhkjh askdjhasdkjahs uyqiwey aså∫∂¢∞¬∫∂ ashksajdh kjasdhkajshda s kjahsdkjas 987897as897 97898623 9s kjsgå∫∂ 432å∫ƒ∂ å∫#¢ ouyqwiuyais kajsdhiuye ajshkkSAH SDFYÑÑÑ å∫∂Ω 87253847b asdbuiasdi as kasuœæ€\n321";
encoded = String::Base64Encode(data);
decodedPtr = String::Base64Decode(encoded, std::addressof(outLen));
decoded = std::string(reinterpret_cast<char*>(decodedPtr), outLen);
REQUIRE(
encoded ==
"a2pzaCAyMyDDpeKIq+KIgiBpczg5IOKIq8K2IMKn4oiCwrYgaTgyM3kga2phaHNkIDIzNHUgYXNkIGthc2poZGlpNzY4MjM0MiBhc2Rramhhc2tqc2FoZCAgIGsgamFzaGQga2Fqc2RoYWtzamRoIHNrYWRoa2poa2poICAgICAgIGFza2RqaGFzZGtqYWhzIHV5cWl3ZXkgYXPDpeKIq+KIgsKi4oiewqziiKviiIIgYXNoa3NhamRoIGtqYXNkaGthanNoZGEgcyBramFoc2RramFzIDk4Nzg5N2FzODk3IDk3ODk4NjIzIDlzIGtqc2fDpeKIq+KIgiA0MzLDpeKIq8aS4oiCIMOl4oirI8KiIG91eXF3aXV5YWlzIGthanNkaGl1eWUgIGFqc2hra1NBSCBTREZZw5HDkcORIMOl4oir4oiCzqkgODcyNTM4NDdiIGFzZGJ1aWFzZGkgYXMga2FzdcWTw6bigqwKMzIx");
REQUIRE(decoded == data);
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论