Decoder.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <LibCore/Stream.h>
  13. #include <LibIPC/Forward.h>
  14. #include <LibIPC/Message.h>
  15. namespace IPC {
  16. template<typename T>
  17. inline ErrorOr<void> decode(Decoder&, T&)
  18. {
  19. static_assert(DependentFalse<T>, "Base IPC::decoder() instantiated");
  20. VERIFY_NOT_REACHED();
  21. }
  22. class Decoder {
  23. public:
  24. Decoder(InputMemoryStream& stream, Core::Stream::LocalSocket& socket)
  25. : m_stream(stream)
  26. , m_socket(socket)
  27. {
  28. }
  29. ErrorOr<void> decode(bool&);
  30. ErrorOr<void> decode(u8&);
  31. ErrorOr<void> decode(u16&);
  32. ErrorOr<void> decode(unsigned&);
  33. ErrorOr<void> decode(unsigned long&);
  34. ErrorOr<void> decode(unsigned long long&);
  35. ErrorOr<void> decode(i8&);
  36. ErrorOr<void> decode(i16&);
  37. ErrorOr<void> decode(i32&);
  38. ErrorOr<void> decode(i64&);
  39. ErrorOr<void> decode(float&);
  40. ErrorOr<void> decode(double&);
  41. ErrorOr<void> decode(String&);
  42. ErrorOr<void> decode(ByteBuffer&);
  43. ErrorOr<void> decode(URL&);
  44. ErrorOr<void> decode(Dictionary&);
  45. ErrorOr<void> decode(File&);
  46. template<typename K, typename V>
  47. ErrorOr<void> decode(HashMap<K, V>& hashmap)
  48. {
  49. u32 size;
  50. TRY(decode(size));
  51. if (size > NumericLimits<i32>::max())
  52. return Error::from_string_literal("IPC: Invalid HashMap size"sv);
  53. for (size_t i = 0; i < size; ++i) {
  54. K key;
  55. TRY(decode(key));
  56. V value;
  57. TRY(decode(value));
  58. TRY(hashmap.try_set(move(key), move(value)));
  59. }
  60. return {};
  61. }
  62. template<typename K, typename V>
  63. ErrorOr<void> decode(OrderedHashMap<K, V>& hashmap)
  64. {
  65. u32 size;
  66. TRY(decode(size));
  67. if (size > NumericLimits<i32>::max())
  68. return Error::from_string_literal("IPC: Invalid HashMap size"sv);
  69. for (size_t i = 0; i < size; ++i) {
  70. K key;
  71. TRY(decode(key));
  72. V value;
  73. TRY(decode(value));
  74. TRY(hashmap.try_set(move(key), move(value)));
  75. }
  76. return {};
  77. }
  78. template<Enum T>
  79. ErrorOr<void> decode(T& enum_value)
  80. {
  81. UnderlyingType<T> inner_value;
  82. TRY(decode(inner_value));
  83. enum_value = T(inner_value);
  84. return {};
  85. }
  86. template<typename T>
  87. ErrorOr<void> decode(T& value)
  88. {
  89. return IPC::decode(*this, value);
  90. }
  91. template<typename T>
  92. ErrorOr<void> decode(Vector<T>& vector)
  93. {
  94. u64 size;
  95. TRY(decode(size));
  96. if (size > NumericLimits<i32>::max())
  97. return Error::from_string_literal("IPC: Invalid Vector size"sv);
  98. VERIFY(vector.is_empty());
  99. TRY(vector.try_ensure_capacity(size));
  100. for (size_t i = 0; i < size; ++i) {
  101. T value;
  102. TRY(decode(value));
  103. vector.template unchecked_append(move(value));
  104. }
  105. return {};
  106. }
  107. template<typename T>
  108. ErrorOr<void> decode(Optional<T>& optional)
  109. {
  110. bool has_value;
  111. TRY(decode(has_value));
  112. if (!has_value) {
  113. optional = {};
  114. return {};
  115. }
  116. T value;
  117. TRY(decode(value));
  118. optional = move(value);
  119. return {};
  120. }
  121. private:
  122. InputMemoryStream& m_stream;
  123. Core::Stream::LocalSocket& m_socket;
  124. };
  125. }