Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2558)

Unified Diff: net/websockets/websocket_deflater.cc

Issue 23623008: Implement WebSocketDeflater. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/websockets/websocket_deflater.h ('k') | net/websockets/websocket_deflater_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..f003aaa17d8ad6183c526dd673069ac3a67bacf4
--- /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),
+ 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;
+
+ 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();
+ // TODO(yhirano): std::vector initializes the expanded area,
+ // but the initialization is actually not necessary.
+ buffer_.resize(buffer_.size() + estimation);
+ 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) {
Adam Rice 2013/08/30 07:16:11 Probably these { } need to be removed too.
yhirano 2013/08/30 09:42:11 Done.
+ 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 = NULL;
+ stream_->avail_in = 0;
+
+ const size_t available_size = 4096;
Adam Rice 2013/08/30 07:16:11 Where did the number 4096 come from?
yhirano 2013/08/30 07:26:34 The deflater works for arbitrary value if it is no
+ do {
Adam Rice 2013/08/30 07:16:11 I just realised that this loop is almost the same
yhirano 2013/08/30 09:42:11 Done.
yhirano 2013/08/30 10:56:31 I'm sorry, I was wrong. Loop condition in AddBytes
+ 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 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;
+}
+
+} // namespace net
« no previous file with comments | « net/websockets/websocket_deflater.h ('k') | net/websockets/websocket_deflater_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698