Files
Aaru/Aaru.Filters/BZip2.cs

225 lines
7.3 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : BZip2.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Filters.
//
// --[ Description ] ----------------------------------------------------------
//
// Allow to open files that are compressed using bzip2.
//
// --[ 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
// ****************************************************************************/
using System;
using System.IO;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2016-09-30 04:32:18 +01:00
using SharpCompress.Compressors;
using SharpCompress.Compressors.BZip2;
2020-02-27 00:33:26 +00:00
namespace Aaru.Filters
{
2021-08-17 16:12:30 +01:00
/// <inheritdoc />
2020-02-29 18:03:35 +00:00
/// <summary>Decompress bz2 files while reading</summary>
public class BZip2 : IFilter
{
Stream _dataStream;
Stream _innerStream;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public string Name => "BZip2";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public Guid Id => new("FCCFB0C3-32EF-40D8-9714-2333F6AC72A9");
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public string Author => "Natalia Portillo";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Close()
{
2020-07-20 21:11:32 +01:00
_dataStream?.Close();
_dataStream = null;
BasePath = null;
Opened = false;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public string BasePath { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public Stream GetDataForkStream() => _innerStream;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public string Path => BasePath;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public Stream GetResourceForkStream() => null;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool HasResourceFork => false;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(byte[] buffer)
{
2020-02-29 18:03:35 +00:00
if(buffer[0] != 0x42 ||
buffer[1] != 0x5A ||
buffer[2] != 0x68 ||
buffer[3] < 0x31 ||
buffer[3] > 0x39)
return false;
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
if(buffer.Length <= 512)
return true;
2017-12-19 20:33:03 +00:00
2020-04-22 00:22:34 +01:00
return buffer[^512] != 0x6B || buffer[^511] != 0x6F || buffer[^510] != 0x6C || buffer[^509] != 0x79;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(Stream stream)
{
byte[] buffer = new byte[4];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(buffer, 0, 4);
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
if(buffer[0] != 0x42 ||
buffer[1] != 0x5A ||
buffer[2] != 0x68 ||
buffer[3] < 0x31 ||
buffer[3] > 0x39)
return false;
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
if(stream.Length <= 512)
return true;
2017-12-19 20:33:03 +00:00
stream.Seek(-512, SeekOrigin.End);
stream.Read(buffer, 0, 4);
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
// Check it is not an UDIF
return buffer[0] != 0x6B || buffer[1] != 0x6F || buffer[2] != 0x6C || buffer[3] != 0x79;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(string path)
{
2020-02-29 18:03:35 +00:00
if(!File.Exists(path))
return false;
2020-02-29 18:03:35 +00:00
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(buffer, 0, 4);
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
if(buffer[0] != 0x42 ||
buffer[1] != 0x5A ||
buffer[2] != 0x68 ||
buffer[3] < 0x31 ||
buffer[3] > 0x39)
return false;
2020-02-29 18:03:35 +00:00
if(stream.Length <= 512)
return true;
stream.Seek(-512, SeekOrigin.End);
stream.Read(buffer, 0, 4);
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
// Check it is not an UDIF
2018-06-20 22:22:21 +01:00
return buffer[0] != 0x6B || buffer[1] != 0x6F || buffer[2] != 0x6C || buffer[3] != 0x79;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(byte[] buffer)
{
_dataStream = new MemoryStream(buffer);
BasePath = null;
CreationTime = DateTime.UtcNow;
LastWriteTime = CreationTime;
_innerStream = new ForcedSeekStream<BZip2Stream>(_dataStream, CompressionMode.Decompress, false);
DataForkLength = _innerStream.Length;
Opened = true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(Stream stream)
{
_dataStream = stream;
BasePath = null;
CreationTime = DateTime.UtcNow;
LastWriteTime = CreationTime;
_innerStream = new ForcedSeekStream<BZip2Stream>(_dataStream, CompressionMode.Decompress, false);
DataForkLength = _innerStream.Length;
Opened = true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(string path)
{
2020-07-20 21:11:32 +01:00
_dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
BasePath = System.IO.Path.GetFullPath(path);
2020-02-29 18:03:35 +00:00
var fi = new FileInfo(path);
CreationTime = fi.CreationTimeUtc;
LastWriteTime = fi.LastWriteTimeUtc;
_innerStream = new ForcedSeekStream<BZip2Stream>(_dataStream, CompressionMode.Decompress, false);
DataForkLength = _innerStream.Length;
Opened = true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public DateTime CreationTime { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public long DataForkLength { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public DateTime LastWriteTime { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public long Length => DataForkLength;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public long ResourceForkLength => 0;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public string Filename
{
get
{
if(BasePath?.EndsWith(".bz2", StringComparison.InvariantCultureIgnoreCase) == true)
return BasePath.Substring(0, BasePath.Length - 4);
return BasePath?.EndsWith(".bzip2", StringComparison.InvariantCultureIgnoreCase) == true
? BasePath.Substring(0, BasePath.Length - 6) : BasePath;
}
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public string ParentFolder => System.IO.Path.GetDirectoryName(BasePath);
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Opened { get; private set; }
}
}