Merge pull request #6654 from Chocobo1/persistence

Webui server fixes
This commit is contained in:
sledgehammer999
2017-04-30 16:34:23 +03:00
committed by GitHub
15 changed files with 349 additions and 223 deletions

View File

@@ -27,116 +27,123 @@
* exception statement from your version.
*/
#include <QByteArray>
#include <zlib.h>
#include "gzip.h"
bool Utils::Gzip::compress(QByteArray src, QByteArray &dest)
{
static const int BUFSIZE = 128 * 1024;
char tmpBuf[BUFSIZE];
int ret;
#include <QByteArray>
dest.clear();
#ifndef ZLIB_CONST
#define ZLIB_CONST // make z_stream.next_in const
#endif
#include <zlib.h>
QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *ok)
{
if (ok) *ok = false;
if (data.isEmpty())
return {};
const int BUFSIZE = 128 * 1024;
char tmpBuf[BUFSIZE] = {0};
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = reinterpret_cast<uchar *>(src.data());
strm.avail_in = src.length();
strm.next_out = reinterpret_cast<uchar *>(tmpBuf);
strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
strm.avail_in = uInt(data.size());
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
strm.avail_out = BUFSIZE;
// 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
// to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper.
ret = deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
int result = deflateInit2(&strm, level, Z_DEFLATED, (15 + 16), 9, Z_DEFAULT_STRATEGY);
if (result != Z_OK)
return {};
if (ret != Z_OK)
return false;
QByteArray output;
output.reserve(deflateBound(&strm, data.size()));
while (strm.avail_in != 0) {
ret = deflate(&strm, Z_NO_FLUSH);
if (ret != Z_OK)
return false;
// feed to deflate
while (strm.avail_in > 0) {
result = deflate(&strm, Z_NO_FLUSH);
if (strm.avail_out == 0) {
dest.append(tmpBuf, BUFSIZE);
strm.next_out = reinterpret_cast<uchar *>(tmpBuf);
strm.avail_out = BUFSIZE;
}
}
int deflateRes = Z_OK;
while (deflateRes == Z_OK) {
if (strm.avail_out == 0) {
dest.append(tmpBuf, BUFSIZE);
strm.next_out = reinterpret_cast<uchar *>(tmpBuf);
strm.avail_out = BUFSIZE;
if (result != Z_OK) {
deflateEnd(&strm);
return {};
}
deflateRes = deflate(&strm, Z_FINISH);
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
strm.avail_out = BUFSIZE;
}
if (deflateRes != Z_STREAM_END)
return false;
// flush the rest from deflate
while (result != Z_STREAM_END) {
result = deflate(&strm, Z_FINISH);
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
strm.avail_out = BUFSIZE;
}
dest.append(tmpBuf, BUFSIZE - strm.avail_out);
deflateEnd(&strm);
return true;
if (ok) *ok = true;
return output;
}
bool Utils::Gzip::uncompress(QByteArray src, QByteArray &dest)
QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok)
{
dest.clear();
if (ok) *ok = false;
if (src.size() <= 4) {
qWarning("uncompress: Input data is truncated");
return false;
}
if (data.isEmpty())
return {};
const int BUFSIZE = 1024 * 1024;
char tmpBuf[BUFSIZE] = {0};
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 = static_cast<uint>(src.size());
strm.next_in = reinterpret_cast<uchar *>(src.data());
strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
strm.avail_in = uInt(data.size());
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
strm.avail_out = BUFSIZE;
const int windowBits = 15;
const int ENABLE_ZLIB_GZIP = 32;
// windowBits must be greater than or equal to the windowBits value provided to deflateInit2() while compressing
// Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection
int result = inflateInit2(&strm, (15 + 32));
if (result != Z_OK)
return {};
int ret = inflateInit2(&strm, windowBits | ENABLE_ZLIB_GZIP); // gzip decoding
if (ret != Z_OK)
return false;
QByteArray output;
// from lzbench, level 9 average compression ratio is: 31.92%, which decompression ratio is: 1 / 0.3192 = 3.13
output.reserve(data.size() * 3);
// run inflate()
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = reinterpret_cast<uchar *>(out);
// run inflate
while (true) {
result = inflate(&strm, Z_NO_FLUSH);
ret = inflate(&strm, Z_NO_FLUSH);
Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
switch (ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
inflateEnd(&strm);
return false;
if (result == Z_STREAM_END) {
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
break;
}
dest.append(out, CHUNK_SIZE - strm.avail_out);
}
while (!strm.avail_out);
if (result != Z_OK) {
inflateEnd(&strm);
return {};
}
output.append(tmpBuf, (BUFSIZE - strm.avail_out));
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
strm.avail_out = BUFSIZE;
}
// clean up and return
inflateEnd(&strm);
return true;
if (ok) *ok = true;
return output;
}

View File

@@ -36,8 +36,8 @@ namespace Utils
{
namespace Gzip
{
bool compress(QByteArray src, QByteArray &dest);
bool uncompress(QByteArray src, QByteArray &dest);
QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr);
QByteArray decompress(const QByteArray &data, bool *ok = nullptr);
}
}