Decoder.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/MemoryStream.h>
  27. #include <AK/URL.h>
  28. #include <LibCore/AnonymousBuffer.h>
  29. #include <LibIPC/Decoder.h>
  30. #include <LibIPC/Dictionary.h>
  31. #include <LibIPC/File.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <sys/socket.h>
  35. namespace IPC {
  36. bool Decoder::decode(bool& value)
  37. {
  38. m_stream >> value;
  39. return !m_stream.handle_any_error();
  40. }
  41. bool Decoder::decode(u8& value)
  42. {
  43. m_stream >> value;
  44. return !m_stream.handle_any_error();
  45. }
  46. bool Decoder::decode(u16& value)
  47. {
  48. m_stream >> value;
  49. return !m_stream.handle_any_error();
  50. }
  51. bool Decoder::decode(u32& value)
  52. {
  53. m_stream >> value;
  54. return !m_stream.handle_any_error();
  55. }
  56. bool Decoder::decode(u64& value)
  57. {
  58. m_stream >> value;
  59. return !m_stream.handle_any_error();
  60. }
  61. bool Decoder::decode(i8& value)
  62. {
  63. m_stream >> value;
  64. return !m_stream.handle_any_error();
  65. }
  66. bool Decoder::decode(i16& value)
  67. {
  68. m_stream >> value;
  69. return !m_stream.handle_any_error();
  70. }
  71. bool Decoder::decode(i32& value)
  72. {
  73. m_stream >> value;
  74. return !m_stream.handle_any_error();
  75. }
  76. bool Decoder::decode(i64& value)
  77. {
  78. m_stream >> value;
  79. return !m_stream.handle_any_error();
  80. }
  81. bool Decoder::decode(float& value)
  82. {
  83. m_stream >> value;
  84. return !m_stream.handle_any_error();
  85. }
  86. bool Decoder::decode(String& value)
  87. {
  88. i32 length = 0;
  89. m_stream >> length;
  90. if (m_stream.handle_any_error())
  91. return false;
  92. if (length < 0) {
  93. value = {};
  94. return true;
  95. }
  96. if (length == 0) {
  97. value = String::empty();
  98. return true;
  99. }
  100. char* text_buffer = nullptr;
  101. auto text_impl = StringImpl::create_uninitialized(static_cast<size_t>(length), text_buffer);
  102. m_stream >> Bytes { text_buffer, static_cast<size_t>(length) };
  103. value = *text_impl;
  104. return !m_stream.handle_any_error();
  105. }
  106. bool Decoder::decode(ByteBuffer& value)
  107. {
  108. i32 length = 0;
  109. m_stream >> length;
  110. if (m_stream.handle_any_error())
  111. return false;
  112. if (length < 0) {
  113. value = {};
  114. return true;
  115. }
  116. if (length == 0) {
  117. value = ByteBuffer::create_uninitialized(0);
  118. return true;
  119. }
  120. value = ByteBuffer::create_uninitialized(length);
  121. m_stream >> value.bytes();
  122. return !m_stream.handle_any_error();
  123. }
  124. bool Decoder::decode(URL& value)
  125. {
  126. String string;
  127. if (!decode(string))
  128. return false;
  129. value = URL(string);
  130. return true;
  131. }
  132. bool Decoder::decode(Dictionary& dictionary)
  133. {
  134. u64 size = 0;
  135. m_stream >> size;
  136. if (m_stream.handle_any_error())
  137. return false;
  138. if (size >= (size_t)NumericLimits<i32>::max()) {
  139. ASSERT_NOT_REACHED();
  140. }
  141. for (size_t i = 0; i < size; ++i) {
  142. String key;
  143. if (!decode(key))
  144. return false;
  145. String value;
  146. if (!decode(value))
  147. return false;
  148. dictionary.add(move(key), move(value));
  149. }
  150. return true;
  151. }
  152. bool Decoder::decode([[maybe_unused]] File& file)
  153. {
  154. #ifdef __serenity__
  155. int fd = recvfd(m_sockfd, O_CLOEXEC);
  156. if (fd < 0) {
  157. dbgln("recvfd: {}", strerror(errno));
  158. return false;
  159. }
  160. file = File(fd, File::ConstructWithReceivedFileDescriptor);
  161. return true;
  162. #else
  163. [[maybe_unused]] auto fd = m_sockfd;
  164. warnln("fd passing is not supported on this platform, sorry :(");
  165. return false;
  166. #endif
  167. }
  168. bool decode(Decoder& decoder, Core::AnonymousBuffer& buffer)
  169. {
  170. bool valid = false;
  171. if (!decoder.decode(valid))
  172. return false;
  173. if (!valid) {
  174. buffer = {};
  175. return true;
  176. }
  177. u32 size;
  178. if (!decoder.decode(size))
  179. return false;
  180. IPC::File anon_file;
  181. if (!decoder.decode(anon_file))
  182. return false;
  183. buffer = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), size);
  184. return buffer.is_valid();
  185. }
  186. }