parent
db7057de69
commit
7711cb1ae7
@ -244,14 +244,6 @@ if (USE_ZLIB)
|
|||||||
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_ZLIB)
|
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_ZLIB)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# brew install libdeflate
|
|
||||||
find_package(Deflate)
|
|
||||||
if (DEFLATE_FOUND)
|
|
||||||
include_directories(${DEFLATE_INCLUDE_DIRS})
|
|
||||||
target_link_libraries(ixwebsocket PRIVATE ${DEFLATE_LIBRARIES})
|
|
||||||
target_compile_definitions(ixwebsocket PUBLIC IXWEBSOCKET_USE_DEFLATE)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
target_link_libraries(ixwebsocket PRIVATE wsock32 ws2_32 shlwapi)
|
target_link_libraries(ixwebsocket PRIVATE wsock32 ws2_32 shlwapi)
|
||||||
target_compile_definitions(ixwebsocket PRIVATE _CRT_SECURE_NO_WARNINGS)
|
target_compile_definitions(ixwebsocket PRIVATE _CRT_SECURE_NO_WARNINGS)
|
||||||
|
@ -14,53 +14,12 @@
|
|||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef IXWEBSOCKET_USE_DEFLATE
|
|
||||||
#include <libdeflate.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
std::string gzipCompress(const std::string& str)
|
std::string gzipCompress(const std::string& str)
|
||||||
{
|
{
|
||||||
#ifndef IXWEBSOCKET_USE_ZLIB
|
#ifndef IXWEBSOCKET_USE_ZLIB
|
||||||
return std::string();
|
return std::string();
|
||||||
#else
|
|
||||||
#ifdef IXWEBSOCKET_USE_DEFLATE
|
|
||||||
int compressionLevel = 6;
|
|
||||||
struct libdeflate_compressor* compressor;
|
|
||||||
|
|
||||||
compressor = libdeflate_alloc_compressor(compressionLevel);
|
|
||||||
|
|
||||||
const void* uncompressed_data = str.data();
|
|
||||||
size_t uncompressed_size = str.size();
|
|
||||||
void* compressed_data;
|
|
||||||
size_t actual_compressed_size;
|
|
||||||
size_t max_compressed_size;
|
|
||||||
|
|
||||||
max_compressed_size = libdeflate_gzip_compress_bound(compressor, uncompressed_size);
|
|
||||||
compressed_data = malloc(max_compressed_size);
|
|
||||||
|
|
||||||
if (compressed_data == NULL)
|
|
||||||
{
|
|
||||||
return std::string();
|
|
||||||
}
|
|
||||||
|
|
||||||
actual_compressed_size = libdeflate_gzip_compress(
|
|
||||||
compressor, uncompressed_data, uncompressed_size, compressed_data, max_compressed_size);
|
|
||||||
|
|
||||||
libdeflate_free_compressor(compressor);
|
|
||||||
|
|
||||||
if (actual_compressed_size == 0)
|
|
||||||
{
|
|
||||||
free(compressed_data);
|
|
||||||
return std::string();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string out;
|
|
||||||
out.assign(reinterpret_cast<char*>(compressed_data), actual_compressed_size);
|
|
||||||
free(compressed_data);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
#else
|
#else
|
||||||
z_stream zs; // z_stream is zlib's control structure
|
z_stream zs; // z_stream is zlib's control structure
|
||||||
memset(&zs, 0, sizeof(zs));
|
memset(&zs, 0, sizeof(zs));
|
||||||
@ -101,7 +60,6 @@ namespace ix
|
|||||||
deflateEnd(&zs);
|
deflateEnd(&zs);
|
||||||
|
|
||||||
return outstring;
|
return outstring;
|
||||||
#endif // IXWEBSOCKET_USE_DEFLATE
|
|
||||||
#endif // IXWEBSOCKET_USE_ZLIB
|
#endif // IXWEBSOCKET_USE_ZLIB
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,26 +75,6 @@ namespace ix
|
|||||||
{
|
{
|
||||||
#ifndef IXWEBSOCKET_USE_ZLIB
|
#ifndef IXWEBSOCKET_USE_ZLIB
|
||||||
return false;
|
return false;
|
||||||
#else
|
|
||||||
#ifdef IXWEBSOCKET_USE_DEFLATE
|
|
||||||
struct libdeflate_decompressor* decompressor;
|
|
||||||
decompressor = libdeflate_alloc_decompressor();
|
|
||||||
|
|
||||||
const void* compressed_data = in.data();
|
|
||||||
size_t compressed_size = in.size();
|
|
||||||
|
|
||||||
// Retrieve uncompressed size from the trailer of the gziped data
|
|
||||||
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&in.front());
|
|
||||||
auto uncompressed_size = loadDecompressedGzipSize(&ptr[compressed_size - 4]);
|
|
||||||
|
|
||||||
// Use it to redimension our output buffer
|
|
||||||
out.resize(uncompressed_size);
|
|
||||||
|
|
||||||
libdeflate_result result = libdeflate_gzip_decompress(
|
|
||||||
decompressor, compressed_data, compressed_size, &out.front(), uncompressed_size, NULL);
|
|
||||||
|
|
||||||
libdeflate_free_decompressor(decompressor);
|
|
||||||
return result == LIBDEFLATE_SUCCESS;
|
|
||||||
#else
|
#else
|
||||||
z_stream inflateState;
|
z_stream inflateState;
|
||||||
memset(&inflateState, 0, sizeof(inflateState));
|
memset(&inflateState, 0, sizeof(inflateState));
|
||||||
@ -177,7 +115,6 @@ namespace ix
|
|||||||
|
|
||||||
inflateEnd(&inflateState);
|
inflateEnd(&inflateState);
|
||||||
return true;
|
return true;
|
||||||
#endif // IXWEBSOCKET_USE_DEFLATE
|
|
||||||
#endif // IXWEBSOCKET_USE_ZLIB
|
#endif // IXWEBSOCKET_USE_ZLIB
|
||||||
}
|
}
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
Loading…
Reference in New Issue
Block a user