summaryrefslogtreecommitdiffstats
path: root/logic/GZip.cpp
blob: 96d58ea8b09338e5b63d89010149c203c977b59b (plain)
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
#include "GZip.h"
#include <zlib.h>
#include <QByteArray>

// HACK: workaround for terrible macro crap on Windows
int wrap_inflate (z_streamp strm, int flush)
{
	return inflate(strm, flush);
}

#ifdef inflate
	#undef inflate
#endif

bool GZip::inflate(const QByteArray &compressedBytes, QByteArray &uncompressedBytes)
{
	if (compressedBytes.size() == 0)
	{
		uncompressedBytes = compressedBytes;
		return true;
	}

	unsigned uncompLength = compressedBytes.size();
	unsigned half_length = compressedBytes.size() / 2;
	uncompressedBytes.clear();
	uncompressedBytes.resize(uncompLength);

	z_stream strm;
	strm.next_in = (Bytef *)compressedBytes.data();
	strm.avail_in = compressedBytes.size();
	strm.total_out = 0;
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;

	bool done = false;

	if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK)
	{
		return false;
	}

	while (!done)
	{
		// If our output buffer is too small
		if (strm.total_out >= uncompLength)
		{
			uncompressedBytes.resize(uncompLength + half_length);
			uncompLength += half_length;
		}

		strm.next_out = (Bytef *)(uncompressedBytes.data() + strm.total_out);
		strm.avail_out = uncompLength - strm.total_out;

		// Inflate another chunk.
		int err = wrap_inflate(&strm, Z_SYNC_FLUSH);
		if (err == Z_STREAM_END)
			done = true;
		else if (err != Z_OK)
		{
			break;
		}
	}

	if (inflateEnd(&strm) != Z_OK)
	{
		return false;
	}

	uncompressedBytes.resize(strm.total_out);
	return true;
}