| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  | #include <AK/MemoryStream.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>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | #include <LibIPC/Decoder.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  | #include <LibIPC/Dictionary.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | #include <LibIPC/File.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-13 20:12:34 +01:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace IPC { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(bool& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(u8& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(u16& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 02:18:58 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(unsigned& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 02:18:58 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(unsigned long& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							|  |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<void> Decoder::decode(unsigned long long& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(i8& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(i16& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(i32& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(i64& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(float& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(double& value) | 
					
						
							| 
									
										
										
										
											2021-08-26 03:05:01 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2021-08-26 03:05:01 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(String& value) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     i32 length; | 
					
						
							|  |  |  |     TRY(decode(length)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |     if (length < 0) { | 
					
						
							|  |  |  |         value = {}; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (length == 0) { | 
					
						
							|  |  |  |         value = String::empty(); | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     char* text_buffer = nullptr; | 
					
						
							|  |  |  |     auto text_impl = StringImpl::create_uninitialized(static_cast<size_t>(length), text_buffer); | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     m_stream >> Bytes { text_buffer, static_cast<size_t>(length) }; | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |     value = *text_impl; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(ByteBuffer& value) | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     i32 length; | 
					
						
							|  |  |  |     TRY(decode(length)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  |     if (length < 0) { | 
					
						
							|  |  |  |         value = {}; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  |     } | 
					
						
							|  |  |  |     if (length == 0) { | 
					
						
							| 
									
										
										
										
											2021-09-06 03:29:52 +04:30
										 |  |  |         value = {}; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:47:39 +00:00
										 |  |  |     value = TRY(ByteBuffer::create_uninitialized(length)); | 
					
						
							| 
									
										
										
										
											2021-09-06 03:29:52 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  |     m_stream >> value.bytes(); | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return m_stream.try_handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(URL& value) | 
					
						
							| 
									
										
										
										
											2020-06-07 22:54:27 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     String string; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     TRY(decode(string)); | 
					
						
							| 
									
										
										
										
											2020-06-07 22:54:27 +02:00
										 |  |  |     value = URL(string); | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-06-07 22:54:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode(Dictionary& dictionary) | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     u64 size; | 
					
						
							|  |  |  |     TRY(decode(size)); | 
					
						
							|  |  |  |     if (size >= (size_t)NumericLimits<i32>::max()) | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (size_t i = 0; i < size; ++i) { | 
					
						
							|  |  |  |         String key; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |         TRY(decode(key)); | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  |         String value; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |         TRY(decode(value)); | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  |         dictionary.add(move(key), move(value)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> Decoder::decode([[maybe_unused]] File& file) | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  |     int fd = TRY(m_socket.receive_fd(O_CLOEXEC)); | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     file = File(fd, File::ConstructWithReceivedFileDescriptor); | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> decode(Decoder& decoder, Core::AnonymousBuffer& buffer) | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     bool valid; | 
					
						
							|  |  |  |     TRY(decoder.decode(valid)); | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  |     if (!valid) { | 
					
						
							|  |  |  |         buffer = {}; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     u32 size; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     TRY(decoder.decode(size)); | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  |     IPC::File anon_file; | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     TRY(decoder.decode(anon_file)); | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     buffer = TRY(Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), size)); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  | ErrorOr<void> decode(Decoder& decoder, Core::DateTime& datetime) | 
					
						
							| 
									
										
										
										
											2021-04-15 10:33:28 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     i64 timestamp; | 
					
						
							|  |  |  |     TRY(decoder.decode(timestamp)); | 
					
						
							| 
									
										
										
										
											2021-04-15 10:33:28 -04:00
										 |  |  |     datetime = Core::DateTime::from_timestamp(static_cast<time_t>(timestamp)); | 
					
						
							| 
									
										
										
										
											2021-11-28 11:56:31 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2021-04-15 10:33:28 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } |