2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-09-10 01:31:52 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-09-10 01:31:52 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2018-06-25 19:08:16 +01:00
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
2016-09-30 04:32:18 +01:00
|
|
|
using SharpCompress.Compressors;
|
|
|
|
|
using SharpCompress.Compressors.BZip2;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filters
|
|
|
|
|
{
|
2017-12-23 04:06:02 +00:00
|
|
|
/// <summary>
|
2017-12-24 02:43:49 +00:00
|
|
|
/// Decompress bz2 files while reading
|
2017-12-23 04:06:02 +00:00
|
|
|
/// </summary>
|
2017-12-26 06:05:12 +00:00
|
|
|
public class BZip2 : IFilter
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
string basePath;
|
2016-09-10 01:31:52 +01:00
|
|
|
DateTime creationTime;
|
2018-06-22 08:08:38 +01:00
|
|
|
Stream dataStream;
|
|
|
|
|
long decompressedSize;
|
|
|
|
|
Stream innerStream;
|
2017-12-24 02:43:49 +00:00
|
|
|
DateTime lastWriteTime;
|
2018-06-22 08:08:38 +01:00
|
|
|
bool opened;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string Name => "BZip2";
|
|
|
|
|
public Guid Id => new Guid("FCCFB0C3-32EF-40D8-9714-2333F6AC72A9");
|
|
|
|
|
public string Author => "Natalia Portillo";
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Close()
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
2017-12-22 16:17:40 +00:00
|
|
|
dataStream?.Close();
|
2016-09-10 01:31:52 +01:00
|
|
|
dataStream = null;
|
2018-06-22 08:08:38 +01:00
|
|
|
basePath = null;
|
|
|
|
|
opened = false;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string GetBasePath() => basePath;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public Stream GetDataForkStream() => innerStream;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string GetPath() => basePath;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public Stream GetResourceForkStream() => null;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public bool HasResourceFork() => false;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool Identify(byte[] buffer)
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
2017-12-21 06:06:19 +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
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(buffer.Length <= 512) return true;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-24 02:43:49 +00:00
|
|
|
return buffer[buffer.Length - 512] != 0x6B || buffer[buffer.Length - 511] != 0x6F ||
|
|
|
|
|
buffer[buffer.Length - 510] != 0x6C || buffer[buffer.Length - 509] != 0x79;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool Identify(Stream stream)
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[4];
|
|
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.Read(buffer, 0, 4);
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2017-12-21 06:06:19 +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
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(stream.Length <= 512) return true;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
stream.Seek(-512, SeekOrigin.End);
|
|
|
|
|
stream.Read(buffer, 0, 4);
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
// Check it is not an UDIF
|
2017-12-22 16:17:40 +00:00
|
|
|
return buffer[0] != 0x6B || buffer[1] != 0x6F || buffer[2] != 0x6C || buffer[3] != 0x79;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool Identify(string path)
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
2017-12-21 06:06:19 +00:00
|
|
|
if(!File.Exists(path)) return false;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
2018-06-22 08:08:38 +01:00
|
|
|
byte[] buffer = new byte[4];
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.Read(buffer, 0, 4);
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
if(buffer[0] != 0x42 || buffer[1] != 0x5A || buffer[2] != 0x68 || buffer[3] < 0x31 ||
|
|
|
|
|
buffer[3] > 0x39) return false;
|
|
|
|
|
|
|
|
|
|
if(stream.Length <= 512) return true;
|
|
|
|
|
|
|
|
|
|
stream.Seek(-512, SeekOrigin.End);
|
|
|
|
|
stream.Read(buffer, 0, 4);
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
// 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;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(byte[] buffer)
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
dataStream = new MemoryStream(buffer);
|
|
|
|
|
basePath = null;
|
|
|
|
|
creationTime = DateTime.UtcNow;
|
|
|
|
|
lastWriteTime = creationTime;
|
|
|
|
|
innerStream = new ForcedSeekStream<BZip2Stream>(dataStream, CompressionMode.Decompress, false, false);
|
2017-06-07 18:23:30 +01:00
|
|
|
decompressedSize = innerStream.Length;
|
2018-06-22 08:08:38 +01:00
|
|
|
opened = true;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(Stream stream)
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
dataStream = stream;
|
|
|
|
|
basePath = null;
|
|
|
|
|
creationTime = DateTime.UtcNow;
|
|
|
|
|
lastWriteTime = creationTime;
|
|
|
|
|
innerStream = new ForcedSeekStream<BZip2Stream>(dataStream, CompressionMode.Decompress, false, false);
|
2017-06-07 18:23:30 +01:00
|
|
|
decompressedSize = innerStream.Length;
|
2018-06-22 08:08:38 +01:00
|
|
|
opened = true;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(string path)
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
|
|
|
|
dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
2018-06-22 08:08:38 +01:00
|
|
|
basePath = Path.GetFullPath(path);
|
2016-09-10 01:31:52 +01:00
|
|
|
|
|
|
|
|
FileInfo fi = new FileInfo(path);
|
2018-06-22 08:08:38 +01:00
|
|
|
creationTime = fi.CreationTimeUtc;
|
|
|
|
|
lastWriteTime = fi.LastWriteTimeUtc;
|
|
|
|
|
innerStream = new ForcedSeekStream<BZip2Stream>(dataStream, CompressionMode.Decompress, false, false);
|
2017-06-07 18:23:30 +01:00
|
|
|
decompressedSize = innerStream.Length;
|
2018-06-22 08:08:38 +01:00
|
|
|
opened = true;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public DateTime GetCreationTime() => creationTime;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public long GetDataForkLength() => decompressedSize;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public DateTime GetLastWriteTime() => lastWriteTime;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public long GetLength() => decompressedSize;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public long GetResourceForkLength() => 0;
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string GetFilename()
|
2016-09-10 01:31:52 +01:00
|
|
|
{
|
2017-12-21 17:45:39 +00:00
|
|
|
if(basePath?.EndsWith(".bz2", StringComparison.InvariantCultureIgnoreCase) == true)
|
2017-07-01 03:26:31 +01:00
|
|
|
return basePath.Substring(0, basePath.Length - 4);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-06-20 22:22:21 +01:00
|
|
|
return basePath?.EndsWith(".bzip2", StringComparison.InvariantCultureIgnoreCase) == true
|
|
|
|
|
? basePath.Substring(0, basePath.Length - 6)
|
|
|
|
|
: basePath;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string GetParentFolder() => Path.GetDirectoryName(basePath);
|
2016-09-10 01:31:52 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public bool IsOpened() => opened;
|
2016-09-10 01:31:52 +01:00
|
|
|
}
|
|
|
|
|
}
|