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