|
|
@ -31,6 +31,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include <vector> |
|
|
|
#include <vector> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <QtGlobal> |
|
|
|
#include <QByteArray> |
|
|
|
#include <QByteArray> |
|
|
|
|
|
|
|
|
|
|
|
#ifndef ZLIB_CONST |
|
|
|
#ifndef ZLIB_CONST |
|
|
@ -40,63 +41,46 @@ |
|
|
|
|
|
|
|
|
|
|
|
QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *ok) |
|
|
|
QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *ok) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ok) *ok = false; |
|
|
|
if (ok) |
|
|
|
|
|
|
|
*ok = false; |
|
|
|
|
|
|
|
|
|
|
|
if (data.isEmpty()) |
|
|
|
if (data.isEmpty()) |
|
|
|
return {}; |
|
|
|
return {}; |
|
|
|
|
|
|
|
|
|
|
|
const int BUFSIZE = 128 * 1024; |
|
|
|
|
|
|
|
std::vector<char> tmpBuf(BUFSIZE); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
z_stream strm {}; |
|
|
|
z_stream strm {}; |
|
|
|
strm.zalloc = Z_NULL; |
|
|
|
strm.zalloc = Z_NULL; |
|
|
|
strm.zfree = Z_NULL; |
|
|
|
strm.zfree = Z_NULL; |
|
|
|
strm.opaque = Z_NULL; |
|
|
|
strm.opaque = Z_NULL; |
|
|
|
strm.next_in = reinterpret_cast<const Bytef *>(data.constData()); |
|
|
|
strm.next_in = reinterpret_cast<const Bytef *>(data.constData()); |
|
|
|
strm.avail_in = uInt(data.size()); |
|
|
|
strm.avail_in = static_cast<uInt>(data.size()); |
|
|
|
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data()); |
|
|
|
|
|
|
|
strm.avail_out = BUFSIZE; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// windowBits = 15 + 16 to enable gzip
|
|
|
|
// windowBits = 15 + 16 to enable gzip
|
|
|
|
// From the zlib manual: windowBits can also be greater than 15 for optional gzip encoding. Add 16 to windowBits
|
|
|
|
// From the zlib manual: windowBits can also be greater than 15 for optional gzip encoding. Add 16 to windowBits
|
|
|
|
// to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper.
|
|
|
|
// to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper.
|
|
|
|
int result = deflateInit2(&strm, level, Z_DEFLATED, (15 + 16), 9, Z_DEFAULT_STRATEGY); |
|
|
|
const int initResult = deflateInit2(&strm, level, Z_DEFLATED, (15 + 16), 9, Z_DEFAULT_STRATEGY); |
|
|
|
if (result != Z_OK) |
|
|
|
if (initResult != Z_OK) |
|
|
|
return {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QByteArray output; |
|
|
|
|
|
|
|
output.reserve(deflateBound(&strm, data.size())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// feed to deflate
|
|
|
|
|
|
|
|
while (strm.avail_in > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
result = deflate(&strm, Z_NO_FLUSH); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (result != Z_OK) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
deflateEnd(&strm); |
|
|
|
|
|
|
|
return {}; |
|
|
|
return {}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out)); |
|
|
|
|
|
|
|
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data()); |
|
|
|
|
|
|
|
strm.avail_out = BUFSIZE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// flush the rest from deflate
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) |
|
|
|
while (result != Z_STREAM_END) |
|
|
|
QByteArray ret {static_cast<qsizetype>(deflateBound(&strm, data.size())), Qt::Uninitialized}; |
|
|
|
{ |
|
|
|
#else |
|
|
|
result = deflate(&strm, Z_FINISH); |
|
|
|
QByteArray ret {static_cast<int>(deflateBound(&strm, data.size())), Qt::Uninitialized}; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
strm.next_out = reinterpret_cast<Bytef *>(ret.data()); |
|
|
|
|
|
|
|
strm.avail_out = ret.size(); |
|
|
|
|
|
|
|
|
|
|
|
output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out)); |
|
|
|
// From the zlib manual: Z_FINISH can be used in the first deflate call after deflateInit if all the compression
|
|
|
|
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data()); |
|
|
|
// is to be done in a single step. In order to complete in one call, avail_out must be at least the value
|
|
|
|
strm.avail_out = BUFSIZE; |
|
|
|
// returned by deflateBound (see below). Then deflate is guaranteed to return Z_STREAM_END.
|
|
|
|
} |
|
|
|
const int deflateResult = deflate(&strm, Z_FINISH); |
|
|
|
|
|
|
|
Q_ASSERT(deflateResult == Z_STREAM_END); |
|
|
|
|
|
|
|
|
|
|
|
deflateEnd(&strm); |
|
|
|
deflateEnd(&strm); |
|
|
|
|
|
|
|
ret.truncate(strm.total_out); |
|
|
|
|
|
|
|
|
|
|
|
if (ok) *ok = true; |
|
|
|
if (ok) |
|
|
|
return output; |
|
|
|
*ok = true; |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok) |
|
|
|
QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok) |
|
|
|