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/websockets/websocket_frame_parser.h" | 5 #include "net/websockets/websocket_frame_parser.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
14 #include "net/base/big_endian.h" | 15 #include "net/base/big_endian.h" |
| 16 #include "net/base/io_buffer.h" |
15 #include "net/websockets/websocket_frame.h" | 17 #include "net/websockets/websocket_frame.h" |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 const uint8 kFinalBit = 0x80; | 21 const uint8 kFinalBit = 0x80; |
20 const uint8 kReserved1Bit = 0x40; | 22 const uint8 kReserved1Bit = 0x40; |
21 const uint8 kReserved2Bit = 0x20; | 23 const uint8 kReserved2Bit = 0x20; |
22 const uint8 kReserved3Bit = 0x10; | 24 const uint8 kReserved3Bit = 0x10; |
23 const uint8 kOpCodeMask = 0xF; | 25 const uint8 kOpCodeMask = 0xF; |
24 const uint8 kMaskBit = 0x80; | 26 const uint8 kMaskBit = 0x80; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 115 |
114 bool final = (first_byte & kFinalBit) != 0; | 116 bool final = (first_byte & kFinalBit) != 0; |
115 bool reserved1 = (first_byte & kReserved1Bit) != 0; | 117 bool reserved1 = (first_byte & kReserved1Bit) != 0; |
116 bool reserved2 = (first_byte & kReserved2Bit) != 0; | 118 bool reserved2 = (first_byte & kReserved2Bit) != 0; |
117 bool reserved3 = (first_byte & kReserved3Bit) != 0; | 119 bool reserved3 = (first_byte & kReserved3Bit) != 0; |
118 OpCode opcode = first_byte & kOpCodeMask; | 120 OpCode opcode = first_byte & kOpCodeMask; |
119 | 121 |
120 bool masked = (second_byte & kMaskBit) != 0; | 122 bool masked = (second_byte & kMaskBit) != 0; |
121 uint64 payload_length = second_byte & kPayloadLengthMask; | 123 uint64 payload_length = second_byte & kPayloadLengthMask; |
122 bool valid_length_format = true; | 124 bool valid_length_format = true; |
| 125 bool message_too_big = false; |
123 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { | 126 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { |
124 if (end - current < 2) | 127 if (end - current < 2) |
125 return; | 128 return; |
126 uint16 payload_length_16; | 129 uint16 payload_length_16; |
127 ReadBigEndian(current, &payload_length_16); | 130 ReadBigEndian(current, &payload_length_16); |
128 current += 2; | 131 current += 2; |
129 payload_length = payload_length_16; | 132 payload_length = payload_length_16; |
130 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) | 133 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) |
131 valid_length_format = false; | 134 valid_length_format = false; |
132 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { | 135 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { |
133 if (end - current < 8) | 136 if (end - current < 8) |
134 return; | 137 return; |
135 ReadBigEndian(current, &payload_length); | 138 ReadBigEndian(current, &payload_length); |
136 current += 8; | 139 current += 8; |
137 if (payload_length <= kuint16max || | 140 if (payload_length <= kuint16max || |
138 payload_length > static_cast<uint64>(kint64max)) { | 141 payload_length > static_cast<uint64>(kint64max)) { |
139 valid_length_format = false; | 142 valid_length_format = false; |
140 } | 143 } |
| 144 if (payload_length > static_cast<uint64>(kint32max)) |
| 145 message_too_big = true; |
141 } | 146 } |
142 if (!valid_length_format) { | 147 if (!valid_length_format || message_too_big) { |
143 failed_ = true; | 148 failed_ = true; |
144 buffer_.clear(); | 149 buffer_.clear(); |
145 current_read_pos_ = 0; | 150 current_read_pos_ = 0; |
146 current_frame_header_.reset(); | 151 current_frame_header_.reset(); |
147 frame_offset_ = 0; | 152 frame_offset_ = 0; |
148 return; | 153 return; |
149 } | 154 } |
150 | 155 |
151 if (masked) { | 156 if (masked) { |
152 if (end - current < kMaskingKeyLength) | 157 if (end - current < kMaskingKeyLength) |
(...skipping 18 matching lines...) Expand all Loading... |
171 | 176 |
172 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( | 177 scoped_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( |
173 bool first_chunk) { | 178 bool first_chunk) { |
174 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; | 179 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; |
175 | 180 |
176 const char* current = &buffer_.front() + current_read_pos_; | 181 const char* current = &buffer_.front() + current_read_pos_; |
177 const char* end = &buffer_.front() + buffer_.size(); | 182 const char* end = &buffer_.front() + buffer_.size(); |
178 uint64 next_size = std::min<uint64>( | 183 uint64 next_size = std::min<uint64>( |
179 end - current, | 184 end - current, |
180 current_frame_header_->payload_length - frame_offset_); | 185 current_frame_header_->payload_length - frame_offset_); |
| 186 // This check must pass because |payload_length| is already checked to be |
| 187 // less than std::numeric_limits<int>::max() when the header is parsed. |
| 188 DCHECK_LE(next_size, static_cast<uint64>(kint32max)); |
181 | 189 |
182 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); | 190 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); |
183 if (first_chunk) { | 191 if (first_chunk) { |
184 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); | 192 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); |
185 } | 193 } |
186 frame_chunk->final_chunk = false; | 194 frame_chunk->final_chunk = false; |
187 frame_chunk->data.assign(current, current + next_size); | 195 if (next_size) { |
188 if (current_frame_header_->masked) { | 196 frame_chunk->data = new IOBufferWithSize(static_cast<int>(next_size)); |
189 // Unmask the payload. | 197 char* io_data = frame_chunk->data->data(); |
190 // TODO(yutak): This could be faster by doing unmasking for each | 198 memcpy(io_data, current, next_size); |
191 // machine word (instead of each byte). | 199 if (current_frame_header_->masked) { |
192 size_t key_offset = frame_offset_ % kMaskingKeyLength; | 200 // Unmask the payload. |
193 for (uint64 i = 0; i < next_size; ++i) { | 201 // TODO(yutak): This could be faster by doing unmasking for each |
194 frame_chunk->data[i] ^= masking_key_[key_offset]; | 202 // machine word (instead of each byte). |
195 key_offset = (key_offset + 1) % kMaskingKeyLength; | 203 size_t key_offset = frame_offset_ % kMaskingKeyLength; |
| 204 for (uint64 i = 0; i < next_size; ++i) { |
| 205 io_data[i] ^= masking_key_[key_offset]; |
| 206 key_offset = (key_offset + 1) % kMaskingKeyLength; |
| 207 } |
196 } | 208 } |
| 209 |
| 210 current_read_pos_ += next_size; |
| 211 frame_offset_ += next_size; |
197 } | 212 } |
198 | 213 |
199 current_read_pos_ += next_size; | |
200 frame_offset_ += next_size; | |
201 | |
202 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); | 214 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); |
203 if (frame_offset_ == current_frame_header_->payload_length) { | 215 if (frame_offset_ == current_frame_header_->payload_length) { |
204 frame_chunk->final_chunk = true; | 216 frame_chunk->final_chunk = true; |
205 current_frame_header_.reset(); | 217 current_frame_header_.reset(); |
206 frame_offset_ = 0; | 218 frame_offset_ = 0; |
207 } | 219 } |
208 | 220 |
209 return frame_chunk.Pass(); | 221 return frame_chunk.Pass(); |
210 } | 222 } |
211 | 223 |
212 } // namespace net | 224 } // namespace net |
OLD | NEW |