diff options
Diffstat (limited to 'include/gzip_helper.hpp')
-rw-r--r-- | include/gzip_helper.hpp | 85 |
1 files changed, 47 insertions, 38 deletions
diff --git a/include/gzip_helper.hpp b/include/gzip_helper.hpp index e14fc1b50b..be13809236 100644 --- a/include/gzip_helper.hpp +++ b/include/gzip_helper.hpp @@ -1,56 +1,65 @@ #pragma once #include <zlib.h> + #include <cstring> #include <string> inline bool gzipInflate(const std::string& compressedBytes, - std::string& uncompressedBytes) { - if (compressedBytes.empty()) { - uncompressedBytes = compressedBytes; - return true; - } - - uncompressedBytes.clear(); + std::string& uncompressedBytes) +{ + if (compressedBytes.empty()) + { + uncompressedBytes = compressedBytes; + return true; + } - unsigned half_length = compressedBytes.size() / 2; + uncompressedBytes.clear(); - z_stream strm{}; + unsigned half_length = compressedBytes.size() / 2; - // The following line is nolint because we're declaring away constness. - // It's not clear why the input buffers on zlib aren't const, so this is a - // bit of a cheat for the moment - strm.next_in = (Bytef*)compressedBytes.data(); // NOLINT - strm.avail_in = compressedBytes.size(); - strm.total_out = 0; - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; + z_stream strm{}; - bool done = false; + // The following line is nolint because we're declaring away constness. + // It's not clear why the input buffers on zlib aren't const, so this is a + // bit of a cheat for the moment + strm.next_in = (Bytef*)compressedBytes.data(); // NOLINT + strm.avail_in = compressedBytes.size(); + strm.total_out = 0; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; - if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK) { - return false; - } + bool done = false; - while (!done) { - // If our output buffer is too small - if (strm.total_out >= uncompressedBytes.size()) { - uncompressedBytes.resize(uncompressedBytes.size() + half_length); + if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK) + { + return false; } - strm.next_out = - (Bytef*)(uncompressedBytes.data() + strm.total_out); // NOLINT - strm.avail_out = - ((uLong)uncompressedBytes.size() - strm.total_out); // NOLINT - - // Inflate another chunk. - int err = inflate(&strm, Z_SYNC_FLUSH); - if (err == Z_STREAM_END) { - done = true; - } else if (err != Z_OK) { - break; + while (!done) + { + // If our output buffer is too small + if (strm.total_out >= uncompressedBytes.size()) + { + uncompressedBytes.resize(uncompressedBytes.size() + half_length); + } + + strm.next_out = + (Bytef*)(uncompressedBytes.data() + strm.total_out); // NOLINT + strm.avail_out = + ((uLong)uncompressedBytes.size() - strm.total_out); // NOLINT + + // Inflate another chunk. + int err = inflate(&strm, Z_SYNC_FLUSH); + if (err == Z_STREAM_END) + { + done = true; + } + else if (err != Z_OK) + { + break; + } } - } - return inflateEnd(&strm) == Z_OK; + return inflateEnd(&strm) == Z_OK; }
\ No newline at end of file |