2020-04-26 17:49:19 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2020-04-26 17:49:19 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-26 17:49:19 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-05-28 15:40:53 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-10-13 11:48:48 -03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2022-01-20 14:01:39 -03:00
|
|
|
#include <AK/Error.h>
|
2022-12-18 20:23:47 -03:00
|
|
|
#include <AK/String.h>
|
2020-10-13 11:48:48 -03:00
|
|
|
#include <AK/StringView.h>
|
2020-04-26 17:49:19 -03:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2024-09-01 12:49:23 -03:00
|
|
|
size_t size_required_to_decode_base64(StringView);
|
|
|
|
|
|
2024-07-15 16:25:08 -03:00
|
|
|
ErrorOr<ByteBuffer> decode_base64(StringView);
|
|
|
|
|
ErrorOr<ByteBuffer> decode_base64url(StringView);
|
2023-01-10 05:34:29 -03:00
|
|
|
|
2024-09-01 18:41:57 -03: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 17:12:21 -03: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 09:21:59 -03:00
|
|
|
|
2020-04-26 17:49:19 -03:00
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-04-26 17:49:19 -03:00
|
|
|
using AK::decode_base64;
|
2024-03-20 09:21:59 -03:00
|
|
|
using AK::decode_base64url;
|
2020-06-12 23:09:31 -03:00
|
|
|
using AK::encode_base64;
|
2024-03-20 09:21:59 -03:00
|
|
|
using AK::encode_base64url;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|