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