diff --git a/rtc_base/byte_buffer.cc b/rtc_base/byte_buffer.cc index 3f0e61e59c..7e808f203a 100644 --- a/rtc_base/byte_buffer.cc +++ b/rtc_base/byte_buffer.cc @@ -46,7 +46,7 @@ bool ByteBufferReader::ReadUInt8(uint8_t* val) { if (!val) return false; - return ReadBytes(reinterpret_cast(val), 1); + return ReadBytes(val, 1); } bool ByteBufferReader::ReadUInt16(uint16_t* val) { @@ -54,7 +54,7 @@ bool ByteBufferReader::ReadUInt16(uint16_t* val) { return false; uint16_t v; - if (!ReadBytes(reinterpret_cast(&v), 2)) { + if (!ReadBytes(reinterpret_cast(&v), 2)) { return false; } else { *val = NetworkToHost16(v); @@ -67,7 +67,7 @@ bool ByteBufferReader::ReadUInt24(uint32_t* val) { return false; uint32_t v = 0; - char* read_into = reinterpret_cast(&v); + uint8_t* read_into = reinterpret_cast(&v); ++read_into; if (!ReadBytes(read_into, 3)) { @@ -83,7 +83,7 @@ bool ByteBufferReader::ReadUInt32(uint32_t* val) { return false; uint32_t v; - if (!ReadBytes(reinterpret_cast(&v), 4)) { + if (!ReadBytes(reinterpret_cast(&v), 4)) { return false; } else { *val = NetworkToHost32(v); @@ -96,7 +96,7 @@ bool ByteBufferReader::ReadUInt64(uint64_t* val) { return false; uint64_t v; - if (!ReadBytes(reinterpret_cast(&v), 8)) { + if (!ReadBytes(reinterpret_cast(&v), 8)) { return false; } else { *val = NetworkToHost64(v); @@ -112,14 +112,14 @@ bool ByteBufferReader::ReadUVarint(uint64_t* val) { // continuation byte (msb=1) if there are more bytes to be read. uint64_t v = 0; for (int i = 0; i < 64; i += 7) { - char byte; + uint8_t byte; if (!ReadBytes(&byte, 1)) { return false; } // Read the first 7 bits of the byte, then offset by bits read so far. v |= (static_cast(byte) & 0x7F) << i; - // True if the msb is not a continuation byte. - if (static_cast(byte) < 0x80) { + // Return if the msb is not a continuation byte. + if (byte < 0x80) { *val = v; return true; } @@ -140,7 +140,16 @@ bool ByteBufferReader::ReadString(std::string* val, size_t len) { } } +bool ByteBufferReader::ReadBytes(rtc::ArrayView val) { + return ReadBytes(val.data(), val.size()); +} + bool ByteBufferReader::ReadBytes(char* val, size_t len) { + return ReadBytes(reinterpret_cast(val), len); +} + +// Private function supporting the other Read* functions. +bool ByteBufferReader::ReadBytes(uint8_t* val, size_t len) { if (len > Length()) { return false; } else { diff --git a/rtc_base/byte_buffer.h b/rtc_base/byte_buffer.h index fe17da20c8..6a923ed796 100644 --- a/rtc_base/byte_buffer.h +++ b/rtc_base/byte_buffer.h @@ -157,6 +157,9 @@ class ByteBufferReader { bool ReadUInt32(uint32_t* val); bool ReadUInt64(uint64_t* val); bool ReadUVarint(uint64_t* val); + bool ReadBytes(rtc::ArrayView val); + // For backwards compatibility. + // TODO(bugs.webrtc.org/15661): Deprecate and remove. bool ReadBytes(char* val, size_t len); // Appends next `len` bytes from the buffer to `val`. Returns false @@ -169,8 +172,9 @@ class ByteBufferReader { // after this call. bool Consume(size_t size); - protected: + private: void Construct(const uint8_t* bytes, size_t size); + bool ReadBytes(uint8_t* val, size_t len); const uint8_t* bytes_; size_t size_; diff --git a/rtc_base/byte_buffer_unittest.cc b/rtc_base/byte_buffer_unittest.cc index 615237c1e6..837fdad7d3 100644 --- a/rtc_base/byte_buffer_unittest.cc +++ b/rtc_base/byte_buffer_unittest.cc @@ -264,4 +264,18 @@ TEST(ByteBufferTest, ReadFromArrayView) { EXPECT_FALSE(read_buffer.ReadUInt8(&val)); } +TEST(ByteBufferTest, ReadToArrayView) { + const uint8_t buf[] = {'a', 'b', 'c'}; + ArrayView stored_view(buf, 3); + ByteBufferReader read_buffer(stored_view); + uint8_t result[] = {'1', '2', '3'}; + EXPECT_TRUE(read_buffer.ReadBytes(rtc::ArrayView(result, 2))); + EXPECT_EQ(result[0], 'a'); + EXPECT_EQ(result[1], 'b'); + EXPECT_EQ(result[2], '3'); + EXPECT_TRUE(read_buffer.ReadBytes(rtc::ArrayView(&result[2], 1))); + EXPECT_EQ(result[2], 'c'); + EXPECT_FALSE(read_buffer.ReadBytes(rtc::ArrayView(result, 1))); +} + } // namespace rtc