Decoder.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <LibIPC/Decoder.h>
  29. #include <LibIPC/Dictionary.h>
  30. namespace IPC {
  31. bool Decoder::decode(bool& value)
  32. {
  33. m_stream >> value;
  34. return !m_stream.handle_any_error();
  35. }
  36. bool Decoder::decode(u8& value)
  37. {
  38. m_stream >> value;
  39. return !m_stream.handle_any_error();
  40. }
  41. bool Decoder::decode(u16& value)
  42. {
  43. m_stream >> value;
  44. return !m_stream.handle_any_error();
  45. }
  46. bool Decoder::decode(u32& value)
  47. {
  48. m_stream >> value;
  49. return !m_stream.handle_any_error();
  50. }
  51. bool Decoder::decode(u64& value)
  52. {
  53. m_stream >> value;
  54. return !m_stream.handle_any_error();
  55. }
  56. bool Decoder::decode(i8& value)
  57. {
  58. m_stream >> value;
  59. return !m_stream.handle_any_error();
  60. }
  61. bool Decoder::decode(i16& value)
  62. {
  63. m_stream >> value;
  64. return !m_stream.handle_any_error();
  65. }
  66. bool Decoder::decode(i32& value)
  67. {
  68. m_stream >> value;
  69. return !m_stream.handle_any_error();
  70. }
  71. bool Decoder::decode(i64& value)
  72. {
  73. m_stream >> value;
  74. return !m_stream.handle_any_error();
  75. }
  76. bool Decoder::decode(float& value)
  77. {
  78. m_stream >> value;
  79. return !m_stream.handle_any_error();
  80. }
  81. bool Decoder::decode(String& value)
  82. {
  83. i32 length = 0;
  84. m_stream >> length;
  85. if (m_stream.handle_any_error())
  86. return false;
  87. if (length < 0) {
  88. value = {};
  89. return true;
  90. }
  91. if (length == 0) {
  92. value = String::empty();
  93. return true;
  94. }
  95. char* text_buffer = nullptr;
  96. auto text_impl = StringImpl::create_uninitialized(static_cast<size_t>(length), text_buffer);
  97. m_stream >> Bytes { text_buffer, static_cast<size_t>(length) };
  98. value = *text_impl;
  99. return !m_stream.handle_any_error();
  100. }
  101. bool Decoder::decode(ByteBuffer& value)
  102. {
  103. i32 length = 0;
  104. m_stream >> length;
  105. if (m_stream.handle_any_error())
  106. return false;
  107. if (length < 0) {
  108. value = {};
  109. return true;
  110. }
  111. if (length == 0) {
  112. value = ByteBuffer::create_uninitialized(0);
  113. return true;
  114. }
  115. value = ByteBuffer::create_uninitialized(length);
  116. m_stream >> value.bytes();
  117. return !m_stream.handle_any_error();
  118. }
  119. bool Decoder::decode(URL& value)
  120. {
  121. String string;
  122. if (!decode(string))
  123. return false;
  124. value = URL(string);
  125. return true;
  126. }
  127. bool Decoder::decode(Dictionary& dictionary)
  128. {
  129. u64 size = 0;
  130. m_stream >> size;
  131. if (m_stream.handle_any_error())
  132. return false;
  133. if (size >= (size_t)NumericLimits<i32>::max()) {
  134. ASSERT_NOT_REACHED();
  135. }
  136. for (size_t i = 0; i < size; ++i) {
  137. String key;
  138. if (!decode(key))
  139. return false;
  140. String value;
  141. if (!decode(value))
  142. return false;
  143. dictionary.add(move(key), move(value));
  144. }
  145. return true;
  146. }
  147. }