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 zlib_stream_.zalloc = 0; |
| 27 zlib_stream_.zfree = 0; |
| 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); |
| 32 DCHECK_EQ(err, Z_OK); |
| 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) { |
| 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 size_t size; |
| 60 if (!DecompressPacket(compressed_buf_->data(), result, decompressed_buf, |
| 61 &size)) { |
| 62 if (size > kMaxPacketPayloadSizeBytes) { |
| 63 cb.Run(net::ERR_FILE_TOO_BIG); |
| 64 } else { |
| 65 cb.Run(net::ERR_UNEXPECTED); |
| 66 } |
| 67 return; |
| 68 } |
| 69 cb.Run(size); |
| 70 } |
| 71 |
| 72 bool CompressedPacketReader::DecompressPacket( |
| 73 char* compressed, |
| 74 int compressed_size, |
| 75 const scoped_refptr<net::GrowableIOBuffer>& decompressed, |
| 76 size_t* decompressed_size) { |
| 77 // Read the size of the uncompressed data from the block heading. |
| 78 *decompressed_size = |
| 79 base::NetToHost32(*reinterpret_cast<uint32_t*>(compressed)); |
| 80 if (*decompressed_size == 0) { |
| 81 // Empty payload - nothing to do. |
| 82 return true; |
| 83 } |
| 84 if (*decompressed_size > kMaxPacketPayloadSizeBytes) { |
| 85 // Payload is too large. |
| 86 return false; |
| 87 } |
| 88 if (static_cast<uint32_t>(decompressed->capacity()) < *decompressed_size) { |
| 89 decompressed->SetCapacity(*decompressed_size); |
| 90 } |
| 91 |
| 92 zlib_stream_.next_in = |
| 93 reinterpret_cast<uint8_t*>(compressed + sizeof(uint32_t)); |
| 94 zlib_stream_.avail_in = |
| 95 static_cast<unsigned>(compressed_size - sizeof(uint32_t)); |
| 96 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(decompressed->data()); |
| 97 zlib_stream_.avail_out = *decompressed_size; |
| 98 int err = inflate(&zlib_stream_, Z_SYNC_FLUSH); |
| 99 if (err != Z_OK) { |
| 100 DLOG(ERROR) << "inflate() returned unexpected error code: " << err; |
| 101 return false; |
| 102 } |
| 103 |
| 104 // Verify that the decompressed block size is a perfect fit for the output |
| 105 // buffer. |
| 106 if (zlib_stream_.avail_in > 0) { |
| 107 DLOG(ERROR) << "Decompressed payload is larger than expected size (" |
| 108 << *decompressed_size << " bytes.)"; |
| 109 return false; |
| 110 } |
| 111 if (zlib_stream_.avail_out > 0) { |
| 112 DLOG(ERROR) << "Unexpected end of compressed block, num of extra bytes: " |
| 113 << zlib_stream_.avail_out; |
| 114 return false; |
| 115 } |
| 116 |
| 117 return true; |
| 118 } |
| 119 |
| 120 } // namespace blimp |
OLD | NEW |