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