mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
AK: Stop using DeprecatedString
in Base64 encoding
This commit is contained in:
parent
99c1b634fc
commit
25f2e4981c
Notes:
sideshowbarker
2024-07-17 05:23:40 +09:00
Author: https://github.com/gmta Commit: https://github.com/SerenityOS/serenity/commit/25f2e4981c Pull-request: https://github.com/SerenityOS/serenity/pull/16580 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/Hendiadyoin1
14 changed files with 47 additions and 27 deletions
|
@ -100,7 +100,7 @@ ErrorOr<ByteBuffer> decode_base64(StringView input)
|
|||
return ByteBuffer::copy(output);
|
||||
}
|
||||
|
||||
DeprecatedString encode_base64(ReadonlyBytes input)
|
||||
ErrorOr<String> encode_base64(ReadonlyBytes input)
|
||||
{
|
||||
StringBuilder output(calculate_base64_encoded_length(input));
|
||||
|
||||
|
@ -131,13 +131,13 @@ DeprecatedString encode_base64(ReadonlyBytes input)
|
|||
char const out2 = is_16bit ? '=' : alphabet[index2];
|
||||
char const out3 = is_8bit ? '=' : alphabet[index3];
|
||||
|
||||
output.append(out0);
|
||||
output.append(out1);
|
||||
output.append(out2);
|
||||
output.append(out3);
|
||||
TRY(output.try_append(out0));
|
||||
TRY(output.try_append(out1));
|
||||
TRY(output.try_append(out2));
|
||||
TRY(output.try_append(out3));
|
||||
}
|
||||
|
||||
return output.to_deprecated_string();
|
||||
return output.to_string();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
namespace AK {
|
||||
|
@ -19,7 +19,7 @@ namespace AK {
|
|||
|
||||
[[nodiscard]] ErrorOr<ByteBuffer> decode_base64(StringView);
|
||||
|
||||
[[nodiscard]] DeprecatedString encode_base64(ReadonlyBytes);
|
||||
[[nodiscard]] ErrorOr<String> encode_base64(ReadonlyBytes);
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
|
|
|
@ -42,8 +42,8 @@ TEST_CASE(test_decode_invalid)
|
|||
TEST_CASE(test_encode)
|
||||
{
|
||||
auto encode_equal = [&](StringView input, StringView expected) {
|
||||
auto encoded = encode_base64(input.bytes());
|
||||
EXPECT(encoded == DeprecatedString(expected));
|
||||
auto encoded = MUST(encode_base64(input.bytes()));
|
||||
EXPECT(encoded == expected);
|
||||
EXPECT_EQ(expected.length(), calculate_base64_encoded_length(input.bytes()));
|
||||
};
|
||||
|
||||
|
|
|
@ -137,9 +137,9 @@ ErrorOr<void> Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json
|
|||
TRY(json_layer.add("opacity_percent"sv, layer.opacity_percent()));
|
||||
TRY(json_layer.add("visible"sv, layer.is_visible()));
|
||||
TRY(json_layer.add("selected"sv, layer.is_selected()));
|
||||
TRY(json_layer.add("bitmap"sv, encode_base64(TRY(Gfx::PNGWriter::encode(layer.content_bitmap())))));
|
||||
TRY(json_layer.add("bitmap"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(layer.content_bitmap()))))));
|
||||
if (layer.is_masked())
|
||||
TRY(json_layer.add("mask"sv, encode_base64(TRY(Gfx::PNGWriter::encode(*layer.mask_bitmap())))));
|
||||
TRY(json_layer.add("mask"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(*layer.mask_bitmap()))))));
|
||||
TRY(json_layer.finish());
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,10 @@ static DeprecatedString get_salt()
|
|||
|
||||
StringBuilder builder;
|
||||
builder.append("$5$"sv);
|
||||
builder.append(encode_base64(ReadonlyBytes(random_data, sizeof(random_data))));
|
||||
|
||||
// FIXME: change to TRY() and make method fallible
|
||||
auto salt_string = MUST(encode_base64({ random_data, sizeof(random_data) }));
|
||||
builder.append(salt_string);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
|
|
@ -52,8 +52,13 @@ char* crypt_r(char const* key, char const* salt, struct crypt_data* data)
|
|||
sha.update(reinterpret_cast<u8 const*>(salt_value), salt_len);
|
||||
|
||||
auto digest = sha.digest();
|
||||
auto string = encode_base64(ReadonlyBytes(digest.immutable_data(), digest.data_length()));
|
||||
auto string_or_error = encode_base64({ digest.immutable_data(), digest.data_length() });
|
||||
if (string_or_error.is_error()) {
|
||||
errno = ENOMEM;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto string = string_or_error.value().bytes_as_string_view();
|
||||
fits = string.copy_characters_to_buffer(data->result + header_len + 1, sizeof(data->result) - header_len - 1);
|
||||
if (!fits) {
|
||||
errno = EINVAL;
|
||||
|
|
|
@ -242,7 +242,9 @@ Optional<HttpRequest::Header> HttpRequest::get_http_basic_authentication_header(
|
|||
builder.append(url.username());
|
||||
builder.append(':');
|
||||
builder.append(url.password());
|
||||
auto token = encode_base64(builder.to_deprecated_string().bytes());
|
||||
|
||||
// FIXME: change to TRY() and make method fallible
|
||||
auto token = MUST(encode_base64(MUST(builder.to_string()).bytes()));
|
||||
builder.clear();
|
||||
builder.append("Basic "sv);
|
||||
builder.append(token);
|
||||
|
|
|
@ -1305,7 +1305,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_network_or_cache_fet
|
|||
// true, set authorizationValue to httpRequest’s current URL, converted to an `Authorization` value.
|
||||
else if (http_request->current_url().includes_credentials() && is_authentication_fetch == IsAuthenticationFetch::Yes) {
|
||||
auto const& url = http_request->current_url();
|
||||
authorization_value = encode_base64(DeprecatedString::formatted("{}:{}", url.username(), url.password()).bytes());
|
||||
auto payload = DeprecatedString::formatted("{}:{}", url.username(), url.password());
|
||||
authorization_value = TRY_OR_RETURN_OOM(realm, encode_base64(payload.bytes())).to_deprecated_string();
|
||||
}
|
||||
|
||||
// 4. If authorizationValue is non-null, then append (`Authorization`, authorizationValue) to
|
||||
|
|
|
@ -181,7 +181,12 @@ DeprecatedString HTMLCanvasElement::to_data_url(DeprecatedString const& type, [[
|
|||
dbgln("Gfx::PNGWriter failed to encode the HTMLCanvasElement: {}", encoded_bitmap_or_error.error());
|
||||
return {};
|
||||
}
|
||||
return AK::URL::create_with_data(type, encode_base64(encoded_bitmap_or_error.value()), true).to_deprecated_string();
|
||||
auto base64_encoded_or_error = encode_base64(encoded_bitmap_or_error.value());
|
||||
if (base64_encoded_or_error.is_error()) {
|
||||
// FIXME: propagate error
|
||||
return {};
|
||||
}
|
||||
return AK::URL::create_with_data(type, base64_encoded_or_error.release_value().to_deprecated_string(), true).to_deprecated_string();
|
||||
}
|
||||
|
||||
void HTMLCanvasElement::present()
|
||||
|
|
|
@ -1423,8 +1423,8 @@ JS_DEFINE_NATIVE_FUNCTION(Window::btoa)
|
|||
byte_string.append(code_point);
|
||||
}
|
||||
|
||||
auto encoded = encode_base64(byte_string.span());
|
||||
return JS::PrimitiveString::create(vm, move(encoded));
|
||||
auto encoded = MUST(encode_base64(byte_string.span()));
|
||||
return JS::PrimitiveString::create(vm, encoded.to_deprecated_string());
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/interaction.html#dom-window-focus
|
||||
|
|
|
@ -137,7 +137,7 @@ WebIDL::ExceptionOr<DeprecatedString> WorkerGlobalScope::btoa(DeprecatedString c
|
|||
|
||||
// Otherwise, the user agent must convert data to a byte sequence whose nth byte is the eight-bit representation of the nth code point of data,
|
||||
// and then must apply forgiving-base64 encode to that byte sequence and return the result.
|
||||
return encode_base64(byte_string.span());
|
||||
return TRY_OR_RETURN_OOM(realm(), encode_base64(byte_string.span())).to_deprecated_string();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
|
||||
|
|
|
@ -180,7 +180,8 @@ void WebSocket::send_client_handshake()
|
|||
// 7. 16-byte nonce encoded as Base64
|
||||
u8 nonce_data[16];
|
||||
fill_with_random(nonce_data, 16);
|
||||
m_websocket_key = encode_base64(ReadonlyBytes(nonce_data, 16));
|
||||
// FIXME: change to TRY() and make method fallible
|
||||
m_websocket_key = MUST(encode_base64({ nonce_data, 16 })).to_deprecated_string();
|
||||
builder.appendff("Sec-WebSocket-Key: {}\r\n", m_websocket_key);
|
||||
|
||||
// 8. Origin (optional field)
|
||||
|
@ -322,7 +323,8 @@ void WebSocket::read_server_handshake()
|
|||
hash.initialize(Crypto::Hash::HashKind::SHA1);
|
||||
hash.update(expected_content);
|
||||
auto expected_sha1 = hash.digest();
|
||||
auto expected_sha1_string = encode_base64(ReadonlyBytes(expected_sha1.immutable_data(), expected_sha1.data_length()));
|
||||
// FIXME: change to TRY() and make method fallible
|
||||
auto expected_sha1_string = MUST(encode_base64({ expected_sha1.immutable_data(), expected_sha1.data_length() }));
|
||||
if (!parts[1].trim_whitespace().equals_ignoring_case(expected_sha1_string)) {
|
||||
dbgln("WebSocket: Server HTTP Handshake Header |Sec-Websocket-Accept| should be '{}', got '{}'. Failing connection.", expected_sha1_string, parts[1]);
|
||||
fatal_error(WebSocket::Error::ConnectionUpgradeFailed);
|
||||
|
|
|
@ -238,7 +238,8 @@ static DeprecatedString folder_image_data()
|
|||
static DeprecatedString cache;
|
||||
if (cache.is_empty()) {
|
||||
auto file = Core::MappedFile::map("/res/icons/16x16/filetype-folder.png"sv).release_value_but_fixme_should_propagate_errors();
|
||||
cache = encode_base64(file->bytes());
|
||||
// FIXME: change to TRY() and make method fallible
|
||||
cache = MUST(encode_base64(file->bytes())).to_deprecated_string();
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
@ -248,7 +249,8 @@ static DeprecatedString file_image_data()
|
|||
static DeprecatedString cache;
|
||||
if (cache.is_empty()) {
|
||||
auto file = Core::MappedFile::map("/res/icons/16x16/filetype-unknown.png"sv).release_value_but_fixme_should_propagate_errors();
|
||||
cache = encode_base64(file->bytes());
|
||||
// FIXME: change to TRY() and make method fallible
|
||||
cache = MUST(encode_base64(file->bytes())).to_deprecated_string();
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
|
|
@ -134,18 +134,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
}
|
||||
|
||||
auto get_salt = []() {
|
||||
auto get_salt = []() -> ErrorOr<DeprecatedString> {
|
||||
char random_data[12];
|
||||
fill_with_random(random_data, sizeof(random_data));
|
||||
|
||||
StringBuilder builder;
|
||||
builder.append("$5$"sv);
|
||||
builder.append(encode_base64(ReadonlyBytes(random_data, sizeof(random_data))));
|
||||
builder.append(TRY(encode_base64({ random_data, sizeof(random_data) })));
|
||||
|
||||
return builder.build();
|
||||
};
|
||||
|
||||
char* hash = crypt(password.characters(), get_salt().characters());
|
||||
char* hash = crypt(password.characters(), TRY(get_salt()).characters());
|
||||
|
||||
struct passwd p;
|
||||
p.pw_name = const_cast<char*>(username.characters());
|
||||
|
|
Loading…
Reference in a new issue