AuroraRuntime/Source/Compression/Compression.cpp
Reece Wilson 8844e8fe64 [+] AuCrypto::BCrypt
> GetForcedMinRounds
> GenSalt
> HashPW
> HashPWEx
> CheckPassword
> CheckPasswordEx
[*] Refactor AuCompression APIs
[*] Clean up AuTryConstructs
[+] Internal compression API for compression based interceptors
[+] Root-level input stream arg check for all compression apis (harden)
[*] Clean up AuCompression code
[+] Solar Designer / OpenWall blowfish crypt
[*] BlowCrypt: accept length input parameter
[*] Split locale into 2 source files
[-] Ugly comment from Open.Win32.cpp. TODO: Readd later. Might warn on empty string bc it makes sense given, "." and "/" normalizes to nothing, and memory pre-idc-if-drops are dropped siliently.
2022-09-15 20:48:50 +01:00

77 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();
if (!AuTryResize(out, startingSize + inflatedLength))
{
return false;
}
auto ret = ZSTD_decompress(&out[startingSize], inflatedLength, source.ptr, source.length);
if (ZSTD_isError(ret))
{
return false;
}
out.resize(startingSize + ret);
read += AuUInt32(deflatedLength);
}
return true;
}
}