1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/byte_buffer.h"
#include <string.h>
namespace rtc {
ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {}
ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
: ByteBufferWriterT(bytes, len) {}
ByteBufferReader::ByteBufferReader(rtc::ArrayView<const uint8_t> bytes) {
Construct(bytes.data(), bytes.size());
}
ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) {
Construct(reinterpret_cast<const uint8_t*>(bytes), len);
}
ByteBufferReader::ByteBufferReader(const char* bytes) {
Construct(reinterpret_cast<const uint8_t*>(bytes), strlen(bytes));
}
ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) {
Construct(reinterpret_cast<const uint8_t*>(buf.Data()), buf.Length());
}
void ByteBufferReader::Construct(const uint8_t* bytes, size_t len) {
bytes_ = bytes;
size_ = len;
start_ = 0;
end_ = len;
}
bool ByteBufferReader::ReadUInt8(uint8_t* val) {
if (!val)
return false;
return ReadBytes(val, 1);
}
bool ByteBufferReader::ReadUInt16(uint16_t* val) {
if (!val)
return false;
uint16_t v;
if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 2)) {
return false;
} else {
*val = NetworkToHost16(v);
return true;
}
}
bool ByteBufferReader::ReadUInt24(uint32_t* val) {
if (!val)
return false;
uint32_t v = 0;
uint8_t* read_into = reinterpret_cast<uint8_t*>(&v);
++read_into;
if (!ReadBytes(read_into, 3)) {
return false;
} else {
*val = NetworkToHost32(v);
return true;
}
}
bool ByteBufferReader::ReadUInt32(uint32_t* val) {
if (!val)
return false;
uint32_t v;
if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 4)) {
return false;
} else {
*val = NetworkToHost32(v);
return true;
}
}
bool ByteBufferReader::ReadUInt64(uint64_t* val) {
if (!val)
return false;
uint64_t v;
if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 8)) {
return false;
} else {
*val = NetworkToHost64(v);
return true;
}
}
bool ByteBufferReader::ReadUVarint(uint64_t* val) {
if (!val) {
return false;
}
// Integers are deserialized 7 bits at a time, with each byte having a
// continuation byte (msb=1) if there are more bytes to be read.
uint64_t v = 0;
for (int i = 0; i < 64; i += 7) {
uint8_t byte;
if (!ReadBytes(&byte, 1)) {
return false;
}
// Read the first 7 bits of the byte, then offset by bits read so far.
v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
// Return if the msb is not a continuation byte.
if (byte < 0x80) {
*val = v;
return true;
}
}
return false;
}
bool ByteBufferReader::ReadString(std::string* val, size_t len) {
if (!val)
return false;
if (len > Length()) {
return false;
} else {
val->append(reinterpret_cast<const char*>(bytes_ + start_), len);
start_ += len;
return true;
}
}
bool ByteBufferReader::ReadBytes(rtc::ArrayView<uint8_t> val) {
if (val.size() == 0) {
return true;
}
return ReadBytes(val.data(), val.size());
}
bool ByteBufferReader::ReadBytes(char* val, size_t len) {
return ReadBytes(reinterpret_cast<uint8_t*>(val), len);
}
// Private function supporting the other Read* functions.
bool ByteBufferReader::ReadBytes(uint8_t* val, size_t len) {
if (len > Length()) {
return false;
}
if (len == 0) {
return true;
}
memcpy(val, bytes_ + start_, len);
start_ += len;
return true;
}
bool ByteBufferReader::Consume(size_t size) {
if (size > Length())
return false;
start_ += size;
return true;
}
} // namespace rtc
|