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.

181 lines
4.2 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. #include "rtc_base/byte_buffer.h"
  11. #include <string.h>
  12. namespace rtc {
  13. ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {}
  14. ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
  15. : ByteBufferWriterT(byte_order) {}
  16. ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
  17. : ByteBufferWriterT(bytes, len) {}
  18. ByteBufferWriter::ByteBufferWriter(const char* bytes,
  19. size_t len,
  20. ByteOrder byte_order)
  21. : ByteBufferWriterT(bytes, len, byte_order) {}
  22. ByteBufferReader::ByteBufferReader(const char* bytes, size_t len)
  23. : ByteBuffer(ORDER_NETWORK) {
  24. Construct(bytes, len);
  25. }
  26. ByteBufferReader::ByteBufferReader(const char* bytes,
  27. size_t len,
  28. ByteOrder byte_order)
  29. : ByteBuffer(byte_order) {
  30. Construct(bytes, len);
  31. }
  32. ByteBufferReader::ByteBufferReader(const char* bytes)
  33. : ByteBuffer(ORDER_NETWORK) {
  34. Construct(bytes, strlen(bytes));
  35. }
  36. ByteBufferReader::ByteBufferReader(const Buffer& buf)
  37. : ByteBuffer(ORDER_NETWORK) {
  38. Construct(buf.data<char>(), buf.size());
  39. }
  40. ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf)
  41. : ByteBuffer(buf.Order()) {
  42. Construct(buf.Data(), buf.Length());
  43. }
  44. void ByteBufferReader::Construct(const char* bytes, size_t len) {
  45. bytes_ = bytes;
  46. size_ = len;
  47. start_ = 0;
  48. end_ = len;
  49. }
  50. bool ByteBufferReader::ReadUInt8(uint8_t* val) {
  51. if (!val)
  52. return false;
  53. return ReadBytes(reinterpret_cast<char*>(val), 1);
  54. }
  55. bool ByteBufferReader::ReadUInt16(uint16_t* val) {
  56. if (!val)
  57. return false;
  58. uint16_t v;
  59. if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
  60. return false;
  61. } else {
  62. *val = (Order() == ORDER_NETWORK) ? NetworkToHost16(v) : v;
  63. return true;
  64. }
  65. }
  66. bool ByteBufferReader::ReadUInt24(uint32_t* val) {
  67. if (!val)
  68. return false;
  69. uint32_t v = 0;
  70. char* read_into = reinterpret_cast<char*>(&v);
  71. if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
  72. ++read_into;
  73. }
  74. if (!ReadBytes(read_into, 3)) {
  75. return false;
  76. } else {
  77. *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
  78. return true;
  79. }
  80. }
  81. bool ByteBufferReader::ReadUInt32(uint32_t* val) {
  82. if (!val)
  83. return false;
  84. uint32_t v;
  85. if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
  86. return false;
  87. } else {
  88. *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
  89. return true;
  90. }
  91. }
  92. bool ByteBufferReader::ReadUInt64(uint64_t* val) {
  93. if (!val)
  94. return false;
  95. uint64_t v;
  96. if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
  97. return false;
  98. } else {
  99. *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v;
  100. return true;
  101. }
  102. }
  103. bool ByteBufferReader::ReadUVarint(uint64_t* val) {
  104. if (!val) {
  105. return false;
  106. }
  107. // Integers are deserialized 7 bits at a time, with each byte having a
  108. // continuation byte (msb=1) if there are more bytes to be read.
  109. uint64_t v = 0;
  110. for (int i = 0; i < 64; i += 7) {
  111. char byte;
  112. if (!ReadBytes(&byte, 1)) {
  113. return false;
  114. }
  115. // Read the first 7 bits of the byte, then offset by bits read so far.
  116. v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
  117. // True if the msb is not a continuation byte.
  118. if (static_cast<uint64_t>(byte) < 0x80) {
  119. *val = v;
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. bool ByteBufferReader::ReadString(std::string* val, size_t len) {
  126. if (!val)
  127. return false;
  128. if (len > Length()) {
  129. return false;
  130. } else {
  131. val->append(bytes_ + start_, len);
  132. start_ += len;
  133. return true;
  134. }
  135. }
  136. bool ByteBufferReader::ReadBytes(char* val, size_t len) {
  137. if (len > Length()) {
  138. return false;
  139. } else {
  140. memcpy(val, bytes_ + start_, len);
  141. start_ += len;
  142. return true;
  143. }
  144. }
  145. bool ByteBufferReader::Consume(size_t size) {
  146. if (size > Length())
  147. return false;
  148. start_ += size;
  149. return true;
  150. }
  151. } // namespace rtc