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

Unified Diff: blimp/net/compressed_packet_writer.cc

Issue 1825263003: Blimp: add packet-level DEFLATE compression using zlib. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Return net::Error values for Compress()/Decompress() Created 4 years, 9 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
Index: blimp/net/compressed_packet_writer.cc
diff --git a/blimp/net/compressed_packet_writer.cc b/blimp/net/compressed_packet_writer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..33609bdec3ae884d8399a096accd96a00cbc4f2d
--- /dev/null
+++ b/blimp/net/compressed_packet_writer.cc
@@ -0,0 +1,117 @@
+// Copyright 2016 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 "blimp/net/compressed_packet_writer.h"
+
+#include <vector>
+
+#include "base/logging.h"
+#include "base/sys_byteorder.h"
+#include "blimp/net/common.h"
+#include "net/base/io_buffer.h"
+#include "third_party/zlib/zlib.h"
+
+namespace blimp {
+namespace {
+
+// Allocate the maxmimum amount of memory to deflate (512KB) for higher
+// compression. (See zconf.h for details on memLevel semantics.)
+const int kZlibMemoryLevel = 9;
+
+// The number of bytes used by the header placed before each compressed block.
+const int kBlockHeaderSize = sizeof(uint32_t);
+
+// DEFLATE block boundary header size, which must be included in the size of
+// compression output buffers.
+const int kZlibBlockHeaderSize = 4;
+
+} // namespace
+
+CompressedPacketWriter::CompressedPacketWriter(scoped_ptr<PacketWriter> sink)
+ : sink_(std::move(sink)), compressed_buf_(new net::GrowableIOBuffer) {
+ DCHECK(sink_);
+
+ memset(&zlib_stream_, 0, sizeof(z_stream));
+
+ // MAX_WBITS means we are using the maximal window size for decompression;
+ // negating it means that we are ignoring headers and CRC checks.
+ int err = deflateInit2(&zlib_stream_, Z_BEST_COMPRESSION, Z_DEFLATED,
+ -MAX_WBITS, // Negative value = no headers or CRC.
+ kZlibMemoryLevel, Z_DEFAULT_STRATEGY);
+ DCHECK_EQ(err, Z_OK);
+}
+
+CompressedPacketWriter::~CompressedPacketWriter() {
+ deflateEnd(&zlib_stream_);
+}
+
+void CompressedPacketWriter::WritePacket(
+ const scoped_refptr<net::DrainableIOBuffer>& buf,
+ const net::CompletionCallback& cb) {
+ DCHECK(buf);
+ DCHECK(!cb.is_null());
+ size_t uncompressed_size = static_cast<size_t>(buf->BytesRemaining());
+
+ if (uncompressed_size > kMaxPacketPayloadSizeBytes) {
+ cb.Run(net::ERR_FILE_TOO_BIG);
+ return;
+ }
+
+ int rv = Compress(buf, compressed_buf_);
+ if (rv < 0) {
+ cb.Run(rv);
+ }
+ uncompressed_size_total_ += uncompressed_size;
+ compressed_size_total_ += rv;
+
+ scoped_refptr<net::DrainableIOBuffer> compressed_outbuf(
+ new net::DrainableIOBuffer(compressed_buf_.get(), rv));
+ sink_->WritePacket(compressed_outbuf, cb);
+ DVLOG(4) << "deflate packet: " << uncompressed_size << " in, " << rv
+ << " out.";
+ DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, "
+ << compressed_size_total_ << " out.";
+}
+
+int CompressedPacketWriter::Compress(
+ const scoped_refptr<net::DrainableIOBuffer>& src_buf,
+ const scoped_refptr<net::GrowableIOBuffer>& dest_buf) {
+ DCHECK_EQ(dest_buf->offset(), 0);
+ const int zlib_output_ubound =
+ deflateBound(&zlib_stream_, src_buf->BytesRemaining()) +
+ kZlibBlockHeaderSize;
+ const int max_packet_size = zlib_output_ubound + kBlockHeaderSize;
+ if (dest_buf->capacity() < max_packet_size) {
+ dest_buf->SetCapacity(max_packet_size);
+ }
+
+ // Write the uncompressed payload size to the beginning of |dest_buf|.
+ *reinterpret_cast<uint32_t*>(dest_buf->data()) =
+ base::HostToNet32(src_buf->BytesRemaining());
+
+ if (src_buf->BytesRemaining() == 0) {
+ return kBlockHeaderSize;
+ }
+
+ zlib_stream_.next_in = reinterpret_cast<uint8_t*>(src_buf->data());
+ zlib_stream_.avail_in = static_cast<unsigned>(src_buf->BytesRemaining());
+ zlib_stream_.next_out =
+ reinterpret_cast<uint8_t*>(&dest_buf->data()[kBlockHeaderSize]);
+ zlib_stream_.avail_out = static_cast<unsigned>(zlib_output_ubound);
+ int err = deflate(&zlib_stream_, Z_SYNC_FLUSH);
+
+ if (err != Z_OK) {
+ DLOG(FATAL) << "Unexpected deflate() return value: " << err;
+ return net::ERR_UNEXPECTED;
+ }
+ if (zlib_stream_.avail_in > 0) {
+ DLOG(ERROR) << "deflate() did not consume all data, remainder: "
+ << zlib_stream_.avail_in << " bytes.";
+ return net::ERR_UNEXPECTED;
+ }
+
+ return zlib_output_ubound - zlib_stream_.avail_out + kBlockHeaderSize;
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698