| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											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-11-21 21:59:12 +03:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sys/socket.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace IPC { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(bool& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(u8& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(u16& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(u32& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(u64& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(i8& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(i16& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(i32& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(i64& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(float& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_stream >> value; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Decoder::decode(String& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     i32 length = 0; | 
					
						
							|  |  |  |     m_stream >> length; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     if (m_stream.handle_any_error()) | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     if (length < 0) { | 
					
						
							|  |  |  |         value = {}; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (length == 0) { | 
					
						
							|  |  |  |         value = String::empty(); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     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; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     return !m_stream.handle_any_error(); | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 23:09:45 +03:30
										 |  |  | bool Decoder::decode(ByteBuffer& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     i32 length = 0; | 
					
						
							|  |  |  |     m_stream >> length; | 
					
						
							|  |  |  |     if (m_stream.handle_any_error()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     if (length < 0) { | 
					
						
							|  |  |  |         value = {}; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (length == 0) { | 
					
						
							|  |  |  |         value = ByteBuffer::create_uninitialized(0); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     value = ByteBuffer::create_uninitialized(length); | 
					
						
							|  |  |  |     m_stream >> value.bytes(); | 
					
						
							|  |  |  |     return !m_stream.handle_any_error(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 22:54:27 +02:00
										 |  |  | bool Decoder::decode(URL& value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     String string; | 
					
						
							|  |  |  |     if (!decode(string)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     value = URL(string); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  | bool Decoder::decode(Dictionary& dictionary) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     u64 size = 0; | 
					
						
							|  |  |  |     m_stream >> size; | 
					
						
							| 
									
										
										
										
											2020-09-20 13:00:54 +02:00
										 |  |  |     if (m_stream.handle_any_error()) | 
					
						
							| 
									
										
										
										
											2020-05-03 22:15:27 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-10-01 19:16:35 +02:00
										 |  |  |     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; | 
					
						
							|  |  |  |         if (!decode(key)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         String value; | 
					
						
							|  |  |  |         if (!decode(value)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         dictionary.add(move(key), move(value)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 16:09:48 -07:00
										 |  |  | bool Decoder::decode([[maybe_unused]] File& file) | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | { | 
					
						
							|  |  |  | #ifdef __serenity__
 | 
					
						
							| 
									
										
										
										
											2021-02-14 10:38:22 +01:00
										 |  |  |     int fd = recvfd(m_sockfd, O_CLOEXEC); | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |     if (fd < 0) { | 
					
						
							|  |  |  |         dbgln("recvfd: {}", strerror(errno)); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     file = File(fd, File::ConstructWithReceivedFileDescriptor); | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |     return true; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2020-12-20 16:09:48 -07:00
										 |  |  |     [[maybe_unused]] auto fd = m_sockfd; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |     warnln("fd passing is not supported on this platform, sorry :("); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 17:18:58 +01:00
										 |  |  | bool decode(Decoder& decoder, Core::AnonymousBuffer& buffer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     bool valid = false; | 
					
						
							|  |  |  |     if (!decoder.decode(valid)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     if (!valid) { | 
					
						
							|  |  |  |         buffer = {}; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     u32 size; | 
					
						
							|  |  |  |     if (!decoder.decode(size)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     IPC::File anon_file; | 
					
						
							|  |  |  |     if (!decoder.decode(anon_file)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buffer = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), size); | 
					
						
							|  |  |  |     return buffer.is_valid(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 12:04:35 +01:00
										 |  |  | } |