2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-09-03 12:54:51 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-12-30 02:41:45 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-07-04 14:44:34 +10:00
|
|
|
#include <AK/Concepts.h>
|
2022-09-13 17:42:39 +02:00
|
|
|
#include <AK/HashMap.h>
|
2021-07-04 14:44:34 +10:00
|
|
|
#include <AK/StdLibExtras.h>
|
2022-11-09 17:05:04 +01:00
|
|
|
#include <AK/Variant.h>
|
2022-02-01 20:49:29 +01:00
|
|
|
#include <LibCore/SharedCircularQueue.h>
|
2023-01-01 22:51:39 -05:00
|
|
|
#include <LibIPC/Concepts.h>
|
2020-05-03 22:15:27 +02:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-02-06 14:54:09 +01:00
|
|
|
#include <LibIPC/Message.h>
|
2019-12-30 02:41:45 +01:00
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
namespace IPC {
|
|
|
|
|
|
2020-05-12 18:05:43 +02:00
|
|
|
template<typename T>
|
2022-11-15 10:01:23 -05:00
|
|
|
bool encode(Encoder&, T const&)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
2020-10-03 16:26:09 +03:30
|
|
|
static_assert(DependentFalse<T>, "Base IPC::encode() was instantiated");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-05-12 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
class Encoder {
|
2019-12-30 02:41:45 +01:00
|
|
|
public:
|
2020-02-05 19:57:18 +01:00
|
|
|
explicit Encoder(MessageBuffer& buffer)
|
2019-12-30 02:41:45 +01:00
|
|
|
: m_buffer(buffer)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<typename T>
|
|
|
|
|
Encoder& operator<<(T const& value)
|
2020-11-07 23:09:45 +03:30
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
encode(value);
|
2020-11-07 23:09:45 +03:30
|
|
|
return *this;
|
|
|
|
|
}
|
2019-12-30 02:41:45 +01:00
|
|
|
|
2020-05-12 18:05:43 +02:00
|
|
|
template<typename T>
|
2023-01-01 22:51:39 -05:00
|
|
|
bool encode(T const& value);
|
2020-05-12 18:05:43 +02:00
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
ErrorOr<void> extend_capacity(size_t capacity)
|
2022-02-01 20:49:29 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
return m_buffer.data.try_ensure_capacity(m_buffer.data.size() + capacity);
|
2022-02-01 20:49:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
void append(u8 value)
|
2022-11-09 17:05:04 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
m_buffer.data.unchecked_append(value);
|
2022-11-09 17:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
ErrorOr<void> append(u8 const* values, size_t count)
|
2021-07-04 14:44:34 +10:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
TRY(extend_capacity(count));
|
|
|
|
|
m_buffer.data.unchecked_append(values, count);
|
|
|
|
|
return {};
|
2021-07-04 14:44:34 +10:00
|
|
|
}
|
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
ErrorOr<void> append_file_descriptor(int fd)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
auto auto_fd = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AutoCloseFileDescriptor(fd)));
|
|
|
|
|
return m_buffer.fds.try_append(move(auto_fd));
|
2020-05-12 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-30 02:41:45 +01:00
|
|
|
private:
|
2021-11-29 02:18:58 +01:00
|
|
|
void encode_u32(u32);
|
|
|
|
|
void encode_u64(u64);
|
|
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
MessageBuffer& m_buffer;
|
2019-12-30 02:41:45 +01:00
|
|
|
};
|
2020-02-05 19:57:18 +01:00
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<Arithmetic T>
|
|
|
|
|
bool encode(Encoder& encoder, T const& value)
|
|
|
|
|
{
|
|
|
|
|
if (encoder.extend_capacity(sizeof(T)).is_error())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if constexpr (sizeof(T) == 1) {
|
|
|
|
|
encoder.append(static_cast<u8>(value));
|
|
|
|
|
} else if constexpr (sizeof(T) == 2) {
|
|
|
|
|
encoder.append(static_cast<u8>(value));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 8));
|
|
|
|
|
} else if constexpr (sizeof(T) == 4) {
|
|
|
|
|
encoder.append(static_cast<u8>(value));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 8));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 16));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 24));
|
|
|
|
|
} else if constexpr (sizeof(T) == 8) {
|
|
|
|
|
encoder.append(static_cast<u8>(value));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 8));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 16));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 24));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 32));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 40));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 48));
|
|
|
|
|
encoder.append(static_cast<u8>(value >> 56));
|
|
|
|
|
} else {
|
|
|
|
|
static_assert(DependentFalse<T>);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Enum T>
|
|
|
|
|
bool encode(Encoder& encoder, T const& value)
|
|
|
|
|
{
|
|
|
|
|
return encoder.encode(to_underlying(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, float const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, double const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, StringView const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, DeprecatedString const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, ByteBuffer const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, JsonValue const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, URL const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, Dictionary const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, File const&);
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool encode(Encoder&, Empty const&);
|
|
|
|
|
|
|
|
|
|
template<Concepts::Vector T>
|
|
|
|
|
bool encode(Encoder& encoder, T const& vector)
|
|
|
|
|
{
|
|
|
|
|
if (!encoder.encode(static_cast<u64>(vector.size())))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (auto const& value : vector) {
|
|
|
|
|
if (!encoder.encode(value))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Concepts::HashMap T>
|
|
|
|
|
bool encode(Encoder& encoder, T const& hashmap)
|
|
|
|
|
{
|
|
|
|
|
if (!encoder.encode(static_cast<u32>(hashmap.size())))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (auto it : hashmap) {
|
|
|
|
|
if (!encoder.encode(it.key))
|
|
|
|
|
return false;
|
|
|
|
|
if (!encoder.encode(it.value))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Concepts::SharedSingleProducerCircularQueue T>
|
|
|
|
|
bool encode(Encoder& encoder, T const& queue)
|
|
|
|
|
{
|
|
|
|
|
return encoder.encode(IPC::File { queue.fd() });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Concepts::Optional T>
|
|
|
|
|
bool encode(Encoder& encoder, T const& optional)
|
|
|
|
|
{
|
|
|
|
|
if (!encoder.encode(optional.has_value()))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (optional.has_value())
|
|
|
|
|
return encoder.encode(optional.value());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Concepts::Variant T>
|
|
|
|
|
bool encode(Encoder& encoder, T const& variant)
|
|
|
|
|
{
|
|
|
|
|
if (!encoder.encode(variant.index()))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return variant.visit([&](auto const& value) {
|
|
|
|
|
return encoder.encode(value);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This must be last so that it knows about the above specializations.
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool Encoder::encode(T const& value)
|
|
|
|
|
{
|
|
|
|
|
return IPC::encode(*this, value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
}
|