2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-09-05 17:37:31 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : ForcedSeekStream.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Filters.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Provides a seekable stream from a forward-readable stream.
|
|
|
|
|
//
|
|
|
|
|
// --[ 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-02-18 10:02:53 +00:00
|
|
|
// Copyright © 2011-2022 Natalia Portillo
|
2016-09-05 17:37:31 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
namespace Aaru.Filters;
|
|
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2022-11-14 09:43:16 +00:00
|
|
|
using Aaru.Helpers;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// ForcedSeekStream allows to seek a forward-readable stream (like System.IO.Compression streams) by doing the
|
|
|
|
|
/// slow and known trick of rewinding and forward reading until arriving the desired position.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public sealed class ForcedSeekStream<T> : Stream where T : Stream
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
const int BUFFER_LEN = 1048576;
|
|
|
|
|
readonly string _backFile;
|
|
|
|
|
readonly FileStream _backStream;
|
|
|
|
|
readonly T _baseStream;
|
|
|
|
|
long _streamLength;
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:Aaru.Filters.ForcedSeekStream`1" /> class.</summary>
|
|
|
|
|
/// <param name="length">The real (uncompressed) length of the stream.</param>
|
|
|
|
|
/// <param name="args">Parameters that are used to create the base stream.</param>
|
2021-08-17 13:56:05 +01:00
|
|
|
/// <inheritdoc />
|
2022-03-06 13:29:38 +00:00
|
|
|
public ForcedSeekStream(long length, params object[] args)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
_streamLength = length;
|
|
|
|
|
_baseStream = (T)Activator.CreateInstance(typeof(T), args);
|
|
|
|
|
_backFile = Path.GetTempFileName();
|
|
|
|
|
_backStream = new FileStream(_backFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(length == 0)
|
2017-06-07 17:08:46 +01:00
|
|
|
CalculateLength();
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:Aaru.Filters.ForcedSeekStream`1" /> class.</summary>
|
|
|
|
|
/// <param name="args">Parameters that are used to create the base stream.</param>
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ForcedSeekStream(params object[] args)
|
|
|
|
|
{
|
|
|
|
|
_baseStream = (T)Activator.CreateInstance(typeof(T), args);
|
|
|
|
|
_backFile = Path.GetTempFileName();
|
|
|
|
|
_backStream = new FileStream(_backFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
|
|
|
|
|
CalculateLength();
|
|
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override bool CanRead => _baseStream.CanRead;
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override bool CanSeek => true;
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override bool CanWrite => false;
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override long Length => _streamLength;
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get => _backStream.Position;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
set => SetPosition(value);
|
|
|
|
|
}
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates the real (uncompressed) length of the stream. It basically reads (uncompresses) the whole stream to
|
|
|
|
|
/// memory discarding its contents, so it should be used as a last resort.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The length.</returns>
|
|
|
|
|
public void CalculateLength()
|
|
|
|
|
{
|
|
|
|
|
int read;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
do
|
2017-06-07 17:08:46 +01:00
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
var buffer = new byte[BUFFER_LEN];
|
2022-11-14 09:43:16 +00:00
|
|
|
read = _baseStream.EnsureRead(buffer, 0, BUFFER_LEN);
|
2022-03-06 13:29:38 +00:00
|
|
|
_backStream.Write(buffer, 0, read);
|
|
|
|
|
} while(read == BUFFER_LEN);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_streamLength = _backStream.Length;
|
|
|
|
|
_backStream.Position = 0;
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
void SetPosition(long position)
|
|
|
|
|
{
|
|
|
|
|
if(position == _backStream.Position)
|
|
|
|
|
return;
|
2017-06-07 17:52:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(position < _backStream.Length)
|
|
|
|
|
{
|
|
|
|
|
_backStream.Position = position;
|
2021-07-16 13:02:36 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(position > _streamLength)
|
|
|
|
|
position = _streamLength;
|
|
|
|
|
|
|
|
|
|
_backStream.Position = _backStream.Length;
|
|
|
|
|
long toPosition = position - _backStream.Position;
|
2022-03-07 07:36:44 +00:00
|
|
|
var fullBufferReads = (int)(toPosition / BUFFER_LEN);
|
|
|
|
|
var restToRead = (int)(toPosition % BUFFER_LEN);
|
2022-03-06 13:29:38 +00:00
|
|
|
byte[] buffer;
|
2022-03-17 23:54:41 +00:00
|
|
|
int bufPos;
|
|
|
|
|
int left;
|
2017-06-07 17:08:46 +01:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
for(var i = 0; i < fullBufferReads; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
|
|
|
|
buffer = new byte[BUFFER_LEN];
|
2021-09-15 18:56:50 +01:00
|
|
|
bufPos = 0;
|
2022-03-06 13:29:38 +00:00
|
|
|
left = BUFFER_LEN;
|
2021-09-15 18:56:50 +01:00
|
|
|
|
|
|
|
|
while(left > 0)
|
|
|
|
|
{
|
2022-11-14 09:43:16 +00:00
|
|
|
int done = _baseStream.EnsureRead(buffer, bufPos, left);
|
2021-09-15 18:56:50 +01:00
|
|
|
left -= done;
|
|
|
|
|
bufPos += done;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_backStream.Write(buffer, 0, BUFFER_LEN);
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
buffer = new byte[restToRead];
|
|
|
|
|
bufPos = 0;
|
|
|
|
|
left = restToRead;
|
|
|
|
|
|
|
|
|
|
while(left > 0)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2022-11-14 09:43:16 +00:00
|
|
|
int done = _baseStream.EnsureRead(buffer, bufPos, left);
|
2022-03-06 13:29:38 +00:00
|
|
|
left -= done;
|
|
|
|
|
bufPos += done;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
_backStream.Write(buffer, 0, restToRead);
|
|
|
|
|
}
|
2021-07-16 13:02:36 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Flush()
|
|
|
|
|
{
|
|
|
|
|
_baseStream.Flush();
|
|
|
|
|
_backStream.Flush();
|
|
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
if(_backStream.Position + count > _streamLength)
|
|
|
|
|
count = (int)(_streamLength - _backStream.Position);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_backStream.Position + count <= _backStream.Length)
|
2022-11-14 09:43:16 +00:00
|
|
|
return _backStream.EnsureRead(buffer, offset, count);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
long oldPosition = _backStream.Position;
|
2022-03-06 13:29:38 +00:00
|
|
|
SetPosition(_backStream.Position + count);
|
|
|
|
|
SetPosition(oldPosition);
|
2021-07-16 13:02:36 +01:00
|
|
|
|
2022-11-14 09:43:16 +00:00
|
|
|
return _backStream.EnsureRead(buffer, offset, count);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override int ReadByte()
|
|
|
|
|
{
|
|
|
|
|
if(_backStream.Position + 1 > _streamLength)
|
|
|
|
|
return -1;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_backStream.Position + 1 <= _backStream.Length)
|
2020-07-20 21:11:32 +01:00
|
|
|
return _backStream.ReadByte();
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
SetPosition(_backStream.Position + 1);
|
|
|
|
|
SetPosition(_backStream.Position - 1);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return _backStream.ReadByte();
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
switch(origin)
|
|
|
|
|
{
|
|
|
|
|
case SeekOrigin.Begin:
|
|
|
|
|
if(offset < 0)
|
|
|
|
|
throw new IOException("Cannot seek before stream start.");
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
SetPosition(offset);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case SeekOrigin.End:
|
|
|
|
|
if(offset > 0)
|
|
|
|
|
throw new IOException("Cannot seek after stream end.");
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_streamLength == 0)
|
|
|
|
|
CalculateLength();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
SetPosition(_streamLength + offset);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
SetPosition(_backStream.Position + offset);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return _backStream.Position;
|
|
|
|
|
}
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void SetLength(long value) => throw new NotSupportedException();
|
2017-06-07 17:08:46 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
2017-06-07 18:29:05 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Close()
|
|
|
|
|
{
|
|
|
|
|
_backStream?.Close();
|
|
|
|
|
File.Delete(_backFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ForcedSeekStream()
|
|
|
|
|
{
|
|
|
|
|
_backStream?.Close();
|
|
|
|
|
File.Delete(_backFile);
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|