OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/websockets/websocket_deflater.h" | 5 #include "net/websockets/websocket_deflater.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <deque> | 9 #include <deque> |
10 #include <vector> | 10 #include <vector> |
(...skipping 13 matching lines...) Expand all Loading... |
24 stream_.reset(NULL); | 24 stream_.reset(NULL); |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 bool WebSocketDeflater::Initialize(int window_bits) { | 28 bool WebSocketDeflater::Initialize(int window_bits) { |
29 DCHECK(!stream_); | 29 DCHECK(!stream_); |
30 stream_.reset(new z_stream); | 30 stream_.reset(new z_stream); |
31 | 31 |
32 DCHECK_LE(8, window_bits); | 32 DCHECK_LE(8, window_bits); |
33 DCHECK_GE(15, window_bits); | 33 DCHECK_GE(15, window_bits); |
| 34 |
| 35 // Use a negative value to compress a raw deflate stream. Upgrade window_bits |
| 36 // = 8 (a 256-byte window) to 9 (a 512-byte window) because zlib is unable to |
| 37 // compress using a 256-byte window. This retains zlib's historical behavior |
| 38 // of silently increasing the window size in this case. See |
| 39 // https://crbug.com/691074. |
| 40 window_bits = -std::max(window_bits, 9); |
| 41 |
34 memset(stream_.get(), 0, sizeof(*stream_)); | 42 memset(stream_.get(), 0, sizeof(*stream_)); |
35 int result = deflateInit2(stream_.get(), | 43 int result = deflateInit2(stream_.get(), |
36 Z_DEFAULT_COMPRESSION, | 44 Z_DEFAULT_COMPRESSION, |
37 Z_DEFLATED, | 45 Z_DEFLATED, |
38 -window_bits, // Negative value for raw deflate | 46 window_bits, |
39 8, // default mem level | 47 8, // default mem level |
40 Z_DEFAULT_STRATEGY); | 48 Z_DEFAULT_STRATEGY); |
41 if (result != Z_OK) { | 49 if (result != Z_OK) { |
42 deflateEnd(stream_.get()); | 50 deflateEnd(stream_.get()); |
43 stream_.reset(); | 51 stream_.reset(); |
44 return false; | 52 return false; |
45 } | 53 } |
46 const size_t kFixedBufferSize = 4096; | 54 const size_t kFixedBufferSize = 4096; |
47 fixed_buffer_.resize(kFixedBufferSize); | 55 fixed_buffer_.resize(kFixedBufferSize); |
48 return true; | 56 return true; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 stream_->next_out = reinterpret_cast<Bytef*>(&fixed_buffer_[0]); | 127 stream_->next_out = reinterpret_cast<Bytef*>(&fixed_buffer_[0]); |
120 stream_->avail_out = fixed_buffer_.size(); | 128 stream_->avail_out = fixed_buffer_.size(); |
121 result = deflate(stream_.get(), flush); | 129 result = deflate(stream_.get(), flush); |
122 size_t size = fixed_buffer_.size() - stream_->avail_out; | 130 size_t size = fixed_buffer_.size() - stream_->avail_out; |
123 buffer_.insert(buffer_.end(), &fixed_buffer_[0], &fixed_buffer_[0] + size); | 131 buffer_.insert(buffer_.end(), &fixed_buffer_[0], &fixed_buffer_[0] + size); |
124 } while (result == Z_OK); | 132 } while (result == Z_OK); |
125 return result; | 133 return result; |
126 } | 134 } |
127 | 135 |
128 } // namespace net | 136 } // namespace net |
OLD | NEW |