SHA256
8
0
forked from pool/cpprest
Files
cpprest/base64.patch

159 lines
5.7 KiB
Diff
Raw Permalink Normal View History

Index: cpprestsdk-2.10.16/Release/src/utilities/base64.cpp
===================================================================
--- cpprestsdk-2.10.16.orig/Release/src/utilities/base64.cpp
+++ cpprestsdk-2.10.16/Release/src/utilities/base64.cpp
@@ -43,30 +43,6 @@ const std::array<unsigned char, 128> _ba
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}};
-struct _triple_byte
-{
- unsigned char _1_1 : 2;
- unsigned char _0 : 6;
- unsigned char _2_1 : 4;
- unsigned char _1_2 : 4;
- unsigned char _3 : 6;
- unsigned char _2_2 : 2;
-};
-
-struct _double_byte
-{
- unsigned char _1_1 : 2;
- unsigned char _0 : 6;
- unsigned char _2_1 : 4;
- unsigned char _1_2 : 4;
-};
-
-struct _single_byte
-{
- unsigned char _1_1 : 2;
- unsigned char _0 : 6;
-};
-
//
// A note on the implementation of BASE64 encoding and decoding:
//
@@ -134,26 +110,14 @@ std::vector<unsigned char> _from_base64(
size_t idx = 0;
for (; size > 4; ++idx)
{
- unsigned char target[3];
- memset(target, 0, sizeof(target));
- _triple_byte* record = reinterpret_cast<_triple_byte*>(target);
-
unsigned char val0 = _base64_dectbl[ptr[0]];
unsigned char val1 = _base64_dectbl[ptr[1]];
unsigned char val2 = _base64_dectbl[ptr[2]];
unsigned char val3 = _base64_dectbl[ptr[3]];
- record->_0 = val0;
- record->_1_1 = val1 >> 4;
- result[idx] = target[0];
-
- record->_1_2 = val1 & 0xF;
- record->_2_1 = val2 >> 2;
- result[++idx] = target[1];
-
- record->_2_2 = val2 & 0x3;
- record->_3 = val3 & 0x3F;
- result[++idx] = target[2];
+ result[idx] = (val0 << 2) | (val1 >> 4);
+ result[++idx] = ((val1 & 0xF) << 4) | (val2 >> 2);
+ result[++idx] = ((val2 & 0x3) << 6) | (val3 & 0x3F);
ptr += 4;
size -= 4;
@@ -163,45 +127,35 @@ std::vector<unsigned char> _from_base64(
// in all the iterations (a performance issue).
{
- unsigned char target[3];
- memset(target, 0, sizeof(target));
- _triple_byte* record = reinterpret_cast<_triple_byte*>(target);
-
unsigned char val0 = _base64_dectbl[ptr[0]];
unsigned char val1 = _base64_dectbl[ptr[1]];
unsigned char val2 = _base64_dectbl[ptr[2]];
unsigned char val3 = _base64_dectbl[ptr[3]];
- record->_0 = val0;
- record->_1_1 = val1 >> 4;
- result[idx] = target[0];
+ result[idx] = (val0 << 2) | (val1 >> 4);
- record->_1_2 = val1 & 0xF;
if (val2 != 254)
{
- record->_2_1 = val2 >> 2;
- result[++idx] = target[1];
+ result[++idx] = ((val1 & 0xF) << 4) | (val2 >> 2);
}
else
{
// There shouldn't be any information (ones) in the unused bits,
- if (record->_1_2 != 0)
+ if ((val1 & 0xF) != 0)
{
throw std::runtime_error("Invalid end of base64 string");
}
return result;
}
- record->_2_2 = val2 & 0x3;
if (val3 != 254)
{
- record->_3 = val3 & 0x3F;
- result[++idx] = target[2];
+ result[++idx] = ((val2 & 0x3) << 6) | (val3 & 0x3F);
}
else
{
// There shouldn't be any information (ones) in the unused bits.
- if (record->_2_2 != 0)
+ if ((val2 & 0x3) != 0)
{
throw std::runtime_error("Invalid end of base64 string");
}
@@ -218,11 +172,10 @@ utility::string_t _to_base64(const unsig
for (; size >= 3;)
{
- const _triple_byte* record = reinterpret_cast<const _triple_byte*>(ptr);
- unsigned char idx0 = record->_0;
- unsigned char idx1 = (record->_1_1 << 4) | record->_1_2;
- unsigned char idx2 = (record->_2_1 << 2) | record->_2_2;
- unsigned char idx3 = record->_3;
+ unsigned char idx0 = ptr[0] >> 2;
+ unsigned char idx1 = ((ptr[0] & 0x3) << 4) | (ptr[1] >> 4);
+ unsigned char idx2 = ((ptr[1] & 0xF) << 2) | (ptr[2] >> 6);
+ unsigned char idx3 = ptr[2] & 0x3F;
result.push_back(char_t(_base64_enctbl[idx0]));
result.push_back(char_t(_base64_enctbl[idx1]));
result.push_back(char_t(_base64_enctbl[idx2]));
@@ -234,9 +187,8 @@ utility::string_t _to_base64(const unsig
{
case 1:
{
- const _single_byte* record = reinterpret_cast<const _single_byte*>(ptr);
- unsigned char idx0 = record->_0;
- unsigned char idx1 = (record->_1_1 << 4);
+ unsigned char idx0 = ptr[0] >> 2;
+ unsigned char idx1 = ((ptr[0] & 0x3) << 4);
result.push_back(char_t(_base64_enctbl[idx0]));
result.push_back(char_t(_base64_enctbl[idx1]));
result.push_back('=');
@@ -245,10 +197,9 @@ utility::string_t _to_base64(const unsig
}
case 2:
{
- const _double_byte* record = reinterpret_cast<const _double_byte*>(ptr);
- unsigned char idx0 = record->_0;
- unsigned char idx1 = (record->_1_1 << 4) | record->_1_2;
- unsigned char idx2 = (record->_2_1 << 2);
+ unsigned char idx0 = ptr[0] >> 2;
+ unsigned char idx1 = ((ptr[0] & 0x3) << 4) | (ptr[1] >> 4);
+ unsigned char idx2 = ((ptr[1] & 0xF) << 2);
result.push_back(char_t(_base64_enctbl[idx0]));
result.push_back(char_t(_base64_enctbl[idx1]));
result.push_back(char_t(_base64_enctbl[idx2]));