Pepper.PluginHung histogram needed UMA_.
[chromium-blink-merge.git] / net / spdy / spdy_frame_reader.cc
blob57bf9ebb4682a5b62ee56ee65b100352c3431b64
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"
10 namespace net {
12 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len)
13 : data_(data),
14 len_(len),
15 ofs_(0) {
18 bool SpdyFrameReader::ReadUInt16(uint16* result) {
19 // Make sure that we have the whole uint16.
20 if (!CanRead(2)) {
21 OnFailure();
22 return false;
25 // Read into result.
26 *result = ntohs(*(reinterpret_cast<const uint16*>(data_ + ofs_)));
28 // Iterate.
29 ofs_ += 2;
31 return true;
34 bool SpdyFrameReader::ReadUInt32(uint32* result) {
35 // Make sure that we have the whole uint32.
36 if (!CanRead(4)) {
37 OnFailure();
38 return false;
41 // Read into result.
42 *result = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_)));
44 // Iterate.
45 ofs_ += 4;
47 return true;
50 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) {
51 // Read resultant length.
52 uint16 result_len;
53 if (!ReadUInt16(&result_len)) {
54 // OnFailure() already called.
55 return false;
58 // Make sure that we have the whole string.
59 if (!CanRead(result_len)) {
60 OnFailure();
61 return false;
64 // Set result.
65 result->set(data_ + ofs_, result_len);
67 // Iterate.
68 ofs_ += result_len;
70 return true;
73 bool SpdyFrameReader::ReadStringPiece32(base::StringPiece* result) {
74 // Read resultant length.
75 uint32 result_len;
76 if (!ReadUInt32(&result_len)) {
77 // OnFailure() already called.
78 return false;
81 // Make sure that we have the whole string.
82 if (!CanRead(result_len)) {
83 OnFailure();
84 return false;
87 // Set result.
88 result->set(data_ + ofs_, result_len);
90 // Iterate.
91 ofs_ += result_len;
93 return true;
96 bool SpdyFrameReader::ReadBytes(void* result, size_t size) {
97 // Make sure that we have enough data to read.
98 if (!CanRead(size)) {
99 OnFailure();
100 return false;
103 // Read into result.
104 memcpy(result, data_ + ofs_, size);
106 // Iterate.
107 ofs_ += size;
109 return true;
112 bool SpdyFrameReader::IsDoneReading() const {
113 return len_ == ofs_;
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
122 // immediately.
123 ofs_ = len_;
126 } // namespace net