From fbe7b0b0201c533f5347aba11bd3697d52568aa6 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 7 Jul 2020 18:17:44 -0700 Subject: [PATCH] WebSocketPerMessageDeflateCompressor can work with vector or std::string --- .../IXWebSocketPerMessageDeflateCodec.cpp | 29 ++++++++++++++----- .../IXWebSocketPerMessageDeflateCodec.h | 4 ++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp index d25b8e66..e762c800 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp +++ b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.cpp @@ -59,11 +59,15 @@ namespace ix return true; } - bool WebSocketPerMessageDeflateCompressor::endsWith(const std::string& value, - const std::string& ending) + template + bool WebSocketPerMessageDeflateCompressor::endsWithEmptyUnCompressedBlock(const T& value) { - if (ending.size() > value.size()) return false; - return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); + if (kEmptyUncompressedBlock.size() > value.size()) return false; + auto N = value.size(); + return value[N - 1] == kEmptyUncompressedBlock[3] && + value[N - 2] == kEmptyUncompressedBlock[2] && + value[N - 3] == kEmptyUncompressedBlock[1] && + value[N - 4] == kEmptyUncompressedBlock[0]; } bool WebSocketPerMessageDeflateCompressor::compress(const std::string& in, std::string& out) @@ -71,11 +75,21 @@ namespace ix return compressData(in, out); } + bool WebSocketPerMessageDeflateCompressor::compress(const std::string& in, std::vector& out) + { + return compressData(in, out); + } + bool WebSocketPerMessageDeflateCompressor::compress(const std::vector& in, std::string& out) { return compressData(in, out); } + bool WebSocketPerMessageDeflateCompressor::compress(const std::vector& in, std::vector& out) + { + return compressData(in, out); + } + template bool WebSocketPerMessageDeflateCompressor::compressData(const T& in, S& out) { // @@ -106,7 +120,8 @@ namespace ix // The normal buffer size should be 6 but // we remove the 4 octets from the tail (#4) uint8_t buf[2] = {0x02, 0x00}; - out.append((char*) (buf), 2); + out.push_back(buf[0]); + out.push_back(buf[1]); return true; } @@ -124,10 +139,10 @@ namespace ix output = _compressBufferSize - _deflateState.avail_out; - out.append((char*) (_compressBuffer.get()), output); + out.insert(out.end(), _compressBuffer.get(), _compressBuffer.get() + output); } while (_deflateState.avail_out == 0); - if (endsWith(out, kEmptyUncompressedBlock)) + if (endsWithEmptyUnCompressedBlock(out)) { out.resize(out.size() - 4); } diff --git a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h index f445d136..194c811f 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h +++ b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h @@ -21,12 +21,14 @@ namespace ix bool init(uint8_t deflateBits, bool clientNoContextTakeOver); bool compress(const std::string& in, std::string& out); + bool compress(const std::string& in, std::vector& out); bool compress(const std::vector& in, std::string& out); + bool compress(const std::vector& in, std::vector& out); template bool compressData(const T& in, S& out); private: - static bool endsWith(const std::string& value, const std::string& ending); + template bool endsWithEmptyUnCompressedBlock(const T& value); int _flush; size_t _compressBufferSize;