OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_BYTEBUFFER_H_ | 11 #ifndef WEBRTC_BASE_BYTEBUFFER_H_ |
12 #define WEBRTC_BASE_BYTEBUFFER_H_ | 12 #define WEBRTC_BASE_BYTEBUFFER_H_ |
13 | 13 |
14 #include <string> | 14 #include <string> |
15 | 15 |
16 #include "webrtc/base/basictypes.h" | 16 #include "webrtc/base/basictypes.h" |
17 #include "webrtc/base/buffer.h" | 17 #include "webrtc/base/buffer.h" |
18 #include "webrtc/base/constructormagic.h" | 18 #include "webrtc/base/constructormagic.h" |
19 | 19 |
20 namespace rtc { | 20 namespace rtc { |
21 | 21 |
22 class ByteBuffer { | 22 class ByteBuffer { |
23 public: | 23 public: |
24 | |
25 enum ByteOrder { | 24 enum ByteOrder { |
26 ORDER_NETWORK = 0, // Default, use network byte order (big endian). | 25 ORDER_NETWORK = 0, // Default, use network byte order (big endian). |
27 ORDER_HOST, // Use the native order of the host. | 26 ORDER_HOST, // Use the native order of the host. |
28 }; | 27 }; |
29 | 28 |
| 29 explicit ByteBuffer(ByteOrder byte_order) : byte_order_(byte_order) {} |
| 30 |
| 31 ByteOrder Order() const { return byte_order_; } |
| 32 |
| 33 private: |
| 34 ByteOrder byte_order_; |
| 35 |
| 36 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBuffer); |
| 37 }; |
| 38 |
| 39 class ByteBufferWriter : public ByteBuffer { |
| 40 public: |
30 // |byte_order| defines order of bytes in the buffer. | 41 // |byte_order| defines order of bytes in the buffer. |
31 ByteBuffer(); | 42 ByteBufferWriter(); |
32 explicit ByteBuffer(ByteOrder byte_order); | 43 explicit ByteBufferWriter(ByteOrder byte_order); |
33 ByteBuffer(const char* bytes, size_t len); | 44 ByteBufferWriter(const char* bytes, size_t len); |
34 ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order); | 45 ByteBufferWriter(const char* bytes, size_t len, ByteOrder byte_order); |
35 | 46 |
36 // Initializes buffer from a zero-terminated string. | 47 ~ByteBufferWriter(); |
37 explicit ByteBuffer(const char* bytes); | |
38 | |
39 explicit ByteBuffer(const Buffer& buf); | |
40 | |
41 ~ByteBuffer(); | |
42 | 48 |
43 const char* Data() const { return bytes_ + start_; } | 49 const char* Data() const { return bytes_ + start_; } |
44 size_t Length() const { return end_ - start_; } | 50 size_t Length() const { return end_ - start_; } |
45 size_t Capacity() const { return size_ - start_; } | 51 size_t Capacity() const { return size_ - start_; } |
46 ByteOrder Order() const { return byte_order_; } | |
47 | |
48 // Read a next value from the buffer. Return false if there isn't | |
49 // enough data left for the specified type. | |
50 bool ReadUInt8(uint8_t* val); | |
51 bool ReadUInt16(uint16_t* val); | |
52 bool ReadUInt24(uint32_t* val); | |
53 bool ReadUInt32(uint32_t* val); | |
54 bool ReadUInt64(uint64_t* val); | |
55 bool ReadBytes(char* val, size_t len); | |
56 | |
57 // Appends next |len| bytes from the buffer to |val|. Returns false | |
58 // if there is less than |len| bytes left. | |
59 bool ReadString(std::string* val, size_t len); | |
60 | 52 |
61 // Write value to the buffer. Resizes the buffer when it is | 53 // Write value to the buffer. Resizes the buffer when it is |
62 // neccessary. | 54 // neccessary. |
63 void WriteUInt8(uint8_t val); | 55 void WriteUInt8(uint8_t val); |
64 void WriteUInt16(uint16_t val); | 56 void WriteUInt16(uint16_t val); |
65 void WriteUInt24(uint32_t val); | 57 void WriteUInt24(uint32_t val); |
66 void WriteUInt32(uint32_t val); | 58 void WriteUInt32(uint32_t val); |
67 void WriteUInt64(uint64_t val); | 59 void WriteUInt64(uint64_t val); |
68 void WriteString(const std::string& val); | 60 void WriteString(const std::string& val); |
69 void WriteBytes(const char* val, size_t len); | 61 void WriteBytes(const char* val, size_t len); |
70 | 62 |
71 // Reserves the given number of bytes and returns a char* that can be written | 63 // 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 | 64 // into. Useful for functions that require a char* buffer and not a |
73 // ByteBuffer. | 65 // ByteBufferWriter. |
74 char* ReserveWriteBuffer(size_t len); | 66 char* ReserveWriteBuffer(size_t len); |
75 | 67 |
76 // Resize the buffer to the specified |size|. This invalidates any remembered | 68 // Resize the buffer to the specified |size|. |
77 // seek positions. | |
78 void Resize(size_t size); | 69 void Resize(size_t size); |
79 | 70 |
| 71 // Clears the contents of the buffer. After this, Length() will be 0. |
| 72 void Clear(); |
| 73 |
| 74 private: |
| 75 void Construct(const char* bytes, size_t size); |
| 76 |
| 77 char* bytes_; |
| 78 size_t size_; |
| 79 size_t start_; |
| 80 size_t end_; |
| 81 |
| 82 // There are sensible ways to define these, but they aren't needed in our code |
| 83 // base. |
| 84 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriter); |
| 85 }; |
| 86 |
| 87 // The ByteBufferReader references the passed data, i.e. the pointer must be |
| 88 // valid during the lifetime of the reader. |
| 89 class ByteBufferReader : public ByteBuffer { |
| 90 public: |
| 91 ByteBufferReader(const char* bytes, size_t len); |
| 92 ByteBufferReader(const char* bytes, size_t len, ByteOrder byte_order); |
| 93 |
| 94 // Initializes buffer from a zero-terminated string. |
| 95 explicit ByteBufferReader(const char* bytes); |
| 96 |
| 97 explicit ByteBufferReader(const Buffer& buf); |
| 98 |
| 99 explicit ByteBufferReader(const ByteBufferWriter& buf); |
| 100 |
| 101 // Returns start of unprocessed data. |
| 102 const char* Data() const { return bytes_ + start_; } |
| 103 // Returns number of unprocessed bytes. |
| 104 size_t Length() const { return end_ - start_; } |
| 105 |
| 106 // Read a next value from the buffer. Return false if there isn't |
| 107 // enough data left for the specified type. |
| 108 bool ReadUInt8(uint8_t* val); |
| 109 bool ReadUInt16(uint16_t* val); |
| 110 bool ReadUInt24(uint32_t* val); |
| 111 bool ReadUInt32(uint32_t* val); |
| 112 bool ReadUInt64(uint64_t* val); |
| 113 bool ReadBytes(char* val, size_t len); |
| 114 |
| 115 // Appends next |len| bytes from the buffer to |val|. Returns false |
| 116 // if there is less than |len| bytes left. |
| 117 bool ReadString(std::string* val, size_t len); |
| 118 |
80 // Moves current position |size| bytes forward. Returns false if | 119 // Moves current position |size| bytes forward. Returns false if |
81 // there is less than |size| bytes left in the buffer. Consume doesn't | 120 // there is less than |size| bytes left in the buffer. Consume doesn't |
82 // permanently remove data, so remembered read positions are still valid | 121 // permanently remove data, so remembered read positions are still valid |
83 // after this call. | 122 // after this call. |
84 bool Consume(size_t size); | 123 bool Consume(size_t size); |
85 | 124 |
86 // Clears the contents of the buffer. After this, Length() will be 0. | 125 private: |
87 void Clear(); | 126 void Construct(const char* bytes, size_t size); |
88 | 127 |
89 // Used with GetReadPosition/SetReadPosition. | 128 const char* bytes_; |
90 class ReadPosition { | |
91 friend class ByteBuffer; | |
92 ReadPosition(size_t start, int version) | |
93 : start_(start), version_(version) { } | |
94 size_t start_; | |
95 int version_; | |
96 }; | |
97 | |
98 // Remembers the current read position for a future SetReadPosition. Any | |
99 // calls to Shift or Resize in the interim will invalidate the position. | |
100 ReadPosition GetReadPosition() const; | |
101 | |
102 // If the given position is still valid, restores that read position. | |
103 bool SetReadPosition(const ReadPosition &position); | |
104 | |
105 private: | |
106 void Construct(const char* bytes, size_t size, ByteOrder byte_order); | |
107 | |
108 char* bytes_; | |
109 size_t size_; | 129 size_t size_; |
110 size_t start_; | 130 size_t start_; |
111 size_t end_; | 131 size_t end_; |
112 int version_; | |
113 ByteOrder byte_order_; | |
114 | 132 |
115 // There are sensible ways to define these, but they aren't needed in our code | 133 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferReader); |
116 // base. | |
117 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBuffer); | |
118 }; | 134 }; |
119 | 135 |
120 } // namespace rtc | 136 } // namespace rtc |
121 | 137 |
122 #endif // WEBRTC_BASE_BYTEBUFFER_H_ | 138 #endif // WEBRTC_BASE_BYTEBUFFER_H_ |
OLD | NEW |