2020-04-26 22:49:19 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2020-04-26 22:49:19 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-26 22:49:19 +02:00
|
|
|
*/
|
|
|
|
|
|
2020-08-10 23:50:38 +02:00
|
|
|
#include <AK/Base64.h>
|
2020-04-26 22:49:19 +02:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
2024-07-15 15:25:08 -04:00
|
|
|
#include <simdutf.h>
|
2024-03-24 08:55:53 -04:00
|
|
|
|
2024-07-15 15:25:08 -04:00
|
|
|
namespace AK {
|
2020-10-13 10:48:48 -04:00
|
|
|
|
2024-09-01 11:49:23 -04:00
|
|
|
size_t size_required_to_decode_base64(StringView input)
|
|
|
|
|
{
|
|
|
|
|
return simdutf::maximal_binary_length_from_base64(input.characters_without_null_termination(), input.length());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
static constexpr simdutf::last_chunk_handling_options to_simdutf_last_chunk_handling(LastChunkHandling last_chunk_handling)
|
2020-04-26 22:49:19 +02:00
|
|
|
{
|
2025-05-03 07:08:26 -04:00
|
|
|
switch (last_chunk_handling) {
|
|
|
|
|
case LastChunkHandling::Loose:
|
|
|
|
|
return simdutf::last_chunk_handling_options::loose;
|
|
|
|
|
case LastChunkHandling::Strict:
|
|
|
|
|
return simdutf::last_chunk_handling_options::strict;
|
|
|
|
|
case LastChunkHandling::StopBeforePartial:
|
|
|
|
|
return simdutf::last_chunk_handling_options::stop_before_partial;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ErrorOr<size_t, InvalidBase64> decode_base64_into_impl(StringView input, ByteBuffer& output, LastChunkHandling last_chunk_handling, simdutf::base64_options options)
|
|
|
|
|
{
|
|
|
|
|
static constexpr auto decode_up_to_bad_character = true;
|
|
|
|
|
auto output_length = output.size();
|
2020-04-26 22:49:19 +02:00
|
|
|
|
2024-09-01 17:41:57 -04:00
|
|
|
auto result = simdutf::base64_to_binary_safe(
|
2024-07-15 15:25:08 -04:00
|
|
|
input.characters_without_null_termination(),
|
|
|
|
|
input.length(),
|
|
|
|
|
reinterpret_cast<char*>(output.data()),
|
2024-09-01 17:41:57 -04:00
|
|
|
output_length,
|
2025-05-03 07:08:26 -04:00
|
|
|
options,
|
|
|
|
|
to_simdutf_last_chunk_handling(last_chunk_handling),
|
|
|
|
|
decode_up_to_bad_character);
|
2024-03-20 12:41:41 -04:00
|
|
|
|
2025-06-01 06:58:47 -04:00
|
|
|
if (result.error != simdutf::SUCCESS && result.error != simdutf::OUTPUT_BUFFER_TOO_SMALL) {
|
2024-09-01 17:41:57 -04:00
|
|
|
output.resize((result.count / 4) * 3);
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
auto error = [&]() {
|
|
|
|
|
switch (result.error) {
|
|
|
|
|
case simdutf::BASE64_EXTRA_BITS:
|
|
|
|
|
return Error::from_string_literal("Extra bits found at end of chunk");
|
|
|
|
|
case simdutf::BASE64_INPUT_REMAINDER:
|
|
|
|
|
return Error::from_string_literal("Invalid trailing data");
|
|
|
|
|
case simdutf::INVALID_BASE64_CHARACTER:
|
|
|
|
|
return Error::from_string_literal("Invalid base64 character");
|
|
|
|
|
default:
|
|
|
|
|
return Error::from_string_literal("Invalid base64-encoded data");
|
|
|
|
|
}
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
return InvalidBase64 { .error = move(error), .valid_input_bytes = result.count };
|
2024-09-01 17:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VERIFY(output_length <= output.size());
|
|
|
|
|
output.resize(output_length);
|
|
|
|
|
|
2025-06-01 06:58:47 -04:00
|
|
|
return result.count;
|
2024-09-01 17:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
static ErrorOr<ByteBuffer> decode_base64_impl(StringView input, LastChunkHandling last_chunk_handling, simdutf::base64_options options)
|
2024-09-01 17:41:57 -04:00
|
|
|
{
|
|
|
|
|
ByteBuffer output;
|
|
|
|
|
TRY(output.try_resize(size_required_to_decode_base64(input)));
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
if (auto result = decode_base64_into_impl(input, output, last_chunk_handling, options); result.is_error())
|
2024-09-01 17:41:57 -04:00
|
|
|
return result.release_error().error;
|
2020-04-26 22:49:19 +02:00
|
|
|
|
2024-03-20 12:41:41 -04:00
|
|
|
return output;
|
2020-04-26 22:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-15 15:25:08 -04:00
|
|
|
static ErrorOr<String> encode_base64_impl(StringView input, simdutf::base64_options options)
|
2020-06-12 22:09:31 -04:00
|
|
|
{
|
2024-03-20 11:18:27 -04:00
|
|
|
Vector<u8> output;
|
2024-08-30 09:41:01 -04:00
|
|
|
TRY(output.try_resize(simdutf::base64_length_from_binary(input.length(), options)));
|
2024-07-15 15:25:08 -04:00
|
|
|
|
2024-08-30 09:41:01 -04:00
|
|
|
simdutf::binary_to_base64(
|
2024-07-15 15:25:08 -04:00
|
|
|
input.characters_without_null_termination(),
|
|
|
|
|
input.length(),
|
|
|
|
|
reinterpret_cast<char*>(output.data()),
|
|
|
|
|
options);
|
|
|
|
|
|
2024-03-20 11:18:27 -04:00
|
|
|
return String::from_utf8_without_validation(output);
|
2020-06-12 22:09:31 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
ErrorOr<ByteBuffer> decode_base64(StringView input, LastChunkHandling last_chunk_handling)
|
2024-03-20 06:21:59 -06:00
|
|
|
{
|
2025-05-03 07:08:26 -04:00
|
|
|
return decode_base64_impl(input, last_chunk_handling, simdutf::base64_default);
|
2024-03-20 06:21:59 -06:00
|
|
|
}
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
ErrorOr<ByteBuffer> decode_base64url(StringView input, LastChunkHandling last_chunk_handling)
|
2024-03-20 06:21:59 -06:00
|
|
|
{
|
2025-05-03 07:08:26 -04:00
|
|
|
return decode_base64_impl(input, last_chunk_handling, simdutf::base64_url);
|
2024-03-20 06:21:59 -06:00
|
|
|
}
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64_into(StringView input, ByteBuffer& output, LastChunkHandling last_chunk_handling)
|
2024-09-01 17:41:57 -04:00
|
|
|
{
|
2025-05-03 07:08:26 -04:00
|
|
|
return decode_base64_into_impl(input, output, last_chunk_handling, simdutf::base64_default);
|
2024-09-01 17:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64url_into(StringView input, ByteBuffer& output, LastChunkHandling last_chunk_handling)
|
2024-09-01 17:41:57 -04:00
|
|
|
{
|
2025-05-03 07:08:26 -04:00
|
|
|
return decode_base64_into_impl(input, output, last_chunk_handling, simdutf::base64_url);
|
2024-09-01 17:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
2024-07-15 16:12:21 -04:00
|
|
|
ErrorOr<String> encode_base64(ReadonlyBytes input, OmitPadding omit_padding)
|
2024-03-20 06:21:59 -06:00
|
|
|
{
|
2024-07-15 16:12:21 -04:00
|
|
|
auto options = omit_padding == OmitPadding::Yes
|
|
|
|
|
? simdutf::base64_default_no_padding
|
|
|
|
|
: simdutf::base64_default;
|
|
|
|
|
|
|
|
|
|
return encode_base64_impl(input, options);
|
2024-03-20 06:21:59 -06:00
|
|
|
}
|
2024-07-15 15:25:08 -04:00
|
|
|
|
2024-07-15 16:12:21 -04:00
|
|
|
ErrorOr<String> encode_base64url(ReadonlyBytes input, OmitPadding omit_padding)
|
2024-03-20 06:21:59 -06:00
|
|
|
{
|
2024-07-15 16:12:21 -04:00
|
|
|
auto options = omit_padding == OmitPadding::Yes
|
|
|
|
|
? simdutf::base64_url
|
|
|
|
|
: simdutf::base64_url_with_padding;
|
|
|
|
|
|
|
|
|
|
return encode_base64_impl(input, options);
|
2024-03-20 06:21:59 -06:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 22:49:19 +02:00
|
|
|
}
|