IXWebSocket/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h

65 lines
1.7 KiB
C
Raw Permalink Normal View History

2019-01-05 02:28:13 +01:00
/*
* IXWebSocketPerMessageDeflateCodec.h
* Author: Benjamin Sergeant
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#ifdef IXWEBSOCKET_USE_ZLIB
2019-01-05 02:28:13 +01:00
#include "zlib.h"
#endif
#include <array>
2019-05-30 17:46:50 +02:00
#include <string>
#include <vector>
#include "IXWebSocketSendData.h"
2019-01-05 02:28:13 +01:00
namespace ix
2019-01-05 02:28:13 +01:00
{
class WebSocketPerMessageDeflateCompressor
{
public:
WebSocketPerMessageDeflateCompressor();
~WebSocketPerMessageDeflateCompressor();
bool init(uint8_t deflateBits, bool clientNoContextTakeOver);
bool compress(const IXWebSocketSendData& in, std::string& out);
2019-01-05 02:28:13 +01:00
bool compress(const std::string& in, std::string& out);
bool compress(const std::string& in, std::vector<uint8_t>& out);
bool compress(const std::vector<uint8_t>& in, std::string& out);
bool compress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out);
2019-01-05 02:28:13 +01:00
private:
2020-07-08 19:39:46 +02:00
template<typename T, typename S>
bool compressData(const T& in, S& out);
template<typename T>
bool endsWithEmptyUnCompressedBlock(const T& value);
2019-01-05 02:28:13 +01:00
int _flush;
std::array<unsigned char, 1 << 14> _compressBuffer;
#ifdef IXWEBSOCKET_USE_ZLIB
2019-01-05 02:28:13 +01:00
z_stream _deflateState;
#endif
2019-01-05 02:28:13 +01:00
};
class WebSocketPerMessageDeflateDecompressor
{
public:
WebSocketPerMessageDeflateDecompressor();
~WebSocketPerMessageDeflateDecompressor();
bool init(uint8_t inflateBits, bool clientNoContextTakeOver);
bool decompress(const std::string& in, std::string& out);
private:
int _flush;
std::array<unsigned char, 1 << 14> _compressBuffer;
#ifdef IXWEBSOCKET_USE_ZLIB
2019-01-05 02:28:13 +01:00
z_stream _inflateState;
#endif
2019-01-05 02:28:13 +01:00
};
2019-05-30 17:46:50 +02:00
} // namespace ix