Decoder.h 2.9 KB

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