/* * 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 == NumericLimits::max()) return DeprecatedString {}; if (length == 0) return DeprecatedString::empty(); char* text_buffer = nullptr; auto text_impl = StringImpl::create_uninitialized(length, text_buffer); Bytes bytes { text_buffer, length }; TRY(decoder.decode_into(bytes)); return DeprecatedString { *text_impl }; } 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 = TRY(decoder.decode()); return URL { url }; } template<> ErrorOr decode(Decoder& decoder) { int fd = TRY(decoder.socket().receive_fd(O_CLOEXEC)); return File { fd, File::ConstructWithReceivedFileDescriptor }; } 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 }; } }