blob: e1278337a48917cf82f0c01be627c384fe56d353 [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(const char* bytes, size_t len)
Joachim Bauch4c6a30c2018-03-07 23:55:3320 : ByteBufferWriterT(bytes, len) {}
henrike@webrtc.orgf0488722014-05-13 18:00:2621
Danil Chapovalov7b46e172019-11-14 16:40:2322ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) {
jbauchf1f87202016-03-30 13:43:3723 Construct(bytes, len);
24}
25
Danil Chapovalov7b46e172019-11-14 16:40:2326ByteBufferReader::ByteBufferReader(const char* bytes) {
jbauchf1f87202016-03-30 13:43:3727 Construct(bytes, strlen(bytes));
28}
29
Danil Chapovalov7b46e172019-11-14 16:40:2330ByteBufferReader::ByteBufferReader(const Buffer& buf) {
jbauchf1f87202016-03-30 13:43:3731 Construct(buf.data<char>(), buf.size());
32}
33
Danil Chapovalov7b46e172019-11-14 16:40:2334ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) {
jbauchf1f87202016-03-30 13:43:3735 Construct(buf.Data(), buf.Length());
36}
37
38void ByteBufferReader::Construct(const char* bytes, size_t len) {
39 bytes_ = bytes;
40 size_ = len;
41 start_ = 0;
42 end_ = len;
43}
44
45bool ByteBufferReader::ReadUInt8(uint8_t* val) {
Yves Gerey665174f2018-06-19 13:03:0546 if (!val)
47 return false;
jbauchf1f87202016-03-30 13:43:3748
49 return ReadBytes(reinterpret_cast<char*>(val), 1);
50}
51
52bool ByteBufferReader::ReadUInt16(uint16_t* val) {
Yves Gerey665174f2018-06-19 13:03:0553 if (!val)
54 return false;
jbauchf1f87202016-03-30 13:43:3755
56 uint16_t v;
57 if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
58 return false;
59 } else {
Danil Chapovalov7b46e172019-11-14 16:40:2360 *val = NetworkToHost16(v);
jbauchf1f87202016-03-30 13:43:3761 return true;
62 }
63}
64
65bool ByteBufferReader::ReadUInt24(uint32_t* val) {
Yves Gerey665174f2018-06-19 13:03:0566 if (!val)
67 return false;
jbauchf1f87202016-03-30 13:43:3768
69 uint32_t v = 0;
70 char* read_into = reinterpret_cast<char*>(&v);
Danil Chapovalov7b46e172019-11-14 16:40:2371 ++read_into;
jbauchf1f87202016-03-30 13:43:3772
73 if (!ReadBytes(read_into, 3)) {
74 return false;
75 } else {
Danil Chapovalov7b46e172019-11-14 16:40:2376 *val = NetworkToHost32(v);
jbauchf1f87202016-03-30 13:43:3777 return true;
78 }
79}
80
81bool ByteBufferReader::ReadUInt32(uint32_t* val) {
Yves Gerey665174f2018-06-19 13:03:0582 if (!val)
83 return false;
jbauchf1f87202016-03-30 13:43:3784
85 uint32_t v;
86 if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
87 return false;
88 } else {
Danil Chapovalov7b46e172019-11-14 16:40:2389 *val = NetworkToHost32(v);
jbauchf1f87202016-03-30 13:43:3790 return true;
91 }
92}
93
94bool ByteBufferReader::ReadUInt64(uint64_t* val) {
Yves Gerey665174f2018-06-19 13:03:0595 if (!val)
96 return false;
jbauchf1f87202016-03-30 13:43:3797
98 uint64_t v;
99 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
100 return false;
101 } else {
Danil Chapovalov7b46e172019-11-14 16:40:23102 *val = NetworkToHost64(v);
jbauchf1f87202016-03-30 13:43:37103 return true;
104 }
105}
106
mikescarlett9a20fa62016-04-11 23:11:38107bool ByteBufferReader::ReadUVarint(uint64_t* val) {
108 if (!val) {
109 return false;
110 }
111 // Integers are deserialized 7 bits at a time, with each byte having a
112 // continuation byte (msb=1) if there are more bytes to be read.
113 uint64_t v = 0;
114 for (int i = 0; i < 64; i += 7) {
115 char byte;
116 if (!ReadBytes(&byte, 1)) {
117 return false;
118 }
119 // Read the first 7 bits of the byte, then offset by bits read so far.
120 v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
121 // True if the msb is not a continuation byte.
122 if (static_cast<uint64_t>(byte) < 0x80) {
123 *val = v;
124 return true;
125 }
126 }
127 return false;
128}
129
jbauchf1f87202016-03-30 13:43:37130bool ByteBufferReader::ReadString(std::string* val, size_t len) {
Yves Gerey665174f2018-06-19 13:03:05131 if (!val)
132 return false;
jbauchf1f87202016-03-30 13:43:37133
134 if (len > Length()) {
135 return false;
136 } else {
137 val->append(bytes_ + start_, len);
138 start_ += len;
139 return true;
140 }
141}
142
143bool ByteBufferReader::ReadBytes(char* val, size_t len) {
144 if (len > Length()) {
145 return false;
146 } else {
147 memcpy(val, bytes_ + start_, len);
148 start_ += len;
149 return true;
150 }
151}
152
153bool ByteBufferReader::Consume(size_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26154 if (size > Length())
155 return false;
156 start_ += size;
157 return true;
158}
159
henrike@webrtc.orgf0488722014-05-13 18:00:26160} // namespace rtc