| 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..b2a577adcc9f5ecc4689b9f0c4990fab9e04f7fa
|
| --- /dev/null
|
| +++ b/net/websockets/websocket_deflater.cc
|
| @@ -0,0 +1,115 @@
|
| +// 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 <vector>
|
| +
|
| +#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() {
|
| + if (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;
|
| +}
|
| +
|
| +scoped_refptr<IOBufferWithSize> WebSocketDeflater::Finish() {
|
| + scoped_refptr<IOBufferWithSize> result;
|
| + 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());
|
| + result = new IOBufferWithSize(2);
|
| + memcpy(result->data(), "\x02\x00", result->size());
|
| + Reset();
|
| + return result;
|
| + }
|
| + stream_->next_in = NULL;
|
| + stream_->avail_in = 0;
|
| +
|
| + const size_t FINISH_AVAILABLE_SIZE = 4096;
|
| + do {
|
| + int result = Deflate(FINISH_AVAILABLE_SIZE, Z_SYNC_FLUSH);
|
| + if (result != Z_OK && result != Z_BUF_ERROR) {
|
| + Reset();
|
| + return NULL;
|
| + }
|
| + } while (!stream_->avail_out);
|
| + // Remove 4 octets from the tail as the specification requires.
|
| + if (buffer_.size() < 4) {
|
| + Reset();
|
| + return NULL;
|
| + }
|
| + result = new IOBufferWithSize(buffer_.size() - 4);
|
| + memcpy(result->data(), buffer_.data(), result->size());
|
| + Reset();
|
| + return result;
|
| +}
|
| +
|
| +void WebSocketDeflater::Reset() {
|
| + buffer_.clear();
|
| + 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::vector 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]);
|
| + stream_->avail_out = avail_out;
|
| +
|
| + int result = deflate(stream_.get(), flush);
|
| + buffer_.resize(buffer_.size() - stream_->avail_out);
|
| + return result;
|
| +}
|
| +
|
| +} // namespace net
|
|
|