Files
Aaru/Aaru.Filters/XZ.cs

252 lines
7.6 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : XZ.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
2017-06-07 19:55:51 +01:00
// Component : Filters.
//
// --[ Description ] ----------------------------------------------------------
//
2017-06-07 19:55:51 +01:00
// Allow to open files that are compressed using xz.
//
// --[ 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-12-03 16:07:10 +00:00
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
2017-06-07 19:55:51 +01:00
using System;
2017-06-07 19:55:51 +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;
using Aaru.Helpers;
2017-06-07 19:55:51 +01:00
using SharpCompress.Compressors.Xz;
namespace Aaru.Filters;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Decompress xz files while reading</summary>
public sealed class XZ : IFilter
{
2022-03-06 13:29:38 +00:00
Stream _dataStream;
Stream _innerStream;
/// <inheritdoc />
public string Name => Localization.XZ_Name;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Guid Id => new("666A8617-0444-4C05-9F4F-DF0FD758D0D2");
2021-08-17 16:12:30 +01:00
/// <inheritdoc />
public string Author => Authors.NataliaPortillo;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public void Close()
{
2022-03-06 13:29:38 +00:00
_dataStream?.Close();
_dataStream = null;
BasePath = null;
}
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string BasePath { get; private set; }
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Stream GetDataForkStream() => _innerStream;
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string Path => BasePath;
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Stream GetResourceForkStream() => null;
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool HasResourceFork => false;
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(byte[] buffer) => buffer[0] == 0xFD && buffer[1] == 0x37 && buffer[2] == 0x7A &&
buffer[3] == 0x58 && buffer[4] == 0x5A && buffer[5] == 0x00 &&
buffer[^2] == 0x59 && buffer[^1] == 0x5A;
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(Stream stream)
{
byte[] buffer = new byte[6];
byte[] footer = new byte[2];
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
if(stream.Length < 8)
return false;
2022-03-06 13:29:38 +00:00
stream.Seek(0, SeekOrigin.Begin);
stream.EnsureRead(buffer, 0, 6);
2022-03-06 13:29:38 +00:00
stream.Seek(-2, SeekOrigin.End);
stream.EnsureRead(footer, 0, 2);
2022-03-06 13:29:38 +00:00
stream.Seek(0, SeekOrigin.Begin);
2017-06-07 19:55:51 +01:00
2022-03-07 07:36:44 +00:00
return buffer[0] == 0xFD && buffer[1] == 0x37 && buffer[2] == 0x7A && buffer[3] == 0x58 && buffer[4] == 0x5A &&
buffer[5] == 0x00 && footer[0] == 0x59 && footer[1] == 0x5A;
2022-03-06 13:29:38 +00:00
}
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(string path)
{
if(!File.Exists(path))
return false;
2017-06-07 19:55:51 +01:00
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[6];
byte[] footer = new byte[2];
2022-03-06 13:29:38 +00:00
if(stream.Length < 8)
return false;
2022-03-06 13:29:38 +00:00
stream.Seek(0, SeekOrigin.Begin);
stream.EnsureRead(buffer, 0, 6);
2022-03-06 13:29:38 +00:00
stream.Seek(-2, SeekOrigin.End);
stream.EnsureRead(footer, 0, 2);
2022-03-06 13:29:38 +00:00
stream.Seek(0, SeekOrigin.Begin);
2022-03-07 07:36:44 +00:00
return buffer[0] == 0xFD && buffer[1] == 0x37 && buffer[2] == 0x7A && buffer[3] == 0x58 && buffer[4] == 0x5A &&
buffer[5] == 0x00 && footer[0] == 0x59 && footer[1] == 0x5A;
2022-03-06 13:29:38 +00:00
}
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(byte[] buffer)
{
_dataStream = new MemoryStream(buffer);
BasePath = null;
CreationTime = DateTime.UtcNow;
LastWriteTime = CreationTime;
GuessSize();
_innerStream = new ForcedSeekStream<XZStream>(DataForkLength, _dataStream);
return ErrorNumber.NoError;
}
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(Stream stream)
{
_dataStream = stream;
BasePath = null;
CreationTime = DateTime.UtcNow;
LastWriteTime = CreationTime;
GuessSize();
_innerStream = new ForcedSeekStream<XZStream>(DataForkLength, _dataStream);
return ErrorNumber.NoError;
}
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(string path)
{
_dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
BasePath = System.IO.Path.GetFullPath(path);
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
var fi = new FileInfo(path);
CreationTime = fi.CreationTimeUtc;
LastWriteTime = fi.LastWriteTimeUtc;
GuessSize();
_innerStream = new ForcedSeekStream<XZStream>(DataForkLength, _dataStream);
2021-09-15 13:03:42 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public DateTime CreationTime { get; private set; }
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public long DataForkLength { get; private set; }
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public DateTime LastWriteTime { get; private set; }
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public long Length => DataForkLength;
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public long ResourceForkLength => 0;
2017-06-07 19:55:51 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string Filename
{
get
2017-06-07 19:55:51 +01:00
{
2022-03-06 13:29:38 +00:00
if(BasePath?.EndsWith(".xz", StringComparison.InvariantCultureIgnoreCase) == true)
2022-11-14 01:15:06 +00:00
return BasePath[..^3];
2017-06-07 19:55:51 +01:00
2022-11-14 01:15:06 +00:00
return BasePath?.EndsWith(".xzip", StringComparison.InvariantCultureIgnoreCase) == true ? BasePath[..^5]
: BasePath;
2018-06-22 08:08:38 +01:00
}
2022-03-06 13:29:38 +00:00
}
2018-06-22 08:08:38 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string ParentFolder => System.IO.Path.GetDirectoryName(BasePath);
2018-06-22 08:08:38 +01:00
2022-03-06 13:29:38 +00:00
void GuessSize()
{
DataForkLength = 0;
// Seek to footer backwards size field
_dataStream.Seek(-8, SeekOrigin.End);
byte[] tmp = new byte[4];
_dataStream.EnsureRead(tmp, 0, 4);
2022-03-06 13:29:38 +00:00
uint backwardSize = (BitConverter.ToUInt32(tmp, 0) + 1) * 4;
// Seek to first indexed record
_dataStream.Seek(-12 - (backwardSize - 2), SeekOrigin.End);
// Skip compressed size
tmp = new byte[backwardSize - 2];
_dataStream.EnsureRead(tmp, 0, tmp.Length);
2022-03-06 13:29:38 +00:00
ulong number = 0;
int ignore = Decode(tmp, tmp.Length, ref number);
// Get compressed size
_dataStream.Seek(-12 - (backwardSize - 2 - ignore), SeekOrigin.End);
tmp = new byte[backwardSize - 2 - ignore];
_dataStream.EnsureRead(tmp, 0, tmp.Length);
2022-03-06 13:29:38 +00:00
Decode(tmp, tmp.Length, ref number);
DataForkLength = (long)number;
_dataStream.Seek(0, SeekOrigin.Begin);
}
2018-06-22 08:08:38 +01:00
2022-11-15 01:35:06 +00:00
static int Decode(byte[] buf, int sizeMax, ref ulong num)
2022-03-06 13:29:38 +00:00
{
2022-11-13 19:38:03 +00:00
switch(sizeMax)
{
case 0: return 0;
case > 9:
sizeMax = 9;
2018-06-22 08:08:38 +01:00
2022-11-13 19:38:03 +00:00
break;
}
2018-06-22 08:08:38 +01:00
2022-03-06 13:29:38 +00:00
num = (ulong)(buf[0] & 0x7F);
int i = 0;
2018-06-22 08:08:38 +01:00
2022-03-06 13:29:38 +00:00
while((buf[i++] & 0x80) == 0x80)
{
if(i >= sizeMax ||
buf[i] == 0x00)
return 0;
num |= (ulong)(buf[i] & 0x7F) << (i * 7);
2018-06-22 08:08:38 +01:00
}
2022-03-06 13:29:38 +00:00
return i;
}
2017-06-07 19:55:51 +01:00
}