7#include "kgzipfilter.h"
8#include "loggingcategory.h"
25 : headerWritten(false)
26 , footerWritten(false)
30 , isInitialized(false)
32 zStream.zalloc =
static_cast<alloc_func
>(
nullptr);
33 zStream.zfree =
static_cast<free_func
>(
nullptr);
34 zStream.opaque =
static_cast<voidpf
>(
nullptr);
46KGzipFilter::KGzipFilter()
51KGzipFilter::~KGzipFilter()
58 switch (filterFlags()) {
71 if (d->isInitialized) {
74 d->zStream.next_in = Z_NULL;
75 d->zStream.avail_in = 0;
77 const int windowBits = (flag == RawDeflate) ? -MAX_WBITS
78 : (flag == GZipHeader) ? MAX_WBITS + 32
80 const int result = inflateInit2(&d->zStream, windowBits);
86 int result = deflateInit2(&d->zStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
97 d->headerWritten =
false;
98 d->footerWritten =
false;
99 d->isInitialized =
true;
111 int result = inflateEnd(&d->zStream);
112 if (result != Z_OK) {
117 int result = deflateEnd(&d->zStream);
118 if (result != Z_OK) {
123 d->isInitialized =
false;
130 int result = inflateReset(&d->zStream);
131 if (result != Z_OK) {
136 int result = deflateReset(&d->zStream);
137 if (result != Z_OK) {
141 d->headerWritten =
false;
142 d->footerWritten =
false;
152 qCDebug(KArchiveLog) <<
"avail=" << d->zStream.avail_in;
155 d->compressed =
false;
156 const Bytef *p = d->zStream.next_in;
157 int i = d->zStream.avail_in;
174 d->compressed =
true;
176 qCDebug(KArchiveLog) <<
"header OK";
182#define put_short(w) \
183 *p++ = uchar((w)&0xff); \
184 *p++ = uchar(ushort(w) >> 8);
188 put_short((n)&0xffff); \
189 put_short((ulong(n)) >> 16);
193 Bytef *p = d->zStream.next_out;
194 int i = d->zStream.avail_out;
199 put_long(time(
nullptr));
203 uint len = fileName.
length();
204 for (uint j = 0; j < len; ++j) {
208 int headerSize = p - d->zStream.next_out;
211 d->crc = crc32(0L,
nullptr, 0);
212 d->zStream.next_out = p;
213 d->zStream.avail_out = i;
214 d->headerWritten =
true;
218void KGzipFilter::writeFooter()
220 Q_ASSERT(d->headerWritten);
221 Q_ASSERT(!d->footerWritten);
222 Bytef *p = d->zStream.next_out;
223 int i = d->zStream.avail_out;
227 put_long(d->zStream.total_in);
228 i -= p - d->zStream.next_out;
229 d->zStream.next_out = p;
230 d->zStream.avail_out = i;
231 d->footerWritten =
true;
236 d->zStream.avail_out = maxlen;
237 d->zStream.next_out =
reinterpret_cast<Bytef *
>(data);
242 qCDebug(KArchiveLog) <<
"avail_in=" << size;
244 d->zStream.avail_in = size;
245 d->zStream.next_in =
reinterpret_cast<Bytef *
>(
const_cast<char *
>(data));
249 return d->zStream.avail_in;
253 return d->zStream.avail_out;
261 if (d->zStream.avail_in > 0) {
262 int n = (d->zStream.avail_in < d->zStream.avail_out) ? d->zStream.avail_in : d->zStream.avail_out;
263 memcpy(d->zStream.next_out, d->zStream.next_in, n);
264 d->zStream.avail_out -= n;
265 d->zStream.next_in += n;
266 d->zStream.avail_in -= n;
267 return KFilterBase::Ok;
269 return KFilterBase::End;
278 return KFilterBase::Error;
281 return KFilterBase::Error;
286 if (!d->compressed) {
287 return uncompress_noop();
292 qCDebug(KArchiveLog) <<
" next_in=" << d->zStream.next_in;
295 while (d->zStream.avail_in > 0) {
296 int result = inflate(&d->zStream, Z_SYNC_FLUSH);
299 qCDebug(KArchiveLog) <<
" -> inflate returned " << result;
301 qCDebug(KArchiveLog) <<
" next_in=" << d->zStream.next_in;
304 if (result == Z_OK) {
305 return KFilterBase::Ok;
309 if (result != Z_STREAM_END) {
310 return KFilterBase::Error;
314 if (d->zStream.avail_in == 0) {
315 return KFilterBase::End;
319 Bytef *data = d->zStream.next_in;
320 uInt size = d->zStream.avail_in;
323 if (!
init(d->mode)) {
324 return KFilterBase::End;
328 d->zStream.next_in = data;
329 d->zStream.avail_in = size;
332 return KFilterBase::End;
337 Q_ASSERT(d->compressed);
340 const Bytef *p = d->zStream.next_in;
341 ulong len = d->zStream.avail_in;
345 const int result = deflate(&d->zStream, finish ? Z_FINISH : Z_NO_FLUSH);
346 if (result != Z_OK && result != Z_STREAM_END) {
349 if (d->headerWritten) {
351 d->crc = crc32(d->crc, p, len - d->zStream.avail_in);
353 KGzipFilter::Result callerResult = result == Z_OK ? KFilterBase::Ok : (Z_STREAM_END ? KFilterBase::End : KFilterBase::Error);
355 if (result == Z_STREAM_END && d->headerWritten && !d->footerWritten) {
356 if (d->zStream.avail_out >= 8 ) {
362 callerResult = KFilterBase::Ok;
Internal class used by KCompressionDevice.
bool writeHeader(const QByteArray &fileName) override
void setInBuffer(const char *data, uint size) override
Result compress(bool finish) override
int inBufferAvailable() const override
void setOutBuffer(char *data, uint maxlen) override
int outBufferAvailable() const override
bool readHeader() override
int mode() const override
bool terminate() override
bool init(int mode) override
Result uncompress() override
qsizetype length() const const
QString number(double n, char format, int precision)