Support ZLib for compression

This commit is contained in:
Sleepy Flower Girl
2021-05-02 00:57:25 -04:00
parent 2cc4f007e6
commit adc4551d83
5 changed files with 63 additions and 19 deletions

View File

@@ -16,6 +16,7 @@ if (NOT ONLY_SLEEPY_DISCORD)
option(USE_LIBOPUS "Use Opus audio codec library" OFF)
option(USE_LIBSODIUM "Use libsodium cryptography library" OFF)
option(USE_ZLIB_NG "Use zlib-ng for data compression" OFF)
option(USE_ZLIB "Use zlib for data compression" OFF)
endif()
#Define a variable to use to check if this file has been executed
@@ -39,6 +40,10 @@ if(USE_WEBSOCKETPP OR USE_UWEBSOCKETS)
endif()
endif()
if(USE_ZLIB_NG AND USE_ZLIB)
message(FATAL_ERROR "can't use zlib and zlib-ng at the same time")
endif()
find_package(Git)
# Find and Download libraries
@@ -236,6 +241,11 @@ if(USE_ZLIB_NG)
add_subdirectory(deps/zlib-ng)
endif()
if(USE_ZLIB)
find_package(ZLIB REQUIRED)
#to do add auto download
endif()
# Get Version Info
if(Git_FOUND)
execute_process(

View File

@@ -1,7 +1,7 @@
#pragma once
#ifdef EXISTENT_ZLIB_NG
#include "zlib-ng_compression.h"
#if defined(EXISTENT_ZLIB_NG) || defined(EXISTENT_ZLIB)
#include "zlib_compression.h"
#else
#include "generic_compression.h"
#endif // EXISTENT_ZLIB_NG

View File

@@ -1,6 +1,10 @@
#pragma once
#include "generic_compression.h"
#include "zlib-ng/zlib-ng.h"
#ifdef EXISTENT_ZLIB
#include "zlib.h"
#elif defined(EXISTENT_ZLIB_NG)
#include "zlib-ng/zlib-ng.h"
#endif
#include <array>
#include <forward_list>
#include <string>
@@ -13,7 +17,7 @@ namespace SleepyDiscord {
constexpr static size_t chunkSize = 16 * 1024;
using Data = std::array<char, chunkSize>;
using Buffer = std::pair<Data, std::size_t>;
using Queue = std::forward_list<Buffer>;
using Iterator = std::forward_list<Buffer>::iterator;
using ConstIterator = std::forward_list<Buffer>::const_iterator;
@@ -25,7 +29,7 @@ namespace SleepyDiscord {
~OutputQueue() = default;
bool empty() const { return queue.empty(); }
//allocates more memory
template<class... Args>
Iterator emplace_back(Args&&... args) {
@@ -36,7 +40,7 @@ namespace SleepyDiscord {
}
return result;
}
Buffer& front() {
return queue.front();
}
@@ -73,6 +77,28 @@ namespace SleepyDiscord {
}
};
#ifdef EXISTENT_ZLIB
namespace ZLib {
using Stream = z_stream;
using Btye = Bytef;
using ConstByte = z_const Bytef;
inline int inflateInitStream(Stream* stream) { return inflateInit(stream); }
inline int inflateEndStream(Stream* stream) { return inflateEnd(stream); }
inline int inflateResetStream(Stream* stream) { return inflateReset(stream); }
inline int inflateStream(Stream* stream, int mode) { return inflate(stream, mode); }
}
#elif defined(EXISTENT_ZLIB_NG)
namespace ZLib {
using Stream = zng_stream;
using Btye = uint8_t;
using ConstByte = const uint8_t;
inline int inflateInitStream(Stream* stream) { return zng_inflateInit(stream); }
inline int inflateEndStream(Stream* stream) { return zng_inflateEnd(stream); }
inline int inflateResetStream(Stream* stream) { return zng_inflateReset(stream); }
inline int inflateStream(Stream* stream, int mode) { return zng_inflate(stream, mode); }
}
#endif
class ZLibCompression : public GenericCompression {
public:
using Output = OutputQueue;
@@ -80,10 +106,10 @@ namespace SleepyDiscord {
ZLibCompression();
~ZLibCompression() {
zng_inflateEnd(&stream);
ZLib::inflateEndStream(&stream);
}
zng_stream stream;
ZLib::Stream stream;
int statusCode;
Output output;
@@ -93,7 +119,7 @@ namespace SleepyDiscord {
void getOutput(std::string& uncompressedOut) override;
inline void resetStream() override {
zng_inflateReset(&stream);
ZLib::inflateResetStream(&stream);
}
inline bool streamEnded() override {

View File

@@ -22,7 +22,7 @@ add_library(sleepy-discord STATIC
voice_connection.cpp
webhook.cpp
websocketpp_websocket.cpp
zlib-ng_compression.cpp
zlib_compression.cpp
)
target_include_directories(sleepy-discord
@@ -157,6 +157,14 @@ if (NOT ONLY_SLEEPY_DISCORD)
else()
target_compile_definitions(sleepy-discord PUBLIC NONEXISTENT_ZLIB_NG)
endif()
if(USE_ZLIB)
list(APPEND LIBRARIES_TO_LINK "ZLIB::ZLIB")
target_compile_definitions(sleepy-discord PUBLIC EXISTENT_ZLIB)
else()
target_compile_definitions(sleepy-discord PUBLIC NONEXISTENT_ZLIB)
endif()
target_link_libraries(sleepy-discord PUBLIC ${LIBRARIES_TO_LINK})
else()
target_compile_definitions(sleepy-discord PUBLIC

View File

@@ -1,13 +1,13 @@
#ifdef EXISTENT_ZLIB_NG
#include "zlib-ng_compression.h"
#if defined(EXISTENT_ZLIB) || defined(EXISTENT_ZLIB_NG)
#include "zlib_compression.h"
namespace SleepyDiscord {
ZLibCompression::ZLibCompression() {
stream = zng_stream{};
stream = ZLib::Stream{};
memset(&stream, 0, sizeof(stream));
statusCode = zng_inflateInit(&stream);
statusCode = ZLib::inflateInitStream(&stream);
if (statusCode != Z_OK) {
zng_inflateEnd(&stream);
ZLib::inflateEndStream(&stream);
}
if (output.empty()) //since are using back(), we need at least one buffer in the output
output.emplace_back(); //make a new output buffer
@@ -16,7 +16,7 @@ namespace SleepyDiscord {
void ZLibCompression::uncompress(const std::string& compressed) {
std::lock_guard<std::mutex> lock(mutex);
stream.next_in = reinterpret_cast<const uint8_t*>(compressed.c_str());
stream.next_in = (ZLib::ConstByte*)(compressed.c_str());
stream.avail_in = static_cast<uint32_t>(compressed.length());
statusCode = Z_BUF_ERROR;
@@ -25,17 +25,17 @@ namespace SleepyDiscord {
Output::Data& data = buffer.first;
std::size_t& size = buffer.second;
stream.next_out = reinterpret_cast<uint8_t*>(data.data() + size);
stream.next_out = reinterpret_cast<ZLib::Btye*>(data.data() + size);
stream.avail_out = static_cast<uint32_t>(data.size() - size);
statusCode = zng_inflate(&stream, Z_SYNC_FLUSH);
statusCode = ZLib::inflateStream(&stream, Z_SYNC_FLUSH);
auto oldSize = size;
size = data.size() - stream.avail_out;
auto deltaSize = size - oldSize;
if (statusCode == Z_STREAM_END) {
statusCode = zng_inflateReset(&stream);
statusCode = ZLib::inflateResetStream(&stream);
}
else if (deltaSize == 0) { //if did anything
if (stream.avail_out == 0)