/* * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace AK { static ErrorOr decode_base64_impl(StringView input, simdutf::base64_options options) { ByteBuffer output; TRY(output.try_resize(simdutf::maximal_binary_length_from_base64(input.characters_without_null_termination(), input.length()))); auto result = simdutf::base64_to_binary( input.characters_without_null_termination(), input.length(), reinterpret_cast(output.data()), options); if (result.error != simdutf::SUCCESS) return Error::from_string_literal("Invalid base64-encoded data"); output.resize(result.count); return output; } static ErrorOr encode_base64_impl(StringView input, simdutf::base64_options options) { Vector output; // simdutf does not append padding to base64url encodings. We use the default encoding option here to allocate room // for the padding characters that we will later append ourselves if necessary. TRY(output.try_resize(simdutf::base64_length_from_binary(input.length(), simdutf::base64_default))); auto size_written = simdutf::binary_to_base64( input.characters_without_null_termination(), input.length(), reinterpret_cast(output.data()), options); if (options == simdutf::base64_url) { for (size_t i = size_written; i < output.size(); ++i) output[i] = '='; } return String::from_utf8_without_validation(output); } ErrorOr decode_base64(StringView input) { return decode_base64_impl(input, simdutf::base64_default); } ErrorOr decode_base64url(StringView input) { return decode_base64_impl(input, simdutf::base64_url); } ErrorOr encode_base64(ReadonlyBytes input) { return encode_base64_impl(input, simdutf::base64_default); } ErrorOr encode_base64url(ReadonlyBytes input) { return encode_base64_impl(input, simdutf::base64_url); } }