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..779db1504c97c667cabf6dd2b5f503382992e93d |
--- /dev/null |
+++ b/net/websockets/websocket_deflater.cc |
@@ -0,0 +1,118 @@ |
+// 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) |
Adam Rice
2013/08/29 12:04:33
The commas go after the initialiser in Chromium st
yhirano
2013/08/30 06:20:35
Done.
|
+ , 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(nullptr); |
Adam Rice
2013/08/29 12:04:33
I'm not sure why "nullptr" compiles. The compiler
yhirano
2013/08/30 06:20:35
Done.
|
+ } |
+} |
+ |
+bool WebSocketDeflater::AddBytes(const char* data, size_t size) { |
+ if (!size) { |
Adam Rice
2013/08/29 12:04:33
Chromium net people seem to prefer no { } for if s
yhirano
2013/08/30 06:20:35
Done.
|
+ return 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 { |
+ size_t current = buffer_.size(); |
+ buffer_.resize(buffer_.size() + estimation); |
Adam Rice
2013/08/29 12:04:33
This is slightly suboptimal since the vector will
yhirano
2013/08/30 06:20:35
Done.
|
+ stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]); |
+ stream_->avail_out = buffer_.size() - current; |
+ |
+ int result = deflate(stream_.get(), Z_NO_FLUSH); |
+ if (result != Z_OK) { |
+ return false; |
+ } |
+ buffer_.resize(buffer_.size() - stream_->avail_out); |
+ estimation *= 2; |
+ } while (!stream_->avail_out); |
+ are_bytes_added_ = true; |
+ |
+ 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 = nullptr; |
Adam Rice
2013/08/29 12:04:33
NULL in Chromium code. Also 2 other places below.
yhirano
2013/08/30 06:20:35
Done.
|
+ stream_->avail_in = 0; |
+ |
+ const size_t available_size = 4096; |
+ do { |
+ size_t current = buffer_.size(); |
+ buffer_.resize(buffer_.size() + available_size); |
+ stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]); |
+ stream_->avail_out = buffer_.size() - current; |
+ |
+ int result = deflate(stream_.get(), Z_SYNC_FLUSH); |
+ buffer_.resize(buffer_.size() - stream_->avail_out); |
+ if (result != Z_OK && result != Z_BUF_ERROR) { |
+ Reset(); |
+ return nullptr; |
+ } |
+ } while (!stream_->avail_out); |
+ // Remove 4 octets from the tail as the specification requires. |
+ if (buffer_.size() < 4) { |
+ Reset(); |
+ return nullptr; |
+ } |
+ 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; |
+} |
+ |
+} // namespace net |