2020-04-26 20:49:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-26 20:49:19 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-28 18:40:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-05-14 11:41:01 +00:00
|
|
|
#include <AK/Array.h>
|
2020-10-13 14:48:48 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2022-01-20 17:01:39 +00:00
|
|
|
#include <AK/Error.h>
|
2022-12-18 23:23:47 +00:00
|
|
|
#include <AK/String.h>
|
2020-10-13 14:48:48 +00:00
|
|
|
#include <AK/StringView.h>
|
2020-04-26 20:49:19 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2023-01-10 08:34:29 +00:00
|
|
|
constexpr Array base64_alphabet = {
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
|
|
|
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
|
|
|
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
|
|
|
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
|
|
|
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
|
|
|
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
|
|
|
'4', '5', '6', '7', '8', '9', '+', '/'
|
|
|
|
};
|
|
|
|
|
|
|
|
consteval auto base64_lookup_table()
|
|
|
|
{
|
|
|
|
Array<i16, 256> table;
|
|
|
|
table.fill(-1);
|
|
|
|
for (size_t i = 0; i < base64_alphabet.size(); ++i) {
|
|
|
|
table[base64_alphabet[i]] = static_cast<i16>(i);
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2022-06-26 16:20:09 +00:00
|
|
|
[[nodiscard]] size_t calculate_base64_decoded_length(StringView);
|
2020-10-13 14:48:48 +00:00
|
|
|
|
2022-06-26 16:20:09 +00:00
|
|
|
[[nodiscard]] size_t calculate_base64_encoded_length(ReadonlyBytes);
|
2020-10-13 14:48:48 +00:00
|
|
|
|
2022-06-26 16:20:09 +00:00
|
|
|
[[nodiscard]] ErrorOr<ByteBuffer> decode_base64(StringView);
|
2020-06-13 02:09:31 +00:00
|
|
|
|
2022-12-18 23:23:47 +00:00
|
|
|
[[nodiscard]] ErrorOr<String> encode_base64(ReadonlyBytes);
|
2020-04-26 20:49:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-04-26 20:49:19 +00:00
|
|
|
using AK::decode_base64;
|
2020-06-13 02:09:31 +00:00
|
|
|
using AK::encode_base64;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|