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 // MAX_WBITS means we are using the maximal window size for decompression; | |
| 28 // a negative value means that we are ignoring headers and CRC checks. | |
| 29 int init_result = inflateInit2(&zlib_stream_, -MAX_WBITS); | |
| 30 DCHECK_EQ(Z_OK, init_result); | |
| 31 } | |
| 32 | |
| 33 CompressedPacketReader::~CompressedPacketReader() { | |
| 34 inflateEnd(&zlib_stream_); | |
| 35 } | |
| 36 | |
| 37 void CompressedPacketReader::ReadPacket( | |
| 38 const scoped_refptr<net::GrowableIOBuffer>& decompressed_buf, | |
| 39 const net::CompletionCallback& callback) { | |
| 40 DCHECK(decompressed_buf); | |
| 41 DCHECK(!callback.is_null()); | |
| 42 source_->ReadPacket( | |
| 43 compressed_buf_, | |
| 44 base::Bind(&CompressedPacketReader::OnCompressedPacketReceived, | |
| 45 weak_factory_.GetWeakPtr(), decompressed_buf, callback)); | |
| 46 } | |
| 47 | |
| 48 void CompressedPacketReader::OnCompressedPacketReceived( | |
| 49 const scoped_refptr<net::GrowableIOBuffer> decompressed_buf, | |
| 50 const net::CompletionCallback& callback, | |
| 51 int result) { | |
| 52 if (result <= 0) { | |
| 53 callback.Run(result); | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 callback.Run(DecompressPacket(decompressed_buf, result)); | |
| 58 } | |
| 59 | |
| 60 int CompressedPacketReader::DecompressPacket( | |
| 61 const scoped_refptr<net::GrowableIOBuffer>& decompressed, | |
| 62 int size) { | |
| 63 compressed_buf_->set_offset(0); | |
| 64 decompressed->set_offset(0); | |
| 65 if (static_cast<size_t>(decompressed->capacity()) < | |
| 66 kMaxPacketPayloadSizeBytes) { | |
| 67 decompressed->SetCapacity(kMaxPacketPayloadSizeBytes); | |
| 68 } | |
| 69 | |
| 70 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(compressed_buf_->data()); | |
| 71 zlib_stream_.avail_in = base::checked_cast<uint32_t>(size); | |
| 72 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(decompressed->data()); | |
| 73 zlib_stream_.avail_out = decompressed->RemainingCapacity(); | |
| 74 int inflate_result = inflate(&zlib_stream_, Z_SYNC_FLUSH); | |
| 75 if (inflate_result != Z_OK) { | |
| 76 DLOG(ERROR) << "inflate() returned unexpected error code: " | |
| 77 << inflate_result; | |
| 78 return net::ERR_UNEXPECTED; | |
| 79 } | |
| 80 DCHECK_GT(decompressed->RemainingCapacity(), | |
| 81 static_cast<int>(zlib_stream_.avail_out)); | |
|
Wez
2016/03/29 23:59:15
Use a checked cast here, otherwise some crazy scen
Kevin M
2016/03/30 00:26:32
Done.
| |
| 82 int decompressed_size = | |
| 83 decompressed->RemainingCapacity() - zlib_stream_.avail_out; | |
| 84 | |
| 85 // Verify that the decompressed output isn't bigger than the maximum allowable | |
| 86 // payload size, by checking if there are bytes waiting to be processed. | |
| 87 if (zlib_stream_.avail_in > 0) { | |
| 88 DLOG(ERROR) | |
| 89 << "Decompressed buffer size exceeds allowable limits; aborting."; | |
| 90 return net::ERR_FILE_TOO_BIG; | |
| 91 } | |
| 92 | |
| 93 return decompressed_size; | |
| 94 } | |
| 95 | |
| 96 } // namespace blimp | |
| OLD | NEW |