mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-11 04:52:21 +02:00
zlib interface
This commit is contained in:
parent
c3644d15de
commit
345746d34d
3 changed files with 199 additions and 0 deletions
57
BeefLibs/Beefy2D/src/utils/Compression.bf
Normal file
57
BeefLibs/Beefy2D/src/utils/Compression.bf
Normal file
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace utils
|
||||
{
|
||||
class Compression
|
||||
{
|
||||
[CallingConvention(.Stdcall), CLink]
|
||||
extern static Span<uint8> Compression_Compress(void* ptr, int size);
|
||||
|
||||
[CallingConvention(.Stdcall), CLink]
|
||||
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)
|
||||
{
|
||||
var outSpan = Compression_Compress(inData.Ptr, inData.Length);
|
||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
||||
return .Err;
|
||||
outData.AddRange(outSpan);
|
||||
Compression_Free(outSpan.Ptr);
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
public static Result<void> Compress(Span<uint8> inData, String outData)
|
||||
{
|
||||
var outSpan = Compression_Compress(inData.Ptr, inData.Length);
|
||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
||||
return .Err;
|
||||
outData.Insert(outData.Length, StringView((.)outSpan.Ptr, outSpan.Length));
|
||||
Compression_Free(outSpan.Ptr);
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
public static Result<void> Decompress(Span<uint8> inData, List<uint8> outData)
|
||||
{
|
||||
var outSpan = Compression_Decompress(inData.Ptr, inData.Length);
|
||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
||||
return .Err;
|
||||
outData.AddRange(outSpan);
|
||||
Compression_Free(outSpan.Ptr);
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
public static Result<void> Decompress(Span<uint8> inData, String outData)
|
||||
{
|
||||
var outSpan = Compression_Decompress(inData.Ptr, inData.Length);
|
||||
if ((outSpan.Length == 0) && (inData.Length != 0))
|
||||
return .Err;
|
||||
outData.Insert(outData.Length, StringView((.)outSpan.Ptr, outSpan.Length));
|
||||
Compression_Free(outSpan.Ptr);
|
||||
return .Ok;
|
||||
}
|
||||
}
|
||||
}
|
126
BeefySysLib/util/Compress.cpp
Normal file
126
BeefySysLib/util/Compress.cpp
Normal file
|
@ -0,0 +1,126 @@
|
|||
#include "Compress.h"
|
||||
#include "third_party/zlib/zlib.h"
|
||||
#include "TLSingleton.h"
|
||||
|
||||
#pragma warning(disable:4190)
|
||||
|
||||
USING_NS_BF;
|
||||
|
||||
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;
|
||||
|
||||
inflateInit(&zs, Z_BEST_COMPRESSION);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
BF_EXPORT Span<uint8> BF_CALLTYPE Compression_Compress(void* ptr, int size)
|
||||
{
|
||||
Array<uint8> outData;
|
||||
if (!Compression::Compress(Span<uint8>((uint8*)ptr, size), outData))
|
||||
return Span<uint8>();
|
||||
uint8* outPtr = outData.mVals;
|
||||
outData.mVals = NULL;
|
||||
return Span<uint8>(outPtr, outData.mSize);
|
||||
}
|
||||
|
||||
BF_EXPORT Span<uint8> BF_CALLTYPE Compression_Decompress(void* ptr, int size)
|
||||
{
|
||||
Array<uint8> outData;
|
||||
if (!Compression::Decompress(Span<uint8>((uint8*)ptr, size), outData))
|
||||
return Span<uint8>();
|
||||
uint8* outPtr = outData.mVals;
|
||||
outData.mVals = NULL;
|
||||
return Span<uint8>(outPtr, outData.mSize);
|
||||
}
|
||||
|
||||
BF_EXPORT void BF_CALLTYPE Compression_Free(void* ptr)
|
||||
{
|
||||
delete ptr;
|
||||
}
|
16
BeefySysLib/util/Compress.h
Normal file
16
BeefySysLib/util/Compress.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include "../Common.h"
|
||||
#include "Array.h"
|
||||
#include "Span.h"
|
||||
|
||||
NS_BF_BEGIN;
|
||||
|
||||
class Compression
|
||||
{
|
||||
public:
|
||||
static bool Compress(Span<uint8> inData, Array<uint8>& outData);
|
||||
static bool Decompress(Span<uint8> inData, Array<uint8>& outData);
|
||||
};
|
||||
|
||||
NS_BF_END;
|
Loading…
Add table
Add a link
Reference in a new issue