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