mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-12 15:57:57 +00:00
Revise Utils::Gzip::compress code
Change signature Add ZLIB_CONST define to make z_stream.next_in const Cast to zlib defined type Bytef* Set memLevel to 9 in deflateInit2() for maximum performance Revise compression loop On returning false, free memory correctly by calling deflateEnd() Reserve space by the estimation of deflateBound()
This commit is contained in:
parent
94b496354b
commit
302c8ba850
@ -93,14 +93,15 @@ void Http::compressContent(Response &response)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// try compressing
|
// try compressing
|
||||||
QByteArray buf;
|
bool ok = false;
|
||||||
if (!Utils::Gzip::compress(response.content, buf))
|
const QByteArray compressedData = Utils::Gzip::compress(response.content, 6, &ok);
|
||||||
|
if (!ok)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// "Content-Encoding: gzip\r\n" is 24 bytes long
|
// "Content-Encoding: gzip\r\n" is 24 bytes long
|
||||||
if ((buf.size() + 24) >= contentSize)
|
if ((compressedData.size() + 24) >= contentSize)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
response.content = buf;
|
response.content = compressedData;
|
||||||
response.headers[HEADER_CONTENT_ENCODING] = QLatin1String("gzip");
|
response.headers[HEADER_CONTENT_ENCODING] = QLatin1String("gzip");
|
||||||
}
|
}
|
||||||
|
@ -27,66 +27,71 @@
|
|||||||
* exception statement from your version.
|
* exception statement from your version.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <QByteArray>
|
|
||||||
#include <zlib.h>
|
|
||||||
|
|
||||||
#include "gzip.h"
|
#include "gzip.h"
|
||||||
|
|
||||||
bool Utils::Gzip::compress(QByteArray src, QByteArray &dest)
|
#include <QByteArray>
|
||||||
{
|
|
||||||
static const int BUFSIZE = 128 * 1024;
|
|
||||||
char tmpBuf[BUFSIZE];
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
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;
|
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<uchar *>(src.data());
|
strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
|
||||||
strm.avail_in = src.length();
|
strm.avail_in = uInt(data.size());
|
||||||
strm.next_out = reinterpret_cast<uchar *>(tmpBuf);
|
strm.next_out = reinterpret_cast<Bytef *>(tmpBuf);
|
||||||
strm.avail_out = BUFSIZE;
|
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.
|
||||||
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)
|
QByteArray output;
|
||||||
return false;
|
output.reserve(deflateBound(&strm, data.size()));
|
||||||
|
|
||||||
while (strm.avail_in != 0) {
|
// feed to deflate
|
||||||
ret = deflate(&strm, Z_NO_FLUSH);
|
while (strm.avail_in > 0) {
|
||||||
if (ret != Z_OK)
|
result = deflate(&strm, Z_NO_FLUSH);
|
||||||
return false;
|
|
||||||
|
|
||||||
if (strm.avail_out == 0) {
|
if (result != Z_OK) {
|
||||||
dest.append(tmpBuf, BUFSIZE);
|
deflateEnd(&strm);
|
||||||
strm.next_out = reinterpret_cast<uchar *>(tmpBuf);
|
return {};
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
// flush the rest from deflate
|
||||||
return false;
|
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);
|
deflateEnd(&strm);
|
||||||
|
|
||||||
return true;
|
if (ok) *ok = true;
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Utils::Gzip::uncompress(QByteArray src, QByteArray &dest)
|
bool Utils::Gzip::uncompress(QByteArray src, QByteArray &dest)
|
||||||
|
@ -36,7 +36,7 @@ namespace Utils
|
|||||||
{
|
{
|
||||||
namespace Gzip
|
namespace Gzip
|
||||||
{
|
{
|
||||||
bool compress(QByteArray src, QByteArray &dest);
|
QByteArray compress(const QByteArray &data, int level = 6, bool *ok = nullptr);
|
||||||
bool uncompress(QByteArray src, QByteArray &dest);
|
bool uncompress(QByteArray src, QByteArray &dest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user