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
|
|
|
|
|
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 {
|
|
|
|
|
2024-09-01 15:49:23 +00:00
|
|
|
size_t size_required_to_decode_base64(StringView);
|
|
|
|
|
2024-07-15 19:25:08 +00:00
|
|
|
ErrorOr<ByteBuffer> decode_base64(StringView);
|
|
|
|
ErrorOr<ByteBuffer> decode_base64url(StringView);
|
2023-01-10 08:34:29 +00:00
|
|
|
|
2024-09-01 21:41:57 +00:00
|
|
|
struct InvalidBase64 {
|
|
|
|
Error error;
|
|
|
|
size_t valid_input_bytes { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
// On success, these return the number of input bytes that were decoded. This might be less than the
|
|
|
|
// string length if the output buffer was not large enough.
|
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64_into(StringView, ByteBuffer&);
|
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64url_into(StringView, ByteBuffer&);
|
|
|
|
|
2024-07-15 20:12:21 +00:00
|
|
|
enum class OmitPadding {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorOr<String> encode_base64(ReadonlyBytes, OmitPadding = OmitPadding::No);
|
|
|
|
ErrorOr<String> encode_base64url(ReadonlyBytes, OmitPadding = OmitPadding::No);
|
2024-03-20 12:21:59 +00:00
|
|
|
|
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;
|
2024-03-20 12:21:59 +00:00
|
|
|
using AK::decode_base64url;
|
2020-06-13 02:09:31 +00:00
|
|
|
using AK::encode_base64;
|
2024-03-20 12:21:59 +00:00
|
|
|
using AK::encode_base64url;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|