fix util::base64_encode

This commit is contained in:
MihailRis 2024-11-18 12:37:48 +03:00
parent 0bfc5d2ad4
commit d660880365
2 changed files with 17 additions and 1 deletions

View File

@ -317,7 +317,7 @@ std::string util::base64_encode(const ubyte* data, size_t size) {
ending[i - fullsegments] = data[i];
}
size_t trailing = size - fullsegments;
{
if (trailing) {
char output[] = "====";
output[0] = B64ABC[(ending[0] & 0b11111100) >> 2];
output[1] =

View File

@ -15,3 +15,19 @@ TEST(stringutil, utf8) {
std::string str2 = util::u32str2str_utf8(u32str);
EXPECT_EQ(str, str2);
}
TEST(stringutil, base64) {
srand(2019);
for (size_t size = 0; size < 30; size++) {
auto bytes = std::make_unique<ubyte[]>(size);
for (int i = 0; i < size; i++) {
bytes[i] = rand();
}
auto base64 = util::base64_encode(bytes.get(), size);
auto decoded = util::base64_decode(base64);
ASSERT_EQ(size, decoded.size());
for (size_t i = 0; i < size; i++) {
ASSERT_EQ(bytes[i], decoded[i]);
}
}
}