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.
7 #include "base/sys_byteorder.h"
8 #include "net/spdy/spdy_frame_reader.h"
12 SpdyFrameReader::SpdyFrameReader(const char* data
, const size_t len
)
18 bool SpdyFrameReader::ReadUInt16(uint16
* result
) {
19 // Make sure that we have the whole uint16.
26 *result
= ntohs(*(reinterpret_cast<const uint16
*>(data_
+ ofs_
)));
34 bool SpdyFrameReader::ReadUInt32(uint32
* result
) {
35 // Make sure that we have the whole uint32.
42 *result
= ntohl(*(reinterpret_cast<const uint32
*>(data_
+ ofs_
)));
50 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece
* result
) {
51 // Read resultant length.
53 if (!ReadUInt16(&result_len
)) {
54 // OnFailure() already called.
58 // Make sure that we have the whole string.
59 if (!CanRead(result_len
)) {
65 result
->set(data_
+ ofs_
, result_len
);
73 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece
* result
) {
74 // Read resultant length.
76 if (!ReadUInt32(&result_len
)) {
77 // OnFailure() already called.
81 // Make sure that we have the whole string.
82 if (!CanRead(result_len
)) {
88 result
->set(data_
+ ofs_
, result_len
);
96 bool SpdyFrameReader::ReadBytes(void* result
, size_t size
) {
97 // Make sure that we have enough data to read.
104 memcpy(result
, data_
+ ofs_
, size
);
112 bool SpdyFrameReader::IsDoneReading() const {
116 bool SpdyFrameReader::CanRead(size_t bytes
) const {
117 return bytes
<= (len_
- ofs_
);
120 void SpdyFrameReader::OnFailure() {
121 // Set our iterator to the end of the buffer so that further reads fail