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.

136 lines
4.7 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. #ifndef TALK_BASE_BYTEBUFFER_H_
  28. #define TALK_BASE_BYTEBUFFER_H_
  29. #include <string>
  30. #include "talk/base/basictypes.h"
  31. #include "talk/base/constructormagic.h"
  32. namespace talk_base {
  33. class ByteBuffer {
  34. public:
  35. enum ByteOrder {
  36. ORDER_NETWORK = 0, // Default, use network byte order (big endian).
  37. ORDER_HOST, // Use the native order of the host.
  38. };
  39. // |byte_order| defines order of bytes in the buffer.
  40. ByteBuffer();
  41. explicit ByteBuffer(ByteOrder byte_order);
  42. ByteBuffer(const char* bytes, size_t len);
  43. ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order);
  44. // Initializes buffer from a zero-terminated string.
  45. explicit ByteBuffer(const char* bytes);
  46. ~ByteBuffer();
  47. const char* Data() const { return bytes_ + start_; }
  48. size_t Length() const { return end_ - start_; }
  49. size_t Capacity() const { return size_ - start_; }
  50. ByteOrder Order() const { return byte_order_; }
  51. // Read a next value from the buffer. Return false if there isn't
  52. // enough data left for the specified type.
  53. bool ReadUInt8(uint8* val);
  54. bool ReadUInt16(uint16* val);
  55. bool ReadUInt24(uint32* val);
  56. bool ReadUInt32(uint32* val);
  57. bool ReadUInt64(uint64* val);
  58. bool ReadBytes(char* val, size_t len);
  59. // Appends next |len| bytes from the buffer to |val|. Returns false
  60. // if there is less than |len| bytes left.
  61. bool ReadString(std::string* val, size_t len);
  62. // Write value to the buffer. Resizes the buffer when it is
  63. // neccessary.
  64. void WriteUInt8(uint8 val);
  65. void WriteUInt16(uint16 val);
  66. void WriteUInt24(uint32 val);
  67. void WriteUInt32(uint32 val);
  68. void WriteUInt64(uint64 val);
  69. void WriteString(const std::string& val);
  70. void WriteBytes(const char* val, size_t len);
  71. // Reserves the given number of bytes and returns a char* that can be written
  72. // into. Useful for functions that require a char* buffer and not a
  73. // ByteBuffer.
  74. char* ReserveWriteBuffer(size_t len);
  75. // Resize the buffer to the specified |size|. This invalidates any remembered
  76. // seek positions.
  77. void Resize(size_t size);
  78. // Moves current position |size| bytes forward. Returns false if
  79. // there is less than |size| bytes left in the buffer. Consume doesn't
  80. // permanently remove data, so remembered read positions are still valid
  81. // after this call.
  82. bool Consume(size_t size);
  83. // Clears the contents of the buffer. After this, Length() will be 0.
  84. void Clear();
  85. // Used with GetReadPosition/SetReadPosition.
  86. class ReadPosition {
  87. friend class ByteBuffer;
  88. ReadPosition(size_t start, int version)
  89. : start_(start), version_(version) { }
  90. size_t start_;
  91. int version_;
  92. };
  93. // Remembers the current read position for a future SetReadPosition. Any
  94. // calls to Shift or Resize in the interim will invalidate the position.
  95. ReadPosition GetReadPosition() const;
  96. // If the given position is still valid, restores that read position.
  97. bool SetReadPosition(const ReadPosition &position);
  98. private:
  99. void Construct(const char* bytes, size_t size, ByteOrder byte_order);
  100. char* bytes_;
  101. size_t size_;
  102. size_t start_;
  103. size_t end_;
  104. int version_;
  105. ByteOrder byte_order_;
  106. // There are sensible ways to define these, but they aren't needed in our code
  107. // base.
  108. DISALLOW_COPY_AND_ASSIGN(ByteBuffer);
  109. };
  110. } // namespace talk_base
  111. #endif // TALK_BASE_BYTEBUFFER_H_