| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-01-13 12:07:00 +01:00
										 |  |  |  * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-03-05 15:22:38 -05:00
										 |  |  |  * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-26 03:05:01 +02:00
										 |  |  | #include <AK/BitCast.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | #include <AK/ByteString.h>
 | 
					
						
							| 
									
										
										
										
											2022-11-07 16:30:55 -05:00
										 |  |  | #include <AK/JsonObject.h>
 | 
					
						
							|  |  |  | #include <AK/JsonValue.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-04 07:08:29 -05:00
										 |  |  | #include <AK/NumericLimits.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-05 15:22:38 -05:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-24 13:51:37 -05:00
										 |  |  | #include <AK/Time.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-07 22:54:27 +02:00
										 |  |  | #include <AK/URL.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-01-01 23:37:35 -05:00
										 |  |  | #include <LibCore/System.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | #include <LibIPC/Encoder.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | #include <LibIPC/File.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace IPC { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-04 07:08:29 -05:00
										 |  |  | ErrorOr<void> Encoder::encode_size(size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (static_cast<u64>(size) > static_cast<u64>(NumericLimits<u32>::max())) | 
					
						
							|  |  |  |         return Error::from_string_literal("Container exceeds the maximum allowed size"); | 
					
						
							|  |  |  |     return encode(static_cast<u32>(size)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, float const& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  |     return encoder.encode(bit_cast<u32>(value)); | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, double const& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  |     return encoder.encode(bit_cast<u64>(value)); | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-05 15:22:38 -05:00
										 |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<void> encode(Encoder& encoder, String const& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto bytes = value.bytes(); | 
					
						
							|  |  |  |     TRY(encoder.encode_size(bytes.size())); | 
					
						
							|  |  |  |     TRY(encoder.append(bytes.data(), bytes.size())); | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, StringView const& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-04 11:01:41 -05:00
										 |  |  |     // NOTE: Do not change this encoding without also updating LibC/netdb.cpp.
 | 
					
						
							|  |  |  |     if (value.is_null()) | 
					
						
							|  |  |  |         return encoder.encode(NumericLimits<u32>::max()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TRY(encoder.encode_size(value.length())); | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     TRY(encoder.append(reinterpret_cast<u8 const*>(value.characters_without_null_termination()), value.length())); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | ErrorOr<void> encode(Encoder& encoder, ByteString const& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-04 11:01:41 -05:00
										 |  |  |     return encoder.encode(value.view()); | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, ByteBuffer const& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-04 07:08:29 -05:00
										 |  |  |     TRY(encoder.encode_size(value.size())); | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     TRY(encoder.append(value.data(), value.size())); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, JsonValue const& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  |     return encoder.encode(value.serialized<StringBuilder>()); | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 13:51:37 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-03-13 16:30:34 +01:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, Duration const& value) | 
					
						
							| 
									
										
										
										
											2023-02-24 13:51:37 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     return encoder.encode(value.to_nanoseconds()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 22:06:22 +01:00
										 |  |  | template<> | 
					
						
							|  |  |  | ErrorOr<void> encode(Encoder& encoder, UnixDateTime const& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return encoder.encode(value.nanoseconds_since_epoch()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, URL const& value) | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     return encoder.encode(value.to_byte_string()); | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, File const& file) | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 12:28:20 +02:00
										 |  |  |     int fd = file.fd(); | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     if (fd != -1) | 
					
						
							|  |  |  |         fd = TRY(Core::System::dup(fd)); | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     TRY(encoder.append_file_descriptor(fd)); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder&, Empty const&) | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2022-11-09 17:05:04 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 11:24:59 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, Core::AnonymousBuffer const& buffer) | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     TRY(encoder.encode(buffer.is_valid())); | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  |     if (buffer.is_valid()) { | 
					
						
							| 
									
										
										
										
											2023-01-04 07:08:29 -05:00
										 |  |  |         TRY(encoder.encode_size(buffer.size())); | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |         TRY(encoder.encode(IPC::File { buffer.fd() })); | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-04-15 10:33:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 11:24:59 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, Core::DateTime const& datetime) | 
					
						
							| 
									
										
										
										
											2021-04-15 10:33:28 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 22:51:39 -05:00
										 |  |  |     return encoder.encode(static_cast<i64>(datetime.timestamp())); | 
					
						
							| 
									
										
										
										
											2021-04-15 10:33:28 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 11:24:59 -05:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  | ErrorOr<void> encode(Encoder& encoder, Core::ProxyData const& proxy) | 
					
						
							| 
									
										
										
										
											2022-04-07 21:10:33 +04:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 23:37:35 -05:00
										 |  |  |     TRY(encoder.encode(proxy.type)); | 
					
						
							|  |  |  |     TRY(encoder.encode(proxy.host_ipv4)); | 
					
						
							|  |  |  |     TRY(encoder.encode(proxy.port)); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2022-04-07 21:10:33 +04:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 11:27:47 +01:00
										 |  |  | } |