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 #include <vector> | |
11 | |
12 #include "base/logging.h" | |
13 #include "net/base/io_buffer.h" | |
14 #include "third_party/zlib/zlib.h" | |
15 | |
16 namespace net { | |
17 | |
18 WebSocketDeflater::WebSocketDeflater(ContextTakeOverMode mode) | |
19 : mode_(mode), are_bytes_added_(false) {} | |
tyoshino (SeeGerritForStatus)
2013/09/09 09:25:42
indentation
yhirano
2013/09/09 09:35:26
Done.
| |
20 | |
21 WebSocketDeflater::~WebSocketDeflater() { | |
22 if (stream_) { | |
23 deflateEnd(stream_.get()); | |
24 stream_.reset(NULL); | |
25 } | |
26 } | |
27 | |
28 bool WebSocketDeflater::Initialize(int window_bits) { | |
29 DCHECK(!stream_); | |
30 stream_.reset(new z_stream); | |
31 | |
32 DCHECK_LE(8, window_bits); | |
33 DCHECK_GE(15, window_bits); | |
34 memset(stream_.get(), 0, sizeof(*stream_)); | |
35 int result = deflateInit2(stream_.get(), | |
36 Z_DEFAULT_COMPRESSION, | |
37 Z_DEFLATED, | |
38 -window_bits, // Negative value for raw deflate | |
39 8, // default mem level | |
40 Z_DEFAULT_STRATEGY); | |
41 if (result != Z_OK) { | |
42 deflateEnd(stream_.get()); | |
43 stream_.reset(); | |
44 return false; | |
45 } | |
46 const size_t kFixedBufferSize = 4096; | |
47 fixed_buffer_.resize(kFixedBufferSize); | |
48 return true; | |
49 } | |
50 | |
51 bool WebSocketDeflater::AddBytes(const char* data, size_t size) { | |
52 if (!size) | |
53 return true; | |
54 | |
55 are_bytes_added_ = true; | |
56 stream_->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data)); | |
57 stream_->avail_in = size; | |
58 | |
59 int result = Deflate(Z_NO_FLUSH); | |
60 DCHECK(result != Z_BUF_ERROR || !stream_->avail_in); | |
61 return result == Z_BUF_ERROR; | |
62 } | |
63 | |
64 bool WebSocketDeflater::Finish() { | |
65 if (!are_bytes_added_) { | |
66 // Since consecutive calls of deflate with Z_SYNC_FLUSH and no input | |
67 // lead to an error, we create and return the output for the empty input | |
68 // manually. | |
69 buffer_.push_back('\x02'); | |
70 buffer_.push_back('\x00'); | |
71 ResetContext(); | |
72 return true; | |
73 } | |
74 stream_->next_in = NULL; | |
75 stream_->avail_in = 0; | |
76 | |
77 int result = Deflate(Z_SYNC_FLUSH); | |
tyoshino (SeeGerritForStatus)
2013/09/09 09:25:42
// Deflate returning Z_BUF_ERROR means that it's s
yhirano
2013/09/09 09:35:26
Done.
| |
78 if (result != Z_BUF_ERROR) { | |
79 ResetContext(); | |
80 return false; | |
81 } | |
82 // Remove 4 octets from the tail as the specification requires. | |
83 if (CurrentOutputSize() < 4) { | |
84 ResetContext(); | |
85 return false; | |
86 } | |
87 buffer_.resize(buffer_.size() - 4); | |
88 ResetContext(); | |
89 return true; | |
90 } | |
91 | |
92 void WebSocketDeflater::PushSyncMark() { | |
93 DCHECK(!are_bytes_added_); | |
94 const char data[] = {'\x00', '\x00', '\xff', '\xff'}; | |
95 buffer_.insert(buffer_.end(), &data[0], &data[sizeof(data)]); | |
96 } | |
97 | |
98 scoped_refptr<IOBufferWithSize> WebSocketDeflater::GetOutput(size_t size) { | |
99 std::deque<char>::iterator begin = buffer_.begin(); | |
100 std::deque<char>::iterator end = begin + std::min(size, buffer_.size()); | |
101 | |
102 scoped_refptr<IOBufferWithSize> result = new IOBufferWithSize(end - begin); | |
103 std::copy(begin, end, result->data()); | |
104 buffer_.erase(begin, end); | |
105 return result; | |
106 } | |
107 | |
108 void WebSocketDeflater::ResetContext() { | |
109 if (mode_ == DO_NOT_TAKE_OVER_CONTEXT) | |
110 deflateReset(stream_.get()); | |
111 are_bytes_added_ = false; | |
112 } | |
113 | |
114 int WebSocketDeflater::Deflate(int flush) { | |
115 int result = Z_OK; | |
116 do { | |
117 stream_->next_out = reinterpret_cast<Bytef*>(&fixed_buffer_[0]); | |
118 stream_->avail_out = fixed_buffer_.size(); | |
119 result = deflate(stream_.get(), flush); | |
120 size_t size = fixed_buffer_.size() - stream_->avail_out; | |
121 buffer_.insert(buffer_.end(), &fixed_buffer_[0], &fixed_buffer_[size]); | |
122 } while (result == Z_OK); | |
123 return result; | |
124 } | |
125 | |
126 } // namespace net | |
OLD | NEW |