2024-07-15 10:59:29 -04:00
|
|
|
/*
|
2025-10-01 12:50:17 -04:00
|
|
|
* Copyright (c) 2024-2025, Tim Flynn <trflynn89@ladybird.org>
|
2024-07-15 10:59:29 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2025-05-03 07:08:26 -04:00
|
|
|
#include <AK/Base64.h>
|
2024-08-31 09:05:40 -04:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/StringView.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Ptr.h>
|
2024-07-15 10:59:29 -04:00
|
|
|
#include <LibJS/Forward.h>
|
2024-08-31 09:05:40 -04:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
2024-07-15 10:59:29 -04:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
class Uint8ArrayConstructorHelpers {
|
2024-08-31 09:05:40 -04:00
|
|
|
public:
|
|
|
|
static void initialize(Realm&, Object& constructor);
|
|
|
|
|
|
|
|
private:
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(from_base64);
|
2024-09-01 18:37:12 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(from_hex);
|
2024-08-31 09:05:40 -04:00
|
|
|
};
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
class Uint8ArrayPrototypeHelpers {
|
2024-07-15 10:59:29 -04:00
|
|
|
public:
|
|
|
|
static void initialize(Realm&, Object& prototype);
|
|
|
|
|
|
|
|
private:
|
2024-08-31 10:24:01 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(set_from_base64);
|
2024-09-01 18:44:39 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(set_from_hex);
|
2025-10-01 12:50:17 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(to_base64);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(to_hex);
|
2024-07-15 10:59:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class Alphabet {
|
|
|
|
Base64,
|
|
|
|
Base64URL,
|
|
|
|
};
|
|
|
|
|
2024-08-31 09:05:40 -04:00
|
|
|
struct DecodeResult {
|
|
|
|
size_t read { 0 }; // [[Read]]
|
|
|
|
ByteBuffer bytes; // [[Bytes]]
|
|
|
|
Optional<Completion> error; // [[Error]]
|
|
|
|
};
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
ThrowCompletionOr<GC::Ref<TypedArrayBase>> validate_uint8_array(VM&);
|
|
|
|
ThrowCompletionOr<ByteBuffer> get_uint8_array_bytes(VM&, TypedArrayBase const&);
|
|
|
|
void set_uint8_array_bytes(TypedArrayBase&, ReadonlyBytes);
|
|
|
|
DecodeResult from_base64(VM&, StringView string, Alphabet alphabet, AK::LastChunkHandling last_chunk_handling, Optional<size_t> max_length = {});
|
|
|
|
DecodeResult from_hex(VM&, StringView string, Optional<size_t> max_length = {});
|
2024-07-15 10:59:29 -04:00
|
|
|
|
|
|
|
}
|