// /*************************************************************************** // 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-2022 Natalia Portillo // ****************************************************************************/ using System.IO; using System.Runtime.InteropServices; using Ionic.BZip2; namespace Aaru.Compression; /// Implements the BZIP2 compression algorithm public class BZip2 { /// Set to true if this algorithm is supported, false otherwise. public static bool IsSupported => true; [DllImport("libAaru.Compression.Native", SetLastError = true)] static extern int AARU_bzip2_decode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize); [DllImport("libAaru.Compression.Native", SetLastError = true)] static extern int AARU_bzip2_encode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize, 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; } }