mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
* TODO:
* README.md: * DiscImageChef.sln: * DiscImageChef/Commands/Ls.cs: * DiscImageChef.Filters/GZip.cs: * DiscImageChef.DiscImages/BLU.cs: * DiscImageChef.DiscImages/VHD.cs: * DiscImageChef.DiscImages/VDI.cs: * DiscImageChef.DiscImages/QED.cs: * DiscImageChef.DiscImages/DIM.cs: * DiscImageChef.DiscImages/GDI.cs: * DiscImageChef.Filters/Filter.cs: * DiscImageChef/Commands/Decode.cs: * DiscImageChef.DiscImages/QCOW.cs: * DiscImageChef.Filters/Filters.cs: * DiscImageChef/Core/Statistics.cs: * DiscImageChef.DiscImages/VHDX.cs: * DiscImageChef.DiscImages/Nero.cs: * DiscImageChef/Commands/Verify.cs: * DiscImageChef.DiscImages/UDIF.cs: * DiscImageChef/Commands/Compare.cs: * DiscImageChef/Commands/Analyze.cs: * DiscImageChef.DiscImages/QCOW2.cs: * DiscImageChef/Commands/Entropy.cs: * DiscImageChef/Commands/Formats.cs: * DiscImageChef/Commands/PrintHex.cs: * DiscImageChef.DiscImages/VMware.cs: * DiscImageChef.Settings/Settings.cs: * DiscImageChef/DetectImageFormat.cs: * DiscImageChef/DiscImageChef.csproj: * DiscImageChef.DiscImages/CDRDAO.cs: * DiscImageChef.DiscImages/CPCDSK.cs: * DiscImageChef/Commands/Checksum.cs: * DiscImageChef.DiscImages/CopyQM.cs: * DiscImageChef.DiscImages/CDRWin.cs: * DiscImageChef/Commands/Configure.cs: * DiscImageChef/Commands/DumpMedia.cs: * DiscImageChef/Commands/Statistics.cs: * DiscImageChef.Filters/ZZZNoFilter.cs: * DiscImageChef.DiscImages/TeleDisk.cs: * DiscImageChef.DiscImages/Apple2MG.cs: * DiscImageChef.Filters/OffsetStream.cs: * DiscImageChef.DiscImages/Parallels.cs: * DiscImageChef/Commands/ExtractFiles.cs: * DiscImageChef.DiscImages/DiskCopy42.cs: * DiscImageChef.DiscImages/Alcohol120.cs: * DiscImageChef.DiscImages/ZZZRawImage.cs: * DiscImageChef/Commands/CreateSidecar.cs: * DiscImageChef.DiscImages/ImagePlugin.cs: * DiscImageChef.DiscImages/BlindWrite5.cs: * DiscImageChef.DiscImages/BlindWrite4.cs: * DiscImageChef.Filters/ForcedSeekStream.cs: * DiscImageChef.Filters/Properties/AssemblyInfo.cs: * DiscImageChef.Filters/DiscImageChef.Filters.csproj: * DiscImageChef.DiscImages/DiscImageChef.DiscImages.csproj: Added filters. * DiscImageChef.Filesystems/CPM/Info.cs: Do not throw identification exceptions. * DiscImageChef/Plugins.cs: Sorted plugins lists.
This commit is contained in:
228
DiscImageChef.Filters/ForcedSeekStream.cs
Normal file
228
DiscImageChef.Filters/ForcedSeekStream.cs
Normal file
@@ -0,0 +1,228 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// 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/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2016 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace DiscImageChef.Filters
|
||||
{
|
||||
/// <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>
|
||||
public class ForcedSeekStream<T> : Stream where T : Stream
|
||||
{
|
||||
T baseStream;
|
||||
long currentPosition;
|
||||
object[] parameters;
|
||||
long streamLength;
|
||||
const int bufferLen = 1048576;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:DiscImageChef.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>
|
||||
public ForcedSeekStream(long length, params object[] args)
|
||||
{
|
||||
parameters = args;
|
||||
Rewind();
|
||||
streamLength = length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:DiscImageChef.Filters.ForcedSeekStream`1"/> class.
|
||||
/// </summary>
|
||||
/// <param name="args">Parameters that are used to create the base stream.</param>
|
||||
public ForcedSeekStream(params object[] args)
|
||||
{
|
||||
parameters = args;
|
||||
Rewind();
|
||||
streamLength = baseStream.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rewinds the stream to start
|
||||
/// </summary>
|
||||
public void Rewind()
|
||||
{
|
||||
baseStream = (T)Activator.CreateInstance(typeof(T), parameters);
|
||||
currentPosition = 0;
|
||||
}
|
||||
|
||||
/// <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()
|
||||
{
|
||||
long count = 0;
|
||||
int read;
|
||||
Rewind();
|
||||
do
|
||||
{
|
||||
byte[] buffer = new byte[bufferLen];
|
||||
read = baseStream.Read(buffer, 0, bufferLen);
|
||||
count += read;
|
||||
}
|
||||
while(read == bufferLen);
|
||||
|
||||
streamLength = count;
|
||||
}
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
return baseStream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
return baseStream.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return currentPosition;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if(value == currentPosition)
|
||||
return;
|
||||
|
||||
if(value < currentPosition)
|
||||
Rewind();
|
||||
|
||||
int fullBufferReads = (int)(value / bufferLen);
|
||||
int restToRead = (int)(value % bufferLen);
|
||||
byte[] buffer;
|
||||
|
||||
for(int i = 0; i < fullBufferReads; i++)
|
||||
{
|
||||
buffer = new byte[bufferLen];
|
||||
baseStream.Read(buffer, 0, bufferLen);
|
||||
}
|
||||
|
||||
buffer = new byte[restToRead];
|
||||
baseStream.Read(buffer, 0, restToRead);
|
||||
|
||||
currentPosition = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
baseStream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int read = baseStream.Read(buffer, offset, count);
|
||||
|
||||
currentPosition += read;
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
int byt = baseStream.ReadByte();
|
||||
|
||||
// Because -1 equals end of stream so we cannot go farther
|
||||
if(byt > 0)
|
||||
currentPosition++;
|
||||
|
||||
return byt;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
switch(origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
if(offset < 0)
|
||||
throw new IOException("Cannot seek before stream start.");
|
||||
Position = offset;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
if(offset > 0)
|
||||
throw new IOException("Cannot seek after stream end.");
|
||||
if(streamLength == 0)
|
||||
CalculateLength();
|
||||
Position = streamLength + offset;
|
||||
break;
|
||||
default:
|
||||
Position = currentPosition + offset;
|
||||
break;
|
||||
}
|
||||
|
||||
return currentPosition;
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user