OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <limits> | |
6 | |
7 #include "base/sys_byteorder.h" | |
8 #include "net/spdy/spdy_frame_reader.h" | |
9 #include "net/spdy/spdy_protocol.h" | |
10 | |
11 namespace net { | |
12 | |
13 SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len) | |
14 : data_(data), | |
15 len_(len), | |
16 ofs_(0) { | |
17 } | |
18 | |
19 bool SpdyFrameReader::ReadUInt8(uint8_t* result) { | |
20 // Make sure that we have the whole uint8_t. | |
21 if (!CanRead(1)) { | |
22 OnFailure(); | |
23 return false; | |
24 } | |
25 | |
26 // Read into result. | |
27 *result = *reinterpret_cast<const uint8_t*>(data_ + ofs_); | |
28 | |
29 // Iterate. | |
30 ofs_ += 1; | |
31 | |
32 return true; | |
33 } | |
34 | |
35 bool SpdyFrameReader::ReadUInt16(uint16_t* result) { | |
36 // Make sure that we have the whole uint16_t. | |
37 if (!CanRead(2)) { | |
38 OnFailure(); | |
39 return false; | |
40 } | |
41 | |
42 // Read into result. | |
43 *result = | |
44 base::NetToHost16(*(reinterpret_cast<const uint16_t*>(data_ + ofs_))); | |
45 | |
46 // Iterate. | |
47 ofs_ += 2; | |
48 | |
49 return true; | |
50 } | |
51 | |
52 bool SpdyFrameReader::ReadUInt32(uint32_t* result) { | |
53 // Make sure that we have the whole uint32_t. | |
54 if (!CanRead(4)) { | |
55 OnFailure(); | |
56 return false; | |
57 } | |
58 | |
59 // Read into result. | |
60 *result = | |
61 base::NetToHost32(*(reinterpret_cast<const uint32_t*>(data_ + ofs_))); | |
62 | |
63 // Iterate. | |
64 ofs_ += 4; | |
65 | |
66 return true; | |
67 } | |
68 | |
69 bool SpdyFrameReader::ReadUInt64(uint64_t* result) { | |
70 // Make sure that we have the whole uint64_t. | |
71 if (!CanRead(8)) { | |
72 OnFailure(); | |
73 return false; | |
74 } | |
75 | |
76 // Read into result. Network byte order is big-endian. | |
77 uint64_t upper = | |
78 base::NetToHost32(*(reinterpret_cast<const uint32_t*>(data_ + ofs_))); | |
79 uint64_t lower = | |
80 base::NetToHost32(*(reinterpret_cast<const uint32_t*>(data_ + ofs_ + 4))); | |
81 *result = (upper << 32) + lower; | |
82 | |
83 // Iterate. | |
84 ofs_ += 8; | |
85 | |
86 return true; | |
87 } | |
88 | |
89 bool SpdyFrameReader::ReadUInt31(uint32_t* result) { | |
90 bool success = ReadUInt32(result); | |
91 | |
92 // Zero out highest-order bit. | |
93 if (success) { | |
94 *result &= 0x7fffffff; | |
95 } | |
96 | |
97 return success; | |
98 } | |
99 | |
100 bool SpdyFrameReader::ReadUInt24(uint32_t* result) { | |
101 // Make sure that we have the whole uint24. | |
102 if (!CanRead(3)) { | |
103 OnFailure(); | |
104 return false; | |
105 } | |
106 | |
107 // Read into result. | |
108 *result = 0; | |
109 memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3); | |
110 *result = base::NetToHost32(*result); | |
111 | |
112 // Iterate. | |
113 ofs_ += 3; | |
114 | |
115 return true; | |
116 } | |
117 | |
118 bool SpdyFrameReader::ReadStringPiece16(SpdyStringPiece* result) { | |
119 // Read resultant length. | |
120 uint16_t result_len; | |
121 if (!ReadUInt16(&result_len)) { | |
122 // OnFailure() already called. | |
123 return false; | |
124 } | |
125 | |
126 // Make sure that we have the whole string. | |
127 if (!CanRead(result_len)) { | |
128 OnFailure(); | |
129 return false; | |
130 } | |
131 | |
132 // Set result. | |
133 *result = SpdyStringPiece(data_ + ofs_, result_len); | |
134 | |
135 // Iterate. | |
136 ofs_ += result_len; | |
137 | |
138 return true; | |
139 } | |
140 | |
141 bool SpdyFrameReader::ReadStringPiece32(SpdyStringPiece* result) { | |
142 // Read resultant length. | |
143 uint32_t result_len; | |
144 if (!ReadUInt32(&result_len)) { | |
145 // OnFailure() already called. | |
146 return false; | |
147 } | |
148 | |
149 // Make sure that we have the whole string. | |
150 if (!CanRead(result_len)) { | |
151 OnFailure(); | |
152 return false; | |
153 } | |
154 | |
155 // Set result. | |
156 *result = SpdyStringPiece(data_ + ofs_, result_len); | |
157 | |
158 // Iterate. | |
159 ofs_ += result_len; | |
160 | |
161 return true; | |
162 } | |
163 | |
164 bool SpdyFrameReader::ReadBytes(void* result, size_t size) { | |
165 // Make sure that we have enough data to read. | |
166 if (!CanRead(size)) { | |
167 OnFailure(); | |
168 return false; | |
169 } | |
170 | |
171 // Read into result. | |
172 memcpy(result, data_ + ofs_, size); | |
173 | |
174 // Iterate. | |
175 ofs_ += size; | |
176 | |
177 return true; | |
178 } | |
179 | |
180 bool SpdyFrameReader::Seek(size_t size) { | |
181 if (!CanRead(size)) { | |
182 OnFailure(); | |
183 return false; | |
184 } | |
185 | |
186 // Iterate. | |
187 ofs_ += size; | |
188 | |
189 return true; | |
190 } | |
191 | |
192 bool SpdyFrameReader::IsDoneReading() const { | |
193 return len_ == ofs_; | |
194 } | |
195 | |
196 bool SpdyFrameReader::CanRead(size_t bytes) const { | |
197 return bytes <= (len_ - ofs_); | |
198 } | |
199 | |
200 void SpdyFrameReader::OnFailure() { | |
201 // Set our iterator to the end of the buffer so that further reads fail | |
202 // immediately. | |
203 ofs_ = len_; | |
204 } | |
205 | |
206 } // namespace net | |
OLD | NEW |