2020-04-26 20:49:19 +00:00
|
|
|
/*
|
2022-02-25 18:35:51 +00:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
|
2020-04-26 20:49:19 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-26 20:49:19 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-28 09:20:43 +00:00
|
|
|
#include <AK/Assertions.h>
|
2020-08-10 21:50:38 +00:00
|
|
|
#include <AK/Base64.h>
|
2022-12-28 09:20:43 +00:00
|
|
|
#include <AK/Error.h>
|
2020-07-22 15:39:07 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-04-26 20:49:19 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
size_t calculate_base64_decoded_length(StringView input)
|
2020-10-13 14:48:48 +00:00
|
|
|
{
|
2024-03-24 12:55:53 +00:00
|
|
|
auto length = input.length() * 3 / 4;
|
|
|
|
|
|
|
|
if (input.ends_with("="sv))
|
|
|
|
--length;
|
|
|
|
if (input.ends_with("=="sv))
|
|
|
|
--length;
|
|
|
|
|
|
|
|
return length;
|
2020-10-13 14:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t calculate_base64_encoded_length(ReadonlyBytes input)
|
|
|
|
{
|
|
|
|
return ((4 * input.size() / 3) + 3) & ~3;
|
|
|
|
}
|
|
|
|
|
2024-03-23 21:39:59 +00:00
|
|
|
static ErrorOr<ByteBuffer> decode_base64_impl(StringView input, ReadonlySpan<i16> alphabet_lookup_table)
|
2020-04-26 20:49:19 +00:00
|
|
|
{
|
2024-03-24 12:51:44 +00:00
|
|
|
input = input.trim_whitespace();
|
|
|
|
|
2024-03-24 12:59:00 +00:00
|
|
|
if (input.length() % 4 != 0)
|
|
|
|
return Error::from_string_literal("Invalid length of Base64 encoded string");
|
|
|
|
|
2024-03-24 12:51:44 +00:00
|
|
|
auto get = [&](size_t offset, bool* is_padding) -> ErrorOr<u8> {
|
2020-04-26 20:49:19 +00:00
|
|
|
if (offset >= input.length())
|
|
|
|
return 0;
|
2024-03-24 12:51:44 +00:00
|
|
|
|
|
|
|
auto ch = static_cast<unsigned char>(input[offset]);
|
2022-02-25 18:35:51 +00:00
|
|
|
if (ch == '=') {
|
2021-10-23 13:43:59 +00:00
|
|
|
if (!is_padding)
|
2022-01-20 17:18:17 +00:00
|
|
|
return Error::from_string_literal("Invalid '=' character outside of padding in base64 data");
|
2021-10-23 13:43:59 +00:00
|
|
|
*is_padding = true;
|
2020-04-26 20:49:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-03-24 12:51:44 +00:00
|
|
|
|
2022-03-14 02:59:29 +00:00
|
|
|
i16 result = alphabet_lookup_table[ch];
|
2021-10-23 13:43:59 +00:00
|
|
|
if (result < 0)
|
2022-01-20 17:18:17 +00:00
|
|
|
return Error::from_string_literal("Invalid character in base64 data");
|
2021-10-23 13:43:59 +00:00
|
|
|
VERIFY(result < 256);
|
|
|
|
return { result };
|
2020-04-26 20:49:19 +00:00
|
|
|
};
|
|
|
|
|
2024-03-20 16:41:41 +00:00
|
|
|
ByteBuffer output;
|
|
|
|
TRY(output.try_resize(calculate_base64_decoded_length(input)));
|
|
|
|
|
|
|
|
size_t input_offset = 0;
|
|
|
|
size_t output_offset = 0;
|
2020-04-26 20:49:19 +00:00
|
|
|
|
2024-03-20 16:41:41 +00:00
|
|
|
while (input_offset < input.length()) {
|
2020-04-26 20:49:19 +00:00
|
|
|
bool in2_is_padding = false;
|
|
|
|
bool in3_is_padding = false;
|
|
|
|
|
2024-03-24 12:51:44 +00:00
|
|
|
u8 const in0 = TRY(get(input_offset++, nullptr));
|
|
|
|
u8 const in1 = TRY(get(input_offset++, nullptr));
|
|
|
|
u8 const in2 = TRY(get(input_offset++, &in2_is_padding));
|
|
|
|
u8 const in3 = TRY(get(input_offset++, &in3_is_padding));
|
2020-04-26 20:49:19 +00:00
|
|
|
|
2024-03-20 16:41:41 +00:00
|
|
|
output[output_offset++] = (in0 << 2) | ((in1 >> 4) & 3);
|
2020-04-26 20:49:19 +00:00
|
|
|
|
|
|
|
if (!in2_is_padding)
|
2024-03-20 16:41:41 +00:00
|
|
|
output[output_offset++] = ((in1 & 0xf) << 4) | ((in2 >> 2) & 0xf);
|
|
|
|
|
2020-04-26 20:49:19 +00:00
|
|
|
if (!in3_is_padding)
|
2024-03-20 16:41:41 +00:00
|
|
|
output[output_offset++] = ((in2 & 0x3) << 6) | in3;
|
2020-04-26 20:49:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 16:41:41 +00:00
|
|
|
return output;
|
2020-04-26 20:49:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 21:39:59 +00:00
|
|
|
static ErrorOr<String> encode_base64_impl(ReadonlyBytes input, ReadonlySpan<char> alphabet)
|
2020-06-13 02:09:31 +00:00
|
|
|
{
|
2024-03-20 15:18:27 +00:00
|
|
|
Vector<u8> output;
|
|
|
|
TRY(output.try_ensure_capacity(calculate_base64_encoded_length(input)));
|
2020-06-13 02:09:31 +00:00
|
|
|
|
2024-03-24 12:45:14 +00:00
|
|
|
auto get = [&](size_t const offset, bool* need_padding = nullptr) -> u8 {
|
2020-07-22 15:39:07 +00:00
|
|
|
if (offset >= input.size()) {
|
2020-06-13 02:09:31 +00:00
|
|
|
if (need_padding)
|
|
|
|
*need_padding = true;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-22 15:39:07 +00:00
|
|
|
return input[offset];
|
2020-06-13 02:09:31 +00:00
|
|
|
};
|
|
|
|
|
2020-07-22 15:39:07 +00:00
|
|
|
for (size_t i = 0; i < input.size(); i += 3) {
|
2020-06-13 02:09:31 +00:00
|
|
|
bool is_8bit = false;
|
|
|
|
bool is_16bit = false;
|
|
|
|
|
2024-03-24 12:45:14 +00:00
|
|
|
u8 const in0 = get(i);
|
|
|
|
u8 const in1 = get(i + 1, &is_16bit);
|
|
|
|
u8 const in2 = get(i + 2, &is_8bit);
|
2020-06-13 02:09:31 +00:00
|
|
|
|
2024-03-24 12:45:14 +00:00
|
|
|
u8 const index0 = (in0 >> 2) & 0x3f;
|
|
|
|
u8 const index1 = ((in0 << 4) | (in1 >> 4)) & 0x3f;
|
|
|
|
u8 const index2 = ((in1 << 2) | (in2 >> 6)) & 0x3f;
|
|
|
|
u8 const index3 = in2 & 0x3f;
|
2020-06-13 02:09:31 +00:00
|
|
|
|
2024-03-20 15:18:27 +00:00
|
|
|
output.unchecked_append(alphabet[index0]);
|
|
|
|
output.unchecked_append(alphabet[index1]);
|
|
|
|
output.unchecked_append(is_16bit ? '=' : alphabet[index2]);
|
|
|
|
output.unchecked_append(is_8bit ? '=' : alphabet[index3]);
|
2020-06-13 02:09:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 15:18:27 +00:00
|
|
|
return String::from_utf8_without_validation(output);
|
2020-06-13 02:09:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 12:21:59 +00:00
|
|
|
ErrorOr<ByteBuffer> decode_base64(StringView input)
|
|
|
|
{
|
2024-03-23 21:39:59 +00:00
|
|
|
static constexpr auto lookup_table = base64_lookup_table();
|
|
|
|
return decode_base64_impl(input, lookup_table);
|
2024-03-20 12:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<ByteBuffer> decode_base64url(StringView input)
|
|
|
|
{
|
2024-03-23 21:39:59 +00:00
|
|
|
static constexpr auto lookup_table = base64url_lookup_table();
|
|
|
|
return decode_base64_impl(input, lookup_table);
|
2024-03-20 12:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<String> encode_base64(ReadonlyBytes input)
|
|
|
|
{
|
2024-03-23 21:39:59 +00:00
|
|
|
return encode_base64_impl(input, base64_alphabet);
|
2024-03-20 12:21:59 +00:00
|
|
|
}
|
|
|
|
ErrorOr<String> encode_base64url(ReadonlyBytes input)
|
|
|
|
{
|
2024-03-23 21:39:59 +00:00
|
|
|
return encode_base64_impl(input, base64url_alphabet);
|
2024-03-20 12:21:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:49:19 +00:00
|
|
|
}
|