1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_data_reader.h"
7 using base::StringPiece
;
11 QuicDataReader::QuicDataReader(const char* data
, const size_t len
)
17 bool QuicDataReader::ReadUInt16(uint16
* result
) {
18 return ReadBytes(result
, sizeof(*result
));
21 bool QuicDataReader::ReadUInt32(uint32
* result
) {
22 return ReadBytes(result
, sizeof(*result
));
25 bool QuicDataReader::ReadUInt48(uint64
* result
) {
27 if (!ReadUInt32(&lo
)) {
32 if (!ReadUInt16(&hi
)) {
43 bool QuicDataReader::ReadUInt64(uint64
* result
) {
44 return ReadBytes(result
, sizeof(*result
));
47 bool QuicDataReader::ReadUInt128(uint128
* result
) {
51 if (!ReadUInt64(&low_hash
)) {
54 if (!ReadUInt64(&high_hash
)) {
58 *result
= uint128(high_hash
, low_hash
);
62 bool QuicDataReader::ReadStringPiece16(StringPiece
* result
) {
63 // Read resultant length.
65 if (!ReadUInt16(&result_len
)) {
66 // OnFailure() already called.
70 return ReadStringPiece(result
, result_len
);
73 bool QuicDataReader::ReadStringPiece(StringPiece
* result
, size_t size
) {
74 // Make sure that we have enough data to read.
81 result
->set(data_
+ pos_
, size
);
89 StringPiece
QuicDataReader::ReadRemainingPayload() {
90 StringPiece payload
= PeekRemainingPayload();
95 StringPiece
QuicDataReader::PeekRemainingPayload() {
96 return StringPiece(data_
+ pos_
, len_
- pos_
);
99 bool QuicDataReader::ReadBytes(void* result
, size_t size
) {
100 // Make sure that we have enough data to read.
101 if (!CanRead(size
)) {
107 memcpy(result
, data_
+ pos_
, size
);
115 bool QuicDataReader::IsDoneReading() const {
119 size_t QuicDataReader::BytesRemaining() const {
123 bool QuicDataReader::CanRead(size_t bytes
) const {
124 return bytes
<= (len_
- pos_
);
127 void QuicDataReader::OnFailure() {
128 // Set our iterator to the end of the buffer so that further reads fail