Base64 encoder and decoder upgraded to operate with any string now

This commit is contained in:
2016-05-16 19:56:23 +02:00
parent c9df50ce32
commit 7049fc0af8
2 changed files with 62 additions and 64 deletions

View File

@@ -33,35 +33,6 @@ const char winstd::base64_enc::lookup[64] = {
};
void winstd::base64_enc::encode(_Out_ std::string &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last)
{
assert(data || !size);
size_t i = 0, j = 0;
// Convert data character by character.
for (;;) {
if (num >= 3) {
encode(out, buf);
num = 0;
j += 4;
}
if (i >= size)
break;
buf[num++] = ((unsigned char*)data)[i++];
}
// If this is the last block, flush the buffer.
if (is_last && num) {
encode(out, buf, num);
num = 0;
j += 4;
}
}
//////////////////////////////////////////////////////////////////////
// winstd::base64_dec
//////////////////////////////////////////////////////////////////////
@@ -85,31 +56,3 @@ const unsigned char winstd::base64_dec::lookup[256] = {
/* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
/* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
};
void winstd::base64_dec::decode(_Out_ std::vector<unsigned char> &out, _Out_ bool &is_last, _In_z_count_(size) const char *data, _In_ size_t size)
{
size_t i = 0, j = 0;
size_t nibbles;
is_last = false;
for (;;) {
if (num >= 4) {
// Buffer full; decode it.
nibbles = decode(out, buf);
j += nibbles;
num = 0;
if (nibbles < 3) {
is_last = true;
break;
}
}
if (i >= size || !data[i])
break;
if ((buf[num] = lookup[(unsigned char)data[i++]]) != 255)
num++;
}
}