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.

250 lines
6.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  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,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/bytebuffer.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <cstring>
  31. #include "talk/base/basictypes.h"
  32. #include "talk/base/byteorder.h"
  33. namespace talk_base {
  34. static const int DEFAULT_SIZE = 4096;
  35. ByteBuffer::ByteBuffer() {
  36. Construct(NULL, DEFAULT_SIZE, ORDER_NETWORK);
  37. }
  38. ByteBuffer::ByteBuffer(ByteOrder byte_order) {
  39. Construct(NULL, DEFAULT_SIZE, byte_order);
  40. }
  41. ByteBuffer::ByteBuffer(const char* bytes, size_t len) {
  42. Construct(bytes, len, ORDER_NETWORK);
  43. }
  44. ByteBuffer::ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order) {
  45. Construct(bytes, len, byte_order);
  46. }
  47. ByteBuffer::ByteBuffer(const char* bytes) {
  48. Construct(bytes, strlen(bytes), ORDER_NETWORK);
  49. }
  50. void ByteBuffer::Construct(const char* bytes, size_t len,
  51. ByteOrder byte_order) {
  52. version_ = 0;
  53. start_ = 0;
  54. size_ = len;
  55. byte_order_ = byte_order;
  56. bytes_ = new char[size_];
  57. if (bytes) {
  58. end_ = len;
  59. memcpy(bytes_, bytes, end_);
  60. } else {
  61. end_ = 0;
  62. }
  63. }
  64. ByteBuffer::~ByteBuffer() {
  65. delete[] bytes_;
  66. }
  67. bool ByteBuffer::ReadUInt8(uint8* val) {
  68. if (!val) return false;
  69. return ReadBytes(reinterpret_cast<char*>(val), 1);
  70. }
  71. bool ByteBuffer::ReadUInt16(uint16* val) {
  72. if (!val) return false;
  73. uint16 v;
  74. if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
  75. return false;
  76. } else {
  77. *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost16(v) : v;
  78. return true;
  79. }
  80. }
  81. bool ByteBuffer::ReadUInt24(uint32* val) {
  82. if (!val) return false;
  83. uint32 v = 0;
  84. char* read_into = reinterpret_cast<char*>(&v);
  85. if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
  86. ++read_into;
  87. }
  88. if (!ReadBytes(read_into, 3)) {
  89. return false;
  90. } else {
  91. *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
  92. return true;
  93. }
  94. }
  95. bool ByteBuffer::ReadUInt32(uint32* val) {
  96. if (!val) return false;
  97. uint32 v;
  98. if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
  99. return false;
  100. } else {
  101. *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost32(v) : v;
  102. return true;
  103. }
  104. }
  105. bool ByteBuffer::ReadUInt64(uint64* val) {
  106. if (!val) return false;
  107. uint64 v;
  108. if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
  109. return false;
  110. } else {
  111. *val = (byte_order_ == ORDER_NETWORK) ? NetworkToHost64(v) : v;
  112. return true;
  113. }
  114. }
  115. bool ByteBuffer::ReadString(std::string* val, size_t len) {
  116. if (!val) return false;
  117. if (len > Length()) {
  118. return false;
  119. } else {
  120. val->append(bytes_ + start_, len);
  121. start_ += len;
  122. return true;
  123. }
  124. }
  125. bool ByteBuffer::ReadBytes(char* val, size_t len) {
  126. if (len > Length()) {
  127. return false;
  128. } else {
  129. memcpy(val, bytes_ + start_, len);
  130. start_ += len;
  131. return true;
  132. }
  133. }
  134. void ByteBuffer::WriteUInt8(uint8 val) {
  135. WriteBytes(reinterpret_cast<const char*>(&val), 1);
  136. }
  137. void ByteBuffer::WriteUInt16(uint16 val) {
  138. uint16 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork16(val) : val;
  139. WriteBytes(reinterpret_cast<const char*>(&v), 2);
  140. }
  141. void ByteBuffer::WriteUInt24(uint32 val) {
  142. uint32 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
  143. char* start = reinterpret_cast<char*>(&v);
  144. if (byte_order_ == ORDER_NETWORK || IsHostBigEndian()) {
  145. ++start;
  146. }
  147. WriteBytes(start, 3);
  148. }
  149. void ByteBuffer::WriteUInt32(uint32 val) {
  150. uint32 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork32(val) : val;
  151. WriteBytes(reinterpret_cast<const char*>(&v), 4);
  152. }
  153. void ByteBuffer::WriteUInt64(uint64 val) {
  154. uint64 v = (byte_order_ == ORDER_NETWORK) ? HostToNetwork64(val) : val;
  155. WriteBytes(reinterpret_cast<const char*>(&v), 8);
  156. }
  157. void ByteBuffer::WriteString(const std::string& val) {
  158. WriteBytes(val.c_str(), val.size());
  159. }
  160. void ByteBuffer::WriteBytes(const char* val, size_t len) {
  161. memcpy(ReserveWriteBuffer(len), val, len);
  162. }
  163. char* ByteBuffer::ReserveWriteBuffer(size_t len) {
  164. if (Length() + len > Capacity())
  165. Resize(Length() + len);
  166. char* start = bytes_ + end_;
  167. end_ += len;
  168. return start;
  169. }
  170. void ByteBuffer::Resize(size_t size) {
  171. size_t len = _min(end_ - start_, size);
  172. if (size <= size_) {
  173. // Don't reallocate, just move data backwards
  174. memmove(bytes_, bytes_ + start_, len);
  175. } else {
  176. // Reallocate a larger buffer.
  177. size_ = _max(size, 3 * size_ / 2);
  178. char* new_bytes = new char[size_];
  179. memcpy(new_bytes, bytes_ + start_, len);
  180. delete [] bytes_;
  181. bytes_ = new_bytes;
  182. }
  183. start_ = 0;
  184. end_ = len;
  185. ++version_;
  186. }
  187. bool ByteBuffer::Consume(size_t size) {
  188. if (size > Length())
  189. return false;
  190. start_ += size;
  191. return true;
  192. }
  193. ByteBuffer::ReadPosition ByteBuffer::GetReadPosition() const {
  194. return ReadPosition(start_, version_);
  195. }
  196. bool ByteBuffer::SetReadPosition(const ReadPosition &position) {
  197. if (position.version_ != version_) {
  198. return false;
  199. }
  200. start_ = position.start_;
  201. return true;
  202. }
  203. void ByteBuffer::Clear() {
  204. memset(bytes_, 0, size_);
  205. start_ = end_ = 0;
  206. ++version_;
  207. }
  208. } // namespace talk_base