byte_buffer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // Reads/Writes from/to buffer using network byte order (big endian)
  19. namespace rtc {
  20. template <class BufferClassT>
  21. class ByteBufferWriterT {
  22. public:
  23. ByteBufferWriterT() { Construct(nullptr, kDefaultCapacity); }
  24. ByteBufferWriterT(const char* bytes, size_t len) { Construct(bytes, len); }
  25. const char* Data() const { return buffer_.data(); }
  26. size_t Length() const { return buffer_.size(); }
  27. size_t Capacity() const { return buffer_.capacity(); }
  28. // Write value to the buffer. Resizes the buffer when it is
  29. // neccessary.
  30. void WriteUInt8(uint8_t val) {
  31. WriteBytes(reinterpret_cast<const char*>(&val), 1);
  32. }
  33. void WriteUInt16(uint16_t val) {
  34. uint16_t v = HostToNetwork16(val);
  35. WriteBytes(reinterpret_cast<const char*>(&v), 2);
  36. }
  37. void WriteUInt24(uint32_t val) {
  38. uint32_t v = HostToNetwork32(val);
  39. char* start = reinterpret_cast<char*>(&v);
  40. ++start;
  41. WriteBytes(start, 3);
  42. }
  43. void WriteUInt32(uint32_t val) {
  44. uint32_t v = HostToNetwork32(val);
  45. WriteBytes(reinterpret_cast<const char*>(&v), 4);
  46. }
  47. void WriteUInt64(uint64_t val) {
  48. uint64_t v = HostToNetwork64(val);
  49. WriteBytes(reinterpret_cast<const char*>(&v), 8);
  50. }
  51. // Serializes an unsigned varint in the format described by
  52. // https://developers.google.com/protocol-buffers/docs/encoding#varints
  53. // with the caveat that integers are 64-bit, not 128-bit.
  54. void WriteUVarint(uint64_t val) {
  55. while (val >= 0x80) {
  56. // Write 7 bits at a time, then set the msb to a continuation byte
  57. // (msb=1).
  58. char byte = static_cast<char>(val) | 0x80;
  59. WriteBytes(&byte, 1);
  60. val >>= 7;
  61. }
  62. char last_byte = static_cast<char>(val);
  63. WriteBytes(&last_byte, 1);
  64. }
  65. void WriteString(const std::string& val) {
  66. WriteBytes(val.c_str(), val.size());
  67. }
  68. void WriteBytes(const char* val, size_t len) { buffer_.AppendData(val, len); }
  69. // Reserves the given number of bytes and returns a char* that can be written
  70. // into. Useful for functions that require a char* buffer and not a
  71. // ByteBufferWriter.
  72. char* ReserveWriteBuffer(size_t len) {
  73. buffer_.SetSize(buffer_.size() + len);
  74. return buffer_.data();
  75. }
  76. // Resize the buffer to the specified |size|.
  77. void Resize(size_t size) { buffer_.SetSize(size); }
  78. // Clears the contents of the buffer. After this, Length() will be 0.
  79. void Clear() { buffer_.Clear(); }
  80. private:
  81. static constexpr size_t kDefaultCapacity = 4096;
  82. void Construct(const char* bytes, size_t size) {
  83. if (bytes) {
  84. buffer_.AppendData(bytes, size);
  85. } else {
  86. buffer_.EnsureCapacity(size);
  87. }
  88. }
  89. BufferClassT buffer_;
  90. // There are sensible ways to define these, but they aren't needed in our code
  91. // base.
  92. RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriterT);
  93. };
  94. class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> {
  95. public:
  96. ByteBufferWriter();
  97. ByteBufferWriter(const char* bytes, size_t len);
  98. private:
  99. RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriter);
  100. };
  101. // The ByteBufferReader references the passed data, i.e. the pointer must be
  102. // valid during the lifetime of the reader.
  103. class ByteBufferReader {
  104. public:
  105. ByteBufferReader(const char* bytes, size_t len);
  106. // Initializes buffer from a zero-terminated string.
  107. explicit ByteBufferReader(const char* bytes);
  108. explicit ByteBufferReader(const Buffer& buf);
  109. explicit ByteBufferReader(const ByteBufferWriter& buf);
  110. // Returns start of unprocessed data.
  111. const char* Data() const { return bytes_ + start_; }
  112. // Returns number of unprocessed bytes.
  113. size_t Length() const { return end_ - start_; }
  114. // Read a next value from the buffer. Return false if there isn't
  115. // enough data left for the specified type.
  116. bool ReadUInt8(uint8_t* val);
  117. bool ReadUInt16(uint16_t* val);
  118. bool ReadUInt24(uint32_t* val);
  119. bool ReadUInt32(uint32_t* val);
  120. bool ReadUInt64(uint64_t* val);
  121. bool ReadUVarint(uint64_t* val);
  122. bool ReadBytes(char* val, size_t len);
  123. // Appends next |len| bytes from the buffer to |val|. Returns false
  124. // if there is less than |len| bytes left.
  125. bool ReadString(std::string* val, size_t len);
  126. // Moves current position |size| bytes forward. Returns false if
  127. // there is less than |size| bytes left in the buffer. Consume doesn't
  128. // permanently remove data, so remembered read positions are still valid
  129. // after this call.
  130. bool Consume(size_t size);
  131. protected:
  132. void Construct(const char* bytes, size_t size);
  133. const char* bytes_;
  134. size_t size_;
  135. size_t start_;
  136. size_t end_;
  137. private:
  138. RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferReader);
  139. };
  140. } // namespace rtc
  141. #endif // RTC_BASE_BYTE_BUFFER_H_