/* * Copyright (c) 2020, Andreas Kling * Copyright (c) 2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include namespace IPC { ErrorOr Decoder::decode_size() { return static_cast(TRY(decode())); } template<> ErrorOr decode(Decoder& decoder) { auto length = TRY(decoder.decode_size()); return String::from_stream(decoder.stream(), length); } template<> ErrorOr decode(Decoder& decoder) { auto length = TRY(decoder.decode_size()); if (length == 0) return ByteString::empty(); return ByteString::create_and_overwrite(length, [&](Bytes bytes) -> ErrorOr { TRY(decoder.decode_into(bytes)); return {}; }); } template<> ErrorOr decode(Decoder& decoder) { auto length = TRY(decoder.decode_size()); if (length == 0) return ByteBuffer {}; auto buffer = TRY(ByteBuffer::create_uninitialized(length)); auto bytes = buffer.bytes(); TRY(decoder.decode_into(bytes)); return buffer; } template<> ErrorOr decode(Decoder& decoder) { auto json = TRY(decoder.decode()); return JsonValue::from_string(json); } template<> ErrorOr decode(Decoder& decoder) { auto nanoseconds = TRY(decoder.decode()); return AK::Duration::from_nanoseconds(nanoseconds); } template<> ErrorOr decode(Decoder& decoder) { auto nanoseconds = TRY(decoder.decode()); return AK::UnixDateTime::from_nanoseconds_since_epoch(nanoseconds); } template<> ErrorOr decode(Decoder& decoder) { auto url_string = TRY(decoder.decode()); URL::URL url { url_string }; bool has_blob_url = TRY(decoder.decode()); if (!has_blob_url) return url; url.set_blob_url_entry(URL::BlobURLEntry { .type = TRY(decoder.decode()), .byte_buffer = TRY(decoder.decode()), .environment_origin = TRY(decoder.decode()), }); return url; } template<> ErrorOr decode(Decoder& decoder) { auto scheme = TRY(decoder.decode()); auto host = TRY(decoder.decode()); auto port = TRY(decoder.decode>()); return URL::Origin { move(scheme), move(host), port }; } template<> ErrorOr decode(Decoder& decoder) { 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; } template<> ErrorOr decode(Decoder&) { return Empty {}; } template<> ErrorOr decode(Decoder& decoder) { if (auto valid = TRY(decoder.decode()); !valid) return Core::AnonymousBuffer {}; auto size = TRY(decoder.decode_size()); auto anon_file = TRY(decoder.decode()); return Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), size); } template<> ErrorOr decode(Decoder& decoder) { auto timestamp = TRY(decoder.decode()); return Core::DateTime::from_timestamp(static_cast(timestamp)); } template<> ErrorOr decode(Decoder& decoder) { auto type = TRY(decoder.decode()); auto host_ipv4 = TRY(decoder.decode()); auto port = TRY(decoder.decode()); return Core::ProxyData { type, host_ipv4, port }; } }