Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / utils / gzip.cpp
blob2375850efe0fefebd8219d22ef119f270c868ea2
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "gzip.h"
32 #include <vector>
34 #include <QtAssert>
35 #include <QByteArray>
37 #ifndef ZLIB_CONST
38 #define ZLIB_CONST // make z_stream.next_in const
39 #endif
40 #include <zlib.h>
42 QByteArray Utils::Gzip::compress(const QByteArray &data, const int level, bool *ok)
44 if (ok)
45 *ok = false;
47 if (data.isEmpty())
48 return {};
50 z_stream strm {};
51 strm.zalloc = Z_NULL;
52 strm.zfree = Z_NULL;
53 strm.opaque = Z_NULL;
54 strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
55 strm.avail_in = static_cast<uInt>(data.size());
57 // windowBits = 15 + 16 to enable gzip
58 // From the zlib manual: windowBits can also be greater than 15 for optional gzip encoding. Add 16 to windowBits
59 // to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper.
60 const int initResult = deflateInit2(&strm, level, Z_DEFLATED, (15 + 16), 9, Z_DEFAULT_STRATEGY);
61 if (initResult != Z_OK)
62 return {};
64 QByteArray ret {static_cast<qsizetype>(deflateBound(&strm, data.size())), Qt::Uninitialized};
65 strm.next_out = reinterpret_cast<Bytef *>(ret.data());
66 strm.avail_out = ret.size();
68 // From the zlib manual: Z_FINISH can be used in the first deflate call after deflateInit if all the compression
69 // is to be done in a single step. In order to complete in one call, avail_out must be at least the value
70 // returned by deflateBound (see below). Then deflate is guaranteed to return Z_STREAM_END.
71 const int deflateResult = deflate(&strm, Z_FINISH);
72 Q_ASSERT(deflateResult == Z_STREAM_END);
74 deflateEnd(&strm);
75 ret.truncate(strm.total_out);
77 if (ok)
78 *ok = true;
79 return ret;
82 QByteArray Utils::Gzip::decompress(const QByteArray &data, bool *ok)
84 if (ok) *ok = false;
86 if (data.isEmpty())
87 return {};
89 const int BUFSIZE = 1024 * 1024;
90 std::vector<char> tmpBuf(BUFSIZE);
92 z_stream strm {};
93 strm.zalloc = Z_NULL;
94 strm.zfree = Z_NULL;
95 strm.opaque = Z_NULL;
96 strm.next_in = reinterpret_cast<const Bytef *>(data.constData());
97 strm.avail_in = uInt(data.size());
98 strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
99 strm.avail_out = BUFSIZE;
101 // windowBits must be greater than or equal to the windowBits value provided to deflateInit2() while compressing
102 // Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection
103 int result = inflateInit2(&strm, (15 + 32));
104 if (result != Z_OK)
105 return {};
107 QByteArray output;
108 // from lzbench, level 9 average compression ratio is: 31.92%, which decompression ratio is: 1 / 0.3192 = 3.13
109 output.reserve(data.size() * 3);
111 // run inflate
112 while (true)
114 result = inflate(&strm, Z_NO_FLUSH);
116 if (result == Z_STREAM_END)
118 output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out));
119 break;
122 if (result != Z_OK)
124 inflateEnd(&strm);
125 return {};
128 output.append(tmpBuf.data(), (BUFSIZE - strm.avail_out));
129 strm.next_out = reinterpret_cast<Bytef *>(tmpBuf.data());
130 strm.avail_out = BUFSIZE;
133 inflateEnd(&strm);
135 if (ok) *ok = true;
136 return output;