// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : ZSTD.cs // Author(s) : Natalia Portillo // // Component : Compression algorithms. // // --[ License ] -------------------------------------------------------------- // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, see . // // ---------------------------------------------------------------------------- // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ using System.Runtime.InteropServices; namespace Aaru.Compression; // ReSharper disable once InconsistentNaming /// Implements the zstandard compression algorithm public class ZSTD { /// Set to true if this algorithm is supported, false otherwise. public static bool IsSupported => Native.IsSupported; [DllImport("libAaru.Compression.Native", SetLastError = true)] static extern nuint AARU_zstd_decode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize); [DllImport("libAaru.Compression.Native", SetLastError = true)] static extern nuint AARU_zstd_encode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize, int compressionLevel); /// Decodes a buffer compressed with ZSTD /// Encoded buffer /// Buffer where to write the decoded data /// The number of decoded bytes public static int DecodeBuffer(byte[] source, byte[] destination) => (int)(Native.IsSupported ? AARU_zstd_decode_buffer(destination, (nuint)destination.Length, source, (nuint)source.Length) : 0); /// Compresses a buffer using ZSTD /// Data to compress /// Buffer to store the compressed data /// Compression level /// Length of the compressed data public static int EncodeBuffer(byte[] source, byte[] destination, int compressionLevel) => (int)(Native.IsSupported ? AARU_zstd_encode_buffer(destination, (nuint)destination.Length, source, (nuint)source.Length, compressionLevel) : 0); }