2021-11-04 18:06:56 +00:00
|
|
|
// /***************************************************************************
|
|
|
|
|
// Aaru Data Preservation Suite
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : BZip2.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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-02-18 10:02:53 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2021-11-04 18:06:56 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using Ionic.BZip2;
|
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Compression;
|
|
|
|
|
|
|
|
|
|
/// <summary>Implements the BZIP2 compression algorithm</summary>
|
2021-11-04 18:06:56 +00:00
|
|
|
public class BZip2
|
|
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
|
2021-11-04 23:28:44 +00:00
|
|
|
public static bool IsSupported => true;
|
|
|
|
|
|
2021-11-04 18:06:56 +00:00
|
|
|
[DllImport("libAaru.Compression.Native", SetLastError = true)]
|
2022-03-15 01:37:37 +00:00
|
|
|
static extern int AARU_bzip2_decode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize);
|
2021-11-04 18:06:56 +00:00
|
|
|
|
|
|
|
|
[DllImport("libAaru.Compression.Native", SetLastError = true)]
|
2022-03-15 01:37:37 +00:00
|
|
|
static extern int AARU_bzip2_encode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize,
|
|
|
|
|
int blockSize100K);
|
2021-11-04 18:06:56 +00:00
|
|
|
|
|
|
|
|
/// <summary>Decodes a buffer compressed with BZIP2</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)
|
|
|
|
|
{
|
2022-11-15 15:58:43 +00:00
|
|
|
uint destinationSize = (uint)destination.Length;
|
2021-11-04 18:06:56 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Compresses a buffer using BZIP2</summary>
|
|
|
|
|
/// <param name="source">Data to compress</param>
|
|
|
|
|
/// <param name="destination">Buffer to store the compressed data</param>
|
2022-03-15 01:37:37 +00:00
|
|
|
/// <param name="blockSize100K">Block size in 100KiB units</param>
|
2021-11-04 18:06:56 +00:00
|
|
|
/// <returns></returns>
|
2022-03-15 01:37:37 +00:00
|
|
|
public static int EncodeBuffer(byte[] source, byte[] destination, int blockSize100K)
|
2021-11-04 18:06:56 +00:00
|
|
|
{
|
2022-11-15 15:58:43 +00:00
|
|
|
uint destinationSize = (uint)destination.Length;
|
2021-11-04 18:06:56 +00:00
|
|
|
|
|
|
|
|
if(Native.IsSupported)
|
|
|
|
|
{
|
2022-03-15 01:37:37 +00:00
|
|
|
AARU_bzip2_encode_buffer(destination, ref destinationSize, source, (uint)source.Length, blockSize100K);
|
2021-11-04 18:06:56 +00:00
|
|
|
|
|
|
|
|
return (int)destinationSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var cmpMs = new MemoryStream(source);
|
2022-03-15 01:37:37 +00:00
|
|
|
using var encStream = new BZip2OutputStream(new MemoryStream(destination), blockSize100K);
|
2021-11-04 18:06:56 +00:00
|
|
|
encStream.Write(source, 0, source.Length);
|
|
|
|
|
|
|
|
|
|
return source.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|