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 "net/quic/crypto/crypto_framer.h" | 5 #include "net/quic/crypto/crypto_framer.h" |
6 | 6 |
7 #include "net/quic/crypto/crypto_protocol.h" | 7 #include "net/quic/crypto/crypto_protocol.h" |
8 #include "net/quic/quic_data_reader.h" | 8 #include "net/quic/quic_data_reader.h" |
9 #include "net/quic/quic_data_writer.h" | 9 #include "net/quic/quic_data_writer.h" |
10 | 10 |
11 using base::StringPiece; | 11 using base::StringPiece; |
12 using std::make_pair; | |
13 using std::pair; | 12 using std::pair; |
14 using std::vector; | 13 using std::vector; |
15 | 14 |
16 namespace net { | 15 namespace net { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 const size_t kQuicTagSize = sizeof(uint32); | 19 const size_t kQuicTagSize = sizeof(uint32); |
21 const size_t kCryptoEndOffsetSize = sizeof(uint32); | 20 const size_t kCryptoEndOffsetSize = sizeof(uint32); |
22 const size_t kNumEntriesSize = sizeof(uint16); | 21 const size_t kNumEntriesSize = sizeof(uint16); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 if (delta > overhead) { | 98 if (delta > overhead) { |
100 pad_length = delta - overhead; | 99 pad_length = delta - overhead; |
101 } | 100 } |
102 len += overhead + pad_length; | 101 len += overhead + pad_length; |
103 } | 102 } |
104 | 103 |
105 if (num_entries > kMaxEntries) { | 104 if (num_entries > kMaxEntries) { |
106 return nullptr; | 105 return nullptr; |
107 } | 106 } |
108 | 107 |
109 | 108 scoped_ptr<char[]> buffer(new char[len]); |
110 QuicDataWriter writer(len); | 109 QuicDataWriter writer(len, buffer.get()); |
111 if (!writer.WriteUInt32(message.tag())) { | 110 if (!writer.WriteUInt32(message.tag())) { |
112 DCHECK(false) << "Failed to write message tag."; | 111 DCHECK(false) << "Failed to write message tag."; |
113 return nullptr; | 112 return nullptr; |
114 } | 113 } |
115 if (!writer.WriteUInt16(static_cast<uint16>(num_entries))) { | 114 if (!writer.WriteUInt16(static_cast<uint16>(num_entries))) { |
116 DCHECK(false) << "Failed to write size."; | 115 DCHECK(false) << "Failed to write size."; |
117 return nullptr; | 116 return nullptr; |
118 } | 117 } |
119 if (!writer.WriteUInt16(0)) { | 118 if (!writer.WriteUInt16(0)) { |
120 DCHECK(false) << "Failed to write padding."; | 119 DCHECK(false) << "Failed to write padding."; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 173 } |
175 } | 174 } |
176 | 175 |
177 if (need_pad_value) { | 176 if (need_pad_value) { |
178 if (!writer.WriteRepeatedByte('-', pad_length)) { | 177 if (!writer.WriteRepeatedByte('-', pad_length)) { |
179 DCHECK(false) << "Failed to write padding."; | 178 DCHECK(false) << "Failed to write padding."; |
180 return nullptr; | 179 return nullptr; |
181 } | 180 } |
182 } | 181 } |
183 | 182 |
184 return new QuicData(writer.take(), len, true); | 183 return new QuicData(buffer.release(), len, true); |
185 } | 184 } |
186 | 185 |
187 void CryptoFramer::Clear() { | 186 void CryptoFramer::Clear() { |
188 message_.Clear(); | 187 message_.Clear(); |
189 tags_and_lengths_.clear(); | 188 tags_and_lengths_.clear(); |
190 error_ = QUIC_NO_ERROR; | 189 error_ = QUIC_NO_ERROR; |
191 state_ = STATE_READING_TAG; | 190 state_ = STATE_READING_TAG; |
192 } | 191 } |
193 | 192 |
194 QuicErrorCode CryptoFramer::Process(StringPiece input) { | 193 QuicErrorCode CryptoFramer::Process(StringPiece input) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 } | 234 } |
236 return QUIC_CRYPTO_TAGS_OUT_OF_ORDER; | 235 return QUIC_CRYPTO_TAGS_OUT_OF_ORDER; |
237 } | 236 } |
238 | 237 |
239 uint32 end_offset; | 238 uint32 end_offset; |
240 reader.ReadUInt32(&end_offset); | 239 reader.ReadUInt32(&end_offset); |
241 | 240 |
242 if (end_offset < last_end_offset) { | 241 if (end_offset < last_end_offset) { |
243 return QUIC_CRYPTO_TAGS_OUT_OF_ORDER; | 242 return QUIC_CRYPTO_TAGS_OUT_OF_ORDER; |
244 } | 243 } |
245 tags_and_lengths_.push_back( | 244 tags_and_lengths_.push_back(std::make_pair( |
246 make_pair(tag, static_cast<size_t>(end_offset - last_end_offset))); | 245 tag, static_cast<size_t>(end_offset - last_end_offset))); |
247 last_end_offset = end_offset; | 246 last_end_offset = end_offset; |
248 } | 247 } |
249 values_len_ = last_end_offset; | 248 values_len_ = last_end_offset; |
250 state_ = STATE_READING_VALUES; | 249 state_ = STATE_READING_VALUES; |
251 } | 250 } |
252 case STATE_READING_VALUES: | 251 case STATE_READING_VALUES: |
253 if (reader.BytesRemaining() < values_len_) { | 252 if (reader.BytesRemaining() < values_len_) { |
254 break; | 253 break; |
255 } | 254 } |
256 for (vector<pair<QuicTag, size_t> >::const_iterator | 255 for (vector<pair<QuicTag, size_t> >::const_iterator |
(...skipping 23 matching lines...) Expand all Loading... |
280 } | 279 } |
281 *end_offset += pad_length; | 280 *end_offset += pad_length; |
282 if (!writer->WriteUInt32(*end_offset)) { | 281 if (!writer->WriteUInt32(*end_offset)) { |
283 DCHECK(false) << "Failed to write end offset."; | 282 DCHECK(false) << "Failed to write end offset."; |
284 return false; | 283 return false; |
285 } | 284 } |
286 return true; | 285 return true; |
287 } | 286 } |
288 | 287 |
289 } // namespace net | 288 } // namespace net |
OLD | NEW |