Chromium Code Reviews| 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 <algorithm> | |
| 9 #include <deque> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "net/base/io_buffer.h" | |
| 13 #include "third_party/zlib/zlib.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 WebSocketDeflater::WebSocketDeflater(int window_bits, | |
| 18 ContextTakeOverMode mode) | |
| 19 : stream_(new z_stream), | |
| 20 mode_(mode), | |
| 21 are_bytes_added_(false) { | |
| 22 DCHECK_LE(8, window_bits); | |
| 23 DCHECK_GE(15, window_bits); | |
| 24 memset(stream_.get(), 0, sizeof(*stream_)); | |
| 25 int result = deflateInit2(stream_.get(), | |
| 26 Z_DEFAULT_COMPRESSION, | |
| 27 Z_DEFLATED, | |
| 28 -window_bits, | |
| 29 8, // default mem level | |
| 30 Z_DEFAULT_STRATEGY); | |
| 31 DCHECK_EQ(Z_OK, result); | |
| 32 } | |
| 33 | |
| 34 WebSocketDeflater::~WebSocketDeflater() { | |
| 35 DCHECK(stream_); | |
| 36 deflateEnd(stream_.get()); | |
| 37 stream_.reset(NULL); | |
| 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 bool WebSocketDeflater::Finish() { | |
| 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 buffer_.push_back('\x02'); | |
| 67 buffer_.push_back('\x00'); | |
| 68 Reset(); | |
| 69 return true; | |
| 70 } | |
| 71 stream_->next_in = NULL; | |
| 72 stream_->avail_in = 0; | |
| 73 | |
| 74 const size_t kFinishAvailableSize = 4096; | |
| 75 do { | |
| 76 int result = Deflate(kFinishAvailableSize, Z_SYNC_FLUSH); | |
| 77 if (result != Z_OK && result != Z_BUF_ERROR) { | |
| 78 Reset(); | |
| 79 return false; | |
| 80 } | |
| 81 } while (!stream_->avail_out); | |
|
tyoshino (SeeGerritForStatus)
2013/09/06 07:09:24
don't we have to continue until we see Z_BUF_ERROR
yhirano
2013/09/06 10:07:26
Done.
| |
| 82 // Remove 4 octets from the tail as the specification requires. | |
| 83 if (CurrentOutputSize() < 4) { | |
| 84 Reset(); | |
| 85 return false; | |
| 86 } | |
| 87 buffer_.resize(buffer_.size() - 4); | |
| 88 Reset(); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 scoped_refptr<IOBufferWithSize> WebSocketDeflater::GetOutput(size_t size) { | |
| 93 std::deque<char>::iterator begin = buffer_.begin(); | |
| 94 std::deque<char>::iterator end = begin + std::min(size, buffer_.size()); | |
| 95 | |
| 96 scoped_refptr<IOBufferWithSize> result = new IOBufferWithSize(end - begin); | |
| 97 std::copy(begin, end, result->data()); | |
| 98 buffer_.erase(begin, end); | |
| 99 return result; | |
| 100 } | |
| 101 | |
| 102 void WebSocketDeflater::Reset() { | |
|
tyoshino (SeeGerritForStatus)
2013/09/06 07:09:24
Reset clears only the state of Deflate. buffer_ is
yhirano
2013/09/06 10:07:26
Done.
| |
| 103 if (mode_ == DO_NOT_TAKE_OVER_CONTEXT) | |
| 104 deflateReset(stream_.get()); | |
| 105 are_bytes_added_ = false; | |
| 106 } | |
| 107 | |
| 108 int WebSocketDeflater::Deflate(size_t avail_out, int flush) { | |
| 109 DCHECK_GT(avail_out, 0u); | |
| 110 size_t current = buffer_.size(); | |
| 111 // TODO(yhirano): std::deque initializes the expanded area, | |
| 112 // but the initialization is actually not necessary. | |
| 113 buffer_.resize(buffer_.size() + avail_out); | |
| 114 stream_->next_out = reinterpret_cast<Bytef*>(&buffer_[current]); | |
|
tyoshino (SeeGerritForStatus)
2013/09/06 07:09:24
is buffer for deque guaranteed to be contiguous?
yhirano
2013/09/06 10:07:26
Thanks, you are right.
I introduced fixed_buffer_
| |
| 115 stream_->avail_out = avail_out; | |
| 116 | |
| 117 int result = deflate(stream_.get(), flush); | |
| 118 buffer_.resize(buffer_.size() - stream_->avail_out); | |
| 119 return result; | |
| 120 } | |
| 121 | |
| 122 } // namespace net | |
| OLD | NEW |