Expunge some more "char" from ByteBufferReader

Bug: webrtc:15661
Change-Id: I620a5327ca64e89d7816a1ea6f69ad8363ff37e1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/327720
Auto-Submit: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41193}
This commit is contained in:
Harald Alvestrand 2023-11-20 09:20:23 +00:00 committed by WebRTC LUCI CQ
parent 7e6315a619
commit f0907c6f5b
3 changed files with 36 additions and 9 deletions

View File

@ -46,7 +46,7 @@ bool ByteBufferReader::ReadUInt8(uint8_t* val) {
if (!val) if (!val)
return false; return false;
return ReadBytes(reinterpret_cast<char*>(val), 1); return ReadBytes(val, 1);
} }
bool ByteBufferReader::ReadUInt16(uint16_t* val) { bool ByteBufferReader::ReadUInt16(uint16_t* val) {
@ -54,7 +54,7 @@ bool ByteBufferReader::ReadUInt16(uint16_t* val) {
return false; return false;
uint16_t v; uint16_t v;
if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) { if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 2)) {
return false; return false;
} else { } else {
*val = NetworkToHost16(v); *val = NetworkToHost16(v);
@ -67,7 +67,7 @@ bool ByteBufferReader::ReadUInt24(uint32_t* val) {
return false; return false;
uint32_t v = 0; uint32_t v = 0;
char* read_into = reinterpret_cast<char*>(&v); uint8_t* read_into = reinterpret_cast<uint8_t*>(&v);
++read_into; ++read_into;
if (!ReadBytes(read_into, 3)) { if (!ReadBytes(read_into, 3)) {
@ -83,7 +83,7 @@ bool ByteBufferReader::ReadUInt32(uint32_t* val) {
return false; return false;
uint32_t v; uint32_t v;
if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) { if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 4)) {
return false; return false;
} else { } else {
*val = NetworkToHost32(v); *val = NetworkToHost32(v);
@ -96,7 +96,7 @@ bool ByteBufferReader::ReadUInt64(uint64_t* val) {
return false; return false;
uint64_t v; uint64_t v;
if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) { if (!ReadBytes(reinterpret_cast<uint8_t*>(&v), 8)) {
return false; return false;
} else { } else {
*val = NetworkToHost64(v); *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. // continuation byte (msb=1) if there are more bytes to be read.
uint64_t v = 0; uint64_t v = 0;
for (int i = 0; i < 64; i += 7) { for (int i = 0; i < 64; i += 7) {
char byte; uint8_t byte;
if (!ReadBytes(&byte, 1)) { if (!ReadBytes(&byte, 1)) {
return false; return false;
} }
// Read the first 7 bits of the byte, then offset by bits read so far. // Read the first 7 bits of the byte, then offset by bits read so far.
v |= (static_cast<uint64_t>(byte) & 0x7F) << i; v |= (static_cast<uint64_t>(byte) & 0x7F) << i;
// True if the msb is not a continuation byte. // Return if the msb is not a continuation byte.
if (static_cast<uint64_t>(byte) < 0x80) { if (byte < 0x80) {
*val = v; *val = v;
return true; return true;
} }
@ -140,7 +140,16 @@ bool ByteBufferReader::ReadString(std::string* val, size_t len) {
} }
} }
bool ByteBufferReader::ReadBytes(rtc::ArrayView<uint8_t> val) {
return ReadBytes(val.data(), val.size());
}
bool ByteBufferReader::ReadBytes(char* val, size_t len) { bool ByteBufferReader::ReadBytes(char* val, size_t len) {
return ReadBytes(reinterpret_cast<uint8_t*>(val), len);
}
// Private function supporting the other Read* functions.
bool ByteBufferReader::ReadBytes(uint8_t* val, size_t len) {
if (len > Length()) { if (len > Length()) {
return false; return false;
} else { } else {

View File

@ -157,6 +157,9 @@ class ByteBufferReader {
bool ReadUInt32(uint32_t* val); bool ReadUInt32(uint32_t* val);
bool ReadUInt64(uint64_t* val); bool ReadUInt64(uint64_t* val);
bool ReadUVarint(uint64_t* val); bool ReadUVarint(uint64_t* val);
bool ReadBytes(rtc::ArrayView<uint8_t> val);
// For backwards compatibility.
// TODO(bugs.webrtc.org/15661): Deprecate and remove.
bool ReadBytes(char* val, size_t len); bool ReadBytes(char* val, size_t len);
// Appends next `len` bytes from the buffer to `val`. Returns false // Appends next `len` bytes from the buffer to `val`. Returns false
@ -169,8 +172,9 @@ class ByteBufferReader {
// after this call. // after this call.
bool Consume(size_t size); bool Consume(size_t size);
protected: private:
void Construct(const uint8_t* bytes, size_t size); void Construct(const uint8_t* bytes, size_t size);
bool ReadBytes(uint8_t* val, size_t len);
const uint8_t* bytes_; const uint8_t* bytes_;
size_t size_; size_t size_;

View File

@ -264,4 +264,18 @@ TEST(ByteBufferTest, ReadFromArrayView) {
EXPECT_FALSE(read_buffer.ReadUInt8(&val)); EXPECT_FALSE(read_buffer.ReadUInt8(&val));
} }
TEST(ByteBufferTest, ReadToArrayView) {
const uint8_t buf[] = {'a', 'b', 'c'};
ArrayView<const uint8_t> stored_view(buf, 3);
ByteBufferReader read_buffer(stored_view);
uint8_t result[] = {'1', '2', '3'};
EXPECT_TRUE(read_buffer.ReadBytes(rtc::ArrayView<uint8_t>(result, 2)));
EXPECT_EQ(result[0], 'a');
EXPECT_EQ(result[1], 'b');
EXPECT_EQ(result[2], '3');
EXPECT_TRUE(read_buffer.ReadBytes(rtc::ArrayView<uint8_t>(&result[2], 1)));
EXPECT_EQ(result[2], 'c');
EXPECT_FALSE(read_buffer.ReadBytes(rtc::ArrayView<uint8_t>(result, 1)));
}
} // namespace rtc } // namespace rtc