You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

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