1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00
Beef/BeefySysLib/util/Compression.cpp

126 lines
2.7 KiB
C++
Raw Normal View History

#include "Compression.h"
2021-11-14 18:01:37 -08:00
#include "third_party/zlib/zlib.h"
#include "TLSingleton.h"
#pragma warning(disable:4190)
USING_NS_BF;
2021-12-02 14:43:36 -08:00
static TLSingleton<Array<uint8>> gCompression_TLDataReturn;
2021-11-14 18:01:37 -08:00
bool Compression::Compress(Span<uint8> inData, Array<uint8>& outData)
{
outData.Reserve(128);
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
zs.avail_in = (int)inData.mSize;
zs.next_in = inData.mVals;
zs.next_out = outData.mVals;
zs.avail_out = outData.mAllocSize;
deflateInit(&zs, Z_BEST_COMPRESSION);
bool isDone = false;
bool hadError = false;
while (true)
{
bool isDone = zs.avail_in == 0;
int err = deflate(&zs, isDone ? Z_FINISH : Z_NO_FLUSH);
outData.mSize = (int)(zs.next_out - outData.mVals);
if (err < 0)
{
hadError = true;
break;
}
if ((isDone) && (err == Z_STREAM_END))
break;
if (zs.avail_out == 0)
{
outData.Reserve((int)outData.mAllocSize + (int)outData.mAllocSize / 2 + 1);
zs.next_out = outData.mVals + outData.mSize;
zs.avail_out = outData.mAllocSize - outData.mSize;
}
}
deflateEnd(&zs);
return !hadError;
}
bool Compression::Decompress(Span<uint8> inData, Array<uint8>& outData)
{
outData.Reserve(128);
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
zs.avail_in = (int)inData.mSize;
zs.next_in = inData.mVals;
zs.next_out = outData.mVals;
zs.avail_out = outData.mAllocSize;
2021-12-02 14:43:36 -08:00
inflateInit(&zs);
2021-11-14 18:01:37 -08:00
bool isDone = false;
bool hadError = false;
while (true)
{
bool isDone = zs.avail_in == 0;
int err = inflate(&zs, isDone ? Z_FINISH : Z_NO_FLUSH);
outData.mSize = (int)(zs.next_out - outData.mVals);
if (err < 0)
{
hadError = true;
break;
}
if ((isDone) && (err == Z_STREAM_END))
break;
if (zs.avail_out == 0)
{
outData.Reserve((int)outData.mAllocSize + (int)outData.mAllocSize / 2 + 1);
zs.next_out = outData.mVals + outData.mSize;
zs.avail_out = outData.mAllocSize - outData.mSize;
}
}
inflateEnd(&zs);
return !hadError;
}
//////////////////////////////////////////////////////////////////////////
2021-12-05 12:08:03 -08:00
BF_EXPORT bool BF_CALLTYPE Compression_Compress(void* ptr, intptr size, void** outPtr, intptr* outSize)
2021-12-02 14:43:36 -08:00
{
auto& outData = *gCompression_TLDataReturn.Get();
outData.Reserve(128);
2021-11-14 18:01:37 -08:00
if (!Compression::Compress(Span<uint8>((uint8*)ptr, size), outData))
2021-12-05 12:08:03 -08:00
return false;
*outPtr = outData.mVals;
*outSize = outData.mSize;
return true;
2021-11-14 18:01:37 -08:00
}
2021-12-05 12:08:03 -08:00
BF_EXPORT bool BF_CALLTYPE Compression_Decompress(void* ptr, intptr size, void** outPtr, intptr* outSize)
2021-11-14 18:01:37 -08:00
{
2021-12-02 14:43:36 -08:00
auto& outData = *gCompression_TLDataReturn.Get();
outData.Reserve(128);
2021-11-14 18:01:37 -08:00
if (!Compression::Decompress(Span<uint8>((uint8*)ptr, size), outData))
2021-12-05 12:08:03 -08:00
return false;
*outPtr = outData.mVals;
*outSize = outData.mSize;
return true;
2021-11-14 18:01:37 -08:00
}