diff options
author | Petr Mrázek <peterix@gmail.com> | 2015-08-18 02:25:24 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2015-08-18 08:51:12 +0200 |
commit | 96fdaebb5c8c8902c98c1fb43e755cf90fc15198 (patch) | |
tree | bb4e1ace6bb0800a5991884d5f07b41267699283 /logic/GZip.cpp | |
parent | 4e3af265dad57a27af4051cb574fb90539d287d0 (diff) | |
download | MultiMC-96fdaebb5c8c8902c98c1fb43e755cf90fc15198.tar MultiMC-96fdaebb5c8c8902c98c1fb43e755cf90fc15198.tar.gz MultiMC-96fdaebb5c8c8902c98c1fb43e755cf90fc15198.tar.lz MultiMC-96fdaebb5c8c8902c98c1fb43e755cf90fc15198.tar.xz MultiMC-96fdaebb5c8c8902c98c1fb43e755cf90fc15198.zip |
GH-926 implement log cleaning functionality
Also adds gzip compressed log support
Diffstat (limited to 'logic/GZip.cpp')
-rw-r--r-- | logic/GZip.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/logic/GZip.cpp b/logic/GZip.cpp new file mode 100644 index 00000000..96d58ea8 --- /dev/null +++ b/logic/GZip.cpp @@ -0,0 +1,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; +} |