Chromium Code Reviews| 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 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.
| |
| 36 zlib_stream_.zfree = 0; | |
| 37 | |
| 38 // MAX_WBITS means we are using the maximal window size for decompression; | |
| 39 // negating it means that we are ignoring headers and CRC checks. | |
| 40 int err = deflateInit2(&zlib_stream_, Z_BEST_COMPRESSION, Z_DEFLATED, | |
| 41 -MAX_WBITS, // Negative value = no headers or CRC. | |
| 42 kZlibMemoryLevel, Z_DEFAULT_STRATEGY); | |
| 43 DCHECK_EQ(err, Z_OK); | |
| 44 } | |
| 45 | |
| 46 CompressedPacketWriter::~CompressedPacketWriter() { | |
| 47 deflateEnd(&zlib_stream_); | |
| 48 } | |
| 49 | |
| 50 void CompressedPacketWriter::WritePacket( | |
| 51 const scoped_refptr<net::DrainableIOBuffer>& buf, | |
| 52 const net::CompletionCallback& cb) { | |
| 53 DCHECK(buf); | |
| 54 DCHECK(!cb.is_null()); | |
| 55 size_t uncompressed_size = static_cast<size_t>(buf->BytesRemaining()); | |
| 56 | |
| 57 if (uncompressed_size > kMaxPacketPayloadSizeBytes) { | |
| 58 cb.Run(net::ERR_FILE_TOO_BIG); | |
| 59 return; | |
| 60 } | |
| 61 | |
| 62 size_t compressed_size; | |
| 63 if (!Compress(buf, compressed_buf_, &compressed_size)) { | |
| 64 cb.Run(net::ERR_UNEXPECTED); | |
| 65 return; | |
| 66 } | |
| 67 uncompressed_size_total_ += uncompressed_size; | |
| 68 compressed_size_total_ += compressed_size; | |
| 69 | |
| 70 scoped_refptr<net::DrainableIOBuffer> compressed_outbuf( | |
| 71 new net::DrainableIOBuffer(compressed_buf_.get(), compressed_size)); | |
| 72 sink_->WritePacket(compressed_outbuf, cb); | |
| 73 DVLOG(4) << "deflate packet: " << uncompressed_size << " in, " | |
| 74 << compressed_size << " out."; | |
| 75 DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, " | |
| 76 << compressed_size_total_ << " out."; | |
| 77 } | |
| 78 | |
| 79 bool CompressedPacketWriter::Compress( | |
| 80 const scoped_refptr<net::DrainableIOBuffer>& src_buf, | |
| 81 const scoped_refptr<net::GrowableIOBuffer>& dest_buf, | |
| 82 size_t* compressed_size) { | |
| 83 DCHECK_EQ(dest_buf->offset(), 0); | |
| 84 const int zlib_output_ubound = | |
| 85 deflateBound(&zlib_stream_, src_buf->BytesRemaining()) + | |
| 86 kZlibBlockHeaderSize; | |
| 87 const int max_packet_size = zlib_output_ubound + kBlockHeaderSize; | |
| 88 if (dest_buf->capacity() < max_packet_size) { | |
| 89 dest_buf->SetCapacity(max_packet_size); | |
| 90 } | |
| 91 | |
| 92 // Write the uncompressed payload size to the beginning of |dest_buf|. | |
| 93 *reinterpret_cast<uint32_t*>(dest_buf->data()) = | |
| 94 base::HostToNet32(src_buf->BytesRemaining()); | |
| 95 | |
| 96 if (src_buf->BytesRemaining() == 0) { | |
| 97 *compressed_size = kBlockHeaderSize; | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(src_buf->data()); | |
| 102 zlib_stream_.avail_in = static_cast<unsigned>(src_buf->BytesRemaining()); | |
| 103 zlib_stream_.next_out = | |
| 104 reinterpret_cast<uint8_t*>(&dest_buf->data()[kBlockHeaderSize]); | |
| 105 zlib_stream_.avail_out = static_cast<unsigned>(zlib_output_ubound); | |
| 106 int err = deflate(&zlib_stream_, Z_SYNC_FLUSH); | |
| 107 | |
| 108 if (err != Z_OK) { | |
| 109 DLOG(FATAL) << "Unexpected deflate() return value: " << err; | |
| 110 return false; | |
| 111 } | |
| 112 if (zlib_stream_.avail_in > 0) { | |
| 113 DLOG(ERROR) << "deflate() did not consume all data, remainder: " | |
| 114 << zlib_stream_.avail_in << " bytes."; | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 *compressed_size = | |
| 119 zlib_output_ubound - zlib_stream_.avail_out + kBlockHeaderSize; | |
| 120 | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 } // namespace blimp | |
| OLD | NEW |