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

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: Wez feedback. 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..ac253dc58d8953847a3da4a75147be1e453f2c3f
--- /dev/null
+++ b/blimp/net/compressed_packet_reader.cc
@@ -0,0 +1,96 @@
+// 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_);
+
+ // 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 init_result = inflateInit2(&zlib_stream_, -MAX_WBITS);
+ DCHECK_EQ(Z_OK, init_result);
+}
+
+CompressedPacketReader::~CompressedPacketReader() {
+ inflateEnd(&zlib_stream_);
+}
+
+void CompressedPacketReader::ReadPacket(
+ const scoped_refptr<net::GrowableIOBuffer>& decompressed_buf,
+ const net::CompletionCallback& callback) {
+ DCHECK(decompressed_buf);
+ DCHECK(!callback.is_null());
+ source_->ReadPacket(
+ compressed_buf_,
+ base::Bind(&CompressedPacketReader::OnCompressedPacketReceived,
+ weak_factory_.GetWeakPtr(), decompressed_buf, callback));
+}
+
+void CompressedPacketReader::OnCompressedPacketReceived(
+ const scoped_refptr<net::GrowableIOBuffer> decompressed_buf,
+ const net::CompletionCallback& callback,
+ int result) {
+ if (result <= 0) {
+ callback.Run(result);
+ return;
+ }
+
+ callback.Run(DecompressPacket(decompressed_buf, result));
+}
+
+int CompressedPacketReader::DecompressPacket(
+ const scoped_refptr<net::GrowableIOBuffer>& decompressed,
+ int size) {
+ compressed_buf_->set_offset(0);
+ decompressed->set_offset(0);
+ if (static_cast<size_t>(decompressed->capacity()) <
+ kMaxPacketPayloadSizeBytes) {
+ decompressed->SetCapacity(kMaxPacketPayloadSizeBytes);
+ }
+
+ zlib_stream_.next_in = reinterpret_cast<uint8_t*>(compressed_buf_->data());
+ zlib_stream_.avail_in = base::checked_cast<uint32_t>(size);
+ zlib_stream_.next_out = reinterpret_cast<uint8_t*>(decompressed->data());
+ zlib_stream_.avail_out = decompressed->RemainingCapacity();
+ int inflate_result = inflate(&zlib_stream_, Z_SYNC_FLUSH);
+ if (inflate_result != Z_OK) {
+ DLOG(ERROR) << "inflate() returned unexpected error code: "
+ << inflate_result;
+ return net::ERR_UNEXPECTED;
+ }
+ DCHECK_GT(decompressed->RemainingCapacity(),
+ 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.
+ int decompressed_size =
+ decompressed->RemainingCapacity() - zlib_stream_.avail_out;
+
+ // Verify that the decompressed output isn't bigger than the maximum allowable
+ // payload size, by checking if there are bytes waiting to be processed.
+ if (zlib_stream_.avail_in > 0) {
+ DLOG(ERROR)
+ << "Decompressed buffer size exceeds allowable limits; aborting.";
+ return net::ERR_FILE_TOO_BIG;
+ }
+
+ return decompressed_size;
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698