Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(524)

Unified Diff: blimp/net/compressed_packet_reader.cc

Issue 1825263003: Blimp: add packet-level DEFLATE compression using zlib. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Integrated CompressedPacketReader/Writer into application Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/net/compressed_packet_reader.cc
diff --git a/blimp/net/compressed_packet_reader.cc b/blimp/net/compressed_packet_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..25137606f45eafb979f2a671e887e502a2010f70
--- /dev/null
+++ b/blimp/net/compressed_packet_reader.cc
@@ -0,0 +1,120 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/net/compressed_packet_reader.h"
+
+#include <iostream>
+
+#include "base/callback_helpers.h"
+#include "base/logging.h"
+#include "base/memory/weak_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/sys_byteorder.h"
+#include "blimp/net/common.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/socket/stream_socket.h"
+
+namespace blimp {
+
+CompressedPacketReader::CompressedPacketReader(scoped_ptr<PacketReader> source)
+ : source_(std::move(source)),
+ compressed_buf_(new net::GrowableIOBuffer),
+ weak_factory_(this) {
+ DCHECK(source_);
+ zlib_stream_.zalloc = 0;
+ zlib_stream_.zfree = 0;
+
+ // MAX_WBITS means we are using the maximal window size for decompression;
+ // a negative value means that we are ignoring headers and CRC checks.
+ int err = inflateInit2(&zlib_stream_, -MAX_WBITS);
+ DCHECK_EQ(err, Z_OK);
+}
+
+CompressedPacketReader::~CompressedPacketReader() {
+ inflateEnd(&zlib_stream_);
+}
+
+void CompressedPacketReader::ReadPacket(
+ const scoped_refptr<net::GrowableIOBuffer>& decompressed_buf,
+ const net::CompletionCallback& cb) {
+ DCHECK(decompressed_buf);
+ DCHECK(!cb.is_null());
+ source_->ReadPacket(
+ compressed_buf_,
+ base::Bind(&CompressedPacketReader::ReadCompressedPacketDone,
+ weak_factory_.GetWeakPtr(), decompressed_buf, cb));
+}
+
+void CompressedPacketReader::ReadCompressedPacketDone(
+ const scoped_refptr<net::GrowableIOBuffer> decompressed_buf,
+ const net::CompletionCallback& cb,
+ int result) {
+ if (result <= 0) {
+ cb.Run(result);
+ return;
+ }
+
+ size_t size;
+ if (!DecompressPacket(compressed_buf_->data(), result, decompressed_buf,
+ &size)) {
+ if (size > kMaxPacketPayloadSizeBytes) {
+ cb.Run(net::ERR_FILE_TOO_BIG);
+ } else {
+ cb.Run(net::ERR_UNEXPECTED);
+ }
+ return;
+ }
+ cb.Run(size);
+}
+
+bool CompressedPacketReader::DecompressPacket(
+ char* compressed,
+ int compressed_size,
+ const scoped_refptr<net::GrowableIOBuffer>& decompressed,
+ size_t* decompressed_size) {
+ // Read the size of the uncompressed data from the block heading.
+ *decompressed_size =
+ base::NetToHost32(*reinterpret_cast<uint32_t*>(compressed));
+ if (*decompressed_size == 0) {
+ // Empty payload - nothing to do.
+ return true;
+ }
+ if (*decompressed_size > kMaxPacketPayloadSizeBytes) {
+ // Payload is too large.
+ return false;
+ }
+ if (static_cast<uint32_t>(decompressed->capacity()) < *decompressed_size) {
+ decompressed->SetCapacity(*decompressed_size);
+ }
+
+ zlib_stream_.next_in =
+ reinterpret_cast<uint8_t*>(compressed + sizeof(uint32_t));
+ zlib_stream_.avail_in =
+ static_cast<unsigned>(compressed_size - sizeof(uint32_t));
+ zlib_stream_.next_out = reinterpret_cast<uint8_t*>(decompressed->data());
+ zlib_stream_.avail_out = *decompressed_size;
+ int err = inflate(&zlib_stream_, Z_SYNC_FLUSH);
+ if (err != Z_OK) {
+ DLOG(ERROR) << "inflate() returned unexpected error code: " << err;
+ return false;
+ }
+
+ // Verify that the decompressed block size is a perfect fit for the output
+ // buffer.
+ if (zlib_stream_.avail_in > 0) {
+ DLOG(ERROR) << "Decompressed payload is larger than expected size ("
+ << *decompressed_size << " bytes.)";
+ return false;
+ }
+ if (zlib_stream_.avail_out > 0) {
+ DLOG(ERROR) << "Unexpected end of compressed block, num of extra bytes: "
+ << zlib_stream_.avail_out;
+ return false;
+ }
+
+ return true;
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698