OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/http2/decoder/decode_http2_structures.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "net/http2/decoder/decode_buffer.h" | |
11 #include "net/http2/http2_constants.h" | |
12 | |
13 namespace net { | |
14 | |
15 // Http2FrameHeader decoding: | |
16 | |
17 void DoDecode(Http2FrameHeader* out, DecodeBuffer* b) { | |
18 DCHECK_NE(nullptr, out); | |
19 DCHECK_NE(nullptr, b); | |
20 DCHECK_LE(Http2FrameHeader::EncodedSize(), b->Remaining()); | |
21 out->payload_length = b->DecodeUInt24(); | |
22 out->type = static_cast<Http2FrameType>(b->DecodeUInt8()); | |
23 out->flags = static_cast<Http2FrameFlag>(b->DecodeUInt8()); | |
24 out->stream_id = b->DecodeUInt31(); | |
25 } | |
26 | |
27 bool MaybeDecode(Http2FrameHeader* out, DecodeBuffer* b) { | |
28 DCHECK_NE(nullptr, out); | |
29 DCHECK_NE(nullptr, b); | |
30 if (b->Remaining() >= Http2FrameHeader::EncodedSize()) { | |
31 DoDecode(out, b); | |
32 return true; | |
33 } | |
34 return false; | |
35 } | |
36 | |
37 bool SlowDecode(Http2FrameHeader* out, DecodeBuffer* b, uint32_t* offset) { | |
38 DCHECK_NE(nullptr, out); | |
39 DCHECK_NE(nullptr, b); | |
40 DCHECK_NE(nullptr, offset); | |
41 DCHECK_GT(Http2FrameHeader::EncodedSize(), *offset); | |
42 if (b->SlowDecodeUInt24(0 /* field_offset */, offset, &out->payload_length) && | |
43 b->SlowDecodeEnum(1 /* field_size */, 3 /* field_offset */, offset, | |
44 &out->type) && | |
45 b->SlowDecodeEnum(1 /* field_size */, 4 /* field_offset */, offset, | |
46 &out->flags) && | |
47 b->SlowDecodeUInt31(5 /* field_offset */, offset, &out->stream_id)) { | |
48 DCHECK_EQ(Http2FrameHeader::EncodedSize(), *offset); | |
49 return true; | |
50 } | |
51 DCHECK_GT(Http2FrameHeader::EncodedSize(), *offset); | |
52 return false; | |
53 } | |
54 | |
55 // Http2PriorityFields decoding: | |
56 | |
57 void DoDecode(Http2PriorityFields* out, DecodeBuffer* b) { | |
58 DCHECK_NE(nullptr, out); | |
59 DCHECK_NE(nullptr, b); | |
60 DCHECK_LE(Http2PriorityFields::EncodedSize(), b->Remaining()); | |
61 uint32_t stream_id_and_flag = b->DecodeUInt32(); | |
62 out->stream_dependency = stream_id_and_flag & StreamIdMask(); | |
63 if (out->stream_dependency == stream_id_and_flag) { | |
64 out->is_exclusive = false; | |
65 } else { | |
66 out->is_exclusive = true; | |
67 } | |
68 // Note that chars are automatically promoted to ints during arithmetic, | |
69 // so 255 + 1 doesn't end up as zero. | |
70 out->weight = b->DecodeUInt8() + 1; | |
71 } | |
72 | |
73 bool MaybeDecode(Http2PriorityFields* out, DecodeBuffer* b) { | |
74 DCHECK_NE(nullptr, out); | |
75 DCHECK_NE(nullptr, b); | |
76 if (b->Remaining() >= Http2PriorityFields::EncodedSize()) { | |
77 DoDecode(out, b); | |
78 return true; | |
79 } | |
80 return false; | |
81 } | |
82 | |
83 bool SlowDecode(Http2PriorityFields* out, DecodeBuffer* b, uint32_t* offset) { | |
84 DCHECK_NE(nullptr, out); | |
85 DCHECK_NE(nullptr, b); | |
86 DCHECK_NE(nullptr, offset); | |
87 DCHECK_GT(Http2PriorityFields::EncodedSize(), *offset); | |
88 const uint32_t start_offset = *offset; | |
89 if (b->SlowDecodeUInt32(0 /* field_offset */, offset, | |
90 &out->stream_dependency) && | |
91 b->SlowDecodeUnsignedInt(1, // field_size | |
92 4, // field_offset | |
93 offset, &out->weight)) { | |
94 DCHECK_EQ(Http2PriorityFields::EncodedSize(), *offset); | |
95 if (start_offset < *offset) { | |
96 // First time here. Extract is_exclusive from stream_dependency. | |
97 const uint32_t stream_id_only = out->stream_dependency & StreamIdMask(); | |
98 if (out->stream_dependency != stream_id_only) { | |
99 out->stream_dependency = stream_id_only; | |
100 out->is_exclusive = true; | |
101 } else { | |
102 out->is_exclusive = false; | |
103 } | |
104 // Need to add one to the weight field because the encoding is 0-255, but | |
105 // interpreted as 1-256. | |
106 ++(out->weight); | |
107 } | |
108 return true; | |
109 } | |
110 DCHECK_GT(Http2PriorityFields::EncodedSize(), *offset); | |
111 return false; | |
112 } | |
113 | |
114 // Http2RstStreamFields decoding: | |
115 | |
116 void DoDecode(Http2RstStreamFields* out, DecodeBuffer* b) { | |
117 DCHECK_NE(nullptr, out); | |
118 DCHECK_NE(nullptr, b); | |
119 DCHECK_LE(Http2RstStreamFields::EncodedSize(), b->Remaining()); | |
120 out->error_code = static_cast<Http2ErrorCode>(b->DecodeUInt32()); | |
121 } | |
122 | |
123 bool MaybeDecode(Http2RstStreamFields* out, DecodeBuffer* b) { | |
124 DCHECK_NE(nullptr, out); | |
125 DCHECK_NE(nullptr, b); | |
126 if (b->Remaining() >= Http2RstStreamFields::EncodedSize()) { | |
127 DoDecode(out, b); | |
128 return true; | |
129 } | |
130 return false; | |
131 } | |
132 | |
133 bool SlowDecode(Http2RstStreamFields* out, DecodeBuffer* b, uint32_t* offset) { | |
134 DCHECK_NE(nullptr, out); | |
135 DCHECK_NE(nullptr, b); | |
136 DCHECK_NE(nullptr, offset); | |
137 DCHECK_GT(Http2RstStreamFields::EncodedSize(), *offset); | |
138 | |
139 if (b->SlowDecodeEnum(4 /* field_size */, 0 /* field_offset */, offset, | |
140 &out->error_code)) { | |
141 DCHECK_EQ(Http2RstStreamFields::EncodedSize(), *offset); | |
142 return true; | |
143 } | |
144 DCHECK_GT(Http2RstStreamFields::EncodedSize(), *offset); | |
145 return false; | |
146 } | |
147 | |
148 // Http2SettingFields decoding: | |
149 | |
150 void DoDecode(Http2SettingFields* out, DecodeBuffer* b) { | |
151 DCHECK_NE(nullptr, out); | |
152 DCHECK_NE(nullptr, b); | |
153 DCHECK_LE(Http2SettingFields::EncodedSize(), b->Remaining()); | |
154 out->parameter = static_cast<Http2SettingsParameter>(b->DecodeUInt16()); | |
155 out->value = b->DecodeUInt32(); | |
156 } | |
157 | |
158 bool MaybeDecode(Http2SettingFields* out, DecodeBuffer* b) { | |
159 DCHECK_NE(nullptr, out); | |
160 DCHECK_NE(nullptr, b); | |
161 if (b->Remaining() >= Http2SettingFields::EncodedSize()) { | |
162 DoDecode(out, b); | |
163 return true; | |
164 } | |
165 return false; | |
166 } | |
167 | |
168 bool SlowDecode(Http2SettingFields* out, DecodeBuffer* b, uint32_t* offset) { | |
169 DCHECK_NE(nullptr, out); | |
170 DCHECK_NE(nullptr, b); | |
171 DCHECK_NE(nullptr, offset); | |
172 DCHECK_LT(*offset, Http2SettingFields::EncodedSize()); | |
173 | |
174 if (b->SlowDecodeEnum(2 /* field_size */, 0 /* field_offset */, offset, | |
175 &out->parameter) && | |
176 b->SlowDecodeUInt32(2 /* field_offset */, offset, &out->value)) { | |
177 DCHECK_EQ(Http2SettingFields::EncodedSize(), *offset); | |
178 return true; | |
179 } | |
180 DCHECK_LT(*offset, Http2SettingFields::EncodedSize()); | |
181 return false; | |
182 } | |
183 | |
184 // Http2PushPromiseFields decoding: | |
185 | |
186 void DoDecode(Http2PushPromiseFields* out, DecodeBuffer* b) { | |
187 DCHECK_NE(nullptr, out); | |
188 DCHECK_NE(nullptr, b); | |
189 DCHECK_LE(Http2PushPromiseFields::EncodedSize(), b->Remaining()); | |
190 out->promised_stream_id = b->DecodeUInt31(); | |
191 } | |
192 | |
193 bool MaybeDecode(Http2PushPromiseFields* out, DecodeBuffer* b) { | |
194 DCHECK_NE(nullptr, out); | |
195 DCHECK_NE(nullptr, b); | |
196 if (b->Remaining() >= Http2PushPromiseFields::EncodedSize()) { | |
197 DoDecode(out, b); | |
198 return true; | |
199 } | |
200 return false; | |
201 } | |
202 | |
203 bool SlowDecode(Http2PushPromiseFields* out, | |
204 DecodeBuffer* b, | |
205 uint32_t* offset) { | |
206 DCHECK_NE(nullptr, out); | |
207 DCHECK_NE(nullptr, b); | |
208 DCHECK_NE(nullptr, offset); | |
209 DCHECK_LT(*offset, Http2PushPromiseFields::EncodedSize()); | |
210 if (b->SlowDecodeUInt31(0 /* field_offset */, offset, | |
211 &out->promised_stream_id)) { | |
212 DCHECK_EQ(Http2PushPromiseFields::EncodedSize(), *offset); | |
213 return true; | |
214 } | |
215 DCHECK_LT(*offset, Http2PushPromiseFields::EncodedSize()); | |
216 return false; | |
217 } | |
218 | |
219 // Http2PingFields decoding: | |
220 | |
221 void DoDecode(Http2PingFields* out, DecodeBuffer* b) { | |
222 DCHECK_NE(nullptr, out); | |
223 DCHECK_NE(nullptr, b); | |
224 DCHECK_LE(Http2PingFields::EncodedSize(), b->Remaining()); | |
225 memcpy(out->opaque_data, b->cursor(), Http2PingFields::EncodedSize()); | |
226 b->AdvanceCursor(Http2PingFields::EncodedSize()); | |
227 } | |
228 | |
229 bool MaybeDecode(Http2PingFields* out, DecodeBuffer* b) { | |
230 DCHECK_NE(nullptr, out); | |
231 DCHECK_NE(nullptr, b); | |
232 if (b->Remaining() >= Http2PingFields::EncodedSize()) { | |
233 DoDecode(out, b); | |
234 return true; | |
235 } | |
236 return false; | |
237 } | |
238 | |
239 bool SlowDecode(Http2PingFields* out, DecodeBuffer* b, uint32_t* offset) { | |
240 DCHECK_NE(nullptr, out); | |
241 DCHECK_NE(nullptr, b); | |
242 DCHECK_NE(nullptr, offset); | |
243 DCHECK_LT(*offset, Http2PingFields::EncodedSize()); | |
244 while (*offset < Http2PingFields::EncodedSize()) { | |
245 if (b->Empty()) { | |
246 return false; | |
247 } | |
248 out->opaque_data[(*offset)++] = b->DecodeUInt8(); | |
249 } | |
250 return true; | |
251 } | |
252 | |
253 // Http2GoAwayFields decoding: | |
254 | |
255 void DoDecode(Http2GoAwayFields* out, DecodeBuffer* b) { | |
256 DCHECK_NE(nullptr, out); | |
257 DCHECK_NE(nullptr, b); | |
258 DCHECK_LE(Http2GoAwayFields::EncodedSize(), b->Remaining()); | |
259 out->last_stream_id = b->DecodeUInt31(); | |
260 out->error_code = static_cast<Http2ErrorCode>(b->DecodeUInt32()); | |
261 } | |
262 | |
263 bool MaybeDecode(Http2GoAwayFields* out, DecodeBuffer* b) { | |
264 DCHECK_NE(nullptr, out); | |
265 DCHECK_NE(nullptr, b); | |
266 if (b->Remaining() >= Http2GoAwayFields::EncodedSize()) { | |
267 DoDecode(out, b); | |
268 return true; | |
269 } | |
270 return false; | |
271 } | |
272 | |
273 bool SlowDecode(Http2GoAwayFields* out, DecodeBuffer* b, uint32_t* offset) { | |
274 DCHECK_NE(nullptr, out); | |
275 DCHECK_NE(nullptr, b); | |
276 DCHECK_NE(nullptr, offset); | |
277 DCHECK_LT(*offset, Http2GoAwayFields::EncodedSize()); | |
278 if (b->SlowDecodeUInt31(0 /* field_offset */, offset, &out->last_stream_id) && | |
279 b->SlowDecodeEnum(4 /* field_size */, 4 /* field_offset */, offset, | |
280 &out->error_code)) { | |
281 DCHECK_EQ(Http2GoAwayFields::EncodedSize(), *offset); | |
282 return true; | |
283 } | |
284 DCHECK_LT(*offset, Http2GoAwayFields::EncodedSize()); | |
285 return false; | |
286 } | |
287 | |
288 // Http2WindowUpdateFields decoding: | |
289 | |
290 void DoDecode(Http2WindowUpdateFields* out, DecodeBuffer* b) { | |
291 DCHECK_NE(nullptr, out); | |
292 DCHECK_NE(nullptr, b); | |
293 DCHECK_LE(Http2WindowUpdateFields::EncodedSize(), b->Remaining()); | |
294 out->window_size_increment = b->DecodeUInt31(); | |
295 } | |
296 | |
297 bool MaybeDecode(Http2WindowUpdateFields* out, DecodeBuffer* b) { | |
298 DCHECK_NE(nullptr, out); | |
299 DCHECK_NE(nullptr, b); | |
300 if (b->Remaining() >= Http2WindowUpdateFields::EncodedSize()) { | |
301 DoDecode(out, b); | |
302 return true; | |
303 } | |
304 return false; | |
305 } | |
306 | |
307 bool SlowDecode(Http2WindowUpdateFields* out, | |
308 DecodeBuffer* b, | |
309 uint32_t* offset) { | |
310 DCHECK_NE(nullptr, out); | |
311 DCHECK_NE(nullptr, b); | |
312 DCHECK_NE(nullptr, offset); | |
313 DCHECK_LT(*offset, Http2WindowUpdateFields::EncodedSize()); | |
314 if (b->SlowDecodeUInt31(0 /* field_offset */, offset, | |
315 &out->window_size_increment)) { | |
316 DCHECK_EQ(Http2WindowUpdateFields::EncodedSize(), *offset); | |
317 return true; | |
318 } | |
319 DCHECK_LT(*offset, Http2WindowUpdateFields::EncodedSize()); | |
320 return false; | |
321 } | |
322 | |
323 // Http2AltSvcFields decoding: | |
324 | |
325 void DoDecode(Http2AltSvcFields* out, DecodeBuffer* b) { | |
326 DCHECK_NE(nullptr, out); | |
327 DCHECK_NE(nullptr, b); | |
328 DCHECK_LE(Http2AltSvcFields::EncodedSize(), b->Remaining()); | |
329 out->origin_length = b->DecodeUInt16(); | |
330 } | |
331 | |
332 bool MaybeDecode(Http2AltSvcFields* out, DecodeBuffer* b) { | |
333 DCHECK_NE(nullptr, out); | |
334 DCHECK_NE(nullptr, b); | |
335 if (b->Remaining() >= Http2AltSvcFields::EncodedSize()) { | |
336 DoDecode(out, b); | |
337 return true; | |
338 } | |
339 return false; | |
340 } | |
341 | |
342 bool SlowDecode(Http2AltSvcFields* out, DecodeBuffer* b, uint32_t* offset) { | |
343 DCHECK_NE(nullptr, out); | |
344 DCHECK_NE(nullptr, b); | |
345 DCHECK_NE(nullptr, offset); | |
346 DCHECK_LT(*offset, Http2AltSvcFields::EncodedSize()); | |
347 if (b->SlowDecodeUInt16(0 /* field_offset */, offset, &out->origin_length)) { | |
348 DCHECK_EQ(Http2AltSvcFields::EncodedSize(), *offset); | |
349 return true; | |
350 } | |
351 DCHECK_LT(*offset, Http2AltSvcFields::EncodedSize()); | |
352 return false; | |
353 } | |
354 | |
355 } // namespace net | |
OLD | NEW |