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.
38 #define ZLIB_CONST // make z_stream.next_in const
42 QByteArray
Utils::Gzip::compress(const QByteArray
&data
, const int level
, bool *ok
)
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
)
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
);
75 ret
.truncate(strm
.total_out
);
82 QByteArray
Utils::Gzip::decompress(const QByteArray
&data
, bool *ok
)
89 const int BUFSIZE
= 1024 * 1024;
90 std::vector
<char> tmpBuf(BUFSIZE
);
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));
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);
114 result
= inflate(&strm
, Z_NO_FLUSH
);
116 if (result
== Z_STREAM_END
)
118 output
.append(tmpBuf
.data(), (BUFSIZE
- strm
.avail_out
));
128 output
.append(tmpBuf
.data(), (BUFSIZE
- strm
.avail_out
));
129 strm
.next_out
= reinterpret_cast<Bytef
*>(tmpBuf
.data());
130 strm
.avail_out
= BUFSIZE
;