blob: 9036bcde6fdf3cbce4826569b8659c7460521ab5 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef RTC_BASE_BYTEBUFFER_H_
12#define RTC_BASE_BYTEBUFFER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:2615
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/buffer.h"
Joachim Bauch4c6a30c2018-03-07 23:55:3317#include "rtc_base/byteorder.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/constructormagic.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5019
20namespace rtc {
21
22class 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
Joachim Bauch4c6a30c2018-03-07 23:55:3339template <class BufferClassT>
40class ByteBufferWriterT : public ByteBuffer {
41 public:
42 // |byte_order| defines order of bytes in the buffer.
43 ByteBufferWriterT() : ByteBuffer(ORDER_NETWORK) {
44 Construct(nullptr, kDefaultCapacity);
45 }
46 explicit ByteBufferWriterT(ByteOrder byte_order) : ByteBuffer(byte_order) {
47 Construct(nullptr, kDefaultCapacity);
48 }
49 ByteBufferWriterT(const char* bytes, size_t len) : ByteBuffer(ORDER_NETWORK) {
50 Construct(bytes, len);
51 }
52 ByteBufferWriterT(const char* bytes, size_t len, ByteOrder byte_order)
53 : ByteBuffer(byte_order) {
54 Construct(bytes, len);
55 }
56
57 const char* Data() const { return buffer_.data(); }
58 size_t Length() const { return buffer_.size(); }
59 size_t Capacity() const { return buffer_.capacity(); }
60
61 // Write value to the buffer. Resizes the buffer when it is
62 // neccessary.
63 void WriteUInt8(uint8_t val) {
64 WriteBytes(reinterpret_cast<const char*>(&val), 1);
65 }
66 void WriteUInt16(uint16_t val) {
67 uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
68 WriteBytes(reinterpret_cast<const char*>(&v), 2);
69 }
70 void WriteUInt24(uint32_t val) {
71 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
72 char* start = reinterpret_cast<char*>(&v);
73 if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
74 ++start;
75 }
76 WriteBytes(start, 3);
77 }
78 void WriteUInt32(uint32_t val) {
79 uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
80 WriteBytes(reinterpret_cast<const char*>(&v), 4);
81 }
82 void WriteUInt64(uint64_t val) {
83 uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
84 WriteBytes(reinterpret_cast<const char*>(&v), 8);
85 }
86 // Serializes an unsigned varint in the format described by
87 // https://developers.google.com/protocol-buffers/docs/encoding#varints
88 // with the caveat that integers are 64-bit, not 128-bit.
89 void WriteUVarint(uint64_t val) {
90 while (val >= 0x80) {
91 // Write 7 bits at a time, then set the msb to a continuation byte
92 // (msb=1).
93 char byte = static_cast<char>(val) | 0x80;
94 WriteBytes(&byte, 1);
95 val >>= 7;
96 }
97 char last_byte = static_cast<char>(val);
98 WriteBytes(&last_byte, 1);
99 }
100 void WriteString(const std::string& val) {
101 WriteBytes(val.c_str(), val.size());
102 }
103 void WriteBytes(const char* val, size_t len) { buffer_.AppendData(val, len); }
104
105 // Reserves the given number of bytes and returns a char* that can be written
106 // into. Useful for functions that require a char* buffer and not a
107 // ByteBufferWriter.
108 char* ReserveWriteBuffer(size_t len) {
109 buffer_.SetSize(buffer_.size() + len);
110 return buffer_.data();
111 }
112
113 // Resize the buffer to the specified |size|.
114 void Resize(size_t size) { buffer_.SetSize(size); }
115
116 // Clears the contents of the buffer. After this, Length() will be 0.
117 void Clear() { buffer_.Clear(); }
118
119 private:
120 static constexpr size_t kDefaultCapacity = 4096;
121
122 void Construct(const char* bytes, size_t size) {
123 if (bytes) {
124 buffer_.AppendData(bytes, size);
125 } else {
126 buffer_.EnsureCapacity(size);
127 }
128 }
129
130 BufferClassT buffer_;
131
132 // There are sensible ways to define these, but they aren't needed in our code
133 // base.
134 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriterT);
135};
136
137class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> {
Henrik Kjellanderec78f1c2017-06-29 05:52:50138 public:
139 // |byte_order| defines order of bytes in the buffer.
140 ByteBufferWriter();
141 explicit ByteBufferWriter(ByteOrder byte_order);
142 ByteBufferWriter(const char* bytes, size_t len);
143 ByteBufferWriter(const char* bytes, size_t len, ByteOrder byte_order);
144
Henrik Kjellanderec78f1c2017-06-29 05:52:50145 private:
Henrik Kjellanderec78f1c2017-06-29 05:52:50146 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriter);
147};
148
149// The ByteBufferReader references the passed data, i.e. the pointer must be
150// valid during the lifetime of the reader.
151class ByteBufferReader : public ByteBuffer {
152 public:
153 ByteBufferReader(const char* bytes, size_t len);
154 ByteBufferReader(const char* bytes, size_t len, ByteOrder byte_order);
155
156 // Initializes buffer from a zero-terminated string.
157 explicit ByteBufferReader(const char* bytes);
158
159 explicit ByteBufferReader(const Buffer& buf);
160
161 explicit ByteBufferReader(const ByteBufferWriter& buf);
162
163 // Returns start of unprocessed data.
164 const char* Data() const { return bytes_ + start_; }
165 // Returns number of unprocessed bytes.
166 size_t Length() const { return end_ - start_; }
167
168 // Read a next value from the buffer. Return false if there isn't
169 // enough data left for the specified type.
170 bool ReadUInt8(uint8_t* val);
171 bool ReadUInt16(uint16_t* val);
172 bool ReadUInt24(uint32_t* val);
173 bool ReadUInt32(uint32_t* val);
174 bool ReadUInt64(uint64_t* val);
175 bool ReadUVarint(uint64_t* val);
176 bool ReadBytes(char* val, size_t len);
177
178 // Appends next |len| bytes from the buffer to |val|. Returns false
179 // if there is less than |len| bytes left.
180 bool ReadString(std::string* val, size_t len);
181
182 // Moves current position |size| bytes forward. Returns false if
183 // there is less than |size| bytes left in the buffer. Consume doesn't
184 // permanently remove data, so remembered read positions are still valid
185 // after this call.
186 bool Consume(size_t size);
187
188 private:
189 void Construct(const char* bytes, size_t size);
190
191 const char* bytes_;
192 size_t size_;
193 size_t start_;
194 size_t end_;
195
196 RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferReader);
197};
198
199} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26200
Mirko Bonadei92ea95e2017-09-15 04:47:31201#endif // RTC_BASE_BYTEBUFFER_H_