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