Chromium Code Reviews| Index: net/websockets/websocket_deflater.cc |
| diff --git a/net/websockets/websocket_deflater.cc b/net/websockets/websocket_deflater.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2c86276b28b2b30eee8191a7ba1fdeee0e07a00d |
| --- /dev/null |
| +++ b/net/websockets/websocket_deflater.cc |
| @@ -0,0 +1,122 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/websockets/websocket_deflater.h" |
| + |
| +#include <string.h> |
| +#include <algorithm> |
| +#include <deque> |
| + |
| +#include "base/logging.h" |
| +#include "net/base/io_buffer.h" |
| +#include "third_party/zlib/zlib.h" |
| + |
| +namespace net { |
| + |
| +WebSocketDeflater::WebSocketDeflater(int window_bits, |
| + ContextTakeOverMode mode) |
| + : stream_(new z_stream), |
| + mode_(mode), |
| + are_bytes_added_(false) { |
| + DCHECK_LE(8, window_bits); |
| + DCHECK_GE(15, window_bits); |
| + memset(stream_.get(), 0, sizeof(*stream_)); |
| + int result = deflateInit2(stream_.get(), |
| + Z_DEFAULT_COMPRESSION, |
| + Z_DEFLATED, |
| + -window_bits, |
| + 8, // default mem level |
| + Z_DEFAULT_STRATEGY); |
| + DCHECK_EQ(Z_OK, result); |
| +} |
| + |
| +WebSocketDeflater::~WebSocketDeflater() { |
| + DCHECK(stream_); |
| + deflateEnd(stream_.get()); |
| + stream_.reset(NULL); |
| +} |
| + |
| +bool WebSocketDeflater::AddBytes(const char* data, size_t size) { |
| + if (!size) |
| + return true; |
| + |
| + are_bytes_added_ = true; |
| + stream_->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data)); |
| + stream_->avail_in = size; |
| + |
| + // The estimation by deflateBound is not accurate if the zlib has some |
| + // remaining input of the last compression. |
| + size_t estimation = deflateBound(stream_.get(), size); |
| + DCHECK_GT(estimation, 0u); |
| + do { |
| + if (Deflate(estimation, Z_NO_FLUSH) != Z_OK) |
| + return false; |
| + estimation *= 2; |
| + } while (stream_->avail_in > 0); |
| + return true; |
| +} |
| + |
| +bool WebSocketDeflater::Finish() { |
| + if (!are_bytes_added_) { |
| + // Since consecutive calls of deflate with Z_SYNC_FLUSH and no input |
| + // lead to an error, we create and return the output for the empty input |
| + // manually. |
| + DCHECK(buffer_.empty()); |
| + buffer_.push_back('\x02'); |
| + buffer_.push_back('\x00'); |
| + Reset(); |
| + return true; |
| + } |
| + stream_->next_in = NULL; |
| + stream_->avail_in = 0; |
| + |
| + const size_t kFinishAvailableSize = 4096; |
| + do { |
| + int result = Deflate(kFinishAvailableSize, Z_SYNC_FLUSH); |
| + if (result != Z_OK && result != Z_BUF_ERROR) { |
| + Reset(); |
| + return false; |
| + } |
| + } while (!stream_->avail_out); |
|
tyoshino (SeeGerritForStatus)
2013/09/06 07:09:24
don't we have to continue until we see Z_BUF_ERROR
yhirano
2013/09/06 10:07:26
Done.
|
| + // Remove 4 octets from the tail as the specification requires. |
| + if (CurrentOutputSize() < 4) { |
| + Reset(); |
| + return false; |
| + } |
| + buffer_.resize(buffer_.size() - 4); |
| + Reset(); |
| + return true; |
| +} |
| + |
| +scoped_refptr<IOBufferWithSize> WebSocketDeflater::GetOutput(size_t size) { |
| + std::deque<char>::iterator begin = buffer_.begin(); |
| + std::deque<char>::iterator end = begin + std::min(size, buffer_.size()); |
| + |
| + scoped_refptr<IOBufferWithSize> result = new IOBufferWithSize(end - begin); |
| + std::copy(begin, end, result->data()); |
| + buffer_.erase(begin, end); |
| + return result; |
| +} |
| + |
| +void WebSocketDeflater::Reset() { |
|
tyoshino (SeeGerritForStatus)
2013/09/06 07:09:24
Reset clears only the state of Deflate. buffer_ is
yhirano
2013/09/06 10:07:26
Done.
|
| + if (mode_ == DO_NOT_TAKE_OVER_CONTEXT) |
| + deflateReset(stream_.get()); |
| + are_bytes_added_ = false; |
| +} |
| + |
| +int WebSocketDeflater::Deflate(size_t avail_out, int flush) { |
| + DCHECK_GT(avail_out, 0u); |
| + size_t current = buffer_.size(); |
| + // TODO(yhirano): std::deque initializes the expanded area, |
| + // but the initialization is actually not necessary. |
| + buffer_.resize(buffer_.size() + avail_out); |
| + stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]); |
|
tyoshino (SeeGerritForStatus)
2013/09/06 07:09:24
is buffer for deque guaranteed to be contiguous?
yhirano
2013/09/06 10:07:26
Thanks, you are right.
I introduced fixed_buffer_
|
| + stream_->avail_out = avail_out; |
| + |
| + int result = deflate(stream_.get(), flush); |
| + buffer_.resize(buffer_.size() - stream_->avail_out); |
| + return result; |
| +} |
| + |
| +} // namespace net |