henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/byte_buffer.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | namespace rtc { |
| 16 | |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 17 | ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 18 | |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 19 | ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order) |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 20 | : ByteBufferWriterT(byte_order) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 21 | |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 22 | ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len) |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 23 | : ByteBufferWriterT(bytes, len) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 24 | |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 25 | ByteBufferWriter::ByteBufferWriter(const char* bytes, |
| 26 | size_t len, |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 27 | ByteOrder byte_order) |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 28 | : ByteBufferWriterT(bytes, len, byte_order) {} |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 29 | |
| 30 | ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) |
| 31 | : ByteBuffer(ORDER_NETWORK) { |
| 32 | Construct(bytes, len); |
| 33 | } |
| 34 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 35 | ByteBufferReader::ByteBufferReader(const char* bytes, |
| 36 | size_t len, |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 37 | ByteOrder byte_order) |
| 38 | : ByteBuffer(byte_order) { |
| 39 | Construct(bytes, len); |
| 40 | } |
| 41 | |
| 42 | ByteBufferReader::ByteBufferReader(const char* bytes) |
| 43 | : ByteBuffer(ORDER_NETWORK) { |
| 44 | Construct(bytes, strlen(bytes)); |
| 45 | } |
| 46 | |
| 47 | ByteBufferReader::ByteBufferReader(const Buffer& buf) |
| 48 | : ByteBuffer(ORDER_NETWORK) { |
| 49 | Construct(buf.data<char>(), buf.size()); |
| 50 | } |
| 51 | |
| 52 | ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) |
| 53 | : ByteBuffer(buf.Order()) { |
| 54 | Construct(buf.Data(), buf.Length()); |
| 55 | } |
| 56 | |
| 57 | void ByteBufferReader::Construct(const char* bytes, size_t len) { |
| 58 | bytes_ = bytes; |
| 59 | size_ = len; |
| 60 | start_ = 0; |
| 61 | end_ = len; |
| 62 | } |
| 63 | |
| 64 | bool ByteBufferReader::ReadUInt8(uint8_t* val) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 65 | if (!val) |
| 66 | return false; |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 67 | |
| 68 | return ReadBytes(reinterpret_cast<char*>(val), 1); |
| 69 | } |
| 70 | |
| 71 | bool ByteBufferReader::ReadUInt16(uint16_t* val) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 72 | if (!val) |
| 73 | return false; |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 74 | |
| 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 | |
| 84 | bool ByteBufferReader::ReadUInt24(uint32_t* val) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 85 | if (!val) |
| 86 | return false; |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 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 | |
| 102 | bool ByteBufferReader::ReadUInt32(uint32_t* val) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 103 | if (!val) |
| 104 | return false; |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 105 | |
| 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 | |
| 115 | bool ByteBufferReader::ReadUInt64(uint64_t* val) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 116 | if (!val) |
| 117 | return false; |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 118 | |
| 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 | |
mikescarlett | 9a20fa6 | 2016-04-11 23:11:38 | [diff] [blame] | 128 | bool 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 | |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 151 | bool ByteBufferReader::ReadString(std::string* val, size_t len) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 152 | if (!val) |
| 153 | return false; |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 154 | |
| 155 | if (len > Length()) { |
| 156 | return false; |
| 157 | } else { |
| 158 | val->append(bytes_ + start_, len); |
| 159 | start_ += len; |
| 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | bool 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 | |
| 174 | bool ByteBufferReader::Consume(size_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 175 | if (size > Length()) |
| 176 | return false; |
| 177 | start_ += size; |
| 178 | return true; |
| 179 | } |
| 180 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 181 | } // namespace rtc |