mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-14 14:24:10 +02:00
Zero-sized compression fixes
This commit is contained in:
parent
46fbba9712
commit
61222f8525
2 changed files with 10 additions and 18 deletions
|
@ -11,16 +11,12 @@ namespace utils
|
||||||
[CallingConvention(.Stdcall), CLink]
|
[CallingConvention(.Stdcall), CLink]
|
||||||
extern static Span<uint8> Compression_Decompress(void* ptr, int size);
|
extern static Span<uint8> Compression_Decompress(void* ptr, int size);
|
||||||
|
|
||||||
[CallingConvention(.Stdcall), CLink]
|
|
||||||
extern static void Compression_Free(void* ptr);
|
|
||||||
|
|
||||||
public static Result<void> Compress(Span<uint8> inData, List<uint8> outData)
|
public static Result<void> Compress(Span<uint8> inData, List<uint8> outData)
|
||||||
{
|
{
|
||||||
var outSpan = Compression_Compress(inData.Ptr, inData.Length);
|
var outSpan = Compression_Compress(inData.Ptr, inData.Length);
|
||||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
if ((outSpan.Length == 0) && (inData.Length != 0))
|
||||||
return .Err;
|
return .Err;
|
||||||
outData.AddRange(outSpan);
|
outData.AddRange(outSpan);
|
||||||
Compression_Free(outSpan.Ptr);
|
|
||||||
return .Ok;
|
return .Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,27 +26,24 @@ namespace utils
|
||||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
if ((outSpan.Length == 0) && (inData.Length != 0))
|
||||||
return .Err;
|
return .Err;
|
||||||
outData.Insert(outData.Length, StringView((.)outSpan.Ptr, outSpan.Length));
|
outData.Insert(outData.Length, StringView((.)outSpan.Ptr, outSpan.Length));
|
||||||
Compression_Free(outSpan.Ptr);
|
|
||||||
return .Ok;
|
return .Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<void> Decompress(Span<uint8> inData, List<uint8> outData)
|
public static Result<void> Decompress(Span<uint8> inData, List<uint8> outData)
|
||||||
{
|
{
|
||||||
var outSpan = Compression_Decompress(inData.Ptr, inData.Length);
|
var outSpan = Compression_Decompress(inData.Ptr, inData.Length);
|
||||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
if (outSpan == default)
|
||||||
return .Err;
|
return .Err;
|
||||||
outData.AddRange(outSpan);
|
outData.AddRange(outSpan);
|
||||||
Compression_Free(outSpan.Ptr);
|
|
||||||
return .Ok;
|
return .Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result<void> Decompress(Span<uint8> inData, String outData)
|
public static Result<void> Decompress(Span<uint8> inData, String outData)
|
||||||
{
|
{
|
||||||
var outSpan = Compression_Decompress(inData.Ptr, inData.Length);
|
var outSpan = Compression_Decompress(inData.Ptr, inData.Length);
|
||||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
if (outSpan == default)
|
||||||
return .Err;
|
return .Err;
|
||||||
outData.Insert(outData.Length, StringView((.)outSpan.Ptr, outSpan.Length));
|
outData.Insert(outData.Length, StringView((.)outSpan.Ptr, outSpan.Length));
|
||||||
Compression_Free(outSpan.Ptr);
|
|
||||||
return .Ok;
|
return .Ok;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
USING_NS_BF;
|
USING_NS_BF;
|
||||||
|
|
||||||
|
static TLSingleton<Array<uint8>> gCompression_TLDataReturn;
|
||||||
|
|
||||||
bool Compression::Compress(Span<uint8> inData, Array<uint8>& outData)
|
bool Compression::Compress(Span<uint8> inData, Array<uint8>& outData)
|
||||||
{
|
{
|
||||||
outData.Reserve(128);
|
outData.Reserve(128);
|
||||||
|
@ -65,7 +67,7 @@ bool Compression::Decompress(Span<uint8> inData, Array<uint8>& outData)
|
||||||
zs.next_out = outData.mVals;
|
zs.next_out = outData.mVals;
|
||||||
zs.avail_out = outData.mAllocSize;
|
zs.avail_out = outData.mAllocSize;
|
||||||
|
|
||||||
inflateInit(&zs, Z_BEST_COMPRESSION);
|
inflateInit(&zs);
|
||||||
|
|
||||||
bool isDone = false;
|
bool isDone = false;
|
||||||
bool hadError = false;
|
bool hadError = false;
|
||||||
|
@ -101,8 +103,9 @@ bool Compression::Decompress(Span<uint8> inData, Array<uint8>& outData)
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
BF_EXPORT Span<uint8> BF_CALLTYPE Compression_Compress(void* ptr, int size)
|
BF_EXPORT Span<uint8> BF_CALLTYPE Compression_Compress(void* ptr, int size)
|
||||||
{
|
{
|
||||||
Array<uint8> outData;
|
auto& outData = *gCompression_TLDataReturn.Get();
|
||||||
|
outData.Reserve(128);
|
||||||
if (!Compression::Compress(Span<uint8>((uint8*)ptr, size), outData))
|
if (!Compression::Compress(Span<uint8>((uint8*)ptr, size), outData))
|
||||||
return Span<uint8>();
|
return Span<uint8>();
|
||||||
uint8* outPtr = outData.mVals;
|
uint8* outPtr = outData.mVals;
|
||||||
|
@ -112,15 +115,11 @@ BF_EXPORT Span<uint8> BF_CALLTYPE Compression_Compress(void* ptr, int size)
|
||||||
|
|
||||||
BF_EXPORT Span<uint8> BF_CALLTYPE Compression_Decompress(void* ptr, int size)
|
BF_EXPORT Span<uint8> BF_CALLTYPE Compression_Decompress(void* ptr, int size)
|
||||||
{
|
{
|
||||||
Array<uint8> outData;
|
auto& outData = *gCompression_TLDataReturn.Get();
|
||||||
|
outData.Reserve(128);
|
||||||
if (!Compression::Decompress(Span<uint8>((uint8*)ptr, size), outData))
|
if (!Compression::Decompress(Span<uint8>((uint8*)ptr, size), outData))
|
||||||
return Span<uint8>();
|
return Span<uint8>();
|
||||||
uint8* outPtr = outData.mVals;
|
uint8* outPtr = outData.mVals;
|
||||||
outData.mVals = NULL;
|
outData.mVals = NULL;
|
||||||
return Span<uint8>(outPtr, outData.mSize);
|
return Span<uint8>(outPtr, outData.mSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
BF_EXPORT void BF_CALLTYPE Compression_Free(void* ptr)
|
|
||||||
{
|
|
||||||
delete ptr;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue