Skip to content

Commit

Permalink
Improve handling of malformed base64
Browse files Browse the repository at this point in the history
A 2-byte chunk is not valid per-se, but I want to maintain the behaviour prior to 13cfa0c as this was not meant to change any semantics.
  • Loading branch information
Sainan committed Dec 3, 2024
1 parent e95e752 commit ce7b54f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CLI/cli_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ static void unit_data()
assert(base64::decode("YQ==") == "a");
assert(base64::decode("SGVsbG8=") == "Hello");
assert(base64::decode("8J+YgA==") == "😀");

assert(base64::decode("YWFh") == "aaa");
assert(base64::decode("YWF") == "aa");
assert(base64::decode("YW") == "a");
assert(base64::decode("a") == "h");
});
test("urlDecode", []
{
Expand Down
4 changes: 2 additions & 2 deletions soup/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ NAMESPACE_SOUP
size_t out_len = size / 4 * 3;
if (auto remainder = (size % 4))
{
out_len += (remainder - 1);
out_len += (remainder - 1) + (remainder == 1);
}
return out_len;
}
Expand Down Expand Up @@ -243,7 +243,7 @@ NAMESPACE_SOUP

if (auto remainder = (size % 4))
{
auto extra = (remainder - 1);
auto extra = (remainder - 1) + (remainder == 1);

uint32_t a = i != size ? table[static_cast<uint8_t>(data[i++])] : 0;
uint32_t b = i != size ? table[static_cast<uint8_t>(data[i++])] : 0;
Expand Down

0 comments on commit ce7b54f

Please sign in to comment.