Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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/websockets/websocket_deflater.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 #include <algorithm> | |
| 9 #include <deque> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "net/base/io_buffer.h" | |
| 13 #include "third_party/zlib/zlib.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 WebSocketDeflater::WebSocketDeflater(int window_bits, | |
| 18 ContextTakeOverMode mode) | |
| 19 : stream_(new z_stream), | |
| 20 mode_(mode), | |
| 21 are_bytes_added_(false) { | |
| 22 DCHECK_LE(8, window_bits); | |
| 23 DCHECK_GE(15, window_bits); | |
| 24 memset(stream_.get(), 0, sizeof(*stream_)); | |
| 25 int result = deflateInit2(stream_.get(), | |
| 26 Z_DEFAULT_COMPRESSION, | |
| 27 Z_DEFLATED, | |
| 28 -window_bits, | |
|
tyoshino (SeeGerritForStatus)
2013/09/09 06:44:13
// Negative value for raw deflate
yhirano
2013/09/09 08:02:34
Done.
| |
| 29 8, // default mem level | |
| 30 Z_DEFAULT_STRATEGY); | |
| 31 DCHECK_EQ(Z_OK, result); | |
|
tyoshino (SeeGerritForStatus)
2013/09/09 06:44:13
Sorry for late comment.
Please add an initializin
yhirano
2013/09/09 08:02:34
Done.
| |
| 32 const size_t kFixedBufferSize = 4096; | |
| 33 fixed_buffer_.resize(kFixedBufferSize); | |
| 34 } | |
| 35 | |
| 36 WebSocketDeflater::~WebSocketDeflater() { | |
| 37 DCHECK(stream_); | |
| 38 deflateEnd(stream_.get()); | |
| 39 stream_.reset(NULL); | |
| 40 } | |
| 41 | |
| 42 bool WebSocketDeflater::AddBytes(const char* data, size_t size) { | |
| 43 if (!size) | |
| 44 return true; | |
| 45 | |
| 46 are_bytes_added_ = true; | |
| 47 stream_->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data)); | |
| 48 stream_->avail_in = size; | |
| 49 | |
| 50 int result = Deflate(Z_NO_FLUSH); | |
| 51 DCHECK(result != Z_BUF_ERROR || !stream_->avail_in); | |
| 52 return result == Z_BUF_ERROR; | |
| 53 } | |
| 54 | |
| 55 bool WebSocketDeflater::Finish() { | |
| 56 if (!are_bytes_added_) { | |
| 57 // Since consecutive calls of deflate with Z_SYNC_FLUSH and no input | |
| 58 // lead to an error, we create and return the output for the empty input | |
| 59 // manually. | |
| 60 DCHECK(buffer_.empty()); | |
| 61 buffer_.push_back('\x02'); | |
| 62 buffer_.push_back('\x00'); | |
| 63 ResetContext(); | |
| 64 return true; | |
| 65 } | |
| 66 stream_->next_in = NULL; | |
| 67 stream_->avail_in = 0; | |
| 68 | |
| 69 int result = Deflate(Z_SYNC_FLUSH); | |
| 70 if (result != Z_BUF_ERROR) { | |
| 71 ResetContext(); | |
| 72 return false; | |
| 73 } | |
| 74 // Remove 4 octets from the tail as the specification requires. | |
| 75 if (CurrentOutputSize() < 4) { | |
| 76 ResetContext(); | |
| 77 return false; | |
| 78 } | |
| 79 buffer_.resize(buffer_.size() - 4); | |
| 80 ResetContext(); | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 scoped_refptr<IOBufferWithSize> WebSocketDeflater::GetOutput(size_t size) { | |
| 85 std::deque<char>::iterator begin = buffer_.begin(); | |
| 86 std::deque<char>::iterator end = begin + std::min(size, buffer_.size()); | |
| 87 | |
| 88 scoped_refptr<IOBufferWithSize> result = new IOBufferWithSize(end - begin); | |
| 89 std::copy(begin, end, result->data()); | |
| 90 buffer_.erase(begin, end); | |
| 91 return result; | |
| 92 } | |
| 93 | |
| 94 void WebSocketDeflater::ResetContext() { | |
| 95 if (mode_ == DO_NOT_TAKE_OVER_CONTEXT) | |
| 96 deflateReset(stream_.get()); | |
| 97 are_bytes_added_ = false; | |
| 98 } | |
| 99 | |
| 100 int WebSocketDeflater::Deflate(int flush) { | |
| 101 int result = Z_OK; | |
| 102 do { | |
| 103 stream_->next_out = reinterpret_cast<Bytef*>(&fixed_buffer_[0]); | |
| 104 stream_->avail_out = fixed_buffer_.size(); | |
| 105 result = deflate(stream_.get(), flush); | |
| 106 size_t size = fixed_buffer_.size() - stream_->avail_out; | |
| 107 buffer_.insert(buffer_.end(), &fixed_buffer_[0], &fixed_buffer_[size]); | |
| 108 } while (result == Z_OK); | |
| 109 return result; | |
| 110 } | |
| 111 | |
| 112 } // namespace net | |
| OLD | NEW |