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

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
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 // 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 int decompressed_size =
81 decompressed->RemainingCapacity() - zlib_stream_.avail_out;
Wez 2016/03/29 21:38:37 Maybe DCHECK that RemainingCapacity() > zlib_strea
Kevin M 2016/03/29 23:48:23 Done.
82
83 // Verify that the decompressed block size is a perfect fit for the output
84 // buffer.
Wez 2016/03/29 21:38:37 Update this comment.
Kevin M 2016/03/29 23:48:23 Done.
85 if (zlib_stream_.avail_in > 0) {
86 DLOG(ERROR)
87 << "Decompressed buffer size exceeds allowable limits; aborting.";
88 return net::ERR_FILE_TOO_BIG;
89 }
90
91 return decompressed_size;
92 }
93
94 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698