2017-06-06 23:04:25 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2017-06-06 23:04:25 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : LZip.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-06-07 18:36:06 +01:00
|
|
|
|
// Component : Filters.
|
2017-06-06 23:04:25 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-06-07 18:36:06 +01:00
|
|
|
|
// Allow to open files that are compressed using lzip.
|
2017-06-06 23:04:25 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
|
// Copyright © 2011-2021 Natalia Portillo
|
2017-06-06 23:04:25 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-06-06 23:04:25 +01:00
|
|
|
|
using System;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
using System.IO;
|
2021-09-16 04:42:14 +01:00
|
|
|
|
using Aaru.CommonTypes.Enums;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
using SharpCompress.Compressors;
|
|
|
|
|
|
using SharpCompress.Compressors.LZMA;
|
|
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.Filters
|
2017-06-06 23:04:25 +01:00
|
|
|
|
{
|
2021-08-17 16:12:30 +01:00
|
|
|
|
/// <inheritdoc />
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Decompress lzip files while reading</summary>
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed class LZip : IFilter
|
2017-06-06 23:04:25 +01:00
|
|
|
|
{
|
2021-09-15 11:25:26 +01:00
|
|
|
|
Stream _dataStream;
|
|
|
|
|
|
Stream _innerStream;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-08-17 21:23:10 +01:00
|
|
|
|
public string Name => "LZip";
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public Guid Id => new("09D715E9-20C0-48B1-A8D9-D8897CEC57C9");
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2018-08-29 22:15:43 +01:00
|
|
|
|
public string Author => "Natalia Portillo";
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public void Close()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_dataStream?.Close();
|
|
|
|
|
|
_dataStream = null;
|
2021-09-15 11:25:26 +01:00
|
|
|
|
BasePath = null;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public string BasePath { get; private set; }
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2020-07-20 21:11:32 +01:00
|
|
|
|
public Stream GetDataForkStream() => _innerStream;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public string Path => BasePath;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2018-08-29 22:15:43 +01:00
|
|
|
|
public Stream GetResourceForkStream() => null;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public bool HasResourceFork => false;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2020-02-29 18:03:35 +00:00
|
|
|
|
public bool Identify(byte[] buffer) => buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 &&
|
|
|
|
|
|
buffer[3] == 0x50 && buffer[4] == 0x01;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(Stream stream)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
byte[] buffer = new byte[5];
|
|
|
|
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(buffer, 0, 5);
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
return buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 && buffer[3] == 0x50 &&
|
|
|
|
|
|
buffer[4] == 0x01;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(string path)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(!File.Exists(path))
|
|
|
|
|
|
return false;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
byte[] buffer = new byte[5];
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(buffer, 0, 5);
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
return buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 && buffer[3] == 0x50 &&
|
|
|
|
|
|
buffer[4] == 0x01;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
|
public ErrorNumber Open(byte[] buffer)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_dataStream = new MemoryStream(buffer);
|
|
|
|
|
|
BasePath = null;
|
|
|
|
|
|
CreationTime = DateTime.UtcNow;
|
|
|
|
|
|
LastWriteTime = CreationTime;
|
|
|
|
|
|
DataForkLength = BitConverter.ToInt64(buffer, buffer.Length - 16);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_innerStream = new ForcedSeekStream<LZipStream>(DataForkLength, _dataStream, CompressionMode.Decompress);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
|
public ErrorNumber Open(Stream stream)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_dataStream = stream;
|
|
|
|
|
|
BasePath = null;
|
|
|
|
|
|
CreationTime = DateTime.UtcNow;
|
|
|
|
|
|
LastWriteTime = CreationTime;
|
2017-06-07 20:03:10 +01:00
|
|
|
|
byte[] tmp = new byte[8];
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_dataStream.Seek(-16, SeekOrigin.End);
|
|
|
|
|
|
_dataStream.Read(tmp, 0, 8);
|
2021-09-15 11:25:26 +01:00
|
|
|
|
DataForkLength = BitConverter.ToInt64(tmp, 0);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_dataStream.Seek(0, SeekOrigin.Begin);
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_innerStream = new ForcedSeekStream<LZipStream>(DataForkLength, _dataStream, CompressionMode.Decompress);
|
2021-09-15 13:03:42 +01:00
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-16 04:42:14 +01:00
|
|
|
|
public ErrorNumber Open(string path)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
2021-09-15 11:25:26 +01:00
|
|
|
|
BasePath = System.IO.Path.GetFullPath(path);
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var fi = new FileInfo(path);
|
2021-09-15 11:25:26 +01:00
|
|
|
|
CreationTime = fi.CreationTimeUtc;
|
|
|
|
|
|
LastWriteTime = fi.LastWriteTimeUtc;
|
2017-06-07 20:03:10 +01:00
|
|
|
|
byte[] tmp = new byte[8];
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_dataStream.Seek(-16, SeekOrigin.End);
|
|
|
|
|
|
_dataStream.Read(tmp, 0, 8);
|
2021-09-15 11:25:26 +01:00
|
|
|
|
DataForkLength = BitConverter.ToInt64(tmp, 0);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_dataStream.Seek(0, SeekOrigin.Begin);
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_innerStream = new ForcedSeekStream<LZipStream>(DataForkLength, _dataStream, CompressionMode.Decompress);
|
2021-09-15 13:03:42 +01:00
|
|
|
|
|
2021-09-16 04:42:14 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public DateTime CreationTime { get; private set; }
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public long DataForkLength { get; private set; }
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public DateTime LastWriteTime { get; private set; }
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public long Length => DataForkLength;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public long ResourceForkLength => 0;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public string Filename
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2021-09-15 11:25:26 +01:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if(BasePath?.EndsWith(".lz", StringComparison.InvariantCultureIgnoreCase) == true)
|
|
|
|
|
|
return BasePath.Substring(0, BasePath.Length - 3);
|
|
|
|
|
|
|
|
|
|
|
|
return BasePath?.EndsWith(".lzip", StringComparison.InvariantCultureIgnoreCase) == true
|
|
|
|
|
|
? BasePath.Substring(0, BasePath.Length - 5) : BasePath;
|
|
|
|
|
|
}
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-15 11:25:26 +01:00
|
|
|
|
public string ParentFolder => System.IO.Path.GetDirectoryName(BasePath);
|
2017-06-06 23:04:25 +01:00
|
|
|
|
}
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|