| 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. |
| 36 // |
| 37 // Upgrade window_bits = 8 to 9 because zlib is unable to compress at |
| 38 // window_bits = 8. Historically, zlib has silently increased the window size |
| 39 // during compression in this case, although this is no longer done for raw |
| 40 // deflate streams since zlib 1.2.9. |
| 41 // |
| 42 // Because of a zlib deflate quirk, back-references will not use the entire |
| 43 // range of 1 << window_bits, but will instead use a restricted range of (1 << |
| 44 // window_bits) - 262. With an increased window_bits = 9, back-references will |
| 45 // be within a range of 250. These can still be decompressed with window_bits |
| 46 // = 8 and the 256-byte window used there. |
| 47 // |
| 48 // Both the requirement to do this upgrade and the ability to compress with |
| 49 // window_bits = 9 while expecting a decompressor to function with window_bits |
| 50 // = 8 are quite specific to zlib's particular deflate implementation, but not |
| 51 // specific to any particular inflate implementation. |
| 52 // |
| 53 // See https://crbug.com/691074 |
| 54 window_bits = -std::max(window_bits, 9); |
| 55 |
| 34 memset(stream_.get(), 0, sizeof(*stream_)); | 56 memset(stream_.get(), 0, sizeof(*stream_)); |
| 35 int result = deflateInit2(stream_.get(), | 57 int result = deflateInit2(stream_.get(), |
| 36 Z_DEFAULT_COMPRESSION, | 58 Z_DEFAULT_COMPRESSION, |
| 37 Z_DEFLATED, | 59 Z_DEFLATED, |
| 38 -window_bits, // Negative value for raw deflate | 60 window_bits, |
| 39 8, // default mem level | 61 8, // default mem level |
| 40 Z_DEFAULT_STRATEGY); | 62 Z_DEFAULT_STRATEGY); |
| 41 if (result != Z_OK) { | 63 if (result != Z_OK) { |
| 42 deflateEnd(stream_.get()); | 64 deflateEnd(stream_.get()); |
| 43 stream_.reset(); | 65 stream_.reset(); |
| 44 return false; | 66 return false; |
| 45 } | 67 } |
| 46 const size_t kFixedBufferSize = 4096; | 68 const size_t kFixedBufferSize = 4096; |
| 47 fixed_buffer_.resize(kFixedBufferSize); | 69 fixed_buffer_.resize(kFixedBufferSize); |
| 48 return true; | 70 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]); | 141 stream_->next_out = reinterpret_cast<Bytef*>(&fixed_buffer_[0]); |
| 120 stream_->avail_out = fixed_buffer_.size(); | 142 stream_->avail_out = fixed_buffer_.size(); |
| 121 result = deflate(stream_.get(), flush); | 143 result = deflate(stream_.get(), flush); |
| 122 size_t size = fixed_buffer_.size() - stream_->avail_out; | 144 size_t size = fixed_buffer_.size() - stream_->avail_out; |
| 123 buffer_.insert(buffer_.end(), &fixed_buffer_[0], &fixed_buffer_[0] + size); | 145 buffer_.insert(buffer_.end(), &fixed_buffer_[0], &fixed_buffer_[0] + size); |
| 124 } while (result == Z_OK); | 146 } while (result == Z_OK); |
| 125 return result; | 147 return result; |
| 126 } | 148 } |
| 127 | 149 |
| 128 } // namespace net | 150 } // namespace net |
| OLD | NEW |