You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_BYTE_BUFFER_H_
  11. #define RTC_BASE_BYTE_BUFFER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <string>
  15. #include "rtc_base/buffer.h"
  16. #include "rtc_base/byte_order.h"
  17. #include "rtc_base/constructor_magic.h"
  18. namespace rtc {
  19. class ByteBuffer {
  20. public:
  21. enum ByteOrder {
  22. ORDER_NETWORK = 0, // Default, use network byte order (big endian).
  23. ORDER_HOST, // Use the native order of the host.
  24. };
  25. explicit ByteBuffer(ByteOrder byte_order) : byte_order_(byte_order) {}
  26. ByteOrder Order() const { return byte_order_; }
  27. private:
  28. ByteOrder byte_order_;
  29. RTC_DISALLOW_COPY_AND_ASSIGN(ByteBuffer);
  30. };
  31. template <class BufferClassT>
  32. class ByteBufferWriterT : public ByteBuffer {
  33. public:
  34. // |byte_order| defines order of bytes in the buffer.
  35. ByteBufferWriterT() : ByteBuffer(ORDER_NETWORK) {
  36. Construct(nullptr, kDefaultCapacity);
  37. }
  38. explicit ByteBufferWriterT(ByteOrder byte_order) : ByteBuffer(byte_order) {
  39. Construct(nullptr, kDefaultCapacity);
  40. }
  41. ByteBufferWriterT(const char* bytes, size_t len) : ByteBuffer(ORDER_NETWORK) {
  42. Construct(bytes, len);
  43. }
  44. ByteBufferWriterT(const char* bytes, size_t len, ByteOrder byte_order)
  45. : ByteBuffer(byte_order) {
  46. Construct(bytes, len);
  47. }
  48. const char* Data() const { return buffer_.data(); }
  49. size_t Length() const { return buffer_.size(); }
  50. size_t Capacity() const { return buffer_.capacity(); }
  51. // Write value to the buffer. Resizes the buffer when it is
  52. // neccessary.
  53. void WriteUInt8(uint8_t val) {
  54. WriteBytes(reinterpret_cast<const char*>(&val), 1);
  55. }
  56. void WriteUInt16(uint16_t val) {
  57. uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
  58. WriteBytes(reinterpret_cast<const char*>(&v), 2);
  59. }
  60. void WriteUInt24(uint32_t val) {
  61. uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
  62. char* start = reinterpret_cast<char*>(&v);
  63. if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
  64. ++start;
  65. }
  66. WriteBytes(start, 3);
  67. }
  68. void WriteUInt32(uint32_t val) {
  69. uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
  70. WriteBytes(reinterpret_cast<const char*>(&v), 4);
  71. }
  72. void WriteUInt64(uint64_t val) {
  73. uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
  74. WriteBytes(reinterpret_cast<const char*>(&v), 8);
  75. }
  76. // Serializes an unsigned varint in the format described by
  77. // https://developers.google.com/protocol-buffers/docs/encoding#varints
  78. // with the caveat that integers are 64-bit, not 128-bit.
  79. void WriteUVarint(uint64_t val) {
  80. while (val >= 0x80) {
  81. // Write 7 bits at a time, then set the msb to a continuation byte
  82. // (msb=1).
  83. char byte = static_cast<char>(val) | 0x80;
  84. WriteBytes(&byte, 1);
  85. val >>= 7;
  86. }
  87. char last_byte = static_cast<char>(val);
  88. WriteBytes(&last_byte, 1);
  89. }
  90. void WriteString(const std::string& val) {
  91. WriteBytes(val.c_str(), val.size());
  92. }
  93. void WriteBytes(const char* val, size_t len) { buffer_.AppendData(val, len); }
  94. // Reserves the given number of bytes and returns a char* that can be written
  95. // into. Useful for functions that require a char* buffer and not a
  96. // ByteBufferWriter.
  97. char* ReserveWriteBuffer(size_t len) {
  98. buffer_.SetSize(buffer_.size() + len);
  99. return buffer_.data();
  100. }
  101. // Resize the buffer to the specified |size|.
  102. void Resize(size_t size) { buffer_.SetSize(size); }
  103. // Clears the contents of the buffer. After this, Length() will be 0.
  104. void Clear() { buffer_.Clear(); }
  105. private:
  106. static constexpr size_t kDefaultCapacity = 4096;
  107. void Construct(const char* bytes, size_t size) {
  108. if (bytes) {
  109. buffer_.AppendData(bytes, size);
  110. } else {
  111. buffer_.EnsureCapacity(size);
  112. }
  113. }
  114. BufferClassT buffer_;
  115. // There are sensible ways to define these, but they aren't needed in our code
  116. // base.
  117. RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriterT);
  118. };
  119. class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> {
  120. public:
  121. // |byte_order| defines order of bytes in the buffer.
  122. ByteBufferWriter();
  123. explicit ByteBufferWriter(ByteOrder byte_order);
  124. ByteBufferWriter(const char* bytes, size_t len);
  125. ByteBufferWriter(const char* bytes, size_t len, ByteOrder byte_order);
  126. private:
  127. RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriter);
  128. };
  129. // The ByteBufferReader references the passed data, i.e. the pointer must be
  130. // valid during the lifetime of the reader.
  131. class ByteBufferReader : public ByteBuffer {
  132. public:
  133. ByteBufferReader(const char* bytes, size_t len);
  134. ByteBufferReader(const char* bytes, size_t len, ByteOrder byte_order);
  135. // Initializes buffer from a zero-terminated string.
  136. explicit ByteBufferReader(const char* bytes);
  137. explicit ByteBufferReader(const Buffer& buf);
  138. explicit ByteBufferReader(const ByteBufferWriter& buf);
  139. // Returns start of unprocessed data.
  140. const char* Data() const { return bytes_ + start_; }
  141. // Returns number of unprocessed bytes.
  142. size_t Length() const { return end_ - start_; }
  143. // Read a next value from the buffer. Return false if there isn't
  144. // enough data left for the specified type.
  145. bool ReadUInt8(uint8_t* val);
  146. bool ReadUInt16(uint16_t* val);
  147. bool ReadUInt24(uint32_t* val);
  148. bool ReadUInt32(uint32_t* val);
  149. bool ReadUInt64(uint64_t* val);
  150. bool ReadUVarint(uint64_t* val);
  151. bool ReadBytes(char* val, size_t len);
  152. // Appends next |len| bytes from the buffer to |val|. Returns false
  153. // if there is less than |len| bytes left.
  154. bool ReadString(std::string* val, size_t len);
  155. // Moves current position |size| bytes forward. Returns false if
  156. // there is less than |size| bytes left in the buffer. Consume doesn't
  157. // permanently remove data, so remembered read positions are still valid
  158. // after this call.
  159. bool Consume(size_t size);
  160. protected:
  161. void Construct(const char* bytes, size_t size);
  162. const char* bytes_;
  163. size_t size_;
  164. size_t start_;
  165. size_t end_;
  166. private:
  167. RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferReader);
  168. };
  169. } // namespace rtc
  170. #endif // RTC_BASE_BYTE_BUFFER_H_