Created
May 29, 2025 09:06
-
-
Save mrexodia/2c7d3d17a6d23d5bfc1770f4ebc7f398 to your computer and use it in GitHub Desktop.
Simple ZLIB utility functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
// https://github.com/richgel999/miniz (MIT) | |
#include <miniz.h> | |
static std::vector<uint8_t> zlib_compress(const void *data, size_t size) { | |
uLongf compressed_size = compressBound(size); | |
std::vector<uint8_t> compressed(compressed_size); | |
int res = compress(compressed.data(), &compressed_size, (const unsigned char *)data, size); | |
if (res != Z_OK) { | |
return {}; | |
} | |
compressed.resize(compressed_size); | |
return compressed; | |
} | |
static std::vector<uint8_t> zlib_decompress(const void *data, size_t size) { | |
std::vector<uint8_t> decompressed(size * 2); | |
uLongf decompressed_size = decompressed.size(); | |
int res; | |
while ((res = uncompress(decompressed.data(), | |
&decompressed_size, | |
(const unsigned char *)data, | |
size)) == Z_BUF_ERROR) { | |
decompressed_size *= 2; | |
decompressed.resize(decompressed_size); | |
} | |
if (res != Z_OK) { | |
return {}; | |
} | |
decompressed.resize(decompressed_size); | |
return decompressed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment