Decoder.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <string.h>
  33. #include <sys/socket.h>
  34. namespace IPC {
  35. bool Decoder::decode(bool& value)
  36. {
  37. m_stream >> value;
  38. return !m_stream.handle_any_error();
  39. }
  40. bool Decoder::decode(u8& value)
  41. {
  42. m_stream >> value;
  43. return !m_stream.handle_any_error();
  44. }
  45. bool Decoder::decode(u16& value)
  46. {
  47. m_stream >> value;
  48. return !m_stream.handle_any_error();
  49. }
  50. bool Decoder::decode(u32& value)
  51. {
  52. m_stream >> value;
  53. return !m_stream.handle_any_error();
  54. }
  55. bool Decoder::decode(u64& value)
  56. {
  57. m_stream >> value;
  58. return !m_stream.handle_any_error();
  59. }
  60. bool Decoder::decode(i8& value)
  61. {
  62. m_stream >> value;
  63. return !m_stream.handle_any_error();
  64. }
  65. bool Decoder::decode(i16& value)
  66. {
  67. m_stream >> value;
  68. return !m_stream.handle_any_error();
  69. }
  70. bool Decoder::decode(i32& value)
  71. {
  72. m_stream >> value;
  73. return !m_stream.handle_any_error();
  74. }
  75. bool Decoder::decode(i64& value)
  76. {
  77. m_stream >> value;
  78. return !m_stream.handle_any_error();
  79. }
  80. bool Decoder::decode(float& value)
  81. {
  82. m_stream >> value;
  83. return !m_stream.handle_any_error();
  84. }
  85. bool Decoder::decode(String& value)
  86. {
  87. i32 length = 0;
  88. m_stream >> length;
  89. if (m_stream.handle_any_error())
  90. return false;
  91. if (length < 0) {
  92. value = {};
  93. return true;
  94. }
  95. if (length == 0) {
  96. value = String::empty();
  97. return true;
  98. }
  99. char* text_buffer = nullptr;
  100. auto text_impl = StringImpl::create_uninitialized(static_cast<size_t>(length), text_buffer);
  101. m_stream >> Bytes { text_buffer, static_cast<size_t>(length) };
  102. value = *text_impl;
  103. return !m_stream.handle_any_error();
  104. }
  105. bool Decoder::decode(ByteBuffer& value)
  106. {
  107. i32 length = 0;
  108. m_stream >> length;
  109. if (m_stream.handle_any_error())
  110. return false;
  111. if (length < 0) {
  112. value = {};
  113. return true;
  114. }
  115. if (length == 0) {
  116. value = ByteBuffer::create_uninitialized(0);
  117. return true;
  118. }
  119. value = ByteBuffer::create_uninitialized(length);
  120. m_stream >> value.bytes();
  121. return !m_stream.handle_any_error();
  122. }
  123. bool Decoder::decode(URL& value)
  124. {
  125. String string;
  126. if (!decode(string))
  127. return false;
  128. value = URL(string);
  129. return true;
  130. }
  131. bool Decoder::decode(Dictionary& dictionary)
  132. {
  133. u64 size = 0;
  134. m_stream >> size;
  135. if (m_stream.handle_any_error())
  136. return false;
  137. if (size >= (size_t)NumericLimits<i32>::max()) {
  138. ASSERT_NOT_REACHED();
  139. }
  140. for (size_t i = 0; i < size; ++i) {
  141. String key;
  142. if (!decode(key))
  143. return false;
  144. String value;
  145. if (!decode(value))
  146. return false;
  147. dictionary.add(move(key), move(value));
  148. }
  149. return true;
  150. }
  151. bool Decoder::decode([[maybe_unused]] File& file)
  152. {
  153. #ifdef __serenity__
  154. int fd = recvfd(m_sockfd);
  155. if (fd < 0) {
  156. dbgln("recvfd: {}", strerror(errno));
  157. return false;
  158. }
  159. file = File(fd, File::ConstructWithReceivedFileDescriptor);
  160. return true;
  161. #else
  162. [[maybe_unused]] auto fd = m_sockfd;
  163. warnln("fd passing is not supported on this platform, sorry :(");
  164. return false;
  165. #endif
  166. }
  167. bool decode(Decoder& decoder, Core::AnonymousBuffer& buffer)
  168. {
  169. bool valid = false;
  170. if (!decoder.decode(valid))
  171. return false;
  172. if (!valid) {
  173. buffer = {};
  174. return true;
  175. }
  176. u32 size;
  177. if (!decoder.decode(size))
  178. return false;
  179. IPC::File anon_file;
  180. if (!decoder.decode(anon_file))
  181. return false;
  182. buffer = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), size);
  183. return buffer.is_valid();
  184. }
  185. }