2020-02-15 11:04:35 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2023-03-05 20:22:38 +00:00
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
2020-02-15 11:04:35 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-15 11:04:35 +00:00
|
|
|
*/
|
|
|
|
|
2022-11-07 21:30:55 +00:00
|
|
|
#include <AK/JsonValue.h>
|
2023-01-04 12:08:29 +00:00
|
|
|
#include <AK/NumericLimits.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-02-08 22:05:44 +00:00
|
|
|
#include <LibCore/Socket.h>
|
2020-02-15 11:04:35 +00:00
|
|
|
#include <LibIPC/Decoder.h>
|
2020-11-21 18:59:12 +00:00
|
|
|
#include <LibIPC/File.h>
|
2024-03-18 03:22:27 +00:00
|
|
|
#include <LibURL/URL.h>
|
2021-02-13 19:12:34 +00:00
|
|
|
#include <fcntl.h>
|
2020-02-15 11:04:35 +00:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2023-01-04 12:08:29 +00:00
|
|
|
ErrorOr<size_t> Decoder::decode_size()
|
|
|
|
{
|
|
|
|
return static_cast<size_t>(TRY(decode<u32>()));
|
|
|
|
}
|
|
|
|
|
2023-03-05 20:22:38 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<String> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
auto length = TRY(decoder.decode_size());
|
|
|
|
return String::from_stream(decoder.stream(), length);
|
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<ByteString> decode(Decoder& decoder)
|
2020-02-15 11:04:35 +00:00
|
|
|
{
|
2023-01-04 12:08:29 +00:00
|
|
|
auto length = TRY(decoder.decode_size());
|
2022-12-23 01:40:33 +00:00
|
|
|
if (length == 0)
|
2023-12-16 14:19:34 +00:00
|
|
|
return ByteString::empty();
|
2021-11-28 10:56:31 +00:00
|
|
|
|
2024-01-22 18:26:55 +00:00
|
|
|
return ByteString::create_and_overwrite(length, [&](Bytes bytes) -> ErrorOr<void> {
|
|
|
|
TRY(decoder.decode_into(bytes));
|
|
|
|
return {};
|
|
|
|
});
|
2020-02-15 11:04:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<ByteBuffer> decode(Decoder& decoder)
|
2020-11-07 19:39:45 +00:00
|
|
|
{
|
2023-01-04 12:08:29 +00:00
|
|
|
auto length = TRY(decoder.decode_size());
|
|
|
|
if (length == 0)
|
2022-12-23 01:40:33 +00:00
|
|
|
return ByteBuffer {};
|
2021-11-28 10:56:31 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(length));
|
|
|
|
auto bytes = buffer.bytes();
|
2021-11-28 10:56:31 +00:00
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
TRY(decoder.decode_into(bytes));
|
|
|
|
return buffer;
|
2020-11-07 19:39:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<JsonValue> decode(Decoder& decoder)
|
2022-11-07 21:30:55 +00:00
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
auto json = TRY(decoder.decode<ByteString>());
|
2022-12-23 01:40:33 +00:00
|
|
|
return JsonValue::from_string(json);
|
2022-11-07 21:30:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 18:51:37 +00:00
|
|
|
template<>
|
2024-07-17 05:44:07 +00:00
|
|
|
ErrorOr<AK::Duration> decode(Decoder& decoder)
|
2023-02-24 18:51:37 +00:00
|
|
|
{
|
|
|
|
auto nanoseconds = TRY(decoder.decode<i64>());
|
2023-03-13 15:30:34 +00:00
|
|
|
return AK::Duration::from_nanoseconds(nanoseconds);
|
2023-02-24 18:51:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:06:22 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<UnixDateTime> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
auto nanoseconds = TRY(decoder.decode<i64>());
|
|
|
|
return AK::UnixDateTime::from_nanoseconds_since_epoch(nanoseconds);
|
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
2024-03-18 03:22:27 +00:00
|
|
|
ErrorOr<URL::URL> decode(Decoder& decoder)
|
2020-06-07 20:54:27 +00:00
|
|
|
{
|
2024-05-05 08:35:02 +00:00
|
|
|
auto url_string = TRY(decoder.decode<ByteString>());
|
|
|
|
URL::URL url { url_string };
|
|
|
|
|
|
|
|
bool has_blob_url = TRY(decoder.decode<bool>());
|
|
|
|
if (!has_blob_url)
|
|
|
|
return url;
|
|
|
|
|
|
|
|
url.set_blob_url_entry(URL::BlobURLEntry {
|
|
|
|
.type = TRY(decoder.decode<String>()),
|
|
|
|
.byte_buffer = TRY(decoder.decode<ByteBuffer>()),
|
2024-10-05 03:54:27 +00:00
|
|
|
.environment_origin = TRY(decoder.decode<URL::Origin>()),
|
2024-05-05 08:35:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return url;
|
2020-06-07 20:54:27 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 02:33:34 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<URL::Origin> decode(Decoder& decoder)
|
|
|
|
{
|
|
|
|
auto scheme = TRY(decoder.decode<ByteString>());
|
|
|
|
auto host = TRY(decoder.decode<URL::Host>());
|
2024-10-05 05:08:18 +00:00
|
|
|
auto port = TRY(decoder.decode<Optional<u16>>());
|
2024-10-05 02:33:34 +00:00
|
|
|
|
|
|
|
return URL::Origin { move(scheme), move(host), port };
|
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<File> decode(Decoder& decoder)
|
2020-11-21 18:59:12 +00:00
|
|
|
{
|
2024-04-17 22:46:24 +00:00
|
|
|
auto file = TRY(decoder.files().try_dequeue());
|
|
|
|
auto fd = file.fd();
|
|
|
|
|
|
|
|
auto fd_flags = TRY(Core::System::fcntl(fd, F_GETFD));
|
|
|
|
TRY(Core::System::fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC));
|
|
|
|
return file;
|
2020-11-21 18:59:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:24:59 +00:00
|
|
|
template<>
|
2022-12-23 01:40:33 +00:00
|
|
|
ErrorOr<Empty> decode(Decoder&)
|
2021-01-16 16:18:58 +00:00
|
|
|
{
|
2022-12-23 01:40:33 +00:00
|
|
|
return Empty {};
|
2021-01-16 16:18:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:24:59 +00:00
|
|
|
template<>
|
2022-12-23 01:40:33 +00:00
|
|
|
ErrorOr<Core::AnonymousBuffer> decode(Decoder& decoder)
|
2021-04-15 14:33:28 +00:00
|
|
|
{
|
2022-12-23 01:40:33 +00:00
|
|
|
if (auto valid = TRY(decoder.decode<bool>()); !valid)
|
|
|
|
return Core::AnonymousBuffer {};
|
|
|
|
|
2023-01-04 12:08:29 +00:00
|
|
|
auto size = TRY(decoder.decode_size());
|
2022-12-23 01:40:33 +00:00
|
|
|
auto anon_file = TRY(decoder.decode<IPC::File>());
|
|
|
|
|
|
|
|
return Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), size);
|
2021-04-15 14:33:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:24:59 +00:00
|
|
|
template<>
|
2022-12-23 01:40:33 +00:00
|
|
|
ErrorOr<Core::DateTime> decode(Decoder& decoder)
|
2022-04-07 16:40:33 +00:00
|
|
|
{
|
2022-12-23 01:40:33 +00:00
|
|
|
auto timestamp = TRY(decoder.decode<i64>());
|
|
|
|
return Core::DateTime::from_timestamp(static_cast<time_t>(timestamp));
|
2022-04-07 16:40:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 01:40:33 +00:00
|
|
|
template<>
|
|
|
|
ErrorOr<Core::ProxyData> decode(Decoder& decoder)
|
2022-11-09 16:05:04 +00:00
|
|
|
{
|
2022-12-23 01:40:33 +00:00
|
|
|
auto type = TRY(decoder.decode<Core::ProxyData::Type>());
|
|
|
|
auto host_ipv4 = TRY(decoder.decode<u32>());
|
|
|
|
auto port = TRY(decoder.decode<int>());
|
|
|
|
|
|
|
|
return Core::ProxyData { type, host_ipv4, port };
|
2022-11-09 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 11:04:35 +00:00
|
|
|
}
|