diff --git a/Aaru.Compression/Aaru.Compression.csproj b/Aaru.Compression/Aaru.Compression.csproj index 5549d9088..03941499b 100644 --- a/Aaru.Compression/Aaru.Compression.csproj +++ b/Aaru.Compression/Aaru.Compression.csproj @@ -74,6 +74,7 @@ + @@ -94,6 +95,7 @@ + diff --git a/Aaru.Compression/LZMA.cs b/Aaru.Compression/LZMA.cs new file mode 100644 index 000000000..c366b4e05 --- /dev/null +++ b/Aaru.Compression/LZMA.cs @@ -0,0 +1,100 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Filename : LZMA.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 SharpCompress.Compressors.LZMA; + +namespace Aaru.Compression; + +public class LZMA +{ + [DllImport("libAaru.Compression.Native", SetLastError = true)] + static extern int AARU_lzma_decode_buffer(byte[] dst_buffer, ref nuint dst_size, byte[] src_buffer, + ref nuint src_size, byte[] props, nuint propsSize); + + [DllImport("libAaru.Compression.Native", SetLastError = true)] + static extern int AARU_lzma_encode_buffer(byte[] dst_buffer, ref nuint dst_size, byte[] src_buffer, nuint src_size, + byte[] outProps, ref nuint outPropsSize, int level, uint dictSize, int lc, + int lp, int pb, int fb, int numThreads); + + /// Decodes a buffer compressed with LZMA + /// Encoded buffer + /// Buffer where to write the decoded data + /// The number of decoded bytes + public static int DecodeBuffer(byte[] source, byte[] destination, byte[] properties) + { + if(Native.IsSupported) + { + var srcSize = (nuint)source.Length; + var dstSize = (nuint)destination.Length; + + AARU_lzma_decode_buffer(destination, ref dstSize, source, ref srcSize, properties, + (nuint)properties.Length); + + return (int)dstSize; + } + + using var cmpMs = new MemoryStream(source); + using var lzmaBlock = new LzmaStream(properties, cmpMs); + lzmaBlock.Read(destination, 0, destination.Length); + + return destination.Length; + } + + /// Compresses a buffer using BZIP2 + /// Data to compress + /// Buffer to store the compressed data + /// + public static int EncodeBuffer(byte[] source, byte[] destination, out byte[] properties, int level, uint dictSize, + int lc, int lp, int pb, int fb) + { + if(Native.IsSupported) + { + properties = new byte[5]; + var dstSize = (nuint)destination.Length; + var propsSize = (nuint)properties.Length; + var srcSize = (nuint)source.Length; + + AARU_lzma_encode_buffer(destination, ref dstSize, source, srcSize, properties, ref propsSize, level, + dictSize, lc, lp, pb, fb, 0); + + return (int)dstSize; + } + + var _lzmaEncoderProperties = new LzmaEncoderProperties(true, (int)dictSize, fb); + + using var lzmaStream = new LzmaStream(_lzmaEncoderProperties, false, new MemoryStream(destination)); + + lzmaStream.Write(source, 0, source.Length); + properties = new byte[lzmaStream.Properties.Length]; + lzmaStream.Properties.CopyTo(properties, 0); + + return destination.Length; + } +} \ No newline at end of file