Complete SyncMessageFilter initialization after SyncChannel initialization
[chromium-blink-merge.git] / net / spdy / spdy_frame_reader.cc
blob3ecce43e17e511d000aaaec6a35a819b37055dcb
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 <limits>
7 #include "base/sys_byteorder.h"
8 #include "net/spdy/spdy_frame_reader.h"
9 #include "net/spdy/spdy_protocol.h"
11 namespace net {
13 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len)
14 : data_(data),
15 len_(len),
16 ofs_(0) {
19 bool SpdyFrameReader::ReadUInt8(uint8* result) {
20 // Make sure that we have the whole uint8.
21 if (!CanRead(1)) {
22 OnFailure();
23 return false;
26 // Read into result.
27 *result = *reinterpret_cast<const uint8*>(data_ + ofs_);
29 // Iterate.
30 ofs_ += 1;
32 return true;
35 bool SpdyFrameReader::ReadUInt16(uint16* result) {
36 // Make sure that we have the whole uint16.
37 if (!CanRead(2)) {
38 OnFailure();
39 return false;
42 // Read into result.
43 *result = ntohs(*(reinterpret_cast<const uint16*>(data_ + ofs_)));
45 // Iterate.
46 ofs_ += 2;
48 return true;
51 bool SpdyFrameReader::ReadUInt32(uint32* result) {
52 // Make sure that we have the whole uint32.
53 if (!CanRead(4)) {
54 OnFailure();
55 return false;
58 // Read into result.
59 *result = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_)));
61 // Iterate.
62 ofs_ += 4;
64 return true;
67 bool SpdyFrameReader::ReadUInt64(uint64* result) {
68 // Make sure that we have the whole uint64.
69 if (!CanRead(8)) {
70 OnFailure();
71 return false;
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;
79 // Iterate.
80 ofs_ += 8;
82 return true;
85 bool SpdyFrameReader::ReadUInt31(uint32* result) {
86 bool success = ReadUInt32(result);
88 // Zero out highest-order bit.
89 if (success) {
90 *result &= 0x7fffffff;
93 return success;
96 bool SpdyFrameReader::ReadUInt24(uint32* result) {
97 // Make sure that we have the whole uint24.
98 if (!CanRead(3)) {
99 OnFailure();
100 return false;
103 // Read into result.
104 *result = 0;
105 memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3);
106 *result = ntohl(*result);
108 // Iterate.
109 ofs_ += 3;
111 return true;
114 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) {
115 // Read resultant length.
116 uint16 result_len;
117 if (!ReadUInt16(&result_len)) {
118 // OnFailure() already called.
119 return false;
122 // Make sure that we have the whole string.
123 if (!CanRead(result_len)) {
124 OnFailure();
125 return false;
128 // Set result.
129 result->set(data_ + ofs_, result_len);
131 // Iterate.
132 ofs_ += result_len;
134 return true;
137 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece* result) {
138 // Read resultant length.
139 uint32 result_len;
140 if (!ReadUInt32(&result_len)) {
141 // OnFailure() already called.
142 return false;
145 // Make sure that we have the whole string.
146 if (!CanRead(result_len)) {
147 OnFailure();
148 return false;
151 // Set result.
152 result->set(data_ + ofs_, result_len);
154 // Iterate.
155 ofs_ += result_len;
157 return true;
160 bool SpdyFrameReader::ReadBytes(void* result, size_t size) {
161 // Make sure that we have enough data to read.
162 if (!CanRead(size)) {
163 OnFailure();
164 return false;
167 // Read into result.
168 memcpy(result, data_ + ofs_, size);
170 // Iterate.
171 ofs_ += size;
173 return true;
176 bool SpdyFrameReader::Seek(size_t size) {
177 if (!CanRead(size)) {
178 OnFailure();
179 return false;
182 // Iterate.
183 ofs_ += size;
185 return true;
188 bool SpdyFrameReader::IsDoneReading() const {
189 return len_ == ofs_;
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
198 // immediately.
199 ofs_ = len_;
202 } // namespace net