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"
9 #include "net/spdy/spdy_protocol.h"
13 SpdyFrameReader::SpdyFrameReader(const char* data
, const size_t len
)
19 bool SpdyFrameReader::ReadUInt8(uint8
* result
) {
20 // Make sure that we have the whole uint8.
27 *result
= *reinterpret_cast<const uint8
*>(data_
+ ofs_
);
35 bool SpdyFrameReader::ReadUInt16(uint16
* result
) {
36 // Make sure that we have the whole uint16.
43 *result
= ntohs(*(reinterpret_cast<const uint16
*>(data_
+ ofs_
)));
51 bool SpdyFrameReader::ReadUInt32(uint32
* result
) {
52 // Make sure that we have the whole uint32.
59 *result
= ntohl(*(reinterpret_cast<const uint32
*>(data_
+ ofs_
)));
67 bool SpdyFrameReader::ReadUInt64(uint64
* result
) {
68 // Make sure that we have the whole uint64.
74 // Read into result. Network byte order is big-endian.
75 uint64 upper
= ntohl(*(reinterpret_cast<const uint32
*>(data_
+ ofs_
)));
76 uint64 lower
= ntohl(*(reinterpret_cast<const uint32
*>(data_
+ ofs_
+ 4)));
77 *result
= (upper
<< 32) + lower
;
85 bool SpdyFrameReader::ReadUInt31(uint32
* result
) {
86 bool success
= ReadUInt32(result
);
88 // Zero out highest-order bit.
90 *result
&= 0x7fffffff;
96 bool SpdyFrameReader::ReadUInt24(uint32
* result
) {
97 // Make sure that we have the whole uint24.
105 memcpy(reinterpret_cast<char*>(result
) + 1, data_
+ ofs_
, 3);
106 *result
= ntohl(*result
);
114 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece
* result
) {
115 // Read resultant length.
117 if (!ReadUInt16(&result_len
)) {
118 // OnFailure() already called.
122 // Make sure that we have the whole string.
123 if (!CanRead(result_len
)) {
129 result
->set(data_
+ ofs_
, result_len
);
137 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece
* result
) {
138 // Read resultant length.
140 if (!ReadUInt32(&result_len
)) {
141 // OnFailure() already called.
145 // Make sure that we have the whole string.
146 if (!CanRead(result_len
)) {
152 result
->set(data_
+ ofs_
, result_len
);
160 bool SpdyFrameReader::ReadBytes(void* result
, size_t size
) {
161 // Make sure that we have enough data to read.
162 if (!CanRead(size
)) {
168 memcpy(result
, data_
+ ofs_
, size
);
176 bool SpdyFrameReader::Seek(size_t size
) {
177 if (!CanRead(size
)) {
188 bool SpdyFrameReader::IsDoneReading() const {
192 bool SpdyFrameReader::CanRead(size_t bytes
) const {
193 return bytes
<= (len_
- ofs_
);
196 void SpdyFrameReader::OnFailure() {
197 // Set our iterator to the end of the buffer so that further reads fail