From 72c870528fdaf6680c32ac6e99acf2da72deafa7 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 4 Nov 2021 18:06:56 +0000 Subject: [PATCH] Use native version for BZIP2, falling back to Ionic. --- Aaru.Compression/Aaru.Compression.csproj | 2 + Aaru.Compression/BZip2.cs | 87 ++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 Aaru.Compression/BZip2.cs diff --git a/Aaru.Compression/Aaru.Compression.csproj b/Aaru.Compression/Aaru.Compression.csproj index 02b8a5d1c..ad2b8345e 100644 --- a/Aaru.Compression/Aaru.Compression.csproj +++ b/Aaru.Compression/Aaru.Compression.csproj @@ -72,6 +72,7 @@ + @@ -90,6 +91,7 @@ + diff --git a/Aaru.Compression/BZip2.cs b/Aaru.Compression/BZip2.cs new file mode 100644 index 000000000..e454d5993 --- /dev/null +++ b/Aaru.Compression/BZip2.cs @@ -0,0 +1,87 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Filename : BZip2.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-2021 Natalia Portillo +// ****************************************************************************/ + +using System.IO; +using System.Runtime.InteropServices; +using Ionic.BZip2; + +namespace Aaru.Compression; + +public class BZip2 +{ + [DllImport("libAaru.Compression.Native", SetLastError = true)] + static extern int AARU_bzip2_decode_buffer(byte[] dst_buffer, ref uint dst_size, byte[] src_buffer, uint src_size); + + [DllImport("libAaru.Compression.Native", SetLastError = true)] + static extern int AARU_bzip2_encode_buffer(byte[] dst_buffer, ref uint dst_size, byte[] src_buffer, uint src_size, + int blockSize100k); + + /// Decodes a buffer compressed with BZIP2 + /// Encoded buffer + /// Buffer where to write the decoded data + /// The number of decoded bytes + public static int DecodeBuffer(byte[] source, byte[] destination) + { + uint destinationSize = (uint)destination.Length; + + if(Native.IsSupported) + { + AARU_bzip2_decode_buffer(destination, ref destinationSize, source, (uint)source.Length); + + return (int)destinationSize; + } + + using var cmpMs = new MemoryStream(source); + using var decStream = new BZip2InputStream(cmpMs, false); + + return decStream.Read(destination, 0, destination.Length); + } + + /// Compresses a buffer using BZIP2 + /// Data to compress + /// Buffer to store the compressed data + /// Block size in 100KiB units + /// + public static int EncodeBuffer(byte[] source, byte[] destination, int blockSize100k) + { + uint destinationSize = (uint)destination.Length; + + if(Native.IsSupported) + { + AARU_bzip2_encode_buffer(destination, ref destinationSize, source, (uint)source.Length, blockSize100k); + + return (int)destinationSize; + } + + using var cmpMs = new MemoryStream(source); + using var encStream = new BZip2OutputStream(new MemoryStream(destination), blockSize100k); + encStream.Write(source, 0, source.Length); + + return source.Length; + } +} \ No newline at end of file