-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGzipStreamDecompressor.h
More file actions
74 lines (59 loc) · 1.86 KB
/
GzipStreamDecompressor.h
File metadata and controls
74 lines (59 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef GZIPSTREAMDECOMPRESSOR_H
#define GZIPSTREAMDECOMPRESSOR_H
#include "Exceptions.h"
#include "Log.h"
// This class handles the decompression of GZ data, it accepts compressed input QByteArray data
// in chunks and produces QByteArray uncompressed data
class GzipStreamDecompressor
{
public:
GzipStreamDecompressor()
{
memset(&s_, 0, sizeof(s_));
int ret = inflateInit2(&s_, 16 + MAX_WBITS);
if (ret != Z_OK) THROW(ProgrammingException, "inflateInit2 failed");
}
~GzipStreamDecompressor()
{
inflateEnd(&s_);
}
bool feed(const QByteArray& chunk, QByteArray& out)
{
s_.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(chunk.constData()));
s_.avail_in = static_cast<uInt>(chunk.size());
uint8_t temp[64 * 1024];
while (s_.avail_in > 0)
{
s_.next_out = temp;
s_.avail_out = sizeof(temp);
int ret = inflate(&s_, Z_NO_FLUSH);
if (ret == Z_STREAM_END)
{
size_t produced = sizeof(temp) - s_.avail_out;
if (produced)
out.append(reinterpret_cast<char*>(temp), produced);
// Allow multi-member gzip
inflateReset(&s_);
continue;
}
if (ret != Z_OK && ret != Z_BUF_ERROR)
{
Log::error("inflate error: " + QString::number(ret) + ", error: " + s_.msg);
return false;
}
size_t produced = sizeof(temp) - s_.avail_out;
if (produced)
{
out.append(reinterpret_cast<char*>(temp), produced);
}
if (ret == Z_BUF_ERROR && produced == 0)
{
break;
}
}
return true;
}
private:
z_stream s_;
};
#endif // GZIPSTREAMDECOMPRESSOR_H