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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/bytebuffer.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 | |
| 15 | #include <algorithm> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "rtc_base/basictypes.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 21 | ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 22 | |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 23 | ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order) |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 24 | : ByteBufferWriterT(byte_order) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 25 | |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 26 | ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len) |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 27 | : ByteBufferWriterT(bytes, len) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 28 | |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 29 | ByteBufferWriter::ByteBufferWriter(const char* bytes, |
| 30 | size_t len, |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 31 | ByteOrder byte_order) |
Joachim Bauch | 4c6a30c | 2018-03-07 23:55:33 | [diff] [blame] | 32 | : ByteBufferWriterT(bytes, len, byte_order) {} |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 33 | |
| 34 | ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) |
| 35 | : ByteBuffer(ORDER_NETWORK) { |
| 36 | Construct(bytes, len); |
| 37 | } |
| 38 | |
| 39 | ByteBufferReader::ByteBufferReader(const char* bytes, size_t len, |
| 40 | ByteOrder byte_order) |
| 41 | : ByteBuffer(byte_order) { |
| 42 | Construct(bytes, len); |
| 43 | } |
| 44 | |
| 45 | ByteBufferReader::ByteBufferReader(const char* bytes) |
| 46 | : ByteBuffer(ORDER_NETWORK) { |
| 47 | Construct(bytes, strlen(bytes)); |
| 48 | } |
| 49 | |
| 50 | ByteBufferReader::ByteBufferReader(const Buffer& buf) |
| 51 | : ByteBuffer(ORDER_NETWORK) { |
| 52 | Construct(buf.data<char>(), buf.size()); |
| 53 | } |
| 54 | |
| 55 | ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) |
| 56 | : ByteBuffer(buf.Order()) { |
| 57 | Construct(buf.Data(), buf.Length()); |
| 58 | } |
| 59 | |
| 60 | void ByteBufferReader::Construct(const char* bytes, size_t len) { |
| 61 | bytes_ = bytes; |
| 62 | size_ = len; |
| 63 | start_ = 0; |
| 64 | end_ = len; |
| 65 | } |
| 66 | |
| 67 | bool ByteBufferReader::ReadUInt8(uint8_t* val) { |
| 68 | if (!val) return false; |
| 69 | |
| 70 | return ReadBytes(reinterpret_cast<char*>(val), 1); |
| 71 | } |
| 72 | |
| 73 | bool 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 | |
| 85 | bool 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 | |
| 102 | bool 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 | |
| 114 | bool 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 | |
mikescarlett | 9a20fa6 | 2016-04-11 23:11:38 | [diff] [blame] | 126 | bool 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 | |
jbauch | f1f8720 | 2016-03-30 13:43:37 | [diff] [blame] | 149 | bool 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 | |
| 161 | bool 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 | |
| 171 | bool ByteBufferReader::Consume(size_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 172 | if (size > Length()) |
| 173 | return false; |
| 174 | start_ += size; |
| 175 | return true; |
| 176 | } |
| 177 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 178 | } // namespace rtc |