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

Side by Side Diff: net/websockets/websocket_deflater.cc

Issue 23623008: Implement WebSocketDeflater. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 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 | « net/websockets/websocket_deflater.h ('k') | net/websockets/websocket_deflater_test.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 (c) 2013 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 "net/websockets/websocket_deflater.h"
6
7 #include <string.h>
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "net/base/io_buffer.h"
12 #include "third_party/zlib/zlib.h"
13
14 namespace net {
15
16 WebSocketDeflater::WebSocketDeflater(int window_bits,
17 ContextTakeOverMode mode)
18 : stream_(new z_stream),
19 mode_(mode),
20 are_bytes_added_(false) {
21 DCHECK_LE(8, window_bits);
22 DCHECK_GE(15, window_bits);
23 memset(stream_.get(), 0, sizeof(*stream_));
24 int result = deflateInit2(stream_.get(),
25 Z_DEFAULT_COMPRESSION,
26 Z_DEFLATED,
27 -window_bits,
28 8, // default mem level
29 Z_DEFAULT_STRATEGY);
30 DCHECK_EQ(Z_OK, result);
31 }
32
33 WebSocketDeflater::~WebSocketDeflater() {
34 if (stream_) {
35 deflateEnd(stream_.get());
36 stream_.reset(NULL);
37 }
38 }
39
40 bool WebSocketDeflater::AddBytes(const char* data, size_t size) {
41 if (!size)
42 return true;
43
44 stream_->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data));
45 stream_->avail_in = size;
46
47 // The estimation by deflateBound is not accurate if the zlib has some
48 // remaining input of the last compression.
49 size_t estimation = deflateBound(stream_.get(), size);
50 DCHECK_GT(estimation, 0u);
51 do {
52 size_t current = buffer_.size();
53 // TODO(yhirano): std::vector initializes the expanded area,
54 // but the initialization is actually not necessary.
55 buffer_.resize(buffer_.size() + estimation);
56 stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]);
57 stream_->avail_out = buffer_.size() - current;
58
59 int result = deflate(stream_.get(), Z_NO_FLUSH);
60 if (result != Z_OK) {
Adam Rice 2013/08/30 07:16:11 Probably these { } need to be removed too.
yhirano 2013/08/30 09:42:11 Done.
61 return false;
62 }
63 buffer_.resize(buffer_.size() - stream_->avail_out);
64 estimation *= 2;
65 } while (!stream_->avail_out);
66 are_bytes_added_ = true;
67
68 return true;
69 }
70
71 scoped_refptr<IOBufferWithSize> WebSocketDeflater::Finish() {
72 scoped_refptr<IOBufferWithSize> result;
73 if (!are_bytes_added_) {
74 // Since consecutive calls of deflate with Z_SYNC_FLUSH and no input
75 // lead to an error, we create and return the output for the empty input
76 // manually.
77 DCHECK(buffer_.empty());
78 result = new IOBufferWithSize(2);
79 memcpy(result->data(), "\x02\x00", result->size());
80 Reset();
81 return result;
82 }
83 stream_->next_in = NULL;
84 stream_->avail_in = 0;
85
86 const size_t available_size = 4096;
Adam Rice 2013/08/30 07:16:11 Where did the number 4096 come from?
yhirano 2013/08/30 07:26:34 The deflater works for arbitrary value if it is no
87 do {
Adam Rice 2013/08/30 07:16:11 I just realised that this loop is almost the same
yhirano 2013/08/30 09:42:11 Done.
yhirano 2013/08/30 10:56:31 I'm sorry, I was wrong. Loop condition in AddBytes
88 size_t current = buffer_.size();
89 buffer_.resize(buffer_.size() + available_size);
90 stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]);
91 stream_->avail_out = buffer_.size() - current;
92
93 int result = deflate(stream_.get(), Z_SYNC_FLUSH);
94 buffer_.resize(buffer_.size() - stream_->avail_out);
95 if (result != Z_OK && result != Z_BUF_ERROR) {
96 Reset();
97 return NULL;
98 }
99 } while (!stream_->avail_out);
100 // Remove 4 octets from the tail as the specification requires.
101 if (buffer_.size() < 4) {
102 Reset();
103 return NULL;
104 }
105 result = new IOBufferWithSize(buffer_.size() - 4);
106 memcpy(result->data(), buffer_.data(), result->size());
107 Reset();
108 return result;
109 }
110
111 void WebSocketDeflater::Reset() {
112 buffer_.clear();
113 if (mode_ == DO_NOT_TAKE_OVER_CONTEXT)
114 deflateReset(stream_.get());
115 are_bytes_added_ = false;
116 }
117
118 } // namespace net
OLDNEW
« no previous file with comments | « net/websockets/websocket_deflater.h ('k') | net/websockets/websocket_deflater_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698