Encoder.cpp 4.8 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/ByteBuffer.h>
  27. #include <AK/String.h>
  28. #include <AK/URL.h>
  29. #include <LibIPC/Dictionary.h>
  30. #include <LibIPC/Encoder.h>
  31. namespace IPC {
  32. Encoder& Encoder::operator<<(bool value)
  33. {
  34. return *this << (u8)value;
  35. }
  36. Encoder& Encoder::operator<<(u8 value)
  37. {
  38. m_buffer.append(value);
  39. return *this;
  40. }
  41. Encoder& Encoder::operator<<(u16 value)
  42. {
  43. m_buffer.ensure_capacity(m_buffer.size() + 2);
  44. m_buffer.unchecked_append((u8)value);
  45. m_buffer.unchecked_append((u8)(value >> 8));
  46. return *this;
  47. }
  48. Encoder& Encoder::operator<<(u32 value)
  49. {
  50. m_buffer.ensure_capacity(m_buffer.size() + 4);
  51. m_buffer.unchecked_append((u8)value);
  52. m_buffer.unchecked_append((u8)(value >> 8));
  53. m_buffer.unchecked_append((u8)(value >> 16));
  54. m_buffer.unchecked_append((u8)(value >> 24));
  55. return *this;
  56. }
  57. Encoder& Encoder::operator<<(u64 value)
  58. {
  59. m_buffer.ensure_capacity(m_buffer.size() + 8);
  60. m_buffer.unchecked_append((u8)value);
  61. m_buffer.unchecked_append((u8)(value >> 8));
  62. m_buffer.unchecked_append((u8)(value >> 16));
  63. m_buffer.unchecked_append((u8)(value >> 24));
  64. m_buffer.unchecked_append((u8)(value >> 32));
  65. m_buffer.unchecked_append((u8)(value >> 40));
  66. m_buffer.unchecked_append((u8)(value >> 48));
  67. m_buffer.unchecked_append((u8)(value >> 56));
  68. return *this;
  69. }
  70. Encoder& Encoder::operator<<(i8 value)
  71. {
  72. m_buffer.append((u8)value);
  73. return *this;
  74. }
  75. Encoder& Encoder::operator<<(i16 value)
  76. {
  77. m_buffer.ensure_capacity(m_buffer.size() + 2);
  78. m_buffer.unchecked_append((u8)value);
  79. m_buffer.unchecked_append((u8)(value >> 8));
  80. return *this;
  81. }
  82. Encoder& Encoder::operator<<(i32 value)
  83. {
  84. m_buffer.ensure_capacity(m_buffer.size() + 4);
  85. m_buffer.unchecked_append((u8)value);
  86. m_buffer.unchecked_append((u8)(value >> 8));
  87. m_buffer.unchecked_append((u8)(value >> 16));
  88. m_buffer.unchecked_append((u8)(value >> 24));
  89. return *this;
  90. }
  91. Encoder& Encoder::operator<<(i64 value)
  92. {
  93. m_buffer.ensure_capacity(m_buffer.size() + 8);
  94. m_buffer.unchecked_append((u8)value);
  95. m_buffer.unchecked_append((u8)(value >> 8));
  96. m_buffer.unchecked_append((u8)(value >> 16));
  97. m_buffer.unchecked_append((u8)(value >> 24));
  98. m_buffer.unchecked_append((u8)(value >> 32));
  99. m_buffer.unchecked_append((u8)(value >> 40));
  100. m_buffer.unchecked_append((u8)(value >> 48));
  101. m_buffer.unchecked_append((u8)(value >> 56));
  102. return *this;
  103. }
  104. Encoder& Encoder::operator<<(float value)
  105. {
  106. union bits {
  107. float as_float;
  108. u32 as_u32;
  109. } u;
  110. u.as_float = value;
  111. return *this << u.as_u32;
  112. }
  113. Encoder& Encoder::operator<<(const char* value)
  114. {
  115. return *this << StringView(value);
  116. }
  117. Encoder& Encoder::operator<<(const StringView& value)
  118. {
  119. m_buffer.append((const u8*)value.characters_without_null_termination(), value.length());
  120. return *this;
  121. }
  122. Encoder& Encoder::operator<<(const String& value)
  123. {
  124. if (value.is_null())
  125. return *this << (i32)-1;
  126. *this << static_cast<i32>(value.length());
  127. return *this << value.view();
  128. }
  129. Encoder& Encoder::operator<<(const ByteBuffer& value)
  130. {
  131. *this << static_cast<i32>(value.size());
  132. m_buffer.append(value.data(), value.size());
  133. return *this;
  134. }
  135. Encoder& Encoder::operator<<(const URL& value)
  136. {
  137. return *this << value.to_string();
  138. }
  139. Encoder& Encoder::operator<<(const Dictionary& dictionary)
  140. {
  141. *this << (u64)dictionary.size();
  142. dictionary.for_each_entry([this](auto& key, auto& value) {
  143. *this << key << value;
  144. });
  145. return *this;
  146. }
  147. }