| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | #include <AK/ByteString.h>
 | 
					
						
							| 
									
										
										
										
											2021-07-04 14:44:34 +10:00
										 |  |  | #include <AK/Concepts.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-12 18:05:43 +02:00
										 |  |  | #include <AK/NumericLimits.h>
 | 
					
						
							| 
									
										
										
										
											2020-10-03 16:26:09 +03:30
										 |  |  | #include <AK/StdLibExtras.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-05 15:22:38 -05:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-01 20:49:29 +01:00
										 |  |  | #include <AK/Try.h>
 | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  | #include <AK/TypeList.h>
 | 
					
						
							|  |  |  | #include <AK/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-01 20:49:29 +01:00
										 |  |  | #include <LibCore/SharedCircularQueue.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-09 03:11:50 +01:00
										 |  |  | #include <LibCore/Socket.h>
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | #include <LibIPC/Concepts.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-01 20:49:29 +01:00
										 |  |  | #include <LibIPC/File.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  | #include <LibIPC/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | #include <LibIPC/Message.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | #include <LibURL/URL.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace IPC { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | inline ErrorOr<T> decode(Decoder&) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-03 16:26:09 +03:30
										 |  |  |     static_assert(DependentFalse<T>, "Base IPC::decoder() instantiated"); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Decoder { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-10 01:00:18 +01:00
										 |  |  |     Decoder(Stream& stream, Core::LocalSocket& socket) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |         : m_stream(stream) | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  |         , m_socket(socket) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     ErrorOr<T> decode(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     ErrorOr<void> decode_into(T& value) | 
					
						
							| 
									
										
										
										
											2022-04-03 00:43:22 +03:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-14 22:04:55 +01:00
										 |  |  |         value = TRY(m_stream.read_value<T>()); | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ErrorOr<void> decode_into(Bytes bytes) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-03-01 15:27:35 +01:00
										 |  |  |         TRY(m_stream.read_until_filled(bytes)); | 
					
						
							| 
									
										
										
										
											2022-04-03 00:43:22 +03:00
										 |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-04 07:08:29 -05:00
										 |  |  |     ErrorOr<size_t> decode_size(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-05 15:22:38 -05:00
										 |  |  |     Stream& stream() { return m_stream; } | 
					
						
							| 
									
										
										
										
											2023-02-08 23:05:44 +01:00
										 |  |  |     Core::LocalSocket& socket() { return m_socket; } | 
					
						
							| 
									
										
										
										
											2021-07-04 14:44:34 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-02-10 01:00:18 +01:00
										 |  |  |     Stream& m_stream; | 
					
						
							| 
									
										
										
										
											2023-02-08 23:05:44 +01:00
										 |  |  |     Core::LocalSocket& m_socket; | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | template<Arithmetic T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     T value { 0 }; | 
					
						
							|  |  |  |     TRY(decoder.decode_into(value)); | 
					
						
							|  |  |  |     return value; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-12 18:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | template<Enum T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto value = TRY(decoder.decode<UnderlyingType<T>>()); | 
					
						
							|  |  |  |     return static_cast<T>(value); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-01 20:49:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-05 15:22:38 -05:00
										 |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<String> decode(Decoder&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | ErrorOr<ByteString> decode(Decoder&); | 
					
						
							| 
									
										
										
										
											2022-02-01 20:49:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<ByteBuffer> decode(Decoder&); | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<JsonValue> decode(Decoder&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 13:51:37 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-03-13 16:30:34 +01:00
										 |  |  | ErrorOr<Duration> decode(Decoder&); | 
					
						
							| 
									
										
										
										
											2023-02-24 13:51:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 22:06:22 +01:00
										 |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<UnixDateTime> decode(Decoder&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | ErrorOr<URL::URL> decode(Decoder&); | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<File> decode(Decoder&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<Empty> decode(Decoder&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 09:12:18 -07:00
										 |  |  | template<Concepts::Array T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     T array {}; | 
					
						
							|  |  |  |     auto size = TRY(decoder.decode_size()); | 
					
						
							|  |  |  |     if (size != array.size()) | 
					
						
							|  |  |  |         return Error::from_string_literal("Array size mismatch"); | 
					
						
							|  |  |  |     for (size_t i = 0; i < array.size(); ++i) | 
					
						
							|  |  |  |         array[i] = TRY(decoder.decode<typename T::ValueType>()); | 
					
						
							|  |  |  |     return array; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | template<Concepts::Vector T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     T vector; | 
					
						
							| 
									
										
										
										
											2023-01-04 07:08:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto size = TRY(decoder.decode_size()); | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  |     TRY(vector.try_ensure_capacity(size)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (size_t i = 0; i < size; ++i) { | 
					
						
							|  |  |  |         auto value = TRY(decoder.decode<typename T::ValueType>()); | 
					
						
							|  |  |  |         vector.template unchecked_append(move(value)); | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  |     return vector; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<Concepts::HashMap T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     T hashmap; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-04 07:08:29 -05:00
										 |  |  |     auto size = TRY(decoder.decode_size()); | 
					
						
							|  |  |  |     TRY(hashmap.try_ensure_capacity(size)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  |     for (size_t i = 0; i < size; ++i) { | 
					
						
							|  |  |  |         auto key = TRY(decoder.decode<typename T::KeyType>()); | 
					
						
							|  |  |  |         auto value = TRY(decoder.decode<typename T::ValueType>()); | 
					
						
							|  |  |  |         TRY(hashmap.try_set(move(key), move(value))); | 
					
						
							| 
									
										
										
										
											2020-05-12 19:02:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  |     return hashmap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<Concepts::SharedSingleProducerCircularQueue T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto anon_file = TRY(decoder.decode<IPC::File>()); | 
					
						
							| 
									
										
										
										
											2023-01-28 20:12:17 +00:00
										 |  |  |     return T::create(anon_file.take_fd()); | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<Concepts::Optional T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (auto has_value = TRY(decoder.decode<bool>()); !has_value) | 
					
						
							|  |  |  |         return T {}; | 
					
						
							|  |  |  |     return T { TRY(decoder.decode<typename T::ValueType>()) }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Detail { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<Concepts::Variant T, size_t Index = 0> | 
					
						
							|  |  |  | ErrorOr<T> decode_variant(Decoder& decoder, size_t index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using ElementList = TypeList<T>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if constexpr (Index < ElementList::size) { | 
					
						
							|  |  |  |         if (index == Index) { | 
					
						
							|  |  |  |             using ElementType = typename ElementList::template Type<Index>; | 
					
						
							|  |  |  |             return T { TRY(decoder.decode<ElementType>()) }; | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return decode_variant<T, Index + 1>(decoder, index); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 20:40:33 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<Concepts::Variant T> | 
					
						
							|  |  |  | ErrorOr<T> decode(Decoder& decoder) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto index = TRY(decoder.decode<typename T::IndexType>()); | 
					
						
							|  |  |  |     return Detail::decode_variant<T>(decoder, index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This must be last so that it knows about the above specializations.
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | ErrorOr<T> Decoder::decode() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return IPC::decode<T>(*this); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |