Skip to content

Commit 25f2e49

Browse files
gmtaawesomekling
authored andcommitted
AK: Stop using DeprecatedString in Base64 encoding
1 parent 99c1b63 commit 25f2e49

File tree

14 files changed

+47
-27
lines changed

14 files changed

+47
-27
lines changed

AK/Base64.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ ErrorOr<ByteBuffer> decode_base64(StringView input)
100100
return ByteBuffer::copy(output);
101101
}
102102

103-
DeprecatedString encode_base64(ReadonlyBytes input)
103+
ErrorOr<String> encode_base64(ReadonlyBytes input)
104104
{
105105
StringBuilder output(calculate_base64_encoded_length(input));
106106

@@ -131,13 +131,13 @@ DeprecatedString encode_base64(ReadonlyBytes input)
131131
char const out2 = is_16bit ? '=' : alphabet[index2];
132132
char const out3 = is_8bit ? '=' : alphabet[index3];
133133

134-
output.append(out0);
135-
output.append(out1);
136-
output.append(out2);
137-
output.append(out3);
134+
TRY(output.try_append(out0));
135+
TRY(output.try_append(out1));
136+
TRY(output.try_append(out2));
137+
TRY(output.try_append(out3));
138138
}
139139

140-
return output.to_deprecated_string();
140+
return output.to_string();
141141
}
142142

143143
}

AK/Base64.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#pragma once
88

99
#include <AK/ByteBuffer.h>
10-
#include <AK/DeprecatedString.h>
1110
#include <AK/Error.h>
11+
#include <AK/String.h>
1212
#include <AK/StringView.h>
1313

1414
namespace AK {
@@ -19,7 +19,7 @@ namespace AK {
1919

2020
[[nodiscard]] ErrorOr<ByteBuffer> decode_base64(StringView);
2121

22-
[[nodiscard]] DeprecatedString encode_base64(ReadonlyBytes);
22+
[[nodiscard]] ErrorOr<String> encode_base64(ReadonlyBytes);
2323
}
2424

2525
#if USING_AK_GLOBALLY

Tests/AK/TestBase64.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ TEST_CASE(test_decode_invalid)
4242
TEST_CASE(test_encode)
4343
{
4444
auto encode_equal = [&](StringView input, StringView expected) {
45-
auto encoded = encode_base64(input.bytes());
46-
EXPECT(encoded == DeprecatedString(expected));
45+
auto encoded = MUST(encode_base64(input.bytes()));
46+
EXPECT(encoded == expected);
4747
EXPECT_EQ(expected.length(), calculate_base64_encoded_length(input.bytes()));
4848
};
4949

Userland/Applications/PixelPaint/Image.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ ErrorOr<void> Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json
137137
TRY(json_layer.add("opacity_percent"sv, layer.opacity_percent()));
138138
TRY(json_layer.add("visible"sv, layer.is_visible()));
139139
TRY(json_layer.add("selected"sv, layer.is_selected()));
140-
TRY(json_layer.add("bitmap"sv, encode_base64(TRY(Gfx::PNGWriter::encode(layer.content_bitmap())))));
140+
TRY(json_layer.add("bitmap"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(layer.content_bitmap()))))));
141141
if (layer.is_masked())
142-
TRY(json_layer.add("mask"sv, encode_base64(TRY(Gfx::PNGWriter::encode(*layer.mask_bitmap())))));
142+
TRY(json_layer.add("mask"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(*layer.mask_bitmap()))))));
143143
TRY(json_layer.finish());
144144
}
145145

Userland/Libraries/LibCore/Account.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ static DeprecatedString get_salt()
3434

3535
StringBuilder builder;
3636
builder.append("$5$"sv);
37-
builder.append(encode_base64(ReadonlyBytes(random_data, sizeof(random_data))));
37+
38+
// FIXME: change to TRY() and make method fallible
39+
auto salt_string = MUST(encode_base64({ random_data, sizeof(random_data) }));
40+
builder.append(salt_string);
3841

3942
return builder.build();
4043
}

Userland/Libraries/LibCrypt/crypt.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ char* crypt_r(char const* key, char const* salt, struct crypt_data* data)
5252
sha.update(reinterpret_cast<u8 const*>(salt_value), salt_len);
5353

5454
auto digest = sha.digest();
55-
auto string = encode_base64(ReadonlyBytes(digest.immutable_data(), digest.data_length()));
55+
auto string_or_error = encode_base64({ digest.immutable_data(), digest.data_length() });
56+
if (string_or_error.is_error()) {
57+
errno = ENOMEM;
58+
return nullptr;
59+
}
5660

61+
auto string = string_or_error.value().bytes_as_string_view();
5762
fits = string.copy_characters_to_buffer(data->result + header_len + 1, sizeof(data->result) - header_len - 1);
5863
if (!fits) {
5964
errno = EINVAL;

Userland/Libraries/LibHTTP/HttpRequest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ Optional<HttpRequest::Header> HttpRequest::get_http_basic_authentication_header(
242242
builder.append(url.username());
243243
builder.append(':');
244244
builder.append(url.password());
245-
auto token = encode_base64(builder.to_deprecated_string().bytes());
245+
246+
// FIXME: change to TRY() and make method fallible
247+
auto token = MUST(encode_base64(MUST(builder.to_string()).bytes()));
246248
builder.clear();
247249
builder.append("Basic "sv);
248250
builder.append(token);

Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_network_or_cache_fet
13051305
// true, set authorizationValue to httpRequest’s current URL, converted to an `Authorization` value.
13061306
else if (http_request->current_url().includes_credentials() && is_authentication_fetch == IsAuthenticationFetch::Yes) {
13071307
auto const& url = http_request->current_url();
1308-
authorization_value = encode_base64(DeprecatedString::formatted("{}:{}", url.username(), url.password()).bytes());
1308+
auto payload = DeprecatedString::formatted("{}:{}", url.username(), url.password());
1309+
authorization_value = TRY_OR_RETURN_OOM(realm, encode_base64(payload.bytes())).to_deprecated_string();
13091310
}
13101311

13111312
// 4. If authorizationValue is non-null, then append (`Authorization`, authorizationValue) to

Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ DeprecatedString HTMLCanvasElement::to_data_url(DeprecatedString const& type, [[
181181
dbgln("Gfx::PNGWriter failed to encode the HTMLCanvasElement: {}", encoded_bitmap_or_error.error());
182182
return {};
183183
}
184-
return AK::URL::create_with_data(type, encode_base64(encoded_bitmap_or_error.value()), true).to_deprecated_string();
184+
auto base64_encoded_or_error = encode_base64(encoded_bitmap_or_error.value());
185+
if (base64_encoded_or_error.is_error()) {
186+
// FIXME: propagate error
187+
return {};
188+
}
189+
return AK::URL::create_with_data(type, base64_encoded_or_error.release_value().to_deprecated_string(), true).to_deprecated_string();
185190
}
186191

187192
void HTMLCanvasElement::present()

Userland/Libraries/LibWeb/HTML/Window.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,8 +1423,8 @@ JS_DEFINE_NATIVE_FUNCTION(Window::btoa)
14231423
byte_string.append(code_point);
14241424
}
14251425

1426-
auto encoded = encode_base64(byte_string.span());
1427-
return JS::PrimitiveString::create(vm, move(encoded));
1426+
auto encoded = MUST(encode_base64(byte_string.span()));
1427+
return JS::PrimitiveString::create(vm, encoded.to_deprecated_string());
14281428
}
14291429

14301430
// https://html.spec.whatwg.org/multipage/interaction.html#dom-window-focus

0 commit comments

Comments
 (0)