| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_inflater.h" | 5 #include "net/websockets/websocket_inflater.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "third_party/zlib/zlib.h" | 13 #include "third_party/zlib/zlib.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 class ShrinkableIOBufferWithSize : public IOBufferWithSize { | 19 class ShrinkableIOBufferWithSize : public IOBufferWithSize { |
| 20 public: | 20 public: |
| 21 explicit ShrinkableIOBufferWithSize(int size) | 21 explicit ShrinkableIOBufferWithSize(int size) : IOBufferWithSize(size) {} |
| 22 : IOBufferWithSize(size) {} | |
| 23 | 22 |
| 24 void Shrink(int new_size) { | 23 void Shrink(int new_size) { |
| 25 DCHECK_LE(new_size, size_); | 24 DCHECK_LE(new_size, size_); |
| 26 size_ = new_size; | 25 size_ = new_size; |
| 27 } | 26 } |
| 28 | 27 |
| 29 private: | 28 private: |
| 30 virtual ~ShrinkableIOBufferWithSize() {} | 29 virtual ~ShrinkableIOBufferWithSize() {} |
| 31 }; | 30 }; |
| 32 | 31 |
| 33 } // namespace | 32 } // namespace |
| 34 | 33 |
| 35 WebSocketInflater::WebSocketInflater() | 34 WebSocketInflater::WebSocketInflater() |
| 36 : input_queue_(kDefaultInputIOBufferCapacity), | 35 : input_queue_(kDefaultInputIOBufferCapacity), |
| 37 output_buffer_(kDefaultBufferCapacity) {} | 36 output_buffer_(kDefaultBufferCapacity) { |
| 37 } |
| 38 | 38 |
| 39 WebSocketInflater::WebSocketInflater(size_t input_queue_capacity, | 39 WebSocketInflater::WebSocketInflater(size_t input_queue_capacity, |
| 40 size_t output_buffer_capacity) | 40 size_t output_buffer_capacity) |
| 41 : input_queue_(input_queue_capacity), | 41 : input_queue_(input_queue_capacity), |
| 42 output_buffer_(output_buffer_capacity) { | 42 output_buffer_(output_buffer_capacity) { |
| 43 DCHECK_GT(input_queue_capacity, 0u); | 43 DCHECK_GT(input_queue_capacity, 0u); |
| 44 DCHECK_GT(output_buffer_capacity, 0u); | 44 DCHECK_GT(output_buffer_capacity, 0u); |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool WebSocketInflater::Initialize(int window_bits) { | 47 bool WebSocketInflater::Initialize(int window_bits) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 break; | 163 break; |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 return result; | 166 return result; |
| 167 } | 167 } |
| 168 | 168 |
| 169 WebSocketInflater::OutputBuffer::OutputBuffer(size_t capacity) | 169 WebSocketInflater::OutputBuffer::OutputBuffer(size_t capacity) |
| 170 : capacity_(capacity), | 170 : capacity_(capacity), |
| 171 buffer_(capacity_ + 1), // 1 for sentinel | 171 buffer_(capacity_ + 1), // 1 for sentinel |
| 172 head_(0), | 172 head_(0), |
| 173 tail_(0) {} | 173 tail_(0) { |
| 174 } |
| 174 | 175 |
| 175 WebSocketInflater::OutputBuffer::~OutputBuffer() {} | 176 WebSocketInflater::OutputBuffer::~OutputBuffer() { |
| 177 } |
| 176 | 178 |
| 177 size_t WebSocketInflater::OutputBuffer::Size() const { | 179 size_t WebSocketInflater::OutputBuffer::Size() const { |
| 178 return (tail_ + buffer_.size() - head_) % buffer_.size(); | 180 return (tail_ + buffer_.size() - head_) % buffer_.size(); |
| 179 } | 181 } |
| 180 | 182 |
| 181 std::pair<char*, size_t> WebSocketInflater::OutputBuffer::GetTail() { | 183 std::pair<char*, size_t> WebSocketInflater::OutputBuffer::GetTail() { |
| 182 DCHECK_LT(tail_, buffer_.size()); | 184 DCHECK_LT(tail_, buffer_.size()); |
| 183 return std::make_pair(&buffer_[tail_], | 185 return std::make_pair(&buffer_[tail_], |
| 184 std::min(capacity_ - Size(), buffer_.size() - tail_)); | 186 std::min(capacity_ - Size(), buffer_.size() - tail_)); |
| 185 } | 187 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 213 DCHECK_LE(advance, Size()); | 215 DCHECK_LE(advance, Size()); |
| 214 head_ = (head_ + advance) % buffer_.size(); | 216 head_ = (head_ + advance) % buffer_.size(); |
| 215 } | 217 } |
| 216 | 218 |
| 217 void WebSocketInflater::OutputBuffer::AdvanceTail(size_t advance) { | 219 void WebSocketInflater::OutputBuffer::AdvanceTail(size_t advance) { |
| 218 DCHECK_LE(advance + Size(), capacity_); | 220 DCHECK_LE(advance + Size(), capacity_); |
| 219 tail_ = (tail_ + advance) % buffer_.size(); | 221 tail_ = (tail_ + advance) % buffer_.size(); |
| 220 } | 222 } |
| 221 | 223 |
| 222 WebSocketInflater::InputQueue::InputQueue(size_t capacity) | 224 WebSocketInflater::InputQueue::InputQueue(size_t capacity) |
| 223 : capacity_(capacity), head_of_first_buffer_(0), tail_of_last_buffer_(0) {} | 225 : capacity_(capacity), head_of_first_buffer_(0), tail_of_last_buffer_(0) { |
| 226 } |
| 224 | 227 |
| 225 WebSocketInflater::InputQueue::~InputQueue() {} | 228 WebSocketInflater::InputQueue::~InputQueue() { |
| 229 } |
| 226 | 230 |
| 227 std::pair<char*, size_t> WebSocketInflater::InputQueue::Top() { | 231 std::pair<char*, size_t> WebSocketInflater::InputQueue::Top() { |
| 228 DCHECK(!IsEmpty()); | 232 DCHECK(!IsEmpty()); |
| 229 if (buffers_.size() == 1) { | 233 if (buffers_.size() == 1) { |
| 230 return std::make_pair(&buffers_.front()->data()[head_of_first_buffer_], | 234 return std::make_pair(&buffers_.front()->data()[head_of_first_buffer_], |
| 231 tail_of_last_buffer_ - head_of_first_buffer_); | 235 tail_of_last_buffer_ - head_of_first_buffer_); |
| 232 } | 236 } |
| 233 return std::make_pair(&buffers_.front()->data()[head_of_first_buffer_], | 237 return std::make_pair(&buffers_.front()->data()[head_of_first_buffer_], |
| 234 capacity_ - head_of_first_buffer_); | 238 capacity_ - head_of_first_buffer_); |
| 235 } | 239 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 size_t num_bytes_to_copy = std::min(size, capacity_ - tail_of_last_buffer_); | 278 size_t num_bytes_to_copy = std::min(size, capacity_ - tail_of_last_buffer_); |
| 275 if (!num_bytes_to_copy) | 279 if (!num_bytes_to_copy) |
| 276 return 0; | 280 return 0; |
| 277 IOBufferWithSize* buffer = buffers_.back().get(); | 281 IOBufferWithSize* buffer = buffers_.back().get(); |
| 278 memcpy(&buffer->data()[tail_of_last_buffer_], data, num_bytes_to_copy); | 282 memcpy(&buffer->data()[tail_of_last_buffer_], data, num_bytes_to_copy); |
| 279 tail_of_last_buffer_ += num_bytes_to_copy; | 283 tail_of_last_buffer_ += num_bytes_to_copy; |
| 280 return num_bytes_to_copy; | 284 return num_bytes_to_copy; |
| 281 } | 285 } |
| 282 | 286 |
| 283 } // namespace net | 287 } // namespace net |
| OLD | NEW |