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>
|
2026-06-21 14:03:10 -03:00
|
|
|
#include <AK/Utf16String.h>
|
2026-06-21 14:03:06 -03:00
|
|
|
#include <AK/Utf16View.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);
|
2026-06-21 14:03:06 -03:00
|
|
|
size_t size_required_to_decode_base64(Utf16View);
|
2024-09-01 12:49:23 -03:00
|
|
|
|
2025-05-03 08:08:26 -03:00
|
|
|
enum class LastChunkHandling {
|
|
|
|
|
Loose,
|
|
|
|
|
Strict,
|
|
|
|
|
StopBeforePartial,
|
|
|
|
|
};
|
|
|
|
|
|
Libraries: Clean up UTF-16 source text paths
Store parser errors, source range filenames, source code filenames,
module source, and Rust parser errors as UTF-16 where they flow back
into JavaScript-visible strings. Keep byte-oriented source buffers
byte-backed.
Remove temporary PrimitiveString, ByteString, and UTF-8 detours from
JSON, RegExp, module debug logging, print formatting, and tests.
2026-06-21 14:03:19 -03:00
|
|
|
enum class Base64DecodeError {
|
|
|
|
|
ExtraBits,
|
|
|
|
|
InputRemainder,
|
|
|
|
|
InvalidCharacter,
|
|
|
|
|
InvalidData,
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-03 08:08:26 -03:00
|
|
|
ErrorOr<ByteBuffer> decode_base64(StringView, LastChunkHandling = LastChunkHandling::Loose);
|
|
|
|
|
ErrorOr<ByteBuffer> decode_base64url(StringView, LastChunkHandling = LastChunkHandling::Loose);
|
2023-01-10 05:34:29 -03:00
|
|
|
|
2024-09-01 18:41:57 -03:00
|
|
|
struct InvalidBase64 {
|
Libraries: Clean up UTF-16 source text paths
Store parser errors, source range filenames, source code filenames,
module source, and Rust parser errors as UTF-16 where they flow back
into JavaScript-visible strings. Keep byte-oriented source buffers
byte-backed.
Remove temporary PrimitiveString, ByteString, and UTF-8 detours from
JSON, RegExp, module debug logging, print formatting, and tests.
2026-06-21 14:03:19 -03:00
|
|
|
Base64DecodeError decode_error { Base64DecodeError::InvalidData };
|
2024-09-01 18:41:57 -03:00
|
|
|
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.
|
2025-05-03 08:08:26 -03:00
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64_into(StringView, ByteBuffer&, LastChunkHandling = LastChunkHandling::Loose);
|
|
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64url_into(StringView, ByteBuffer&, LastChunkHandling = LastChunkHandling::Loose);
|
2026-06-21 14:03:06 -03:00
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64_into(Utf16View, ByteBuffer&, LastChunkHandling = LastChunkHandling::Loose);
|
|
|
|
|
ErrorOr<size_t, InvalidBase64> decode_base64url_into(Utf16View, ByteBuffer&, LastChunkHandling = LastChunkHandling::Loose);
|
2024-09-01 18:41:57 -03:00
|
|
|
|
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);
|
2026-06-21 14:03:10 -03:00
|
|
|
ErrorOr<Utf16String> encode_base64_to_utf16(ReadonlyBytes, OmitPadding = OmitPadding::No);
|
|
|
|
|
ErrorOr<Utf16String> encode_base64url_to_utf16(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;
|
2026-06-21 14:03:10 -03:00
|
|
|
using AK::encode_base64_to_utf16;
|
2024-03-20 09:21:59 -03:00
|
|
|
using AK::encode_base64url;
|
2026-06-21 14:03:10 -03:00
|
|
|
using AK::encode_base64url_to_utf16;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|