zlib compression fix

This commit is contained in:
Phillip Stephens 2025-01-02 03:39:10 -08:00
parent e1cac75d80
commit a681cef0a4

View File

@ -10,8 +10,7 @@
namespace athena::io::Compression { namespace athena::io::Compression {
atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen) { void zlibInitZStrm(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen, z_stream& strm) {
z_stream strm = {};
strm.avail_in = srcLen; strm.avail_in = srcLen;
strm.avail_out = dstLen; strm.avail_out = dstLen;
strm.next_in = const_cast<Bytef*>(src); strm.next_in = const_cast<Bytef*>(src);
@ -19,53 +18,34 @@ atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
strm.opaque = Z_NULL; strm.opaque = Z_NULL;
}
atInt32 ret; atInt32 zlibInflate(z_stream& strm, atInt32 bits) {
// 15 window bits, and the | 16 tells zlib to to detect if using gzip or zlib atInt32 ret = inflateInit2(&strm, bits);
ret = inflateInit2(&strm, MAX_WBITS | 16);
if (ret == Z_OK) { if (ret == Z_OK) {
ret = inflate(&strm, Z_FINISH); ret = inflate(&strm, Z_FINISH);
if (ret == Z_STREAM_END) { if (ret == Z_STREAM_END) {
ret = strm.total_out; ret = strm.total_out;
} }
} }
inflateEnd(&strm);
return ret; return ret;
} }
atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen) {
atInt32 compressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen) {
z_stream strm = {}; z_stream strm = {};
strm.total_in = strm.avail_in = srcLen; zlibInitZStrm(src, srcLen, dst, dstLen, strm);
strm.total_out = strm.avail_out = dstLen;
strm.next_in = (Bytef*)src;
strm.next_out = (Bytef*)dst;
strm.zalloc = Z_NULL; // 15 window bits, and the | 16 tells zlib to to detect if using gzip or zlib
strm.zfree = Z_NULL; atInt32 ret = zlibInflate(strm, MAX_WBITS | 16);
strm.opaque = Z_NULL; if (ret != Z_STREAM_END) {
// Try again without gzip
atInt32 err = -1; zlibInitZStrm(src, srcLen, dst, dstLen, strm);
atInt32 ret = -1; ret = zlibInflate(strm, MAX_WBITS);
if (ret == Z_STREAM_END) {
err = deflateInit(&strm, Z_BEST_COMPRESSION);
if (err == Z_OK) {
err = deflate(&strm, Z_FINISH);
if (err == Z_STREAM_END)
ret = strm.total_out; ret = strm.total_out;
else {
deflateEnd(&strm);
return err;
} }
} else { } else {
deflateEnd(&strm); ret = strm.total_out;
return err;
} }
inflateEnd(&strm);
deflateEnd(&strm);
return ret; return ret;
} }