Files
Aaru/Aaru.Compression/ZSTD.cs

70 lines
3.4 KiB
C#
Raw Permalink Normal View History

2021-11-04 18:40:08 +00:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ZSTD.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2021-11-04 18:40:08 +00:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
using System.Runtime.InteropServices;
namespace Aaru.Compression;
2022-03-15 01:37:37 +00:00
// ReSharper disable once InconsistentNaming
/// <summary>Implements the zstandard compression algorithm</summary>
public partial class ZSTD
2021-11-04 18:40:08 +00:00
{
2022-03-07 07:36:44 +00:00
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => Native.IsSupported;
[LibraryImport("libAaru.Compression.Native", SetLastError = true)]
private static partial nuint AARU_zstd_decode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize);
2021-11-04 18:40:08 +00:00
[LibraryImport("libAaru.Compression.Native", SetLastError = true)]
private static partial nuint AARU_zstd_encode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize, int compressionLevel);
2021-11-04 18:40:08 +00:00
/// <summary>Decodes a buffer compressed with ZSTD</summary>
/// <param name="source">Encoded buffer</param>
/// <param name="destination">Buffer where to write the decoded data</param>
/// <returns>The number of decoded bytes</returns>
public static int DecodeBuffer(byte[] source, byte[] destination) =>
(int)(Native.IsSupported
? AARU_zstd_decode_buffer(destination, (nuint)destination.Length, source, (nuint)source.Length)
: 0);
2021-11-04 18:40:08 +00:00
/// <summary>Compresses a buffer using ZSTD</summary>
/// <param name="source">Data to compress</param>
/// <param name="destination">Buffer to store the compressed data</param>
2022-03-18 01:32:22 +00:00
/// <param name="compressionLevel">Compression level</param>
/// <returns>Length of the compressed data</returns>
2021-11-04 18:40:08 +00:00
public static int EncodeBuffer(byte[] source, byte[] destination, int compressionLevel) =>
(int)(Native.IsSupported
2024-05-01 04:05:22 +01:00
? AARU_zstd_encode_buffer(destination,
(nuint)destination.Length,
source,
(nuint)source.Length,
compressionLevel)
: 0);
2021-11-04 18:40:08 +00:00
}