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::ReadUInt31(uint32* result) {
68 bool success = ReadUInt32(result);
70 // Zero out highest-order bit.
72 *result &= 0x7fffffff;
78 bool SpdyFrameReader::ReadUInt24(uint32* result) {
79 // Make sure that we have the whole uint24.
87 memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3);
88 *result = ntohl(*result);
96 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) {
97 // Read resultant length.
99 if (!ReadUInt16(&result_len)) {
100 // OnFailure() already called.
104 // Make sure that we have the whole string.
105 if (!CanRead(result_len)) {
111 result->set(data_ + ofs_, result_len);
119 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece* result) {
120 // Read resultant length.
122 if (!ReadUInt32(&result_len)) {
123 // OnFailure() already called.
127 // Make sure that we have the whole string.
128 if (!CanRead(result_len)) {
134 result->set(data_ + ofs_, result_len);
142 bool SpdyFrameReader::ReadBytes(void* result, size_t size) {
143 // Make sure that we have enough data to read.
144 if (!CanRead(size)) {
150 memcpy(result, data_ + ofs_, size);
158 bool SpdyFrameReader::Seek(size_t size) {
159 if (!CanRead(size)) {
170 bool SpdyFrameReader::IsDoneReading() const {
174 bool SpdyFrameReader::CanRead(size_t bytes) const {
175 return bytes <= (len_ - ofs_);
178 void SpdyFrameReader::OnFailure() {
179 // Set our iterator to the end of the buffer so that further reads fail