2020-02-15 10:27:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-01-13 11:07:00 +00:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-02-15 10:27:47 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-15 10:27:47 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-26 01:05:01 +00:00
|
|
|
#include <AK/BitCast.h>
|
2020-11-07 19:39:45 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2022-11-07 21:30:55 +00:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2023-01-04 12:08:29 +00:00
|
|
|
#include <AK/NumericLimits.h>
|
2020-06-07 20:54:27 +00:00
|
|
|
#include <AK/URL.h>
|
2021-01-16 16:18:58 +00:00
|
|
|
#include <LibCore/AnonymousBuffer.h>
|
2021-04-15 14:33:28 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2022-04-07 16:40:33 +00:00
|
|
|
#include <LibCore/Proxy.h>
|
2023-01-02 04:37:35 +00:00
|
|
|
#include <LibCore/System.h>
|
2020-05-03 20:15:27 +00:00
|
|
|
#include <LibIPC/Dictionary.h>
|
2020-02-15 10:27:47 +00:00
|
|
|
#include <LibIPC/Encoder.h>
|
2020-11-21 18:59:12 +00:00
|
|
|
#include <LibIPC/File.h>
|
2020-02-15 10:27:47 +00:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2023-01-04 12:08:29 +00:00
|
|
|
ErrorOr<void> Encoder::encode_size(size_t size)
|
|
|
|
{
|
|
|
|
if (static_cast<u64>(size) > static_cast<u64>(NumericLimits<u32>::max()))
|
|
|
|
return Error::from_string_literal("Container exceeds the maximum allowed size");
|
|
|
|
return encode(static_cast<u32>(size));
|
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, float const& value)
|
2020-02-15 10:27:47 +00:00
|
|
|
{
|
2023-01-02 03:51:39 +00:00
|
|
|
return encoder.encode(bit_cast<u32>(value));
|
2020-02-15 10:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, double const& value)
|
2020-02-15 10:27:47 +00:00
|
|
|
{
|
2023-01-02 03:51:39 +00:00
|
|
|
return encoder.encode(bit_cast<u64>(value));
|
2020-02-15 10:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, StringView const& value)
|
2020-02-15 10:27:47 +00:00
|
|
|
{
|
2023-01-04 16:01:41 +00:00
|
|
|
// NOTE: Do not change this encoding without also updating LibC/netdb.cpp.
|
|
|
|
if (value.is_null())
|
|
|
|
return encoder.encode(NumericLimits<u32>::max());
|
|
|
|
|
|
|
|
TRY(encoder.encode_size(value.length()));
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.append(reinterpret_cast<u8 const*>(value.characters_without_null_termination()), value.length()));
|
|
|
|
return {};
|
2020-02-15 10:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, DeprecatedString const& value)
|
2020-02-15 10:27:47 +00:00
|
|
|
{
|
2023-01-04 16:01:41 +00:00
|
|
|
return encoder.encode(value.view());
|
2020-02-15 10:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, ByteBuffer const& value)
|
2020-02-15 10:27:47 +00:00
|
|
|
{
|
2023-01-04 12:08:29 +00:00
|
|
|
TRY(encoder.encode_size(value.size()));
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.append(value.data(), value.size()));
|
|
|
|
return {};
|
2020-02-15 10:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, JsonValue const& value)
|
2020-02-15 10:27:47 +00:00
|
|
|
{
|
2023-01-02 03:51:39 +00:00
|
|
|
return encoder.encode(value.serialized<StringBuilder>());
|
2020-02-15 10:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, URL const& value)
|
2020-11-07 19:39:45 +00:00
|
|
|
{
|
2023-01-02 03:51:39 +00:00
|
|
|
return encoder.encode(value.to_deprecated_string());
|
2020-11-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, Dictionary const& dictionary)
|
2022-11-07 21:30:55 +00:00
|
|
|
{
|
2023-01-04 12:08:29 +00:00
|
|
|
TRY(encoder.encode_size(dictionary.size()));
|
2020-06-07 20:54:27 +00:00
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(dictionary.try_for_each_entry([&](auto const& key, auto const& value) -> ErrorOr<void> {
|
|
|
|
TRY(encoder.encode(key));
|
|
|
|
TRY(encoder.encode(value));
|
|
|
|
return {};
|
|
|
|
}));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
return {};
|
2020-05-03 20:15:27 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, File const& file)
|
2020-11-21 18:59:12 +00:00
|
|
|
{
|
2021-05-02 10:28:20 +00:00
|
|
|
int fd = file.fd();
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
if (fd != -1)
|
|
|
|
fd = TRY(Core::System::dup(fd));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.append_file_descriptor(fd));
|
|
|
|
return {};
|
2020-11-21 18:59:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 03:51:39 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder&, Empty const&)
|
2022-11-09 16:05:04 +00:00
|
|
|
{
|
2023-01-02 04:37:35 +00:00
|
|
|
return {};
|
2022-11-09 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:24:59 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
|
2021-01-16 16:18:58 +00:00
|
|
|
{
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.encode(buffer.is_valid()));
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2021-01-16 16:18:58 +00:00
|
|
|
if (buffer.is_valid()) {
|
2023-01-04 12:08:29 +00:00
|
|
|
TRY(encoder.encode_size(buffer.size()));
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.encode(IPC::File { buffer.fd() }));
|
2021-01-16 16:18:58 +00:00
|
|
|
}
|
2023-01-02 03:51:39 +00:00
|
|
|
|
2023-01-02 04:37:35 +00:00
|
|
|
return {};
|
2021-01-16 16:18:58 +00:00
|
|
|
}
|
2021-04-15 14:33:28 +00:00
|
|
|
|
2022-11-15 16:24:59 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, Core::DateTime const& datetime)
|
2021-04-15 14:33:28 +00:00
|
|
|
{
|
2023-01-02 03:51:39 +00:00
|
|
|
return encoder.encode(static_cast<i64>(datetime.timestamp()));
|
2021-04-15 14:33:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:24:59 +00:00
|
|
|
template<>
|
2023-01-02 04:37:35 +00:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, Core::ProxyData const& proxy)
|
2022-04-07 16:40:33 +00:00
|
|
|
{
|
2023-01-02 04:37:35 +00:00
|
|
|
TRY(encoder.encode(proxy.type));
|
|
|
|
TRY(encoder.encode(proxy.host_ipv4));
|
|
|
|
TRY(encoder.encode(proxy.port));
|
|
|
|
return {};
|
2022-04-07 16:40:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 10:27:47 +00:00
|
|
|
}
|