From 754ff41b9cbe49af321d23e439c4efaa9fdcf135 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 24 Mar 2024 08:51:44 -0400 Subject: [PATCH] AK: Remove whitespace skipping feature from AK's Base64 decoder This was added in commit f2663f477fc010ce6450b759d342228bfa403714 as a partial implementation of what is now LibWeb's forgiving Base64 decoder. All use cases within LibWeb that require whitespace skipping now use that implementation instead. Removing this feature from AK allows us to know the exact output size of a decoded Base64 string. We can still trim whitespace at the start and end of the input though; for example, this is useful when reading from a file that may have a newline at the end of the file. --- AK/Base64.cpp | 25 ++++++++++--------------- Tests/AK/TestBase64.cpp | 7 ++++--- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 1ec9701b08..112c02e603 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -26,19 +25,20 @@ size_t calculate_base64_encoded_length(ReadonlyBytes input) static ErrorOr decode_base64_impl(StringView input, ReadonlySpan alphabet_lookup_table) { - auto get = [&](size_t& offset, bool* is_padding, bool& parsed_something) -> ErrorOr { - while (offset < input.length() && is_ascii_space(input[offset])) - ++offset; + input = input.trim_whitespace(); + + auto get = [&](size_t offset, bool* is_padding) -> ErrorOr { if (offset >= input.length()) return 0; - auto ch = static_cast(input[offset++]); - parsed_something = true; + + auto ch = static_cast(input[offset]); if (ch == '=') { if (!is_padding) return Error::from_string_literal("Invalid '=' character outside of padding in base64 data"); *is_padding = true; return 0; } + i16 result = alphabet_lookup_table[ch]; if (result < 0) return Error::from_string_literal("Invalid character in base64 data"); @@ -56,15 +56,10 @@ static ErrorOr decode_base64_impl(StringView input, ReadonlySpan> 4) & 3); diff --git a/Tests/AK/TestBase64.cpp b/Tests/AK/TestBase64.cpp index 9ca486dba9..fb410113fe 100644 --- a/Tests/AK/TestBase64.cpp +++ b/Tests/AK/TestBase64.cpp @@ -25,8 +25,8 @@ TEST_CASE(test_decode) decode_equal("Zm9vYg=="sv, "foob"sv); decode_equal("Zm9vYmE="sv, "fooba"sv); decode_equal("Zm9vYmFy"sv, "foobar"sv); - decode_equal("Z m\r9\n v\v Ym\tFy"sv, "foobar"sv); - EXPECT_EQ(decode_base64(" ZD Qg\r\nPS An Zm91cic\r\n 7"sv).value(), decode_base64("ZDQgPSAnZm91cic7"sv).value()); + decode_equal(" Zm9vYmFy "sv, "foobar"sv); + decode_equal(" \n\r \t Zm9vYmFy \n"sv, "foobar"sv); decode_equal("aGVsbG8/d29ybGQ="sv, "hello?world"sv); } @@ -88,7 +88,8 @@ TEST_CASE(test_urldecode) decode_equal("Zm9vYg=="sv, "foob"sv); decode_equal("Zm9vYmE="sv, "fooba"sv); decode_equal("Zm9vYmFy"sv, "foobar"sv); - decode_equal("Z m\r9\n v\v Ym\tFy"sv, "foobar"sv); + decode_equal(" Zm9vYmFy "sv, "foobar"sv); + decode_equal(" \n\r \t Zm9vYmFy \n"sv, "foobar"sv); decode_equal("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEu"sv, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."sv); decode_equal("aGVsbG8_d29ybGQ="sv, "hello?world"sv);