diff options
Diffstat (limited to 'net/quic/quic_data_reader.cc')
-rw-r--r-- | net/quic/quic_data_reader.cc | 76 |
1 files changed, 18 insertions, 58 deletions
diff --git a/net/quic/quic_data_reader.cc b/net/quic/quic_data_reader.cc index 263bafb..3bb7fc3 100644 --- a/net/quic/quic_data_reader.cc +++ b/net/quic/quic_data_reader.cc @@ -15,38 +15,11 @@ QuicDataReader::QuicDataReader(const char* data, const size_t len) } bool QuicDataReader::ReadUInt16(uint16* result) { - // Make sure that we have the whole uint16. - // TODO(rch): use sizeof instead of magic numbers. - // Refactor to use a common Read(void* buffer, size_t len) - // method that will do the memcpy and the advancement of pos_. - if (!CanRead(2)) { - OnFailure(); - return false; - } - - // Read into result. - memcpy(result, data_ + pos_, 2); - - // Iterate. - pos_ += 2; - - return true; + return ReadBytes(result, sizeof(*result)); } bool QuicDataReader::ReadUInt32(uint32* result) { - // Make sure that we have the whole uint32. - if (!CanRead(4)) { - OnFailure(); - return false; - } - - // Read into result. - memcpy(result, data_ + pos_, 4); - - // Iterate. - pos_ += 4; - - return true; + return ReadBytes(result, sizeof(*result)); } bool QuicDataReader::ReadUInt48(uint64* result) { @@ -68,19 +41,7 @@ bool QuicDataReader::ReadUInt48(uint64* result) { } bool QuicDataReader::ReadUInt64(uint64* result) { - // Make sure that we have the whole uint64. - if (!CanRead(8)) { - OnFailure(); - return false; - } - - // Read into result. - memcpy(result, data_ + pos_, 8); - - // Iterate. - pos_ += 8; - - return true; + return ReadBytes(result, sizeof(*result)); } bool QuicDataReader::ReadUInt128(uint128* result) { @@ -109,15 +70,15 @@ bool QuicDataReader::ReadStringPiece16(StringPiece* result) { return ReadStringPiece(result, result_len); } -bool QuicDataReader::ReadBytes(void* result, size_t size) { +bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) { // Make sure that we have enough data to read. if (!CanRead(size)) { OnFailure(); return false; } - // Read into result. - memcpy(result, data_ + pos_, size); + // Set result. + result->set(data_ + pos_, size); // Iterate. pos_ += size; @@ -125,16 +86,25 @@ bool QuicDataReader::ReadBytes(void* result, size_t size) { return true; } +StringPiece QuicDataReader::ReadRemainingPayload() { + StringPiece payload = PeekRemainingPayload(); + pos_ = len_; + return payload; +} -bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) { +StringPiece QuicDataReader::PeekRemainingPayload() { + return StringPiece(data_ + pos_, len_ - pos_); +} + +bool QuicDataReader::ReadBytes(void* result, size_t size) { // Make sure that we have enough data to read. if (!CanRead(size)) { OnFailure(); return false; } - // Set result. - result->set(data_ + pos_, size); + // Read into result. + memcpy(result, data_ + pos_, size); // Iterate. pos_ += size; @@ -142,16 +112,6 @@ bool QuicDataReader::ReadStringPiece(StringPiece* result, size_t size) { return true; } -StringPiece QuicDataReader::PeekRemainingPayload() { - return StringPiece(data_ + pos_, len_ - pos_); -} - -StringPiece QuicDataReader::ReadRemainingPayload() { - StringPiece payload = PeekRemainingPayload(); - pos_ = len_; - return payload; -} - bool QuicDataReader::IsDoneReading() const { return len_ == pos_; } |