Files
Aaru/Aaru.Filters/XZ.cs

251 lines
8.3 KiB
C#
Raw Permalink 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/>.
//
// ----------------------------------------------------------------------------
2025-08-14 02:49:52 +01:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
2017-06-07 19:55:51 +01:00
using System;
2017-06-07 19:55:51 +01:00
using System.IO;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2017-06-07 19:55:51 +01:00
using SharpCompress.Compressors.Xz;
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 xz files while reading</summary>
2020-07-22 13:20:25 +01:00
public sealed class XZ : IFilter
{
2020-07-20 21:11:32 +01:00
string _basePath;
DateTime _creationTime;
Stream _dataStream;
long _decompressedSize;
Stream _innerStream;
DateTime _lastWriteTime;
bool _opened;
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public string Name => "XZ";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public Guid Id => new Guid("666A8617-0444-4C05-9F4F-DF0FD758D0D2");
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 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Close()
2017-06-07 19:55:51 +01:00
{
2020-07-20 21:11:32 +01:00
_dataStream?.Close();
_dataStream = null;
_basePath = null;
_opened = false;
2017-06-07 19:55:51 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public string GetBasePath() => _basePath;
2017-06-07 19:55:51 +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 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public string GetPath() => _basePath;
2017-06-07 19:55:51 +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 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public bool HasResourceFork() => false;
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-04-22 00:22:34 +01:00
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
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(Stream stream)
2017-06-07 19:55:51 +01:00
{
byte[] buffer = new byte[6];
byte[] footer = new byte[2];
if(stream.Length < 8)
return false;
2017-06-07 19:55:51 +01:00
stream.Seek(0, SeekOrigin.Begin);
stream.Read(buffer, 0, 6);
stream.Seek(-2, SeekOrigin.End);
stream.Read(footer, 0, 2);
stream.Seek(0, SeekOrigin.Begin);
2017-12-19 20:33:03 +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;
2017-06-07 19:55:51 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(string path)
2017-06-07 19:55:51 +01:00
{
2020-02-29 18:03:35 +00:00
if(!File.Exists(path))
return false;
2017-06-07 19:55:51 +01:00
2020-02-29 18:03:35 +00:00
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[6];
byte[] footer = new byte[2];
if(stream.Length < 8)
return false;
stream.Seek(0, SeekOrigin.Begin);
stream.Read(buffer, 0, 6);
stream.Seek(-2, SeekOrigin.End);
stream.Read(footer, 0, 2);
stream.Seek(0, SeekOrigin.Begin);
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;
2017-06-07 19:55:51 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(byte[] buffer)
2017-06-07 19:55:51 +01:00
{
2020-07-20 21:11:32 +01:00
_dataStream = new MemoryStream(buffer);
_basePath = null;
_creationTime = DateTime.UtcNow;
_lastWriteTime = _creationTime;
2017-06-07 19:55:51 +01:00
GuessSize();
2020-07-20 21:11:32 +01:00
_innerStream = new ForcedSeekStream<XZStream>(_decompressedSize, _dataStream);
_opened = true;
2017-06-07 19:55:51 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(Stream stream)
2017-06-07 19:55:51 +01:00
{
2020-07-20 21:11:32 +01:00
_dataStream = stream;
_basePath = null;
_creationTime = DateTime.UtcNow;
_lastWriteTime = _creationTime;
2017-06-07 19:55:51 +01:00
GuessSize();
2020-07-20 21:11:32 +01:00
_innerStream = new ForcedSeekStream<XZStream>(_decompressedSize, _dataStream);
_opened = true;
2017-06-07 19:55:51 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(string path)
2017-06-07 19:55:51 +01:00
{
2020-07-20 21:11:32 +01:00
_dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
_basePath = Path.GetFullPath(path);
2017-06-07 19:55:51 +01:00
2020-02-29 18:03:35 +00:00
var fi = new FileInfo(path);
2020-07-20 21:11:32 +01:00
_creationTime = fi.CreationTimeUtc;
_lastWriteTime = fi.LastWriteTimeUtc;
2017-06-07 19:55:51 +01:00
GuessSize();
2020-07-20 21:11:32 +01:00
_innerStream = new ForcedSeekStream<XZStream>(_decompressedSize, _dataStream);
_opened = true;
2017-06-07 19:55:51 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public DateTime GetCreationTime() => _creationTime;
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public long GetDataForkLength() => _decompressedSize;
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public DateTime GetLastWriteTime() => _lastWriteTime;
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public long GetLength() => _decompressedSize;
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public long GetResourceForkLength() => 0;
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public string GetFilename()
2017-06-07 19:55:51 +01:00
{
2020-07-20 21:11:32 +01:00
if(_basePath?.EndsWith(".xz", StringComparison.InvariantCultureIgnoreCase) == true)
return _basePath.Substring(0, _basePath.Length - 3);
2020-07-20 21:11:32 +01:00
return _basePath?.EndsWith(".xzip", StringComparison.InvariantCultureIgnoreCase) == true
? _basePath.Substring(0, _basePath.Length - 5) : _basePath;
2017-06-07 19:55:51 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public string GetParentFolder() => Path.GetDirectoryName(_basePath);
2017-06-07 19:55:51 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public bool IsOpened() => _opened;
2018-06-22 08:08:38 +01:00
void GuessSize()
{
2020-07-20 21:11:32 +01:00
_decompressedSize = 0;
2020-02-29 18:03:35 +00:00
2018-06-22 08:08:38 +01:00
// Seek to footer backwards size field
2020-07-20 21:11:32 +01:00
_dataStream.Seek(-8, SeekOrigin.End);
2018-06-22 08:08:38 +01:00
byte[] tmp = new byte[4];
2020-07-20 21:11:32 +01:00
_dataStream.Read(tmp, 0, 4);
2018-06-22 08:08:38 +01:00
uint backwardSize = (BitConverter.ToUInt32(tmp, 0) + 1) * 4;
2020-02-29 18:03:35 +00:00
2018-06-22 08:08:38 +01:00
// Seek to first indexed record
2020-07-20 21:11:32 +01:00
_dataStream.Seek(-12 - (backwardSize - 2), SeekOrigin.End);
2018-06-22 08:08:38 +01:00
// Skip compressed size
tmp = new byte[backwardSize - 2];
2020-07-20 21:11:32 +01:00
_dataStream.Read(tmp, 0, tmp.Length);
2018-06-22 08:08:38 +01:00
ulong number = 0;
int ignore = Decode(tmp, tmp.Length, ref number);
// Get compressed size
2020-07-20 21:11:32 +01:00
_dataStream.Seek(-12 - (backwardSize - 2 - ignore), SeekOrigin.End);
2018-06-22 08:08:38 +01:00
tmp = new byte[backwardSize - 2 - ignore];
2020-07-20 21:11:32 +01:00
_dataStream.Read(tmp, 0, tmp.Length);
2018-06-22 08:08:38 +01:00
Decode(tmp, tmp.Length, ref number);
2020-07-20 21:11:32 +01:00
_decompressedSize = (long)number;
2018-06-22 08:08:38 +01:00
2020-07-20 21:11:32 +01:00
_dataStream.Seek(0, SeekOrigin.Begin);
2018-06-22 08:08:38 +01:00
}
int Decode(byte[] buf, int sizeMax, ref ulong num)
{
2020-02-29 18:03:35 +00:00
if(sizeMax == 0)
return 0;
2018-06-22 08:08:38 +01:00
2020-02-29 18:03:35 +00:00
if(sizeMax > 9)
sizeMax = 9;
2018-06-22 08:08:38 +01:00
num = (ulong)(buf[0] & 0x7F);
int i = 0;
while((buf[i++] & 0x80) == 0x80)
{
2020-02-29 18:03:35 +00:00
if(i >= sizeMax ||
buf[i] == 0x00)
return 0;
2018-06-22 08:08:38 +01:00
num |= (ulong)(buf[i] & 0x7F) << (i * 7);
}
return i;
}
}
2017-06-07 19:55:51 +01:00
}