1
0

Added Base64Encode().

This commit is contained in:
madmaxoft 2014-01-07 12:36:36 +01:00
parent 778c329ad2
commit e3bb82d95a
2 changed files with 51 additions and 0 deletions

View File

@ -765,6 +765,54 @@ AString Base64Decode(const AString & a_Base64String)
AString Base64Encode(const AString & a_Input)
{
static const char BASE64[64] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};
std::string output;
output.resize(((a_Input.size() + 2) / 3) * 4);
size_t output_index = 0;
size_t size_full24 = (a_Input.size() / 3) * 3;
for (size_t i = 0; i < size_full24; i += 3)
{
output[output_index++] = BASE64[(unsigned char)a_Input[i] >> 2];
output[output_index++] = BASE64[((unsigned char)a_Input[i] << 4 | (unsigned char)a_Input[i + 1] >> 4) & 63];
output[output_index++] = BASE64[((unsigned char)a_Input[i + 1] << 2 | (unsigned char)a_Input[i + 2] >> 6) & 63];
output[output_index++] = BASE64[(unsigned char)a_Input[i + 2] & 63];
}
if (size_full24 < a_Input.size())
{
output[output_index++] = BASE64[(unsigned char)a_Input[size_full24] >> 2];
if (size_full24 + 1 == a_Input.size())
{
output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4) & 63];
output[output_index++] = '=';
}
else
{
output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4 | (unsigned char)a_Input[size_full24 + 1] >> 4) & 63];
output[output_index++] = BASE64[((unsigned char)a_Input[size_full24 + 1] << 2) & 63];
}
output[output_index++] = '=';
}
assert(output_index == output.size());
return output;
}
short GetBEShort(const char * a_Mem)
{
return (((short)a_Mem[0]) << 8) | a_Mem[1];

View File

@ -81,6 +81,9 @@ extern AString ReplaceAllCharOccurrences(const AString & a_String, char a_From,
/// Decodes a Base64-encoded string into the raw data
extern AString Base64Decode(const AString & a_Base64String);
/// Encodes a string into Base64
extern AString Base64Encode(const AString & a_Input);
/// Reads two bytes from the specified memory location and interprets them as BigEndian short
extern short GetBEShort(const char * a_Mem);