2020-02-15 11:27:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-01-13 12:07:00 +01:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-02-15 11:27:47 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-15 11:27:47 +01:00
|
|
|
*/
|
|
|
|
|
2021-08-26 03:05:01 +02:00
|
|
|
#include <AK/BitCast.h>
|
2020-11-07 23:09:45 +03:30
|
|
|
#include <AK/ByteBuffer.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2022-11-07 16:30:55 -05:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2020-06-07 22:54:27 +02:00
|
|
|
#include <AK/URL.h>
|
2021-01-16 17:18:58 +01:00
|
|
|
#include <LibCore/AnonymousBuffer.h>
|
2021-04-15 10:33:28 -04:00
|
|
|
#include <LibCore/DateTime.h>
|
2022-04-07 21:10:33 +04:30
|
|
|
#include <LibCore/Proxy.h>
|
2020-05-03 22:15:27 +02:00
|
|
|
#include <LibIPC/Dictionary.h>
|
2020-02-15 11:27:47 +01:00
|
|
|
#include <LibIPC/Encoder.h>
|
2020-11-21 21:59:12 +03:00
|
|
|
#include <LibIPC/File.h>
|
2020-02-15 11:27:47 +01:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, float const& value)
|
2020-02-15 11:27:47 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
return encoder.encode(bit_cast<u32>(value));
|
2020-02-15 11:27:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, double const& value)
|
2020-02-15 11:27:47 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
return encoder.encode(bit_cast<u64>(value));
|
2020-02-15 11:27:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, StringView const& value)
|
2020-02-15 11:27:47 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
auto result = encoder.append(reinterpret_cast<u8 const*>(value.characters_without_null_termination()), value.length());
|
|
|
|
return !result.is_error();
|
2020-02-15 11:27:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, DeprecatedString const& value)
|
2020-02-15 11:27:47 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
if (value.is_null())
|
|
|
|
return encoder.encode(-1);
|
2021-08-26 03:05:01 +02:00
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
if (!encoder.encode(static_cast<i32>(value.length())))
|
|
|
|
return false;
|
|
|
|
return encoder.encode(value.view());
|
2020-02-15 11:27:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, ByteBuffer const& value)
|
2020-02-15 11:27:47 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
if (!encoder.encode(static_cast<i32>(value.size())))
|
|
|
|
return false;
|
2020-02-15 11:27:47 +01:00
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
auto result = encoder.append(value.data(), value.size());
|
|
|
|
return !result.is_error();
|
2020-02-15 11:27:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, JsonValue const& value)
|
2020-02-15 11:27:47 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
return encoder.encode(value.serialized<StringBuilder>());
|
2020-02-15 11:27:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, URL const& value)
|
2020-11-07 23:09:45 +03:30
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
return encoder.encode(value.to_deprecated_string());
|
2020-11-07 23:09:45 +03:30
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, Dictionary const& dictionary)
|
2022-11-07 16:30:55 -05:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
if (!encoder.encode(static_cast<u64>(dictionary.size())))
|
|
|
|
return false;
|
2022-11-07 16:30:55 -05:00
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
bool had_error = false;
|
2020-06-07 22:54:27 +02:00
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
dictionary.for_each_entry([&](auto const& key, auto const& value) {
|
|
|
|
if (had_error)
|
|
|
|
return;
|
|
|
|
if (!encoder.encode(key) || !encoder.encode(value))
|
|
|
|
had_error = true;
|
2020-05-03 22:15:27 +02:00
|
|
|
});
|
2023-01-01 22:51:39 -05:00
|
|
|
|
|
|
|
return !had_error;
|
2020-05-03 22:15:27 +02:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder& encoder, File const& file)
|
2020-11-21 21:59:12 +03:00
|
|
|
{
|
2021-05-02 12:28:20 +02:00
|
|
|
int fd = file.fd();
|
2023-01-01 22:51:39 -05:00
|
|
|
|
2021-05-02 12:28:20 +02:00
|
|
|
if (fd != -1) {
|
|
|
|
auto result = dup(fd);
|
|
|
|
if (result < 0) {
|
|
|
|
perror("dup");
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
fd = result;
|
|
|
|
}
|
2023-01-01 22:51:39 -05:00
|
|
|
|
|
|
|
if (encoder.append_file_descriptor(fd).is_error())
|
|
|
|
return false;
|
|
|
|
return true;
|
2020-11-21 21:59:12 +03:00
|
|
|
}
|
|
|
|
|
2023-01-01 22:51:39 -05:00
|
|
|
template<>
|
|
|
|
bool encode(Encoder&, Empty const&)
|
2022-11-09 17:05:04 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
return true;
|
2022-11-09 17:05:04 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:24:59 -05:00
|
|
|
template<>
|
2021-09-03 12:54:51 +02:00
|
|
|
bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
|
2021-01-16 17:18:58 +01:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
if (!encoder.encode(buffer.is_valid()))
|
|
|
|
return false;
|
|
|
|
|
2021-01-16 17:18:58 +01:00
|
|
|
if (buffer.is_valid()) {
|
2023-01-01 22:51:39 -05:00
|
|
|
if (!encoder.encode(static_cast<u32>(buffer.size())))
|
|
|
|
return false;
|
|
|
|
if (!encoder.encode(IPC::File { buffer.fd() }))
|
|
|
|
return false;
|
2021-01-16 17:18:58 +01:00
|
|
|
}
|
2023-01-01 22:51:39 -05:00
|
|
|
|
2021-01-16 17:18:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-15 10:33:28 -04:00
|
|
|
|
2022-11-15 11:24:59 -05:00
|
|
|
template<>
|
2021-09-03 12:54:51 +02:00
|
|
|
bool encode(Encoder& encoder, Core::DateTime const& datetime)
|
2021-04-15 10:33:28 -04:00
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
return encoder.encode(static_cast<i64>(datetime.timestamp()));
|
2021-04-15 10:33:28 -04:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:24:59 -05:00
|
|
|
template<>
|
2022-04-07 21:10:33 +04:30
|
|
|
bool encode(Encoder& encoder, Core::ProxyData const& proxy)
|
|
|
|
{
|
2023-01-01 22:51:39 -05:00
|
|
|
if (!encoder.encode(proxy.type))
|
|
|
|
return false;
|
|
|
|
if (!encoder.encode(proxy.host_ipv4))
|
|
|
|
return false;
|
|
|
|
if (!encoder.encode(proxy.port))
|
|
|
|
return false;
|
2022-04-07 21:10:33 +04:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-15 11:27:47 +01:00
|
|
|
}
|