2020-02-15 08:04:35 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2025-07-28 17:11:50 -03:00
|
|
|
* Copyright (c) 2023-2025, Tim Flynn <trflynn89@ladybird.org>
|
2020-02-15 08:04:35 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-15 08:04:35 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
#include <AK/ByteString.h>
|
2021-07-04 01:44:34 -03:00
|
|
|
#include <AK/Concepts.h>
|
2020-02-15 08:04:35 -03:00
|
|
|
#include <AK/Forward.h>
|
2024-04-17 19:46:24 -03:00
|
|
|
#include <AK/Queue.h>
|
2020-10-03 09:56:09 -03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2026-02-21 13:13:02 -03:00
|
|
|
#include <AK/Stream.h>
|
2023-03-05 17:22:38 -03:00
|
|
|
#include <AK/String.h>
|
2026-02-21 13:13:02 -03:00
|
|
|
#include <AK/Time.h>
|
2022-02-01 16:49:29 -03:00
|
|
|
#include <AK/Try.h>
|
2022-11-09 13:05:04 -03:00
|
|
|
#include <AK/TypeList.h>
|
|
|
|
|
#include <AK/Variant.h>
|
2025-06-13 20:20:29 -03:00
|
|
|
#include <LibCore/Forward.h>
|
2026-03-13 13:11:22 -03:00
|
|
|
#include <LibIPC/Attachment.h>
|
2022-12-22 22:40:33 -03:00
|
|
|
#include <LibIPC/Concepts.h>
|
2022-02-01 16:49:29 -03:00
|
|
|
#include <LibIPC/File.h>
|
2020-05-03 17:15:27 -03:00
|
|
|
#include <LibIPC/Forward.h>
|
2024-10-04 23:33:34 -03:00
|
|
|
#include <LibURL/Origin.h>
|
2024-03-18 00:22:27 -03:00
|
|
|
#include <LibURL/URL.h>
|
2020-02-15 08:04:35 -03:00
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2022-12-22 22:40:33 -03:00
|
|
|
inline ErrorOr<T> decode(Decoder&)
|
2020-02-15 08:04:35 -03:00
|
|
|
{
|
2020-10-03 09:56:09 -03:00
|
|
|
static_assert(DependentFalse<T>, "Base IPC::decoder() instantiated");
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-02-15 08:04:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Decoder {
|
|
|
|
|
public:
|
2026-03-13 13:11:22 -03:00
|
|
|
Decoder(Stream& stream, Queue<Attachment>& attachments)
|
2020-02-15 08:04:35 -03:00
|
|
|
: m_stream(stream)
|
2026-03-13 13:11:22 -03:00
|
|
|
, m_attachments(attachments)
|
2020-02-15 08:04:35 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<typename T>
|
|
|
|
|
ErrorOr<T> decode();
|
2020-02-15 08:04:35 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<typename T>
|
|
|
|
|
ErrorOr<void> decode_into(T& value)
|
2022-04-02 18:43:22 -03:00
|
|
|
{
|
2023-01-14 18:04:55 -03:00
|
|
|
value = TRY(m_stream.read_value<T>());
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> decode_into(Bytes bytes)
|
|
|
|
|
{
|
2023-03-01 11:27:35 -03:00
|
|
|
TRY(m_stream.read_until_filled(bytes));
|
2022-04-02 18:43:22 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:08:29 -03:00
|
|
|
ErrorOr<size_t> decode_size();
|
|
|
|
|
|
2023-03-05 17:22:38 -03:00
|
|
|
Stream& stream() { return m_stream; }
|
2026-03-13 13:11:22 -03:00
|
|
|
Queue<Attachment>& attachments() { return m_attachments; }
|
2021-07-04 01:44:34 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
private:
|
2023-02-09 21:00:18 -03:00
|
|
|
Stream& m_stream;
|
2026-03-13 13:11:22 -03:00
|
|
|
Queue<Attachment>& m_attachments;
|
2022-12-22 22:40:33 -03:00
|
|
|
};
|
2020-02-15 08:04:35 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<Arithmetic T>
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
T value { 0 };
|
|
|
|
|
TRY(decoder.decode_into(value));
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2020-05-12 13:05:43 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<Enum T>
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
auto value = TRY(decoder.decode<UnderlyingType<T>>());
|
|
|
|
|
return static_cast<T>(value);
|
|
|
|
|
}
|
2022-02-01 16:49:29 -03:00
|
|
|
|
2026-05-20 08:08:47 -03:00
|
|
|
template<Concepts::DistinctNumeric T>
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
return T { TRY(decoder.decode<typename T::Type>()) };
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-05 17:22:38 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<String> decode(Decoder&);
|
2025-07-28 17:11:50 -03:00
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
ErrorOr<Utf16String> decode(Decoder&);
|
2023-03-05 17:22:38 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<>
|
2023-12-16 11:19:34 -03:00
|
|
|
ErrorOr<ByteString> decode(Decoder&);
|
2022-02-01 16:49:29 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<ByteBuffer> decode(Decoder&);
|
2022-11-09 13:05:04 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<JsonValue> decode(Decoder&);
|
|
|
|
|
|
2023-02-24 15:51:37 -03:00
|
|
|
template<>
|
2024-07-17 02:44:07 -03:00
|
|
|
ErrorOr<AK::Duration> decode(Decoder&);
|
2023-02-24 15:51:37 -03:00
|
|
|
|
2023-03-13 18:06:22 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<UnixDateTime> decode(Decoder&);
|
|
|
|
|
|
2025-11-28 11:18:57 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<IPv4Address> decode(Decoder&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
ErrorOr<IPv6Address> decode(Decoder&);
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<>
|
2024-03-18 00:22:27 -03:00
|
|
|
ErrorOr<URL::URL> decode(Decoder&);
|
2022-12-22 22:40:33 -03:00
|
|
|
|
2024-10-04 23:33:34 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<URL::Origin> decode(Decoder&);
|
|
|
|
|
|
2024-11-27 12:12:17 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<URL::Host> decode(Decoder&);
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<File> decode(Decoder&);
|
|
|
|
|
|
2026-03-11 16:10:08 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<TransportHandle> decode(Decoder&);
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<Empty> decode(Decoder&);
|
|
|
|
|
|
2025-06-13 20:20:29 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<Core::AnonymousBuffer> decode(Decoder&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
ErrorOr<Core::ProxyData> decode(Decoder&);
|
|
|
|
|
|
2025-08-17 19:37:34 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<URL::BlobURLEntry::Blob> decode(Decoder&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
ErrorOr<URL::BlobURLEntry::MediaSource> decode(Decoder&);
|
|
|
|
|
|
2024-03-05 13:12:18 -03:00
|
|
|
template<Concepts::Array T>
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
T array {};
|
|
|
|
|
auto size = TRY(decoder.decode_size());
|
|
|
|
|
if (size != array.size())
|
|
|
|
|
return Error::from_string_literal("Array size mismatch");
|
|
|
|
|
for (size_t i = 0; i < array.size(); ++i)
|
|
|
|
|
array[i] = TRY(decoder.decode<typename T::ValueType>());
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<Concepts::Vector T>
|
2025-12-01 07:52:44 -03:00
|
|
|
requires(!IsArithmetic<typename T::ValueType>)
|
2022-12-22 22:40:33 -03:00
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
T vector;
|
2023-01-04 09:08:29 -03:00
|
|
|
|
|
|
|
|
auto size = TRY(decoder.decode_size());
|
2022-12-22 22:40:33 -03:00
|
|
|
TRY(vector.try_ensure_capacity(size));
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
|
auto value = TRY(decoder.decode<typename T::ValueType>());
|
2024-06-04 17:19:15 -03:00
|
|
|
vector.unchecked_append(move(value));
|
2022-11-09 13:05:04 -03:00
|
|
|
}
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
return vector;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 07:52:44 -03:00
|
|
|
template<Concepts::Vector T>
|
|
|
|
|
requires(IsArithmetic<typename T::ValueType>)
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
T vector;
|
|
|
|
|
auto size = TRY(decoder.decode_size());
|
2026-01-21 17:37:02 -03:00
|
|
|
if (Checked<size_t>::multiplication_would_overflow(size, sizeof(typename T::ValueType)))
|
|
|
|
|
return Error::from_string_literal("IPC decode: Vector size would overflow");
|
2026-01-21 17:49:28 -03:00
|
|
|
TRY(vector.try_resize(size));
|
2025-12-01 07:52:44 -03:00
|
|
|
TRY(decoder.decode_into({ reinterpret_cast<u8*>(vector.data()), size * sizeof(typename T::ValueType) }));
|
|
|
|
|
return vector;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
template<Concepts::HashMap T>
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
T hashmap;
|
|
|
|
|
|
2023-01-04 09:08:29 -03:00
|
|
|
auto size = TRY(decoder.decode_size());
|
|
|
|
|
TRY(hashmap.try_ensure_capacity(size));
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
|
auto key = TRY(decoder.decode<typename T::KeyType>());
|
|
|
|
|
auto value = TRY(decoder.decode<typename T::ValueType>());
|
|
|
|
|
TRY(hashmap.try_set(move(key), move(value)));
|
2020-05-12 14:02:44 -03:00
|
|
|
}
|
|
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
return hashmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Concepts::Optional T>
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
if (auto has_value = TRY(decoder.decode<bool>()); !has_value)
|
|
|
|
|
return T {};
|
|
|
|
|
return T { TRY(decoder.decode<typename T::ValueType>()) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
|
|
template<Concepts::Variant T, size_t Index = 0>
|
|
|
|
|
ErrorOr<T> decode_variant(Decoder& decoder, size_t index)
|
|
|
|
|
{
|
|
|
|
|
using ElementList = TypeList<T>;
|
|
|
|
|
|
|
|
|
|
if constexpr (Index < ElementList::size) {
|
|
|
|
|
if (index == Index) {
|
|
|
|
|
using ElementType = typename ElementList::template Type<Index>;
|
|
|
|
|
return T { TRY(decoder.decode<ElementType>()) };
|
2022-11-09 13:05:04 -03:00
|
|
|
}
|
2022-12-22 22:40:33 -03:00
|
|
|
|
|
|
|
|
return decode_variant<T, Index + 1>(decoder, index);
|
|
|
|
|
} else {
|
2026-01-21 19:35:43 -03:00
|
|
|
// Index was validated before calling decode_variant
|
|
|
|
|
VERIFY_NOT_REACHED();
|
2022-11-09 13:05:04 -03:00
|
|
|
}
|
2022-12-22 22:40:33 -03:00
|
|
|
}
|
2022-11-09 13:05:04 -03:00
|
|
|
|
2022-12-22 22:40:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Concepts::Variant T>
|
|
|
|
|
ErrorOr<T> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
auto index = TRY(decoder.decode<typename T::IndexType>());
|
2026-01-21 19:35:43 -03:00
|
|
|
if (index >= TypeList<T>::size)
|
|
|
|
|
return Error::from_string_literal("IPC decode: Invalid variant index");
|
2022-12-22 22:40:33 -03:00
|
|
|
return Detail::decode_variant<T>(decoder, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This must be last so that it knows about the above specializations.
|
|
|
|
|
template<typename T>
|
|
|
|
|
ErrorOr<T> Decoder::decode()
|
|
|
|
|
{
|
|
|
|
|
return IPC::decode<T>(*this);
|
|
|
|
|
}
|
2020-02-15 08:04:35 -03:00
|
|
|
|
|
|
|
|
}
|