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), |
| 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 are_bytes_added_ = true; |
| 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 if (Deflate(estimation, Z_NO_FLUSH) != Z_OK) |
| 54 return false; |
| 55 estimation *= 2; |
| 56 } while (stream_->avail_in > 0); |
| 57 return true; |
| 58 } |
| 59 |
| 60 scoped_refptr<IOBufferWithSize> WebSocketDeflater::Finish() { |
| 61 scoped_refptr<IOBufferWithSize> result; |
| 62 if (!are_bytes_added_) { |
| 63 // Since consecutive calls of deflate with Z_SYNC_FLUSH and no input |
| 64 // lead to an error, we create and return the output for the empty input |
| 65 // manually. |
| 66 DCHECK(buffer_.empty()); |
| 67 result = new IOBufferWithSize(2); |
| 68 memcpy(result->data(), "\x02\x00", result->size()); |
| 69 Reset(); |
| 70 return result; |
| 71 } |
| 72 stream_->next_in = NULL; |
| 73 stream_->avail_in = 0; |
| 74 |
| 75 const size_t FINISH_AVAILABLE_SIZE = 4096; |
| 76 do { |
| 77 int result = Deflate(FINISH_AVAILABLE_SIZE, Z_SYNC_FLUSH); |
| 78 if (result != Z_OK && result != Z_BUF_ERROR) { |
| 79 Reset(); |
| 80 return NULL; |
| 81 } |
| 82 } while (!stream_->avail_out); |
| 83 // Remove 4 octets from the tail as the specification requires. |
| 84 if (buffer_.size() < 4) { |
| 85 Reset(); |
| 86 return NULL; |
| 87 } |
| 88 result = new IOBufferWithSize(buffer_.size() - 4); |
| 89 memcpy(result->data(), buffer_.data(), result->size()); |
| 90 Reset(); |
| 91 return result; |
| 92 } |
| 93 |
| 94 void WebSocketDeflater::Reset() { |
| 95 buffer_.clear(); |
| 96 if (mode_ == DO_NOT_TAKE_OVER_CONTEXT) |
| 97 deflateReset(stream_.get()); |
| 98 are_bytes_added_ = false; |
| 99 } |
| 100 |
| 101 int WebSocketDeflater::Deflate(size_t avail_out, int flush) { |
| 102 DCHECK_GT(avail_out, 0u); |
| 103 size_t current = buffer_.size(); |
| 104 // TODO(yhirano): std::vector initializes the expanded area, |
| 105 // but the initialization is actually not necessary. |
| 106 buffer_.resize(buffer_.size() + avail_out); |
| 107 stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]); |
| 108 stream_->avail_out = avail_out; |
| 109 |
| 110 int result = deflate(stream_.get(), flush); |
| 111 buffer_.resize(buffer_.size() - stream_->avail_out); |
| 112 return result; |
| 113 } |
| 114 |
| 115 } // namespace net |
OLD | NEW |