2021-12-05 17:54:36 +00:00
|
|
|
#include "ZCompression.h"
|
|
|
|
|
2024-12-11 15:18:45 +00:00
|
|
|
#ifdef DARKFLAME_PLATFORM_WIN32
|
2024-12-05 19:45:21 +00:00
|
|
|
#include "zlib-ng.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-12-11 15:18:45 +00:00
|
|
|
// Yes, I know there is a "zlib compat mode", it doesn't work, it's really fucking dumb
|
|
|
|
|
|
|
|
#define z_stream zng_stream
|
|
|
|
|
|
|
|
#define z_deflateInit zng_deflateInit
|
|
|
|
#define z_deflate zng_deflate
|
|
|
|
#define z_deflateEnd zng_deflateEnd
|
|
|
|
|
|
|
|
#define z_inflateInit zng_inflateInit
|
|
|
|
#define z_inflate zng_inflate
|
|
|
|
#define z_inflateEnd zng_inflateEnd
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include "zlib.h"
|
|
|
|
#endif
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
namespace ZCompression {
|
|
|
|
int32_t GetMaxCompressedLength(int32_t nLenSrc) {
|
|
|
|
int32_t n16kBlocks = (nLenSrc + 16383) / 16384; // round up any fraction of a block
|
|
|
|
return (nLenSrc + 6 + (n16kBlocks * 5));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Compress(const uint8_t* abSrc, int32_t nLenSrc, uint8_t* abDst, int32_t nLenDst) {
|
2024-12-11 15:18:45 +00:00
|
|
|
|
|
|
|
z_stream zInfo = { 0 };
|
2021-12-05 17:54:36 +00:00
|
|
|
zInfo.total_in = zInfo.avail_in = nLenSrc;
|
|
|
|
zInfo.total_out = zInfo.avail_out = nLenDst;
|
|
|
|
zInfo.next_in = const_cast<Bytef*>(abSrc);
|
|
|
|
zInfo.next_out = abDst;
|
|
|
|
|
|
|
|
int nErr, nRet = -1;
|
2024-12-11 15:18:45 +00:00
|
|
|
nErr = z_deflateInit(&zInfo, Z_DEFAULT_COMPRESSION); // zlib function
|
2021-12-05 17:54:36 +00:00
|
|
|
if (nErr == Z_OK) {
|
2024-12-11 15:18:45 +00:00
|
|
|
nErr = z_deflate(&zInfo, Z_FINISH); // zlib function
|
2021-12-05 17:54:36 +00:00
|
|
|
if (nErr == Z_STREAM_END) {
|
|
|
|
nRet = zInfo.total_out;
|
|
|
|
}
|
|
|
|
}
|
2024-12-11 15:18:45 +00:00
|
|
|
z_deflateEnd(&zInfo); // zlib function
|
2021-12-05 17:54:36 +00:00
|
|
|
return(nRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Decompress(const uint8_t* abSrc, int32_t nLenSrc, uint8_t* abDst, int32_t nLenDst, int32_t& nErr) {
|
|
|
|
// Get the size of the decompressed data
|
2024-12-11 15:18:45 +00:00
|
|
|
z_stream zInfo = { 0 };
|
2021-12-05 17:54:36 +00:00
|
|
|
zInfo.total_in = zInfo.avail_in = nLenSrc;
|
|
|
|
zInfo.total_out = zInfo.avail_out = nLenDst;
|
|
|
|
zInfo.next_in = const_cast<Bytef*>(abSrc);
|
|
|
|
zInfo.next_out = abDst;
|
|
|
|
|
|
|
|
int nRet = -1;
|
2024-12-11 15:18:45 +00:00
|
|
|
nErr = z_inflateInit(&zInfo); // zlib function
|
2021-12-05 17:54:36 +00:00
|
|
|
if (nErr == Z_OK) {
|
2024-12-11 15:18:45 +00:00
|
|
|
nErr = z_inflate(&zInfo, Z_FINISH); // zlib function
|
2021-12-05 17:54:36 +00:00
|
|
|
if (nErr == Z_STREAM_END) {
|
|
|
|
nRet = zInfo.total_out;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2024-12-11 15:18:45 +00:00
|
|
|
z_inflateEnd(&zInfo); // zlib function
|
2021-12-05 17:54:36 +00:00
|
|
|
return(nRet);
|
|
|
|
}
|
2022-07-16 23:24:16 +00:00
|
|
|
}
|
|
|
|
|