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> | |
15 | 14 |
16 #include "webrtc/base/basictypes.h" | 15 // This header is deprecated and is just left here temporarily during |
17 #include "webrtc/base/buffer.h" | 16 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
18 #include "webrtc/base/constructormagic.h" | 17 #include "webrtc/rtc_base/bytebuffer.h" |
19 | |
20 namespace rtc { | |
21 | |
22 class ByteBuffer { | |
23 public: | |
24 enum ByteOrder { | |
25 ORDER_NETWORK = 0, // Default, use network byte order (big endian). | |
26 ORDER_HOST, // Use the native order of the host. | |
27 }; | |
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: | |
41 // |byte_order| defines order of bytes in the buffer. | |
42 ByteBufferWriter(); | |
43 explicit ByteBufferWriter(ByteOrder byte_order); | |
44 ByteBufferWriter(const char* bytes, size_t len); | |
45 ByteBufferWriter(const char* bytes, size_t len, ByteOrder byte_order); | |
46 | |
47 ~ByteBufferWriter(); | |
48 | |
49 const char* Data() const { return bytes_; } | |
50 size_t Length() const { return end_; } | |
51 size_t Capacity() const { return size_; } | |
52 | |
53 // Write value to the buffer. Resizes the buffer when it is | |
54 // neccessary. | |
55 void WriteUInt8(uint8_t val); | |
56 void WriteUInt16(uint16_t val); | |
57 void WriteUInt24(uint32_t val); | |
58 void WriteUInt32(uint32_t val); | |
59 void WriteUInt64(uint64_t val); | |
60 void WriteUVarint(uint64_t val); | |
61 void WriteString(const std::string& val); | |
62 void WriteBytes(const char* val, size_t len); | |
63 | |
64 // Reserves the given number of bytes and returns a char* that can be written | |
65 // into. Useful for functions that require a char* buffer and not a | |
66 // ByteBufferWriter. | |
67 char* ReserveWriteBuffer(size_t len); | |
68 | |
69 // Resize the buffer to the specified |size|. | |
70 void Resize(size_t size); | |
71 | |
72 // Clears the contents of the buffer. After this, Length() will be 0. | |
73 void Clear(); | |
74 | |
75 private: | |
76 void Construct(const char* bytes, size_t size); | |
77 | |
78 char* bytes_; | |
79 size_t size_; | |
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 ReadUVarint(uint64_t* val); | |
114 bool ReadBytes(char* val, size_t len); | |
115 | |
116 // Appends next |len| bytes from the buffer to |val|. Returns false | |
117 // if there is less than |len| bytes left. | |
118 bool ReadString(std::string* val, size_t len); | |
119 | |
120 // Moves current position |size| bytes forward. Returns false if | |
121 // there is less than |size| bytes left in the buffer. Consume doesn't | |
122 // permanently remove data, so remembered read positions are still valid | |
123 // after this call. | |
124 bool Consume(size_t size); | |
125 | |
126 private: | |
127 void Construct(const char* bytes, size_t size); | |
128 | |
129 const char* bytes_; | |
130 size_t size_; | |
131 size_t start_; | |
132 size_t end_; | |
133 | |
134 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferReader); | |
135 }; | |
136 | |
137 } // namespace rtc | |
138 | 18 |
139 #endif // WEBRTC_BASE_BYTEBUFFER_H_ | 19 #endif // WEBRTC_BASE_BYTEBUFFER_H_ |
OLD | NEW |