| 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 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "net/base/big_endian.h" | 14 #include "net/base/big_endian.h" |
| 15 #include "net/base/io_buffer.h" |
| 15 #include "net/websockets/websocket_frame.h" | 16 #include "net/websockets/websocket_frame.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const uint8 kFinalBit = 0x80; | 20 const uint8 kFinalBit = 0x80; |
| 20 const uint8 kReserved1Bit = 0x40; | 21 const uint8 kReserved1Bit = 0x40; |
| 21 const uint8 kReserved2Bit = 0x20; | 22 const uint8 kReserved2Bit = 0x20; |
| 22 const uint8 kReserved3Bit = 0x10; | 23 const uint8 kReserved3Bit = 0x10; |
| 23 const uint8 kOpCodeMask = 0xF; | 24 const uint8 kOpCodeMask = 0xF; |
| 24 const uint8 kMaskBit = 0x80; | 25 const uint8 kMaskBit = 0x80; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 const char* end = &buffer_.front() + buffer_.size(); | 178 const char* end = &buffer_.front() + buffer_.size(); |
| 178 uint64 next_size = std::min<uint64>( | 179 uint64 next_size = std::min<uint64>( |
| 179 end - current, | 180 end - current, |
| 180 current_frame_header_->payload_length - frame_offset_); | 181 current_frame_header_->payload_length - frame_offset_); |
| 181 | 182 |
| 182 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); | 183 scoped_ptr<WebSocketFrameChunk> frame_chunk(new WebSocketFrameChunk); |
| 183 if (first_chunk) { | 184 if (first_chunk) { |
| 184 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); | 185 frame_chunk->header.reset(new WebSocketFrameHeader(*current_frame_header_)); |
| 185 } | 186 } |
| 186 frame_chunk->final_chunk = false; | 187 frame_chunk->final_chunk = false; |
| 187 frame_chunk->data.assign(current, current + next_size); | 188 frame_chunk->data = new IOBuffer(next_size); |
| 189 DCHECK(frame_chunk->data); |
| 190 char* io_data = frame_chunk->data->data(); |
| 191 memcpy(io_data, current, next_size); |
| 188 if (current_frame_header_->masked) { | 192 if (current_frame_header_->masked) { |
| 189 // Unmask the payload. | 193 // Unmask the payload. |
| 190 // TODO(yutak): This could be faster by doing unmasking for each | 194 // TODO(yutak): This could be faster by doing unmasking for each |
| 191 // machine word (instead of each byte). | 195 // machine word (instead of each byte). |
| 192 size_t key_offset = frame_offset_ % kMaskingKeyLength; | 196 size_t key_offset = frame_offset_ % kMaskingKeyLength; |
| 193 for (uint64 i = 0; i < next_size; ++i) { | 197 for (uint64 i = 0; i < next_size; ++i) { |
| 194 frame_chunk->data[i] ^= masking_key_[key_offset]; | 198 io_data[i] ^= masking_key_[key_offset]; |
| 195 key_offset = (key_offset + 1) % kMaskingKeyLength; | 199 key_offset = (key_offset + 1) % kMaskingKeyLength; |
| 196 } | 200 } |
| 197 } | 201 } |
| 198 | 202 |
| 199 current_read_pos_ += next_size; | 203 current_read_pos_ += next_size; |
| 200 frame_offset_ += next_size; | 204 frame_offset_ += next_size; |
| 201 | 205 |
| 202 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); | 206 DCHECK_LE(frame_offset_, current_frame_header_->payload_length); |
| 203 if (frame_offset_ == current_frame_header_->payload_length) { | 207 if (frame_offset_ == current_frame_header_->payload_length) { |
| 204 frame_chunk->final_chunk = true; | 208 frame_chunk->final_chunk = true; |
| 205 current_frame_header_.reset(); | 209 current_frame_header_.reset(); |
| 206 frame_offset_ = 0; | 210 frame_offset_ = 0; |
| 207 } | 211 } |
| 208 | 212 |
| 209 return frame_chunk.Pass(); | 213 return frame_chunk.Pass(); |
| 210 } | 214 } |
| 211 | 215 |
| 212 } // namespace net | 216 } // namespace net |
| OLD | NEW |