Reece Wilson
1ff9feb303
[+] Upcoming failure categories [+] Updated SysPushXXX prototypes [*] Restore bandwidth OnTick position for extrapolate (using current frame stats, ref to last) fractional lerps into the future with ref to average [*] Compression.cpp AuList<AuUInt8> upgrade was incomplete & could've been improved with modern apis
79 lines
2.0 KiB
C++
79 lines
2.0 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: Compression.cpp
|
|
Date: 2021-6-17
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "Compression.hpp"
|
|
|
|
#include "zstd.h"
|
|
|
|
namespace Aurora::Compression
|
|
{
|
|
AUKN_SYM bool Compress(const Memory::MemoryViewRead &source, Memory::ByteBuffer &out, int uCompressionLevel)
|
|
{
|
|
if (!AuTryResize(out, source.length))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
auto ret = ZSTD_compress(out.data(), out.size(), source.ptr, source.length, uCompressionLevel);
|
|
if (ZSTD_isError(ret))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
out.resize(ret);
|
|
return true;
|
|
}
|
|
|
|
AUKN_SYM bool Decompress(const Memory::MemoryViewRead &source, AuByteBuffer &out)
|
|
{
|
|
AuUInt32 read = 0;
|
|
|
|
while (read != source.length)
|
|
{
|
|
auto startPtr = reinterpret_cast<const AuUInt8 *>(source.ptr) + read;
|
|
auto deflatedLength = ZSTD_findFrameCompressedSize(startPtr, source.length - read);
|
|
auto inflatedLength = ZSTD_getFrameContentSize(startPtr, source.length - read);
|
|
|
|
if (inflatedLength == ZSTD_CONTENTSIZE_ERROR)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (inflatedLength == ZSTD_CONTENTSIZE_UNKNOWN)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (ZSTD_isError(inflatedLength))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
auto startingSize = out.size();
|
|
|
|
auto view = out.GetOrAllocateLinearWriteable(inflatedLength);
|
|
if (!view)
|
|
{
|
|
SysPushErrorMemory();
|
|
return false;
|
|
}
|
|
|
|
auto ret = ZSTD_decompress(view.ptr, inflatedLength, source.ptr, source.length);
|
|
if (ZSTD_isError(ret))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
out.writePtr = (AuUInt8 *)view.ptr + ret;
|
|
|
|
read += AuUInt32(deflatedLength);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |