OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/net/compressed_packet_writer.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/sys_byteorder.h" |
| 11 #include "blimp/net/common.h" |
| 12 #include "net/base/io_buffer.h" |
| 13 #include "third_party/zlib/zlib.h" |
| 14 |
| 15 namespace blimp { |
| 16 namespace { |
| 17 |
| 18 // Allocate the maxmimum amount of memory to deflate (512KB) for higher |
| 19 // compression. (See zconf.h for details on memLevel semantics.) |
| 20 const int kZlibMemoryLevel = 9; |
| 21 |
| 22 // The number of bytes used by the header placed before each compressed block. |
| 23 const int kBlockHeaderSize = sizeof(uint32_t); |
| 24 |
| 25 // DEFLATE block boundary header size, which must be included in the size of |
| 26 // compression output buffers. |
| 27 const int kZlibBlockHeaderSize = 4; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 CompressedPacketWriter::CompressedPacketWriter(scoped_ptr<PacketWriter> sink) |
| 32 : sink_(std::move(sink)), compressed_buf_(new net::GrowableIOBuffer) { |
| 33 DCHECK(sink_); |
| 34 |
| 35 memset(&zlib_stream_, 0, sizeof(z_stream)); |
| 36 |
| 37 // MAX_WBITS means we are using the maximal window size for decompression; |
| 38 // negating it means that we are ignoring headers and CRC checks. |
| 39 int err = deflateInit2(&zlib_stream_, Z_BEST_COMPRESSION, Z_DEFLATED, |
| 40 -MAX_WBITS, // Negative value = no headers or CRC. |
| 41 kZlibMemoryLevel, Z_DEFAULT_STRATEGY); |
| 42 DCHECK_EQ(err, Z_OK); |
| 43 } |
| 44 |
| 45 CompressedPacketWriter::~CompressedPacketWriter() { |
| 46 deflateEnd(&zlib_stream_); |
| 47 } |
| 48 |
| 49 void CompressedPacketWriter::WritePacket( |
| 50 const scoped_refptr<net::DrainableIOBuffer>& buf, |
| 51 const net::CompletionCallback& cb) { |
| 52 DCHECK(buf); |
| 53 DCHECK(!cb.is_null()); |
| 54 size_t uncompressed_size = static_cast<size_t>(buf->BytesRemaining()); |
| 55 |
| 56 if (uncompressed_size > kMaxPacketPayloadSizeBytes) { |
| 57 cb.Run(net::ERR_FILE_TOO_BIG); |
| 58 return; |
| 59 } |
| 60 |
| 61 int rv = Compress(buf, compressed_buf_); |
| 62 if (rv < 0) { |
| 63 cb.Run(rv); |
| 64 } |
| 65 uncompressed_size_total_ += uncompressed_size; |
| 66 compressed_size_total_ += rv; |
| 67 |
| 68 scoped_refptr<net::DrainableIOBuffer> compressed_outbuf( |
| 69 new net::DrainableIOBuffer(compressed_buf_.get(), rv)); |
| 70 sink_->WritePacket(compressed_outbuf, cb); |
| 71 DVLOG(4) << "deflate packet: " << uncompressed_size << " in, " << rv |
| 72 << " out."; |
| 73 DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, " |
| 74 << compressed_size_total_ << " out."; |
| 75 } |
| 76 |
| 77 int CompressedPacketWriter::Compress( |
| 78 const scoped_refptr<net::DrainableIOBuffer>& src_buf, |
| 79 const scoped_refptr<net::GrowableIOBuffer>& dest_buf) { |
| 80 DCHECK_EQ(dest_buf->offset(), 0); |
| 81 const int zlib_output_ubound = |
| 82 deflateBound(&zlib_stream_, src_buf->BytesRemaining()) + |
| 83 kZlibBlockHeaderSize; |
| 84 const int max_packet_size = zlib_output_ubound + kBlockHeaderSize; |
| 85 if (dest_buf->capacity() < max_packet_size) { |
| 86 dest_buf->SetCapacity(max_packet_size); |
| 87 } |
| 88 |
| 89 // Write the uncompressed payload size to the beginning of |dest_buf|. |
| 90 *reinterpret_cast<uint32_t*>(dest_buf->data()) = |
| 91 base::HostToNet32(src_buf->BytesRemaining()); |
| 92 |
| 93 if (src_buf->BytesRemaining() == 0) { |
| 94 return kBlockHeaderSize; |
| 95 } |
| 96 |
| 97 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(src_buf->data()); |
| 98 zlib_stream_.avail_in = static_cast<unsigned>(src_buf->BytesRemaining()); |
| 99 zlib_stream_.next_out = |
| 100 reinterpret_cast<uint8_t*>(&dest_buf->data()[kBlockHeaderSize]); |
| 101 zlib_stream_.avail_out = static_cast<unsigned>(zlib_output_ubound); |
| 102 int err = deflate(&zlib_stream_, Z_SYNC_FLUSH); |
| 103 |
| 104 if (err != Z_OK) { |
| 105 DLOG(FATAL) << "Unexpected deflate() return value: " << err; |
| 106 return net::ERR_UNEXPECTED; |
| 107 } |
| 108 if (zlib_stream_.avail_in > 0) { |
| 109 DLOG(ERROR) << "deflate() did not consume all data, remainder: " |
| 110 << zlib_stream_.avail_in << " bytes."; |
| 111 return net::ERR_UNEXPECTED; |
| 112 } |
| 113 |
| 114 return zlib_output_ubound - zlib_stream_.avail_out + kBlockHeaderSize; |
| 115 } |
| 116 |
| 117 } // namespace blimp |
OLD | NEW |