Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "blimp/net/compressed_packet_writer.h" | 5 #include "blimp/net/compressed_packet_writer.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
| 12 #include "blimp/net/common.h" | 12 #include "blimp/net/common.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "third_party/zlib/zlib.h" | 14 #include "third_party/zlib/zlib.h" |
| 15 | 15 |
| 16 namespace blimp { | 16 namespace blimp { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Allocate the maxmimum amount of memory to deflate (512KB) for higher | 19 // Allocate the maxmimum amount of memory to deflate (512KB) for higher |
| 20 // compression. (See zconf.h for details on memLevel semantics.) | 20 // compression. (See zconf.h for details on memLevel semantics.) |
| 21 const int kZlibMemoryLevel = 9; | 21 const int kZlibMemoryLevel = 9; |
| 22 | 22 |
| 23 // Byte count of the zlib end-of-block marker (0x00, 0x00, 0xFF, 0xFF). | |
| 24 // Used for buffer preallocation. | |
| 25 const size_t kZlibBlockFooterLength = 4; | |
|
Wez
2016/04/12 23:54:34
nit: You call it a marker but name it "footer" - b
Kevin M
2016/04/13 00:53:34
Done, done.
| |
| 26 | |
| 23 } // namespace | 27 } // namespace |
| 24 | 28 |
| 25 CompressedPacketWriter::CompressedPacketWriter( | 29 CompressedPacketWriter::CompressedPacketWriter( |
| 26 std::unique_ptr<PacketWriter> sink) | 30 std::unique_ptr<PacketWriter> sink) |
| 27 : sink_(std::move(sink)), compressed_buf_(new net::GrowableIOBuffer) { | 31 : sink_(std::move(sink)), compressed_buf_(new net::GrowableIOBuffer) { |
| 28 DCHECK(sink_); | 32 DCHECK(sink_); |
| 29 | 33 |
| 30 memset(&zlib_stream_, 0, sizeof(z_stream)); | 34 memset(&zlib_stream_, 0, sizeof(z_stream)); |
| 31 | 35 |
| 32 // MAX_WBITS means we are using the maximal window size for decompression; | 36 // MAX_WBITS means we are using the maximal window size for decompression; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 << compress_result << " out."; | 85 << compress_result << " out."; |
| 82 DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, " | 86 DVLOG(3) << "deflate total: " << uncompressed_size_total_ << " in, " |
| 83 << compressed_size_total_ << " out."; | 87 << compressed_size_total_ << " out."; |
| 84 } | 88 } |
| 85 | 89 |
| 86 int CompressedPacketWriter::Compress( | 90 int CompressedPacketWriter::Compress( |
| 87 const scoped_refptr<net::DrainableIOBuffer>& src_buf, | 91 const scoped_refptr<net::DrainableIOBuffer>& src_buf, |
| 88 const scoped_refptr<net::GrowableIOBuffer>& dest_buf) { | 92 const scoped_refptr<net::GrowableIOBuffer>& dest_buf) { |
| 89 DCHECK_EQ(0, dest_buf->offset()); | 93 DCHECK_EQ(0, dest_buf->offset()); |
| 90 | 94 |
| 95 // Preallocate a buffer that's large enough to fit the compressed contents of | |
| 96 // src_buf, plus some overhead for zlib. | |
| 91 const int zlib_output_ubound = | 97 const int zlib_output_ubound = |
| 92 deflateBound(&zlib_stream_, src_buf->BytesRemaining()); | 98 deflateBound(&zlib_stream_, src_buf->BytesRemaining()) + |
| 99 kZlibBlockFooterLength; | |
|
Wez
2016/04/12 23:54:34
Indentation looks wrong - git cl format this?
Kevin M
2016/04/13 00:53:35
Already formatted.
| |
| 93 if (dest_buf->capacity() < zlib_output_ubound) { | 100 if (dest_buf->capacity() < zlib_output_ubound) { |
| 94 dest_buf->SetCapacity(zlib_output_ubound); | 101 dest_buf->SetCapacity(zlib_output_ubound); |
| 95 } | 102 } |
| 96 | 103 |
| 97 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(src_buf->data()); | 104 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(src_buf->data()); |
| 98 zlib_stream_.avail_in = static_cast<unsigned>(src_buf->BytesRemaining()); | 105 zlib_stream_.avail_in = static_cast<unsigned>(src_buf->BytesRemaining()); |
| 99 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(dest_buf->data()); | 106 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(dest_buf->data()); |
| 100 zlib_stream_.avail_out = static_cast<unsigned>(zlib_output_ubound); | 107 zlib_stream_.avail_out = static_cast<unsigned>(zlib_output_ubound); |
| 101 int deflate_error = deflate(&zlib_stream_, Z_SYNC_FLUSH); | 108 int deflate_error = deflate(&zlib_stream_, Z_SYNC_FLUSH); |
| 102 | 109 |
| 103 if (deflate_error != Z_OK) { | 110 if (deflate_error != Z_OK) { |
| 104 DLOG(FATAL) << "Unexpected deflate() return value: " << deflate_error; | 111 DLOG(FATAL) << "Unexpected deflate() return value: " << deflate_error; |
| 105 return net::ERR_UNEXPECTED; | 112 return net::ERR_UNEXPECTED; |
| 106 } | 113 } |
| 107 if (zlib_stream_.avail_in > 0) { | 114 if (zlib_stream_.avail_in > 0) { |
| 108 DLOG(ERROR) << "deflate() did not consume all data, remainder: " | 115 DLOG(ERROR) << "deflate() did not consume all data, remainder: " |
| 109 << zlib_stream_.avail_in << " bytes."; | 116 << zlib_stream_.avail_in << " bytes."; |
| 110 return net::ERR_UNEXPECTED; | 117 return net::ERR_UNEXPECTED; |
| 111 } | 118 } |
| 112 | 119 |
| 113 return zlib_output_ubound - zlib_stream_.avail_out; | 120 return zlib_output_ubound - zlib_stream_.avail_out; |
| 114 } | 121 } |
| 115 | 122 |
| 116 } // namespace blimp | 123 } // namespace blimp |
| OLD | NEW |