| 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/numerics/safe_conversions.h" | |
| 11 #include "base/sys_byteorder.h" | |
| 12 #include "blimp/net/common.h" | |
| 13 #include "net/base/io_buffer.h" | |
| 14 #include "third_party/zlib/zlib.h" | |
| 15 | |
| 16 namespace blimp { | |
| 17 namespace { | |
| 18 | |
| 19 // Allocate the maxmimum amount of memory to deflate (512KB) for higher | |
| 20 // compression. (See zconf.h for details on memLevel semantics.) | |
| 21 const int kZlibMemoryLevel = 9; | |
| 22 | |
| 23 // Byte count of the zlib end-of-block marker (0x00, 0x00, 0xFF, 0xFF). | |
| 24 // Used for buffer preallocation. | |
| 25 const size_t kZlibBlockMarkerSize = 4; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 CompressedPacketWriter::CompressedPacketWriter( | |
| 30 std::unique_ptr<PacketWriter> sink) | |
| 31 : sink_(std::move(sink)), compressed_buf_(new net::GrowableIOBuffer) { | |
| 32 DCHECK(sink_); | |
| 33 | |
| 34 memset(&zlib_stream_, 0, sizeof(z_stream)); | |
| 35 | |
| 36 // MAX_WBITS means we are using the maximal window size for decompression; | |
| 37 // negating it means that we are ignoring headers and CRC checks. | |
| 38 int init_error = | |
| 39 deflateInit2(&zlib_stream_, Z_BEST_COMPRESSION, Z_DEFLATED, | |
| 40 -MAX_WBITS, // Negative value = no headers or CRC. | |
| 41 kZlibMemoryLevel, Z_DEFAULT_STRATEGY); | |
| 42 CHECK_EQ(Z_OK, init_error); | |
| 43 } | |
| 44 | |
| 45 CompressedPacketWriter::~CompressedPacketWriter() { | |
| 46 deflateEnd(&zlib_stream_); | |
| 47 } | |
| 48 | |
| 49 void CompressedPacketWriter::WritePacket( | |
| 50 const scoped_refptr<net::DrainableIOBuffer>& write_buffer, | |
| 51 const net::CompletionCallback& callback) { | |
| 52 DCHECK(write_buffer); | |
| 53 DCHECK(!callback.is_null()); | |
| 54 size_t uncompressed_size = | |
| 55 base::checked_cast<size_t>(write_buffer->BytesRemaining()); | |
| 56 | |
| 57 // Zero-length input => zero-length output. | |
| 58 if (uncompressed_size == 0) { | |
| 59 sink_->WritePacket(write_buffer, callback); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 // Don't compress anything that's bigger than the maximum allowable payload | |
| 64 // size. | |
| 65 if (uncompressed_size > kMaxPacketPayloadSizeBytes) { | |
| 66 callback.Run(net::ERR_FILE_TOO_BIG); | |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 int compress_result = Compress(write_buffer, compressed_buf_); | |
| 71 if (compress_result < 0) { | |
| 72 callback.Run(compress_result); | |
| 73 return; | |
| 74 } | |
| 75 | |
| 76 #if !defined(NDEBUG) | |
| 77 uncompressed_size_total_ += uncompressed_size; | |
| 78 compressed_size_total_ += compress_result; | |
| 79 #endif // !defined(NDEBUG) | |
| 80 | |
| 81 scoped_refptr<net::DrainableIOBuffer> compressed_outbuf( | |
| 82 new net::DrainableIOBuffer(compressed_buf_.get(), compress_result)); | |
| 83 sink_->WritePacket(compressed_outbuf, callback); | |
| 84 DVLOG(4) << "deflate packet: " << uncompressed_size << " in, " | |
| 85 << compress_result << " out."; | |
| 86 DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, " | |
| 87 << compressed_size_total_ << " out."; | |
| 88 } | |
| 89 | |
| 90 int CompressedPacketWriter::Compress( | |
| 91 const scoped_refptr<net::DrainableIOBuffer>& src_buf, | |
| 92 const scoped_refptr<net::GrowableIOBuffer>& dest_buf) { | |
| 93 DCHECK_EQ(0, dest_buf->offset()); | |
| 94 | |
| 95 // Preallocate a buffer that's large enough to fit the compressed contents of | |
| 96 // src_buf, plus some overhead for zlib. | |
| 97 const int zlib_output_ubound = | |
| 98 deflateBound(&zlib_stream_, src_buf->BytesRemaining()) + | |
| 99 kZlibBlockMarkerSize; | |
| 100 if (dest_buf->capacity() < zlib_output_ubound) { | |
| 101 dest_buf->SetCapacity(zlib_output_ubound); | |
| 102 } | |
| 103 | |
| 104 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(src_buf->data()); | |
| 105 zlib_stream_.avail_in = static_cast<unsigned>(src_buf->BytesRemaining()); | |
| 106 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(dest_buf->data()); | |
| 107 zlib_stream_.avail_out = static_cast<unsigned>(zlib_output_ubound); | |
| 108 int deflate_error = deflate(&zlib_stream_, Z_SYNC_FLUSH); | |
| 109 | |
| 110 if (deflate_error != Z_OK) { | |
| 111 DLOG(FATAL) << "Unexpected deflate() return value: " << deflate_error; | |
| 112 return net::ERR_UNEXPECTED; | |
| 113 } | |
| 114 if (zlib_stream_.avail_in > 0) { | |
| 115 DLOG(ERROR) << "deflate() did not consume all data, remainder: " | |
| 116 << zlib_stream_.avail_in << " bytes."; | |
| 117 return net::ERR_UNEXPECTED; | |
| 118 } | |
| 119 | |
| 120 return zlib_output_ubound - zlib_stream_.avail_out; | |
| 121 } | |
| 122 | |
| 123 } // namespace blimp | |
| OLD | NEW |