OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 | 6 |
7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
8 #include "net/spdy/spdy_frame_reader.h" | 8 #include "net/spdy/spdy_frame_reader.h" |
| 9 #include "net/spdy/spdy_protocol.h" |
9 | 10 |
10 namespace net { | 11 namespace net { |
11 | 12 |
12 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len) | 13 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len) |
13 : data_(data), | 14 : data_(data), |
14 len_(len), | 15 len_(len), |
15 ofs_(0) { | 16 ofs_(0) { |
16 } | 17 } |
17 | 18 |
| 19 bool SpdyFrameReader::ReadUInt8(uint8* result) { |
| 20 // Make sure that we have the whole uint8. |
| 21 if (!CanRead(1)) { |
| 22 OnFailure(); |
| 23 return false; |
| 24 } |
| 25 |
| 26 // Read into result. |
| 27 *result = *reinterpret_cast<const uint8*>(data_ + ofs_); |
| 28 |
| 29 // Iterate. |
| 30 ofs_ += 1; |
| 31 |
| 32 return true; |
| 33 } |
| 34 |
18 bool SpdyFrameReader::ReadUInt16(uint16* result) { | 35 bool SpdyFrameReader::ReadUInt16(uint16* result) { |
19 // Make sure that we have the whole uint16. | 36 // Make sure that we have the whole uint16. |
20 if (!CanRead(2)) { | 37 if (!CanRead(2)) { |
21 OnFailure(); | 38 OnFailure(); |
22 return false; | 39 return false; |
23 } | 40 } |
24 | 41 |
25 // Read into result. | 42 // Read into result. |
26 *result = ntohs(*(reinterpret_cast<const uint16*>(data_ + ofs_))); | 43 *result = ntohs(*(reinterpret_cast<const uint16*>(data_ + ofs_))); |
27 | 44 |
(...skipping 12 matching lines...) Expand all Loading... |
40 | 57 |
41 // Read into result. | 58 // Read into result. |
42 *result = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_))); | 59 *result = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_))); |
43 | 60 |
44 // Iterate. | 61 // Iterate. |
45 ofs_ += 4; | 62 ofs_ += 4; |
46 | 63 |
47 return true; | 64 return true; |
48 } | 65 } |
49 | 66 |
| 67 bool SpdyFrameReader::ReadUInt31(uint32* result) { |
| 68 bool success = ReadUInt32(result); |
| 69 |
| 70 // Zero out highest-order bit. |
| 71 if (success) { |
| 72 *result &= kStreamIdMask; |
| 73 } |
| 74 |
| 75 return success; |
| 76 } |
| 77 |
50 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) { | 78 bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) { |
51 // Read resultant length. | 79 // Read resultant length. |
52 uint16 result_len; | 80 uint16 result_len; |
53 if (!ReadUInt16(&result_len)) { | 81 if (!ReadUInt16(&result_len)) { |
54 // OnFailure() already called. | 82 // OnFailure() already called. |
55 return false; | 83 return false; |
56 } | 84 } |
57 | 85 |
58 // Make sure that we have the whole string. | 86 // Make sure that we have the whole string. |
59 if (!CanRead(result_len)) { | 87 if (!CanRead(result_len)) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 130 |
103 // Read into result. | 131 // Read into result. |
104 memcpy(result, data_ + ofs_, size); | 132 memcpy(result, data_ + ofs_, size); |
105 | 133 |
106 // Iterate. | 134 // Iterate. |
107 ofs_ += size; | 135 ofs_ += size; |
108 | 136 |
109 return true; | 137 return true; |
110 } | 138 } |
111 | 139 |
| 140 bool SpdyFrameReader::Seek(size_t size) { |
| 141 if (!CanRead(size)) { |
| 142 OnFailure(); |
| 143 return false; |
| 144 } |
| 145 |
| 146 // Iterate. |
| 147 ofs_ += size; |
| 148 |
| 149 return true; |
| 150 } |
| 151 |
112 bool SpdyFrameReader::IsDoneReading() const { | 152 bool SpdyFrameReader::IsDoneReading() const { |
113 return len_ == ofs_; | 153 return len_ == ofs_; |
114 } | 154 } |
115 | 155 |
116 bool SpdyFrameReader::CanRead(size_t bytes) const { | 156 bool SpdyFrameReader::CanRead(size_t bytes) const { |
117 return bytes <= (len_ - ofs_); | 157 return bytes <= (len_ - ofs_); |
118 } | 158 } |
119 | 159 |
120 void SpdyFrameReader::OnFailure() { | 160 void SpdyFrameReader::OnFailure() { |
121 // Set our iterator to the end of the buffer so that further reads fail | 161 // Set our iterator to the end of the buffer so that further reads fail |
122 // immediately. | 162 // immediately. |
123 ofs_ = len_; | 163 ofs_ = len_; |
124 } | 164 } |
125 | 165 |
126 } // namespace net | 166 } // namespace net |
OLD | NEW |