OLD | NEW |
---|---|
(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) | |
Adam Rice
2013/08/29 12:04:33
The commas go after the initialiser in Chromium st
yhirano
2013/08/30 06:20:35
Done.
| |
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(nullptr); | |
Adam Rice
2013/08/29 12:04:33
I'm not sure why "nullptr" compiles. The compiler
yhirano
2013/08/30 06:20:35
Done.
| |
37 } | |
38 } | |
39 | |
40 bool WebSocketDeflater::AddBytes(const char* data, size_t size) { | |
41 if (!size) { | |
Adam Rice
2013/08/29 12:04:33
Chromium net people seem to prefer no { } for if s
yhirano
2013/08/30 06:20:35
Done.
| |
42 return true; | |
43 } | |
44 | |
45 stream_->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data)); | |
46 stream_->avail_in = size; | |
47 | |
48 // The estimation by deflateBound is not accurate if the zlib has some | |
49 // remaining input of the last compression. | |
50 size_t estimation = deflateBound(stream_.get(), size); | |
51 DCHECK_GT(estimation, 0u); | |
52 do { | |
53 size_t current = buffer_.size(); | |
54 buffer_.resize(buffer_.size() + estimation); | |
Adam Rice
2013/08/29 12:04:33
This is slightly suboptimal since the vector will
yhirano
2013/08/30 06:20:35
Done.
| |
55 stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]); | |
56 stream_->avail_out = buffer_.size() - current; | |
57 | |
58 int result = deflate(stream_.get(), Z_NO_FLUSH); | |
59 if (result != Z_OK) { | |
60 return false; | |
61 } | |
62 buffer_.resize(buffer_.size() - stream_->avail_out); | |
63 estimation *= 2; | |
64 } while (!stream_->avail_out); | |
65 are_bytes_added_ = true; | |
66 | |
67 return true; | |
68 } | |
69 | |
70 scoped_refptr<IOBufferWithSize> WebSocketDeflater::Finish() { | |
71 scoped_refptr<IOBufferWithSize> result; | |
72 if (!are_bytes_added_) { | |
73 // Since consecutive calls of deflate with Z_SYNC_FLUSH and | |
74 // no input lead to an error, we create and return the output | |
75 // for the empty input manually. | |
76 DCHECK(buffer_.empty()); | |
77 result = new IOBufferWithSize(2); | |
78 memcpy(result->data(), "\x02\x00", result->size()); | |
79 Reset(); | |
80 return result; | |
81 } | |
82 stream_->next_in = nullptr; | |
Adam Rice
2013/08/29 12:04:33
NULL in Chromium code. Also 2 other places below.
yhirano
2013/08/30 06:20:35
Done.
| |
83 stream_->avail_in = 0; | |
84 | |
85 const size_t available_size = 4096; | |
86 do { | |
87 size_t current = buffer_.size(); | |
88 buffer_.resize(buffer_.size() + available_size); | |
89 stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]); | |
90 stream_->avail_out = buffer_.size() - current; | |
91 | |
92 int result = deflate(stream_.get(), Z_SYNC_FLUSH); | |
93 buffer_.resize(buffer_.size() - stream_->avail_out); | |
94 if (result != Z_OK && result != Z_BUF_ERROR) { | |
95 Reset(); | |
96 return nullptr; | |
97 } | |
98 } while (!stream_->avail_out); | |
99 // Remove 4 octets from the tail as the specification requires. | |
100 if (buffer_.size() < 4) { | |
101 Reset(); | |
102 return nullptr; | |
103 } | |
104 result = new IOBufferWithSize(buffer_.size() - 4); | |
105 memcpy(result->data(), buffer_.data(), result->size()); | |
106 Reset(); | |
107 return result; | |
108 } | |
109 | |
110 void WebSocketDeflater::Reset() { | |
111 buffer_.clear(); | |
112 if (mode_ == DO_NOT_TAKE_OVER_CONTEXT) { | |
113 deflateReset(stream_.get()); | |
114 } | |
115 are_bytes_added_ = false; | |
116 } | |
117 | |
118 } // namespace net | |
OLD | NEW |