1/*
2 * IXWebSocketPerMessageDeflateCodec.h
3 * Author: Benjamin Sergeant
4 * Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
5 */
6
7#pragma once
8
9#ifdef IXWEBSOCKET_USE_ZLIB
10#include "zlib.h"
11#endif
12#include <array>
13#include <string>
14#include <vector>
15#include "IXWebSocketSendData.h"
16
17namespace ix
18{
19 class WebSocketPerMessageDeflateCompressor
20 {
21 public:
22 WebSocketPerMessageDeflateCompressor();
23 ~WebSocketPerMessageDeflateCompressor();
24
25 bool init(uint8_t deflateBits, bool clientNoContextTakeOver);
26 bool compress(const IXWebSocketSendData& in, std::string& out);
27 bool compress(const std::string& in, std::string& out);
28 bool compress(const std::string& in, std::vector<uint8_t>& out);
29 bool compress(const std::vector<uint8_t>& in, std::string& out);
30 bool compress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out);
31
32 private:
33 template<typename T, typename S>
34 bool compressData(const T& in, S& out);
35 template<typename T>
36 bool endsWithEmptyUnCompressedBlock(const T& value);
37
38 int _flush;
39 std::array<unsigned char, 1 << 14> _compressBuffer;
40
41#ifdef IXWEBSOCKET_USE_ZLIB
42 z_stream _deflateState;
43#endif
44 };
45
46 class WebSocketPerMessageDeflateDecompressor
47 {
48 public:
49 WebSocketPerMessageDeflateDecompressor();
50 ~WebSocketPerMessageDeflateDecompressor();
51
52 bool init(uint8_t inflateBits, bool clientNoContextTakeOver);
53 bool decompress(const std::string& in, std::string& out);
54
55 private:
56 int _flush;
57 std::array<unsigned char, 1 << 14> _compressBuffer;
58
59#ifdef IXWEBSOCKET_USE_ZLIB
60 z_stream _inflateState;
61#endif
62 };
63
64} // namespace ix
65