Decoder.cpp 3.6 KB

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