AuroraRuntime/Source/Compression/Compression.cpp

75 lines
1.7 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Compression.cpp
Date: 2021-6-17
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "Compression.hpp"
#include "zstd.h"
namespace Aurora::Compression
{
AUKN_SYM bool Compress(const void *buffer, AuUInt32 length, AuList<AuUInt8> &out, int compressionLevel )
{
if (!TryResize(out, length))
{
return false;
}
auto ret = ZSTD_compress(&out[0], out.size(), buffer, length, compressionLevel);
if (ZSTD_isError(ret))
{
return false;
}
out.resize(ret);
return true;
}
AUKN_SYM bool Compress(const AuList<AuUInt8> &in, AuList<AuUInt8> &out, int compressionLevel)
{
return Compress(in.data(), in.size(), out, compressionLevel);
}
AUKN_SYM bool Decompress(const void *buffer, AuUInt32 length, AuList<AuUInt8> &out)
{
auto inflatedLength = ZSTD_getFrameContentSize(buffer, length);
if (inflatedLength == ZSTD_CONTENTSIZE_ERROR)
{
return false;
}
if (inflatedLength == ZSTD_CONTENTSIZE_UNKNOWN)
{
return false;
}
if (ZSTD_isError(inflatedLength))
{
return false;
}
if (!TryResize(out, inflatedLength))
{
return false;
}
auto ret = ZSTD_decompress(&out[0], out.size(), buffer, length);
if (ZSTD_isError(ret))
{
return false;
}
out.resize(ret);
return true;
}
AUKN_SYM bool Decompress(const AuList<AuUInt8> &in, AuList<AuUInt8> &out)
{
return Decompress(in.data(), in.size(), out);
}
}