This source file includes following definitions.
- pos_
- ReadUInt16
- ReadUInt32
- ReadUInt48
- ReadUInt64
- ReadUInt128
- ReadUFloat16
- ReadStringPiece16
- ReadStringPiece
- ReadRemainingPayload
- PeekRemainingPayload
- ReadBytes
- IsDoneReading
- BytesRemaining
- CanRead
- OnFailure
#include "net/quic/quic_data_reader.h"
#include "net/base/int128.h"
#include "net/quic/quic_protocol.h"
using base::StringPiece;
namespace net {
QuicDataReader::QuicDataReader(const char* data, const size_t len)
: data_(data),
len_(len),
pos_(0) {
}
bool QuicDataReader::ReadUInt16(uint16* result) {
return ReadBytes(result, sizeof(*result));
}
bool QuicDataReader::ReadUInt32(uint32* result) {
return ReadBytes(result, sizeof(*result));
}
bool QuicDataReader::ReadUInt48(uint64* result) {
uint32 lo;
if (!ReadUInt32(&lo)) {
return false;
}
uint16 hi;
if (!ReadUInt16(&hi)) {
return false;
}
*result = hi;
*result <<= 32;
*result += lo;
return true;
}
bool QuicDataReader::ReadUInt64(uint64* result) {
return ReadBytes(result, sizeof(*result));
}
bool QuicDataReader::ReadUInt128(uint128* result) {
uint64 high_hash;
uint64 low_hash;
if (!ReadUInt64(&low_hash)) {
return false;
}
if (!ReadUInt64(&high_hash)) {
return false;
}
*result = uint128(high_hash, low_hash);
return true;
}
bool QuicDataReader::ReadUFloat16(uint64* result) {
uint16 value;
if (!ReadUInt16(&value)) {
return false;
}
*result = value;
if (*result < (1 << kUFloat16MantissaEffectiveBits)) {
return true;
}
uint16 exponent = value >> kUFloat16MantissaBits;
--exponent;
DCHECK_GE(exponent, 1);
DCHECK_LE(exponent, kUFloat16MaxExponent);
*result -= exponent << kUFloat16MantissaBits;
*result <<= exponent;
DCHECK_GE(value, 1 << kUFloat16MantissaEffectiveBits);
DCHECK_LE(value, kUFloat16MaxValue);
return true;
}
bool QuicDataReader::ReadStringPiece16(StringPiece* result) {
uint16 result_len;
if (!ReadUInt16(&result_len)) {
return false;
}
return ReadStringPiece(result, result_len);
}
bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) {
if (!CanRead(size)) {
OnFailure();
return false;
}
result->set(data_ + pos_, size);
pos_ += size;
return true;
}
StringPiece QuicDataReader::ReadRemainingPayload() {
StringPiece payload = PeekRemainingPayload();
pos_ = len_;
return payload;
}
StringPiece QuicDataReader::PeekRemainingPayload() {
return StringPiece(data_ + pos_, len_ - pos_);
}
bool QuicDataReader::ReadBytes(void* result, size_t size) {
if (!CanRead(size)) {
OnFailure();
return false;
}
memcpy(result, data_ + pos_, size);
pos_ += size;
return true;
}
bool QuicDataReader::IsDoneReading() const {
return len_ == pos_;
}
size_t QuicDataReader::BytesRemaining() const {
return len_ - pos_;
}
bool QuicDataReader::CanRead(size_t bytes) const {
return bytes <= (len_ - pos_);
}
void QuicDataReader::OnFailure() {
pos_ = len_;
}
}