blob: f152d4f1f182e042d59ea6579b2471c950018437 [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#include "rtc_base/bytebuffer.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
henrike@webrtc.orgf0488722014-05-13 18:00:2613#include <string.h>
14
15#include <algorithm>
16
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/basictypes.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2618
19namespace rtc {
20
Joachim Bauch4c6a30c2018-03-07 23:55:3321ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {}
henrike@webrtc.orgf0488722014-05-13 18:00:2622
jbauchf1f87202016-03-30 13:43:3723ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
Joachim Bauch4c6a30c2018-03-07 23:55:3324 : ByteBufferWriterT(byte_order) {}
henrike@webrtc.orgf0488722014-05-13 18:00:2625
jbauchf1f87202016-03-30 13:43:3726ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
Joachim Bauch4c6a30c2018-03-07 23:55:3327 : ByteBufferWriterT(bytes, len) {}
henrike@webrtc.orgf0488722014-05-13 18:00:2628
Joachim Bauch4c6a30c2018-03-07 23:55:3329ByteBufferWriter::ByteBufferWriter(const char* bytes,
30 size_t len,
jbauchf1f87202016-03-30 13:43:3731 ByteOrder byte_order)
Joachim Bauch4c6a30c2018-03-07 23:55:3332 : ByteBufferWriterT(bytes, len, byte_order) {}
jbauchf1f87202016-03-30 13:43:3733
34ByteBufferReader::ByteBufferReader(const char* bytes, size_t len)
35 : ByteBuffer(ORDER_NETWORK) {
36 Construct(bytes, len);
37}
38
39ByteBufferReader::ByteBufferReader(const char* bytes, size_t len,
40 ByteOrder byte_order)
41 : ByteBuffer(byte_order) {
42 Construct(bytes, len);
43}
44
45ByteBufferReader::ByteBufferReader(const char* bytes)
46 : ByteBuffer(ORDER_NETWORK) {
47 Construct(bytes, strlen(bytes));
48}
49
50ByteBufferReader::ByteBufferReader(const Buffer& buf)
51 : ByteBuffer(ORDER_NETWORK) {
52 Construct(buf.data<char>(), buf.size());
53}
54
55ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf)
56 : ByteBuffer(buf.Order()) {
57 Construct(buf.Data(), buf.Length());
58}
59
60void ByteBufferReader::Construct(const char* bytes, size_t len) {
61 bytes_ = bytes;
62 size_ = len;
63 start_ = 0;
64 end_ = len;
65}
66
67bool ByteBufferReader::ReadUInt8(uint8_t* val) {
68 if (!val) return false;
69
70 return ReadBytes(reinterpret_cast<char*>(val), 1);
71}
72
73bool ByteBufferReader::ReadUInt16(uint16_t* val) {
74 if (!val) return false;
75
76 uint16_t v;
77 if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
78 return false;
79 } else {
80 *val = (Order() == ORDER_NETWORK) ? NetworkToHost16(v) : v;
81 return true;
82 }
83}
84
85bool ByteBufferReader::ReadUInt24(uint32_t* val) {
86 if (!val) return false;
87
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) {
103 if (!val) return false;
104
105 uint32_t v;
106 if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
107 return false;
108 } else {
109 *val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
110 return true;
111 }
112}
113
114bool ByteBufferReader::ReadUInt64(uint64_t* val) {
115 if (!val) return false;
116
117 uint64_t v;
118 if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
119 return false;
120 } else {
121 *val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v;
122 return true;
123 }
124}
125
mikescarlett9a20fa62016-04-11 23:11:38126bool ByteBufferReader::ReadUVarint(uint64_t* val) {
127 if (!val) {
128 return false;
129 }
130 // Integers are deserialized 7 bits at a time, with each byte having a
131 // continuation byte (msb=1) if there are more bytes to be read.
132 uint64_t v = 0;
133 for (int i = 0; i < 64; i += 7) {
134 char byte;
135 if (!ReadBytes(&byte, 1)) {
136 return false;
137 }
138 // Read the first 7 bits of the byte, then offset by bits read so far.
139 v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
140 // True if the msb is not a continuation byte.
141 if (static_cast<uint64_t>(byte) < 0x80) {
142 *val = v;
143 return true;
144 }
145 }
146 return false;
147}
148
jbauchf1f87202016-03-30 13:43:37149bool ByteBufferReader::ReadString(std::string* val, size_t len) {
150 if (!val) return false;
151
152 if (len > Length()) {
153 return false;
154 } else {
155 val->append(bytes_ + start_, len);
156 start_ += len;
157 return true;
158 }
159}
160
161bool ByteBufferReader::ReadBytes(char* val, size_t len) {
162 if (len > Length()) {
163 return false;
164 } else {
165 memcpy(val, bytes_ + start_, len);
166 start_ += len;
167 return true;
168 }
169}
170
171bool ByteBufferReader::Consume(size_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26172 if (size > Length())
173 return false;
174 start_ += size;
175 return true;
176}
177
henrike@webrtc.orgf0488722014-05-13 18:00:26178} // namespace rtc