summaryrefslogtreecommitdiff
path: root/chromium/net/websockets/websocket_inflater.cc
blob: 7767821f462bf7a292ed09664cf925d3a0422cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/websockets/websocket_inflater.h"

#include <algorithm>
#include <deque>
#include <vector>

#include "base/logging.h"
#include "net/base/io_buffer.h"
#include "third_party/zlib/zlib.h"

namespace net {

namespace {

class ShrinkableIOBufferWithSize : public IOBufferWithSize {
 public:
  explicit ShrinkableIOBufferWithSize(size_t size) : IOBufferWithSize(size) {}

  void Shrink(int new_size) {
    CHECK_GE(new_size, 0);
    CHECK_LE(new_size, size_);
    size_ = new_size;
  }

 private:
  ~ShrinkableIOBufferWithSize() override {}
};

}  // namespace

WebSocketInflater::WebSocketInflater()
    : input_queue_(kDefaultInputIOBufferCapacity),
      output_buffer_(kDefaultBufferCapacity) {}

WebSocketInflater::WebSocketInflater(size_t input_queue_capacity,
                                     size_t output_buffer_capacity)
    : input_queue_(input_queue_capacity),
      output_buffer_(output_buffer_capacity) {
  DCHECK_GT(input_queue_capacity, 0u);
  DCHECK_GT(output_buffer_capacity, 0u);
}

bool WebSocketInflater::Initialize(int window_bits) {
  DCHECK_LE(8, window_bits);
  DCHECK_GE(15, window_bits);
  stream_.reset(new z_stream);
  memset(stream_.get(), 0, sizeof(*stream_));
  int result = inflateInit2(stream_.get(), -window_bits);
  if (result != Z_OK) {
    inflateEnd(stream_.get());
    stream_.reset();
    return false;
  }
  return true;
}

WebSocketInflater::~WebSocketInflater() {
  if (stream_) {
    inflateEnd(stream_.get());
    stream_.reset();
  }
}

bool WebSocketInflater::AddBytes(const char* data, size_t size) {
  if (!size)
    return true;

  if (!input_queue_.IsEmpty()) {
    // choked
    input_queue_.Push(data, size);
    return true;
  }

  int result = InflateWithFlush(data, size);
  if (stream_->avail_in > 0)
    input_queue_.Push(&data[size - stream_->avail_in], stream_->avail_in);

  return result == Z_OK || result == Z_BUF_ERROR;
}

bool WebSocketInflater::Finish() {
  return AddBytes("\x00\x00\xff\xff", 4);
}

scoped_refptr<IOBufferWithSize> WebSocketInflater::GetOutput(size_t size) {
  scoped_refptr<ShrinkableIOBufferWithSize> buffer =
      new ShrinkableIOBufferWithSize(size);
  size_t num_bytes_copied = 0;

  while (num_bytes_copied < size && output_buffer_.Size() > 0) {
    size_t num_bytes_to_copy =
        std::min(output_buffer_.Size(), size - num_bytes_copied);
    output_buffer_.Read(&buffer->data()[num_bytes_copied], num_bytes_to_copy);
    num_bytes_copied += num_bytes_to_copy;
    int result = InflateChokedInput();
    if (result != Z_OK && result != Z_BUF_ERROR)
      return NULL;
  }
  buffer->Shrink(num_bytes_copied);
  return buffer;
}

int WebSocketInflater::InflateWithFlush(const char* next_in, size_t avail_in) {
  int result = Inflate(next_in, avail_in, Z_NO_FLUSH);
  if (result != Z_OK && result != Z_BUF_ERROR)
    return result;

  if (CurrentOutputSize() > 0)
    return result;
  // CurrentOutputSize() == 0 means there is no data to be output,
  // so we should make sure it by using Z_SYNC_FLUSH.
  return Inflate(reinterpret_cast<const char*>(stream_->next_in),
                 stream_->avail_in,
                 Z_SYNC_FLUSH);
}

int WebSocketInflater::Inflate(const char* next_in,
                               size_t avail_in,
                               int flush) {
  stream_->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(next_in));
  stream_->avail_in = avail_in;

  int result = Z_BUF_ERROR;
  do {
    std::pair<char*, size_t> tail = output_buffer_.GetTail();
    if (!tail.second)
      break;

    stream_->next_out = reinterpret_cast<Bytef*>(tail.first);
    stream_->avail_out = tail.second;
    result = inflate(stream_.get(), flush);
    output_buffer_.AdvanceTail(tail.second - stream_->avail_out);
    if (result == Z_STREAM_END) {
      // Received a block with BFINAL set to 1. Reset the decompression state.
      result = inflateReset(stream_.get());
    } else if (tail.second == stream_->avail_out) {
      break;
    }
  } while (result == Z_OK || result == Z_BUF_ERROR);
  return result;
}

int WebSocketInflater::InflateChokedInput() {
  if (input_queue_.IsEmpty())
    return InflateWithFlush(NULL, 0);

  int result = Z_BUF_ERROR;
  while (!input_queue_.IsEmpty()) {
    std::pair<char*, size_t> top = input_queue_.Top();

    result = InflateWithFlush(top.first, top.second);
    input_queue_.Consume(top.second - stream_->avail_in);

    if (result != Z_OK && result != Z_BUF_ERROR)
      return result;

    if (stream_->avail_in > 0) {
      // There are some data which are not consumed.
      break;
    }
  }
  return result;
}

WebSocketInflater::OutputBuffer::OutputBuffer(size_t capacity)
    : capacity_(capacity),
      buffer_(capacity_ + 1),  // 1 for sentinel
      head_(0),
      tail_(0) {}

WebSocketInflater::OutputBuffer::~OutputBuffer() {}

size_t WebSocketInflater::OutputBuffer::Size() const {
  return (tail_ + buffer_.size() - head_) % buffer_.size();
}

std::pair<char*, size_t> WebSocketInflater::OutputBuffer::GetTail() {
  DCHECK_LT(tail_, buffer_.size());
  return std::make_pair(&buffer_[tail_],
                        std::min(capacity_ - Size(), buffer_.size() - tail_));
}

void WebSocketInflater::OutputBuffer::Read(char* dest, size_t size) {
  DCHECK_LE(size, Size());

  size_t num_bytes_copied = 0;
  if (tail_ < head_) {
    size_t num_bytes_to_copy = std::min(size, buffer_.size() - head_);
    DCHECK_LT(head_, buffer_.size());
    memcpy(&dest[num_bytes_copied], &buffer_[head_], num_bytes_to_copy);
    AdvanceHead(num_bytes_to_copy);
    num_bytes_copied += num_bytes_to_copy;
  }

  if (num_bytes_copied == size)
    return;
  DCHECK_LE(head_, tail_);
  size_t num_bytes_to_copy = size - num_bytes_copied;
  DCHECK_LE(num_bytes_to_copy, tail_ - head_);
  DCHECK_LT(head_, buffer_.size());
  memcpy(&dest[num_bytes_copied], &buffer_[head_], num_bytes_to_copy);
  AdvanceHead(num_bytes_to_copy);
  num_bytes_copied += num_bytes_to_copy;
  DCHECK_EQ(size, num_bytes_copied);
  return;
}

void WebSocketInflater::OutputBuffer::AdvanceHead(size_t advance) {
  DCHECK_LE(advance, Size());
  head_ = (head_ + advance) % buffer_.size();
}

void WebSocketInflater::OutputBuffer::AdvanceTail(size_t advance) {
  DCHECK_LE(advance + Size(), capacity_);
  tail_ = (tail_ + advance) % buffer_.size();
}

WebSocketInflater::InputQueue::InputQueue(size_t capacity)
    : capacity_(capacity), head_of_first_buffer_(0), tail_of_last_buffer_(0) {}

WebSocketInflater::InputQueue::~InputQueue() {}

std::pair<char*, size_t> WebSocketInflater::InputQueue::Top() {
  DCHECK(!IsEmpty());
  if (buffers_.size() == 1) {
    return std::make_pair(&buffers_.front()->data()[head_of_first_buffer_],
                          tail_of_last_buffer_ - head_of_first_buffer_);
  }
  return std::make_pair(&buffers_.front()->data()[head_of_first_buffer_],
                        capacity_ - head_of_first_buffer_);
}

void WebSocketInflater::InputQueue::Push(const char* data, size_t size) {
  if (!size)
    return;

  size_t num_copied_bytes = 0;
  if (!IsEmpty())
    num_copied_bytes += PushToLastBuffer(data, size);

  while (num_copied_bytes < size) {
    DCHECK(IsEmpty() || tail_of_last_buffer_ == capacity_);

    buffers_.push_back(new IOBufferWithSize(capacity_));
    tail_of_last_buffer_ = 0;
    num_copied_bytes +=
        PushToLastBuffer(&data[num_copied_bytes], size - num_copied_bytes);
  }
}

void WebSocketInflater::InputQueue::Consume(size_t size) {
  DCHECK(!IsEmpty());
  DCHECK_LE(size + head_of_first_buffer_, capacity_);

  head_of_first_buffer_ += size;
  if (head_of_first_buffer_ == capacity_) {
    buffers_.pop_front();
    head_of_first_buffer_ = 0;
  }
  if (buffers_.size() == 1 && head_of_first_buffer_ == tail_of_last_buffer_) {
    buffers_.pop_front();
    head_of_first_buffer_ = 0;
    tail_of_last_buffer_ = 0;
  }
}

size_t WebSocketInflater::InputQueue::PushToLastBuffer(const char* data,
                                                       size_t size) {
  DCHECK(!IsEmpty());
  size_t num_bytes_to_copy = std::min(size, capacity_ - tail_of_last_buffer_);
  if (!num_bytes_to_copy)
    return 0;
  IOBufferWithSize* buffer = buffers_.back().get();
  memcpy(&buffer->data()[tail_of_last_buffer_], data, num_bytes_to_copy);
  tail_of_last_buffer_ += num_bytes_to_copy;
  return num_bytes_to_copy;
}

}  // namespace net