websocket_deflater.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 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. #include "net/websockets/websocket_deflater.h"
  5. #include <string.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include "base/check_op.h"
  9. #include "base/containers/circular_deque.h"
  10. #include "net/base/io_buffer.h"
  11. #include "third_party/zlib/zlib.h"
  12. namespace net {
  13. WebSocketDeflater::WebSocketDeflater(ContextTakeOverMode mode) : mode_(mode) {}
  14. WebSocketDeflater::~WebSocketDeflater() {
  15. if (stream_) {
  16. deflateEnd(stream_.get());
  17. stream_.reset(nullptr);
  18. }
  19. }
  20. bool WebSocketDeflater::Initialize(int window_bits) {
  21. DCHECK(!stream_);
  22. stream_ = std::make_unique<z_stream>();
  23. DCHECK_LE(8, window_bits);
  24. DCHECK_GE(15, window_bits);
  25. // Use a negative value to compress a raw deflate stream.
  26. //
  27. // Upgrade window_bits = 8 to 9 because zlib is unable to compress at
  28. // window_bits = 8. Historically, zlib has silently increased the window size
  29. // during compression in this case, although this is no longer done for raw
  30. // deflate streams since zlib 1.2.9.
  31. //
  32. // Because of a zlib deflate quirk, back-references will not use the entire
  33. // range of 1 << window_bits, but will instead use a restricted range of (1 <<
  34. // window_bits) - 262. With an increased window_bits = 9, back-references will
  35. // be within a range of 250. These can still be decompressed with window_bits
  36. // = 8 and the 256-byte window used there.
  37. //
  38. // Both the requirement to do this upgrade and the ability to compress with
  39. // window_bits = 9 while expecting a decompressor to function with window_bits
  40. // = 8 are quite specific to zlib's particular deflate implementation, but not
  41. // specific to any particular inflate implementation.
  42. //
  43. // See https://crbug.com/691074
  44. window_bits = -std::max(window_bits, 9);
  45. memset(stream_.get(), 0, sizeof(*stream_));
  46. int result = deflateInit2(stream_.get(),
  47. Z_DEFAULT_COMPRESSION,
  48. Z_DEFLATED,
  49. window_bits,
  50. 8, // default mem level
  51. Z_DEFAULT_STRATEGY);
  52. if (result != Z_OK) {
  53. deflateEnd(stream_.get());
  54. stream_.reset();
  55. return false;
  56. }
  57. const size_t kFixedBufferSize = 4096;
  58. fixed_buffer_.resize(kFixedBufferSize);
  59. return true;
  60. }
  61. bool WebSocketDeflater::AddBytes(const char* data, size_t size) {
  62. if (!size)
  63. return true;
  64. are_bytes_added_ = true;
  65. stream_->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data));
  66. stream_->avail_in = size;
  67. int result = Deflate(Z_NO_FLUSH);
  68. DCHECK(result != Z_BUF_ERROR || !stream_->avail_in);
  69. return result == Z_BUF_ERROR;
  70. }
  71. bool WebSocketDeflater::Finish() {
  72. if (!are_bytes_added_) {
  73. // Since consecutive calls of deflate with Z_SYNC_FLUSH and no input
  74. // lead to an error, we create and return the output for the empty input
  75. // manually.
  76. buffer_.push_back('\x00');
  77. ResetContext();
  78. return true;
  79. }
  80. stream_->next_in = nullptr;
  81. stream_->avail_in = 0;
  82. int result = Deflate(Z_SYNC_FLUSH);
  83. // Deflate returning Z_BUF_ERROR means that it's successfully flushed and
  84. // blocked for input data.
  85. if (result != Z_BUF_ERROR) {
  86. ResetContext();
  87. return false;
  88. }
  89. // Remove 4 octets from the tail as the specification requires.
  90. if (CurrentOutputSize() < 4) {
  91. ResetContext();
  92. return false;
  93. }
  94. buffer_.resize(buffer_.size() - 4);
  95. ResetContext();
  96. return true;
  97. }
  98. void WebSocketDeflater::PushSyncMark() {
  99. DCHECK(!are_bytes_added_);
  100. const char data[] = {'\x00', '\x00', '\xff', '\xff'};
  101. buffer_.insert(buffer_.end(), &data[0], &data[sizeof(data)]);
  102. }
  103. scoped_refptr<IOBufferWithSize> WebSocketDeflater::GetOutput(size_t size) {
  104. size_t length_to_copy = std::min(size, buffer_.size());
  105. base::circular_deque<char>::iterator begin = buffer_.begin();
  106. base::circular_deque<char>::iterator end = begin + length_to_copy;
  107. auto result = base::MakeRefCounted<IOBufferWithSize>(length_to_copy);
  108. std::copy(begin, end, result->data());
  109. buffer_.erase(begin, end);
  110. return result;
  111. }
  112. void WebSocketDeflater::ResetContext() {
  113. if (mode_ == DO_NOT_TAKE_OVER_CONTEXT)
  114. deflateReset(stream_.get());
  115. are_bytes_added_ = false;
  116. }
  117. int WebSocketDeflater::Deflate(int flush) {
  118. int result = Z_OK;
  119. do {
  120. stream_->next_out = reinterpret_cast<Bytef*>(fixed_buffer_.data());
  121. stream_->avail_out = fixed_buffer_.size();
  122. result = deflate(stream_.get(), flush);
  123. size_t size = fixed_buffer_.size() - stream_->avail_out;
  124. buffer_.insert(buffer_.end(), fixed_buffer_.data(),
  125. fixed_buffer_.data() + size);
  126. } while (result == Z_OK);
  127. return result;
  128. }
  129. } // namespace net