Chromium Code Reviews| 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..27e0526fe4949417896d244f2c918a6a1fb57f2b |
| --- /dev/null |
| +++ b/blimp/net/compressed_packet_writer.cc |
| @@ -0,0 +1,124 @@ |
| +// 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_); |
| + |
| + zlib_stream_.zalloc = 0; |
|
maniscalco
2016/03/25 22:23:17
Is there a reason you're zero-initializing only th
Kevin M
2016/03/25 22:51:41
From what I've read, deflateInit2 can be depended
maniscalco
2016/03/25 23:00:08
Cool, I think that's wise. It looks like most oth
Kevin M
2016/03/25 23:37:19
Done.
|
| + zlib_stream_.zfree = 0; |
| + |
| + // 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; |
| + } |
| + |
| + size_t compressed_size; |
| + if (!Compress(buf, compressed_buf_, &compressed_size)) { |
| + cb.Run(net::ERR_UNEXPECTED); |
| + return; |
| + } |
| + uncompressed_size_total_ += uncompressed_size; |
| + compressed_size_total_ += compressed_size; |
| + |
| + scoped_refptr<net::DrainableIOBuffer> compressed_outbuf( |
| + new net::DrainableIOBuffer(compressed_buf_.get(), compressed_size)); |
| + sink_->WritePacket(compressed_outbuf, cb); |
| + DVLOG(4) << "deflate packet: " << uncompressed_size << " in, " |
| + << compressed_size << " out."; |
| + DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, " |
| + << compressed_size_total_ << " out."; |
| +} |
| + |
| +bool CompressedPacketWriter::Compress( |
| + const scoped_refptr<net::DrainableIOBuffer>& src_buf, |
| + const scoped_refptr<net::GrowableIOBuffer>& dest_buf, |
| + size_t* compressed_size) { |
| + 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) { |
| + *compressed_size = kBlockHeaderSize; |
| + return true; |
| + } |
| + |
| + 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 false; |
| + } |
| + if (zlib_stream_.avail_in > 0) { |
| + DLOG(ERROR) << "deflate() did not consume all data, remainder: " |
| + << zlib_stream_.avail_in << " bytes."; |
| + return false; |
| + } |
| + |
| + *compressed_size = |
| + zlib_output_ubound - zlib_stream_.avail_out + kBlockHeaderSize; |
| + |
| + return true; |
| +} |
| + |
| +} // namespace blimp |