/*** 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 Memory::MemoryViewRead &source, Memory::ByteBuffer &out, int compressionLevel) { if (!AuTryResize(out, source.length)) { return false; } auto ret = ZSTD_compress(out.data(), out.size(), source.ptr, source.length, compressionLevel); 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(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 += deflatedLength; } return true; } }