Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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_reader.h" | |
| 6 | |
| 7 #include <iostream> | |
| 8 | |
| 9 #include "base/callback_helpers.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/sys_byteorder.h" | |
| 14 #include "blimp/net/common.h" | |
| 15 #include "net/base/io_buffer.h" | |
| 16 #include "net/base/net_errors.h" | |
| 17 #include "net/socket/stream_socket.h" | |
| 18 | |
| 19 namespace blimp { | |
| 20 | |
| 21 CompressedPacketReader::CompressedPacketReader(scoped_ptr<PacketReader> source) | |
| 22 : source_(std::move(source)), | |
| 23 compressed_buf_(new net::GrowableIOBuffer), | |
| 24 weak_factory_(this) { | |
| 25 DCHECK(source_); | |
| 26 | |
| 27 memset(&zlib_stream_, 0, sizeof(z_stream)); | |
| 28 | |
| 29 // MAX_WBITS means we are using the maximal window size for decompression; | |
| 30 // a negative value means that we are ignoring headers and CRC checks. | |
| 31 int err = inflateInit2(&zlib_stream_, -MAX_WBITS); | |
|
Wez
2016/03/29 00:45:39
nit: |init_result|?
Kevin M
2016/03/29 01:25:55
Done.
| |
| 32 DCHECK_EQ(err, Z_OK); | |
|
Wez
2016/03/29 00:45:39
nit: Suggest DCHECK_EQ(Z_OK, err)
Kevin M
2016/03/29 01:25:55
Done.
| |
| 33 } | |
| 34 | |
| 35 CompressedPacketReader::~CompressedPacketReader() { | |
| 36 inflateEnd(&zlib_stream_); | |
| 37 } | |
| 38 | |
| 39 void CompressedPacketReader::ReadPacket( | |
| 40 const scoped_refptr<net::GrowableIOBuffer>& decompressed_buf, | |
| 41 const net::CompletionCallback& cb) { | |
|
Wez
2016/03/29 00:45:39
nit: |callback|, here and below.
Kevin M
2016/03/29 01:25:55
Done.
| |
| 42 DCHECK(decompressed_buf); | |
| 43 DCHECK(!cb.is_null()); | |
| 44 source_->ReadPacket( | |
| 45 compressed_buf_, | |
| 46 base::Bind(&CompressedPacketReader::ReadCompressedPacketDone, | |
| 47 weak_factory_.GetWeakPtr(), decompressed_buf, cb)); | |
| 48 } | |
| 49 | |
| 50 void CompressedPacketReader::ReadCompressedPacketDone( | |
| 51 const scoped_refptr<net::GrowableIOBuffer> decompressed_buf, | |
| 52 const net::CompletionCallback& cb, | |
| 53 int result) { | |
| 54 if (result <= 0) { | |
| 55 cb.Run(result); | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 cb.Run(DecompressPacket(compressed_buf_->data(), result, decompressed_buf)); | |
| 60 } | |
| 61 | |
| 62 int CompressedPacketReader::DecompressPacket( | |
| 63 char* compressed, | |
| 64 int compressed_size, | |
| 65 const scoped_refptr<net::GrowableIOBuffer>& decompressed) { | |
| 66 // Read the size of the uncompressed data from the block heading. | |
| 67 size_t decompressed_size = | |
| 68 base::NetToHost32(*reinterpret_cast<uint32_t*>(compressed)); | |
|
Wez
2016/03/29 00:45:39
Don't you need to check that |compressed_size| is
Kevin M
2016/03/29 01:25:55
Done.
| |
| 69 if (decompressed_size == 0) { | |
| 70 // Empty payload - nothing to do. | |
|
Wez
2016/03/29 00:45:39
Why would we allow an empty but compressed-looking
Kevin M
2016/03/29 01:25:55
Good question. Done.
| |
| 71 return 0; | |
| 72 } | |
| 73 if (decompressed_size > kMaxPacketPayloadSizeBytes) { | |
| 74 // Payload is too large. | |
| 75 return net::ERR_FILE_TOO_BIG; | |
| 76 } | |
| 77 if (static_cast<uint32_t>(decompressed->capacity()) < decompressed_size) { | |
|
Wez
2016/03/29 00:45:39
nit: Don't abutt this buffer-resize logic into the
Kevin M
2016/03/29 01:25:55
Done.
| |
| 78 decompressed->SetCapacity(decompressed_size); | |
| 79 } | |
| 80 | |
| 81 zlib_stream_.next_in = | |
| 82 reinterpret_cast<uint8_t*>(compressed + sizeof(uint32_t)); | |
| 83 zlib_stream_.avail_in = | |
| 84 static_cast<unsigned>(compressed_size - sizeof(uint32_t)); | |
|
Wez
2016/03/29 00:45:39
unsigned what?
What about using a safe type-conve
Kevin M
2016/03/29 01:25:55
Done.
| |
| 85 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(decompressed->data()); | |
| 86 zlib_stream_.avail_out = decompressed_size; | |
| 87 int err = inflate(&zlib_stream_, Z_SYNC_FLUSH); | |
|
Wez
2016/03/29 00:45:39
nit: |inflate_result|
Kevin M
2016/03/29 01:25:55
Done.
| |
| 88 if (err != Z_OK) { | |
| 89 DLOG(ERROR) << "inflate() returned unexpected error code: " << err; | |
| 90 return net::ERR_UNEXPECTED; | |
| 91 } | |
| 92 | |
| 93 // Verify that the decompressed block size is a perfect fit for the output | |
| 94 // buffer. | |
| 95 if (zlib_stream_.avail_in > 0) { | |
|
Wez
2016/03/29 00:45:40
This provides us a mechanism for telling when our
Kevin M
2016/03/29 01:25:55
I was on the fence about this. I favored the simpl
Kevin M
2016/03/29 20:36:23
Done. Removed the payload header size. Instead of
| |
| 96 DLOG(ERROR) << "Decompressed payload is larger than expected size (" | |
| 97 << decompressed_size << " bytes.)"; | |
| 98 return net::ERR_FILE_TOO_BIG; | |
| 99 } | |
| 100 if (zlib_stream_.avail_out > 0) { | |
| 101 DLOG(ERROR) << "Unexpected end of compressed block, num of extra bytes: " | |
| 102 << zlib_stream_.avail_out; | |
| 103 return net::ERR_UNEXPECTED; | |
| 104 } | |
| 105 | |
| 106 return decompressed_size; | |
| 107 } | |
| 108 | |
| 109 } // namespace blimp | |
| OLD | NEW |