mirror of
https://gitlab.com/spaceti-app/integrations/common/server-sdk.git
synced 2026-07-12 22:00:39 +02:00
84 lines
1.9 KiB
C++
84 lines
1.9 KiB
C++
#include "utils.h"
|
|
|
|
#include <QFileInfo>
|
|
#include <QDir>
|
|
|
|
#ifdef USE_ZLIB
|
|
#include <zlib.h> // gzip decompress
|
|
#endif
|
|
|
|
namespace serversdk {
|
|
|
|
|
|
#ifdef USE_ZLIB
|
|
/**
|
|
* @brief Decompress gzip data
|
|
* @param compressed gzip data
|
|
* @return decompressed data
|
|
*/
|
|
QByteArray Utils::decompress(const QByteArray &compressed)
|
|
{
|
|
if (compressed.size() <= 4) {
|
|
qWarning("gUncompress: Input data is truncated");
|
|
return QByteArray();
|
|
}
|
|
|
|
QByteArray result;
|
|
|
|
int ret;
|
|
z_stream strm;
|
|
static const int CHUNK_SIZE = 1024;
|
|
char out[CHUNK_SIZE];
|
|
|
|
/* allocate inflate state */
|
|
strm.zalloc = Z_NULL;
|
|
strm.zfree = Z_NULL;
|
|
strm.opaque = Z_NULL;
|
|
strm.avail_in = compressed.size();
|
|
strm.next_in = (Bytef*)(compressed.data());
|
|
|
|
ret = inflateInit2(&strm, 15 + 32); // gzip decoding
|
|
if (ret != Z_OK)
|
|
return QByteArray();
|
|
|
|
// run inflate()
|
|
do {
|
|
strm.avail_out = CHUNK_SIZE;
|
|
strm.next_out = (Bytef*)(out);
|
|
|
|
ret = inflate(&strm, Z_NO_FLUSH);
|
|
Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
|
|
|
|
switch (ret) {
|
|
case Z_NEED_DICT:
|
|
return QByteArray();
|
|
case Z_DATA_ERROR:
|
|
case Z_MEM_ERROR:
|
|
(void)inflateEnd(&strm);
|
|
return QByteArray();
|
|
}
|
|
|
|
result.append(out, CHUNK_SIZE - strm.avail_out);
|
|
} while (strm.avail_out == 0);
|
|
|
|
// clean up and return
|
|
inflateEnd(&strm);
|
|
|
|
// Calculate compression ratio
|
|
// original :200
|
|
// compressed :100
|
|
// ratio = 0.5
|
|
float ratio = (float)compressed.count() / (float)result.count();
|
|
float compression = ratio * 100.0;
|
|
|
|
qCInfo(sdkutils) << "compress data:" << compressed.count()
|
|
<< "uncompressed data:" << result.count()
|
|
<< "compression:" << compression;
|
|
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
|
|
} // namespace core
|