blob: 4983a3d76de1abff9b4e0acac568aa5554c7519a [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2013 The Chromium Authors
[email protected]f8e65162013-09-10 16:13:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_
6#define NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_
7
tfarinaea94afc232015-10-20 04:23:368#include <stddef.h>
Avi Drissman13fc8932015-12-20 04:40:469
danakj9c5cab52016-04-16 00:54:3310#include <memory>
[email protected]f8e65162013-09-10 16:13:0411#include <vector>
12
Brett Wilsonc6a0c822017-09-12 00:04:2913#include "base/containers/circular_deque.h"
Bence Béky65623972018-03-05 15:31:5614#include "base/memory/scoped_refptr.h"
[email protected]f8e65162013-09-10 16:13:0415#include "net/base/net_export.h"
16
17extern "C" struct z_stream_s;
18
19namespace net {
20
21class IOBufferWithSize;
22
23class NET_EXPORT_PRIVATE WebSocketDeflater {
24 public:
25 enum ContextTakeOverMode {
26 DO_NOT_TAKE_OVER_CONTEXT,
27 TAKE_OVER_CONTEXT,
[email protected]9c50b042014-04-28 06:40:1528 NUM_CONTEXT_TAKEOVER_MODE_TYPES,
[email protected]f8e65162013-09-10 16:13:0429 };
30
31 explicit WebSocketDeflater(ContextTakeOverMode mode);
Peter Boström293b1342021-09-22 17:31:4332
33 WebSocketDeflater(const WebSocketDeflater&) = delete;
34 WebSocketDeflater& operator=(const WebSocketDeflater&) = delete;
35
[email protected]f8e65162013-09-10 16:13:0436 ~WebSocketDeflater();
37
38 // Returns true if there is no error and false otherwise.
39 // This function must be called exactly once before calling any of
40 // following methods.
41 // |window_bits| must be between 8 and 15 (both inclusive).
42 bool Initialize(int window_bits);
43
44 // Adds bytes to |stream_|.
45 // Returns true if there is no error and false otherwise.
46 bool AddBytes(const char* data, size_t size);
47
48 // Flushes the current processing data.
49 // Returns true if there is no error and false otherwise.
50 bool Finish();
51
52 // Pushes "\x00\x00\xff\xff" to the end of the buffer.
53 void PushSyncMark();
54
55 // Returns the current deflated output.
56 // If the current output is larger than |size| bytes,
57 // returns the first |size| bytes of the current output.
58 // The returned bytes will be dropped from the current output and never be
59 // returned thereafter.
60 scoped_refptr<IOBufferWithSize> GetOutput(size_t size);
61
62 // Returns the size of the current deflated output.
63 size_t CurrentOutputSize() const { return buffer_.size(); }
64
65 private:
66 void ResetContext();
67 int Deflate(int flush);
68
danakj9c5cab52016-04-16 00:54:3369 std::unique_ptr<z_stream_s> stream_;
[email protected]f8e65162013-09-10 16:13:0470 ContextTakeOverMode mode_;
Brett Wilsonc6a0c822017-09-12 00:04:2971 base::circular_deque<char> buffer_;
[email protected]f8e65162013-09-10 16:13:0472 std::vector<char> fixed_buffer_;
73 // true if bytes were added after last Finish().
Tsuyoshi Horoa0b9c0f2022-06-09 01:41:5174 bool are_bytes_added_ = false;
[email protected]f8e65162013-09-10 16:13:0475};
76
77} // namespace net
78
79#endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATER_H_