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

Side by Side 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, 8 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 unified diff | Download patch
« no previous file with comments | « blimp/net/compressed_packet_reader.h ('k') | blimp/net/compressed_packet_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 init_result = inflateInit2(&zlib_stream_, -MAX_WBITS);
32 DCHECK_EQ(Z_OK, init_result);
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& callback) {
42 DCHECK(decompressed_buf);
43 DCHECK(!callback.is_null());
44 source_->ReadPacket(
45 compressed_buf_,
46 base::Bind(&CompressedPacketReader::OnCompressedPacketReceived,
47 weak_factory_.GetWeakPtr(), decompressed_buf, callback));
48 }
49
50 void CompressedPacketReader::OnCompressedPacketReceived(
51 const scoped_refptr<net::GrowableIOBuffer> decompressed_buf,
52 const net::CompletionCallback& callback,
53 int result) {
54 if (result <= 0) {
55 callback.Run(result);
56 return;
57 }
58
59 callback.Run(DecompressPacket(decompressed_buf, result));
60 }
61
62 int CompressedPacketReader::DecompressPacket(
63 const scoped_refptr<net::GrowableIOBuffer>& decompressed,
64 int size) {
65 compressed_buf_->set_offset(0);
66 decompressed->set_offset(0);
67 if (static_cast<size_t>(decompressed->capacity()) <
68 kMaxPacketPayloadSizeBytes) {
69 decompressed->SetCapacity(kMaxPacketPayloadSizeBytes);
70 }
71
72 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(compressed_buf_->data());
73 zlib_stream_.avail_in = base::checked_cast<uint32_t>(size);
74 zlib_stream_.next_out = reinterpret_cast<uint8_t*>(decompressed->data());
75 zlib_stream_.avail_out = decompressed->RemainingCapacity();
76 int inflate_result = inflate(&zlib_stream_, Z_SYNC_FLUSH);
77 if (inflate_result != Z_OK) {
78 DLOG(ERROR) << "inflate() returned unexpected error code: "
79 << inflate_result;
80 return net::ERR_UNEXPECTED;
81 }
82 DCHECK_GT(decompressed->RemainingCapacity(),
83 base::checked_cast<int>(zlib_stream_.avail_out));
84 int decompressed_size =
85 decompressed->RemainingCapacity() - zlib_stream_.avail_out;
86
87 // Verify that the decompressed output isn't bigger than the maximum allowable
88 // payload size, by checking if there are bytes waiting to be processed.
89 if (zlib_stream_.avail_in > 0) {
90 DLOG(ERROR)
91 << "Decompressed buffer size exceeds allowable limits; aborting.";
92 return net::ERR_FILE_TOO_BIG;
93 }
94
95 return decompressed_size;
96 }
97
98 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/compressed_packet_reader.h ('k') | blimp/net/compressed_packet_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698