Decoder.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Concepts.h>
  8. #include <AK/Forward.h>
  9. #include <AK/NumericLimits.h>
  10. #include <AK/StdLibExtras.h>
  11. #include <AK/String.h>
  12. #include <AK/Try.h>
  13. #include <LibCore/SharedCircularQueue.h>
  14. #include <LibCore/Stream.h>
  15. #include <LibIPC/File.h>
  16. #include <LibIPC/Forward.h>
  17. #include <LibIPC/Message.h>
  18. namespace IPC {
  19. template<typename T>
  20. inline ErrorOr<void> decode(Decoder&, T&)
  21. {
  22. static_assert(DependentFalse<T>, "Base IPC::decoder() instantiated");
  23. VERIFY_NOT_REACHED();
  24. }
  25. class Decoder {
  26. public:
  27. Decoder(InputMemoryStream& stream, Core::Stream::LocalSocket& socket)
  28. : m_stream(stream)
  29. , m_socket(socket)
  30. {
  31. }
  32. ErrorOr<void> decode(bool&);
  33. ErrorOr<void> decode(u8&);
  34. ErrorOr<void> decode(u16&);
  35. ErrorOr<void> decode(unsigned&);
  36. ErrorOr<void> decode(unsigned long&);
  37. ErrorOr<void> decode(unsigned long long&);
  38. ErrorOr<void> decode(i8&);
  39. ErrorOr<void> decode(i16&);
  40. ErrorOr<void> decode(i32&);
  41. ErrorOr<void> decode(i64&);
  42. ErrorOr<void> decode(float&);
  43. ErrorOr<void> decode(double&);
  44. ErrorOr<void> decode(String&);
  45. ErrorOr<void> decode(ByteBuffer&);
  46. ErrorOr<void> decode(URL&);
  47. ErrorOr<void> decode(Dictionary&);
  48. ErrorOr<void> decode(File&);
  49. template<typename K, typename V>
  50. ErrorOr<void> decode(HashMap<K, V>& hashmap)
  51. {
  52. u32 size;
  53. TRY(decode(size));
  54. if (size > NumericLimits<i32>::max())
  55. return Error::from_string_literal("IPC: Invalid HashMap size"sv);
  56. for (size_t i = 0; i < size; ++i) {
  57. K key;
  58. TRY(decode(key));
  59. V value;
  60. TRY(decode(value));
  61. TRY(hashmap.try_set(move(key), move(value)));
  62. }
  63. return {};
  64. }
  65. template<typename K, typename V>
  66. ErrorOr<void> decode(OrderedHashMap<K, V>& hashmap)
  67. {
  68. u32 size;
  69. TRY(decode(size));
  70. if (size > NumericLimits<i32>::max())
  71. return Error::from_string_literal("IPC: Invalid HashMap size"sv);
  72. for (size_t i = 0; i < size; ++i) {
  73. K key;
  74. TRY(decode(key));
  75. V value;
  76. TRY(decode(value));
  77. TRY(hashmap.try_set(move(key), move(value)));
  78. }
  79. return {};
  80. }
  81. template<Enum T>
  82. ErrorOr<void> decode(T& enum_value)
  83. {
  84. UnderlyingType<T> inner_value;
  85. TRY(decode(inner_value));
  86. enum_value = T(inner_value);
  87. return {};
  88. }
  89. template<typename T>
  90. ErrorOr<void> decode(T& value)
  91. {
  92. return IPC::decode(*this, value);
  93. }
  94. template<typename T>
  95. ErrorOr<void> decode(Vector<T>& vector)
  96. {
  97. u64 size;
  98. TRY(decode(size));
  99. if (size > NumericLimits<i32>::max())
  100. return Error::from_string_literal("IPC: Invalid Vector size"sv);
  101. VERIFY(vector.is_empty());
  102. TRY(vector.try_ensure_capacity(size));
  103. for (size_t i = 0; i < size; ++i) {
  104. T value;
  105. TRY(decode(value));
  106. vector.template unchecked_append(move(value));
  107. }
  108. return {};
  109. }
  110. template<typename T, size_t Size>
  111. ErrorOr<void> decode(Core::SharedSingleProducerCircularQueue<T, Size>& queue)
  112. {
  113. // FIXME: We don't support decoding into valid queues.
  114. VERIFY(!queue.is_valid());
  115. IPC::File anon_file;
  116. TRY(decode(anon_file));
  117. queue = TRY((Core::SharedSingleProducerCircularQueue<T, Size>::try_create(anon_file.take_fd())));
  118. return {};
  119. }
  120. template<typename T>
  121. ErrorOr<void> decode(Optional<T>& optional)
  122. {
  123. bool has_value;
  124. TRY(decode(has_value));
  125. if (!has_value) {
  126. optional = {};
  127. return {};
  128. }
  129. T value;
  130. TRY(decode(value));
  131. optional = move(value);
  132. return {};
  133. }
  134. private:
  135. InputMemoryStream& m_stream;
  136. Core::Stream::LocalSocket& m_socket;
  137. };
  138. }