WebSocketPerMessageDeflateCompressor can work with vector or std::string
This commit is contained in:
		| @@ -59,11 +59,15 @@ namespace ix | |||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     bool WebSocketPerMessageDeflateCompressor::endsWith(const std::string& value, |     template<typename T> | ||||||
|                                                         const std::string& ending) |     bool WebSocketPerMessageDeflateCompressor::endsWithEmptyUnCompressedBlock(const T& value) | ||||||
|     { |     { | ||||||
|         if (ending.size() > value.size()) return false; |         if (kEmptyUncompressedBlock.size() > value.size()) return false; | ||||||
|         return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); |         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) |     bool WebSocketPerMessageDeflateCompressor::compress(const std::string& in, std::string& out) | ||||||
| @@ -71,11 +75,21 @@ namespace ix | |||||||
|         return compressData(in, out); |         return compressData(in, out); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     bool WebSocketPerMessageDeflateCompressor::compress(const std::string& in, std::vector<uint8_t>& out) | ||||||
|  |     { | ||||||
|  |         return compressData(in, out); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     bool WebSocketPerMessageDeflateCompressor::compress(const std::vector<uint8_t>& in, std::string& out) |     bool WebSocketPerMessageDeflateCompressor::compress(const std::vector<uint8_t>& in, std::string& out) | ||||||
|     { |     { | ||||||
|         return compressData(in, out); |         return compressData(in, out); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     bool WebSocketPerMessageDeflateCompressor::compress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out) | ||||||
|  |     { | ||||||
|  |         return compressData(in, out); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     template<typename T, typename S> bool WebSocketPerMessageDeflateCompressor::compressData(const T& in, S& out) |     template<typename T, typename S> bool WebSocketPerMessageDeflateCompressor::compressData(const T& in, S& out) | ||||||
|     { |     { | ||||||
|         // |         // | ||||||
| @@ -106,7 +120,8 @@ namespace ix | |||||||
|             // The normal buffer size should be 6 but |             // The normal buffer size should be 6 but | ||||||
|             // we remove the 4 octets from the tail (#4) |             // we remove the 4 octets from the tail (#4) | ||||||
|             uint8_t buf[2] = {0x02, 0x00}; |             uint8_t buf[2] = {0x02, 0x00}; | ||||||
|             out.append((char*) (buf), 2); |             out.push_back(buf[0]); | ||||||
|  |             out.push_back(buf[1]); | ||||||
|  |  | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
| @@ -124,10 +139,10 @@ namespace ix | |||||||
|  |  | ||||||
|             output = _compressBufferSize - _deflateState.avail_out; |             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); |         } while (_deflateState.avail_out == 0); | ||||||
|  |  | ||||||
|         if (endsWith(out, kEmptyUncompressedBlock)) |         if (endsWithEmptyUnCompressedBlock(out)) | ||||||
|         { |         { | ||||||
|             out.resize(out.size() - 4); |             out.resize(out.size() - 4); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -21,12 +21,14 @@ namespace ix | |||||||
|  |  | ||||||
|         bool init(uint8_t deflateBits, bool clientNoContextTakeOver); |         bool init(uint8_t deflateBits, bool clientNoContextTakeOver); | ||||||
|         bool compress(const std::string& in, std::string& out); |         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::string& out); | ||||||
|  |         bool compress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out); | ||||||
|  |  | ||||||
|         template<typename T, typename S> bool compressData(const T& in, S& out); |         template<typename T, typename S> bool compressData(const T& in, S& out); | ||||||
|  |  | ||||||
|     private: |     private: | ||||||
|         static bool endsWith(const std::string& value, const std::string& ending); |         template<typename T> bool endsWithEmptyUnCompressedBlock(const T& value); | ||||||
|  |  | ||||||
|         int _flush; |         int _flush; | ||||||
|         size_t _compressBufferSize; |         size_t _compressBufferSize; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user