/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: Compression.cpp Date: 2021-6-17 Author: Reece ***/ #include #include "Compression.hpp" #include "zstd.h" namespace Aurora::Compression { AUKN_SYM bool Compress(const void *buffer, AuUInt32 length, AuList &out, int compressionLevel ) { if (!AuTryResize(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 &in, AuList &out, int compressionLevel) { return Compress(in.data(), in.size(), out, compressionLevel); } AUKN_SYM bool Decompress(const void *buffer, AuUInt32 length, AuList &out) { AuUInt32 read = 0; while (read != length) { auto startPtr = reinterpret_cast(buffer) + read; auto deflatedLength = ZSTD_findFrameCompressedSize(startPtr, length - read); auto inflatedLength = ZSTD_getFrameContentSize(startPtr, 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, buffer, length); if (ZSTD_isError(ret)) { return false; } out.resize(startingSize + ret); read += deflatedLength; } return true; } AUKN_SYM bool Decompress(const AuList &in, AuList &out) { return Decompress(in.data(), in.size(), out); } }