mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 04:50:29 +00:00
AK: Ignore whitespace while decoding base64
This matches how other implementations behave. 1% progression on ACID3. :^)
This commit is contained in:
parent
17f34488f6
commit
f2663f477f
Notes:
sideshowbarker
2024-07-17 18:16:35 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f2663f477f
3 changed files with 38 additions and 10 deletions
|
@ -1,11 +1,12 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Array.h>
|
||||
#include <AK/Base64.h>
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Vector.h>
|
||||
|
@ -50,17 +51,21 @@ size_t calculate_base64_encoded_length(ReadonlyBytes input)
|
|||
|
||||
ErrorOr<ByteBuffer> decode_base64(StringView input)
|
||||
{
|
||||
auto get = [&](const size_t offset, bool* is_padding) -> ErrorOr<u8> {
|
||||
auto get = [&](size_t& offset, bool* is_padding, bool& parsed_something) -> ErrorOr<u8> {
|
||||
constexpr auto table = make_lookup_table();
|
||||
while (offset < input.length() && is_ascii_space(input[offset]))
|
||||
++offset;
|
||||
if (offset >= input.length())
|
||||
return 0;
|
||||
if (input[offset] == '=') {
|
||||
auto ch = static_cast<unsigned char>(input[offset++]);
|
||||
parsed_something = true;
|
||||
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 = table[static_cast<unsigned char>(input[offset])];
|
||||
i16 result = table[ch];
|
||||
if (result < 0)
|
||||
return Error::from_string_literal("Invalid character in base64 data");
|
||||
VERIFY(result < 256);
|
||||
|
@ -70,14 +75,20 @@ ErrorOr<ByteBuffer> decode_base64(StringView input)
|
|||
Vector<u8> output;
|
||||
output.ensure_capacity(calculate_base64_decoded_length(input));
|
||||
|
||||
for (size_t i = 0; i < input.length(); i += 4) {
|
||||
size_t offset = 0;
|
||||
while (offset < input.length()) {
|
||||
bool in2_is_padding = false;
|
||||
bool in3_is_padding = false;
|
||||
|
||||
const u8 in0 = TRY(get(i, nullptr));
|
||||
const u8 in1 = TRY(get(i + 1, nullptr));
|
||||
const u8 in2 = TRY(get(i + 2, &in2_is_padding));
|
||||
const u8 in3 = TRY(get(i + 3, &in3_is_padding));
|
||||
bool parsed_something = false;
|
||||
|
||||
const u8 in0 = TRY(get(offset, nullptr, parsed_something));
|
||||
const u8 in1 = TRY(get(offset, nullptr, parsed_something));
|
||||
const u8 in2 = TRY(get(offset, &in2_is_padding, parsed_something));
|
||||
const u8 in3 = TRY(get(offset, &in3_is_padding, parsed_something));
|
||||
|
||||
if (!parsed_something)
|
||||
break;
|
||||
|
||||
const u8 out0 = (in0 << 2) | ((in1 >> 4) & 3);
|
||||
const u8 out1 = ((in1 & 0xf) << 4) | ((in2 >> 2) & 0xf);
|
||||
|
|
|
@ -27,6 +27,8 @@ TEST_CASE(test_decode)
|
|||
decode_equal("Zm9vYg==", "foob");
|
||||
decode_equal("Zm9vYmE=", "fooba");
|
||||
decode_equal("Zm9vYmFy", "foobar");
|
||||
decode_equal("Z m\r9\n v\v Ym\tFy", "foobar");
|
||||
EXPECT_EQ(decode_base64(" ZD Qg\r\nPS An Zm91cic\r\n 7"sv).value(), decode_base64("ZDQgPSAnZm91cic7"sv).value());
|
||||
}
|
||||
|
||||
TEST_CASE(test_decode_invalid)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/Base64.h>
|
||||
#include <AK/URL.h>
|
||||
|
||||
TEST_CASE(construct)
|
||||
|
@ -280,6 +281,20 @@ TEST_CASE(data_url_base64_encoded_with_whitespace)
|
|||
EXPECT_EQ(url.serialize(), "data:text/html;base64, test with whitespace ");
|
||||
}
|
||||
|
||||
TEST_CASE(data_url_base64_encoded_with_inline_whitespace)
|
||||
{
|
||||
URL url("data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207%20");
|
||||
EXPECT(url.is_valid());
|
||||
EXPECT_EQ(url.scheme(), "data");
|
||||
EXPECT(url.host().is_null());
|
||||
EXPECT_EQ(url.data_mime_type(), "text/javascript");
|
||||
EXPECT(url.data_payload_is_base64());
|
||||
EXPECT_EQ(url.data_payload(), " ZD Qg\r\nPS An Zm91cic\r\n 7 "sv);
|
||||
auto decode_result = decode_base64(url.data_payload());
|
||||
EXPECT_EQ(decode_result.is_error(), false);
|
||||
EXPECT_EQ(StringView(decode_result.value()), "d4 = 'four';"sv);
|
||||
}
|
||||
|
||||
TEST_CASE(trailing_slash_with_complete_url)
|
||||
{
|
||||
EXPECT_EQ(URL("http://a/b/").complete_url("c/").serialize(), "http://a/b/c/");
|
||||
|
|
Loading…
Reference in a new issue