blob: 9f7912094e7aff305ebd20440489310ebcc4bcf5 [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
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/byte_buffer.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
henrike@webrtc.orgf0488722014-05-13 18:00:2613#include <string.h>
14
henrike@webrtc.orgf0488722014-05-13 18:00:2615namespace rtc {
16
Joachim Bauch4c6a30c2018-03-07 23:55:3317ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {}
henrike@webrtc.orgf0488722014-05-13 18:00:2618
jbauchf1f87202016-03-30 13:43:3719ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
Joachim Bauch4c6a30c2018-03-07 23:55:3320 : ByteBufferWriterT(byte_order) {}
henrike@webrtc.orgf0488722014-05-13 18:00:2621
jbauchf1f87202016-03-30 13:43:3722ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
Joachim Bauch4c6a30c2018-03-07 23:55:3323 : ByteBufferWriterT(bytes, len) {}
henrike@webrtc.orgf0488722014-05-13 18:00:2624
Joachim Bauch4c6a30c2018-03-07 23:55:3325ByteBufferWriter::ByteBufferWriter(const char* bytes,
26 size_t len,
jbauchf1f87202016-03-30 13:43:3727 ByteOrder byte_order)
Joachim Bauch4c6a30c2018-03-07 23:55:3328 : ByteBufferWriterT(bytes, len, byte_order) {}
jbauchf1f87202016-03-30 13:43:3729
30ByteBufferReader::ByteBufferReader(const char* bytes, size_t len)
31 : ByteBuffer(ORDER_NETWORK) {
32 Construct(bytes, len);
33}
34
Yves Gerey665174f2018-06-19 13:03:0535ByteBufferReader::ByteBufferReader(const char* bytes,
36 size_t len,
jbauchf1f87202016-03-30 13:43:3737 ByteOrder byte_order)
38 : ByteBuffer(byte_order) {
39 Construct(bytes, len);
40}
41
42ByteBufferReader::ByteBufferReader(const char* bytes)
43 : ByteBuffer(ORDER_NETWORK) {
44 Construct(bytes, strlen(bytes));
45}
46
47ByteBufferReader::ByteBufferReader(const Buffer& buf)
48 : ByteBuffer(ORDER_NETWORK) {
49 Construct(buf.data<char>(), buf.size());
50}
51
52ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf)
53 : ByteBuffer(buf.Order()) {
54 Construct(buf.Data(), buf.Length());
55}
56
57void ByteBufferReader::Construct(const char* bytes, size_t len) {
58 bytes_ = bytes;
59 size_ = len;
60 start_ = 0;
61 end_ = len;
62}
63
64bool ByteBufferReader::ReadUInt8(uint8_t* val) {
Yves Gerey665174f2018-06-19 13:03:0565 if (!val)
66 return false;
jbauchf1f87202016-03-30 13:43:3767
68 return ReadBytes(reinterpret_cast<char*>(val), 1);
69}
70
71bool ByteBufferReader::ReadUInt16(uint16_t* val) {
Yves Gerey665174f2018-06-19 13:03:0572 if (!val)
73 return false;
jbauchf1f87202016-03-30 13:43:3774
75 uint16_t v;
76 if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
77 return false;
78 } else {
79 *val = (Order() == ORDER_NETWORK) ? NetworkToHost16(v) : v;
80 return true;
81 }
82}
83
84bool ByteBufferReader::ReadUInt24(uint32_t* val) {
Yves Gerey665174f2018-06-19 13:03:0585 if (!val)
86 return false;
jbauchf1f87202016-03-30 13:43:3787
88 uint32_t v = 0;
89 char* read_into = reinterpret_cast<char*>(&v);
90 if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
91 ++read_into;
92 }
93
94 if (!ReadBytes(read_into, 3)) {
95 return false;
96 } else {
97 *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
98 return true;
99 }
100}
101
102bool ByteBufferReader::ReadUInt32(uint32_t* val) {
Yves Gerey665174f2018-06-19 13:03:05103 if (!val)
104 return false;
jbauchf1f87202016-03-30 13:43:37105
106 uint32_t v;
107 if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
108 return false;
109 } else {
110 *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
111 return true;
112 }
113}
114
115bool ByteBufferReader::ReadUInt64(uint64_t* val) {
Yves Gerey665174f2018-06-19 13:03:05116 if (!val)
117 return false;
jbauchf1f87202016-03-30 13:43:37118
119 uint64_t v;
120 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
121 return false;
122 } else {
123 *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v;
124 return true;
125 }
126}
127
mikescarlett9a20fa62016-04-11 23:11:38128bool ByteBufferReader::ReadUVarint(uint64_t* val) {
129 if (!val) {
130 return false;
131 }
132 // Integers are deserialized 7 bits at a time, with each byte having a
133 // continuation byte (msb=1) if there are more bytes to be read.
134 uint64_t v = 0;
135 for (int i = 0; i < 64; i += 7) {
136 char byte;
137 if (!ReadBytes(&byte, 1)) {
138 return false;
139 }
140 // Read the first 7 bits of the byte, then offset by bits read so far.
141 v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
142 // True if the msb is not a continuation byte.
143 if (static_cast<uint64_t>(byte) < 0x80) {
144 *val = v;
145 return true;
146 }
147 }
148 return false;
149}
150
jbauchf1f87202016-03-30 13:43:37151bool ByteBufferReader::ReadString(std::string* val, size_t len) {
Yves Gerey665174f2018-06-19 13:03:05152 if (!val)
153 return false;
jbauchf1f87202016-03-30 13:43:37154
155 if (len > Length()) {
156 return false;
157 } else {
158 val->append(bytes_ + start_, len);
159 start_ += len;
160 return true;
161 }
162}
163
164bool ByteBufferReader::ReadBytes(char* val, size_t len) {
165 if (len > Length()) {
166 return false;
167 } else {
168 memcpy(val, bytes_ + start_, len);
169 start_ += len;
170 return true;
171 }
172}
173
174bool ByteBufferReader::Consume(size_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26175 if (size > Length())
176 return false;
177 start_ += size;
178 return true;
179}
180
henrike@webrtc.orgf0488722014-05-13 18:00:26181} // namespace rtc