mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Redo SectorStream classes
This commit is contained in:
parent
944245fb8b
commit
12b2825475
14 changed files with 348 additions and 673 deletions
|
@ -8,7 +8,7 @@ using GalaSoft.MvvmLight.Command;
|
|||
using libhac;
|
||||
using libhac.Nand;
|
||||
using libhac.Savefile;
|
||||
using libhac.XTSSharp;
|
||||
using libhac.Streams;
|
||||
|
||||
namespace NandReaderGui.ViewModel
|
||||
{
|
||||
|
|
|
@ -101,7 +101,7 @@ namespace hactoolnet
|
|||
|
||||
if (ctx.Options.RomfsOutDir != null || ctx.Options.RomfsOut != null)
|
||||
{
|
||||
NcaSection section = nca.Sections.FirstOrDefault(x => x.Type == SectionType.Romfs || x.Type == SectionType.Bktr);
|
||||
NcaSection section = nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs || x?.Type == SectionType.Bktr);
|
||||
|
||||
if (section == null)
|
||||
{
|
||||
|
|
|
@ -4,6 +4,7 @@ using DiscUtils;
|
|||
using DiscUtils.Fat;
|
||||
using DiscUtils.Partitions;
|
||||
using DiscUtils.Streams;
|
||||
using libhac.Streams;
|
||||
using libhac.XTSSharp;
|
||||
|
||||
namespace libhac.Nand
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using libhac.XTSSharp;
|
||||
using libhac.Streams;
|
||||
|
||||
namespace libhac
|
||||
{
|
||||
public class Aes128CtrStream : SectorStream
|
||||
{
|
||||
private const int CryptChunkSize = 0x4000;
|
||||
private const int BlockSize = 0x10;
|
||||
|
||||
private readonly long _counterOffset;
|
||||
private readonly byte[] _tempBuffer;
|
||||
|
@ -30,7 +31,7 @@ namespace libhac
|
|||
/// <param name="key">The decryption key</param>
|
||||
/// <param name="counter">The intial counter</param>
|
||||
public Aes128CtrStream(Stream baseStream, byte[] key, byte[] counter)
|
||||
: base(baseStream, 0x10, 0)
|
||||
: base(baseStream, BlockSize)
|
||||
{
|
||||
_counterOffset = 0;
|
||||
Length = baseStream.Length;
|
||||
|
@ -50,9 +51,9 @@ namespace libhac
|
|||
/// <param name="counterOffset">Offset to add to the counter</param>
|
||||
/// <param name="ctrHi">The value of the upper 64 bits of the counter</param>
|
||||
public Aes128CtrStream(Stream baseStream, byte[] key, long offset, long length, long counterOffset, byte[] ctrHi = null)
|
||||
: base(baseStream, CryptChunkSize, offset)
|
||||
: base(baseStream, BlockSize, CryptChunkSize / BlockSize, offset)
|
||||
{
|
||||
var initialCounter = new byte[0x10];
|
||||
var initialCounter = new byte[BlockSize];
|
||||
if (ctrHi != null)
|
||||
{
|
||||
Array.Copy(ctrHi, initialCounter, 8);
|
||||
|
@ -134,11 +135,5 @@ namespace libhac
|
|||
|
||||
return _decryptor.TransformBlock(_tempBuffer, 0, bytesRead, buffer, offset);
|
||||
}
|
||||
|
||||
protected override void ValidateSizeMultiple(long value)
|
||||
{
|
||||
if (value % 0x10 != 0)
|
||||
throw new ArgumentException($"Value needs to be a multiple of {SectorSize}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using libhac.XTSSharp;
|
||||
using libhac.Streams;
|
||||
|
||||
namespace libhac
|
||||
{
|
||||
|
@ -106,10 +106,5 @@ namespace libhac
|
|||
Counter[5] = (byte)(value >> 16);
|
||||
Counter[4] = (byte)(value >> 24);
|
||||
}
|
||||
|
||||
// todo: Make SectorStream play nicer with reading multiple
|
||||
// blocks at a time to remove the need for this hack
|
||||
protected override void ValidateSize(int value) { }
|
||||
protected override void ValidateSize(long value) { }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.IO;
|
||||
using System.Numerics;
|
||||
using System.Security.Cryptography;
|
||||
using libhac.XTSSharp;
|
||||
using libhac.Streams;
|
||||
|
||||
namespace libhac
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using libhac.Streams;
|
||||
using libhac.XTSSharp;
|
||||
|
||||
namespace libhac
|
||||
|
|
167
libhac/Streams/RandomAccessSectorStream.cs
Normal file
167
libhac/Streams/RandomAccessSectorStream.cs
Normal file
|
@ -0,0 +1,167 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace libhac.Streams
|
||||
{
|
||||
public class RandomAccessSectorStream : Stream
|
||||
{
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _bufferSize;
|
||||
private readonly SectorStream _baseStream;
|
||||
private readonly bool _keepOpen;
|
||||
private int _readBytes; // Number of bytes read into buffer
|
||||
private int _bufferPos;
|
||||
private long _currentSector;
|
||||
private bool _bufferDirty;
|
||||
|
||||
public RandomAccessSectorStream(SectorStream baseStream)
|
||||
: this(baseStream, true) { }
|
||||
|
||||
public RandomAccessSectorStream(SectorStream baseStream, bool keepOpen)
|
||||
{
|
||||
_baseStream = baseStream;
|
||||
_keepOpen = keepOpen;
|
||||
_bufferSize = baseStream.SectorSize * baseStream.MaxSectors;
|
||||
_buffer = new byte[_bufferSize];
|
||||
}
|
||||
|
||||
private void FillBuffer(long sectorNum)
|
||||
{
|
||||
WriteSectorIfDirty();
|
||||
|
||||
_currentSector = sectorNum;
|
||||
long startPos = sectorNum * _bufferSize;
|
||||
_baseStream.Position = startPos;
|
||||
|
||||
_readBytes = _baseStream.Read(_buffer, 0, _bufferSize);
|
||||
}
|
||||
|
||||
private void WriteSectorIfDirty()
|
||||
{
|
||||
if (_readBytes == 0 || !_bufferDirty) return;
|
||||
|
||||
_baseStream.Position = _currentSector * _bufferSize;
|
||||
_baseStream.Write(_buffer, 0, _readBytes);
|
||||
|
||||
_readBytes = 0;
|
||||
_bufferDirty = false;
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
WriteSectorIfDirty();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
long remaining = Math.Min(count, Length - Position);
|
||||
if (remaining <= 0) return 0;
|
||||
|
||||
if (_readBytes == 0) FillBuffer(Position / _bufferSize);
|
||||
int outOffset = offset;
|
||||
int totalBytesRead = 0;
|
||||
|
||||
while (remaining > 0)
|
||||
{
|
||||
int bytesToRead = (int)Math.Min(remaining, _bufferSize - _bufferPos);
|
||||
|
||||
Buffer.BlockCopy(_buffer, _bufferPos, buffer, outOffset, bytesToRead);
|
||||
|
||||
outOffset += bytesToRead;
|
||||
_bufferPos += bytesToRead;
|
||||
totalBytesRead += bytesToRead;
|
||||
remaining -= bytesToRead;
|
||||
|
||||
if (_bufferPos == _bufferSize)
|
||||
{
|
||||
FillBuffer(_currentSector + 1);
|
||||
_bufferPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return totalBytesRead;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
Position = offset;
|
||||
break;
|
||||
case SeekOrigin.Current:
|
||||
Position += offset;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
Position = Length - offset;
|
||||
break;
|
||||
}
|
||||
|
||||
return Position;
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int remaining = count;
|
||||
int outOffset = offset;
|
||||
|
||||
while (remaining > 0)
|
||||
{
|
||||
if (_readBytes == 0) FillBuffer(Position / _bufferSize);
|
||||
|
||||
var bytesToWrite = Math.Min(remaining, _readBytes - _bufferPos);
|
||||
|
||||
Buffer.BlockCopy(buffer, outOffset, _buffer, _bufferPos, bytesToWrite);
|
||||
|
||||
outOffset += bytesToWrite;
|
||||
_bufferPos += bytesToWrite;
|
||||
remaining -= bytesToWrite;
|
||||
_bufferDirty = true;
|
||||
|
||||
if (_bufferPos == _bufferSize)
|
||||
{
|
||||
WriteSectorIfDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanRead => _baseStream.CanRead;
|
||||
public override bool CanSeek => _baseStream.CanSeek;
|
||||
public override bool CanWrite => _baseStream.CanWrite;
|
||||
public override long Length => _baseStream.Length;
|
||||
public override long Position
|
||||
{
|
||||
get => _baseStream.Position - _readBytes + _bufferPos;
|
||||
set
|
||||
{
|
||||
if (value < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(value));
|
||||
|
||||
long sectorNum = value / _bufferSize;
|
||||
|
||||
if (sectorNum != _currentSector)
|
||||
{
|
||||
FillBuffer(sectorNum);
|
||||
}
|
||||
|
||||
_bufferPos = (int)(value % _bufferSize);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
Flush();
|
||||
|
||||
base.Dispose(disposing);
|
||||
|
||||
if (!_keepOpen)
|
||||
_baseStream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
158
libhac/Streams/SectorStream.cs
Normal file
158
libhac/Streams/SectorStream.cs
Normal file
|
@ -0,0 +1,158 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace libhac.Streams
|
||||
{
|
||||
public class SectorStream : Stream
|
||||
{
|
||||
private readonly Stream _baseStream;
|
||||
private readonly long _offset;
|
||||
private readonly int _maxBufferSize;
|
||||
private readonly bool _keepOpen;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the sectors.
|
||||
/// </summary>
|
||||
public int SectorSize { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The maximum number of sectors that can be read or written in a single operation.
|
||||
/// </summary>
|
||||
public int MaxSectors { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The current sector this stream is at
|
||||
/// </summary>
|
||||
protected long CurrentSector { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream to read/write from</param>
|
||||
/// <param name="sectorSize">The size of the sectors to read/write</param>
|
||||
public SectorStream(Stream baseStream, int sectorSize)
|
||||
: this(baseStream, sectorSize, 1, 0)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream to read/write from</param>
|
||||
/// <param name="sectorSize">The size of the sectors to read/write</param>
|
||||
/// <param name="maxSectors">The maximum number of sectors to read/write at once</param>
|
||||
/// <param name="offset">Offset to start counting sectors</param>
|
||||
public SectorStream(Stream baseStream, int sectorSize, int maxSectors, long offset)
|
||||
: this(baseStream, sectorSize, maxSectors, offset, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream to read/write from</param>
|
||||
/// <param name="sectorSize">The size of the sectors to read/write</param>
|
||||
/// <param name="maxSectors">The maximum number of sectors to read/write at once</param>
|
||||
/// <param name="offset">Offset to start counting sectors</param>
|
||||
/// <param name="keepOpen">Should this stream leave the base stream open when disposed?</param>
|
||||
public SectorStream(Stream baseStream, int sectorSize, int maxSectors, long offset, bool keepOpen)
|
||||
{
|
||||
SectorSize = sectorSize;
|
||||
_baseStream = baseStream;
|
||||
MaxSectors = maxSectors;
|
||||
_offset = offset;
|
||||
_keepOpen = keepOpen;
|
||||
_maxBufferSize = MaxSectors * SectorSize;
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_baseStream.Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
ValidateSize(count);
|
||||
int bytesRead = _baseStream.Read(buffer, offset, count);
|
||||
CurrentSector += bytesRead / SectorSize;
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
Position = offset;
|
||||
break;
|
||||
case SeekOrigin.Current:
|
||||
Position += offset;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
Position = Length - offset;
|
||||
break;
|
||||
}
|
||||
|
||||
return Position;
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
ValidateSize(count);
|
||||
_baseStream.Write(buffer, offset, count);
|
||||
CurrentSector += count / SectorSize;
|
||||
}
|
||||
|
||||
public override bool CanRead => _baseStream.CanRead;
|
||||
public override bool CanSeek => _baseStream.CanSeek;
|
||||
public override bool CanWrite => _baseStream.CanWrite;
|
||||
public override long Length => _baseStream.Length - _offset;
|
||||
public override long Position
|
||||
{
|
||||
get => _baseStream.Position - _offset;
|
||||
set
|
||||
{
|
||||
ValidateSizeMultiple(value);
|
||||
_baseStream.Position = value + _offset;
|
||||
CurrentSector = value / SectorSize;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that the size is a multiple of the sector size and smaller than the max buffer size
|
||||
/// </summary>
|
||||
protected void ValidateSize(long value)
|
||||
{
|
||||
ValidateSizeMultiple(value);
|
||||
|
||||
if (value > _maxBufferSize)
|
||||
throw new ArgumentException($"Value cannot be greater than {_maxBufferSize}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that the size is a multiple of the sector size
|
||||
/// </summary>
|
||||
protected void ValidateSizeMultiple(long value)
|
||||
{
|
||||
if (value < 0)
|
||||
throw new ArgumentException("Value must be non-negative");
|
||||
if (value % SectorSize != 0)
|
||||
throw new ArgumentException($"Value must be a multiple of {SectorSize}");
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_keepOpen)
|
||||
{
|
||||
_baseStream.Dispose();
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -317,6 +317,11 @@ namespace libhac
|
|||
public static int DivideByRoundUp(int value, int divisor) => (value + divisor - 1) / divisor;
|
||||
public static long DivideByRoundUp(long value, long divisor) => (value + divisor - 1) / divisor;
|
||||
|
||||
public static int AlignUp(int value, int multiple) => AlignDown(value + multiple - 1, multiple);
|
||||
public static long AlignUp(long value, long multiple) => AlignDown(value + multiple - 1, multiple);
|
||||
public static int AlignDown(int value, int multiple) => value - value % multiple;
|
||||
public static long AlignDown(long value, long multiple) => value - value % multiple;
|
||||
|
||||
public static void MemDump(this StringBuilder sb, string prefix, byte[] data)
|
||||
{
|
||||
|
||||
|
@ -357,7 +362,8 @@ namespace libhac
|
|||
case 1: return "3.0.0";
|
||||
case 2: return "3.0.1-3.0.2";
|
||||
case 3: return "4.0.0-4.1.0";
|
||||
case 4: return "5.0.0";
|
||||
case 4: return "5.0.0-5.1.0";
|
||||
case 5: return "6.0.0";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,390 +0,0 @@
|
|||
// Copyright (c) 2010 Gareth Lennox (garethl@dwakn.com)
|
||||
// All rights reserved.
|
||||
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
|
||||
// * Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
// * Neither the name of Gareth Lennox nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from this
|
||||
// software without specific prior written permission.
|
||||
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace libhac.XTSSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// A wraps a sector based stream and provides random access to it
|
||||
/// </summary>
|
||||
public class RandomAccessSectorStream : Stream
|
||||
{
|
||||
private byte[] _buffer;
|
||||
private readonly int _bufferSize;
|
||||
private readonly SectorStream _s;
|
||||
private readonly bool _keepOpen;
|
||||
private bool _bufferDirty;
|
||||
private bool _bufferLoaded;
|
||||
private int _bufferPos;
|
||||
private int _currentBufferSize;
|
||||
|
||||
private long PhysicalRead { get; set; }
|
||||
private long VirtualRead { get; set; }
|
||||
private int CacheMisses { get; set; }
|
||||
private int CacheHits { get; set; }
|
||||
|
||||
// List should work just fine for a tiny cache
|
||||
private List<Sector> Cache { get; }
|
||||
private const int CacheSize = 4;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="s">Base stream</param>
|
||||
public RandomAccessSectorStream(SectorStream s)
|
||||
: this(s, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="s">Base stream</param>
|
||||
/// <param name="keepOpen">Should this stream leave the base stream open when disposed?</param>
|
||||
public RandomAccessSectorStream(SectorStream s, bool keepOpen)
|
||||
{
|
||||
_s = s;
|
||||
_keepOpen = keepOpen;
|
||||
_buffer = new byte[s.SectorSize];
|
||||
_bufferSize = s.SectorSize;
|
||||
|
||||
Cache = new List<Sector>(CacheSize);
|
||||
|
||||
for (int i = 0; i < CacheSize; i++)
|
||||
{
|
||||
Cache.Add(new Sector
|
||||
{
|
||||
Position = -1,
|
||||
Data = new byte[_bufferSize]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports reading.
|
||||
/// </summary>
|
||||
/// <returns>true if the stream supports reading; otherwise, false.</returns>
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return _s.CanRead; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports seeking.
|
||||
/// </summary>
|
||||
/// <returns>true if the stream supports seeking; otherwise, false.</returns>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return _s.CanSeek; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports writing.
|
||||
/// </summary>
|
||||
/// <returns>true if the stream supports writing; otherwise, false.</returns>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return _s.CanWrite; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length in bytes of the stream.
|
||||
/// </summary>
|
||||
/// <returns>A long value representing the length of the stream in bytes.</returns>
|
||||
public override long Length
|
||||
{
|
||||
get { return _s.Length; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the position within the current stream.
|
||||
/// </summary>
|
||||
/// <returns>The current position within the stream.</returns>
|
||||
public override long Position
|
||||
{
|
||||
get { return _bufferLoaded ? (_s.Position - _currentBufferSize + _bufferPos) : _s.Position + _bufferPos; }
|
||||
set
|
||||
{
|
||||
if (value < 0L)
|
||||
throw new ArgumentOutOfRangeException("value");
|
||||
|
||||
var sectorPosition = (value % _bufferSize);
|
||||
var position = value - sectorPosition;
|
||||
|
||||
//see if its within the current sector
|
||||
if (_bufferLoaded)
|
||||
{
|
||||
var basePosition = _s.Position - _bufferSize;
|
||||
if (value > basePosition && value < basePosition + _bufferSize)
|
||||
{
|
||||
_bufferPos = (int)sectorPosition;
|
||||
return;
|
||||
}
|
||||
}
|
||||
//outside the current buffer
|
||||
|
||||
//write it
|
||||
if (_bufferDirty)
|
||||
WriteSector();
|
||||
|
||||
_s.Position = position;
|
||||
|
||||
//read this sector
|
||||
ReadSector();
|
||||
|
||||
//bump us forward if need be
|
||||
_bufferPos = (int)sectorPosition;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases the unmanaged resources used by the <see cref="T:System.IO.Stream"/> and optionally releases the managed resources.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources. </param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
Flush();
|
||||
|
||||
base.Dispose(disposing);
|
||||
|
||||
if (!_keepOpen)
|
||||
_s.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
if (_bufferDirty)
|
||||
WriteSector();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the position within the current stream.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The new position within the current stream.
|
||||
/// </returns>
|
||||
/// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param>
|
||||
/// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
long newPosition;
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
newPosition = offset;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
newPosition = Length - offset;
|
||||
break;
|
||||
default:
|
||||
newPosition = Position + offset;
|
||||
break;
|
||||
}
|
||||
|
||||
Position = newPosition;
|
||||
|
||||
return newPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the length of the current stream.
|
||||
/// </summary>
|
||||
/// <param name="value">The desired length of the current stream in bytes.</param>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
var remainder = value % _s.SectorSize;
|
||||
|
||||
if (remainder > 0)
|
||||
{
|
||||
value = (value - remainder) + _bufferSize;
|
||||
}
|
||||
|
||||
_s.SetLength(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
/// </summary>
|
||||
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
|
||||
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source. </param>
|
||||
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
|
||||
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
var position = Position;
|
||||
|
||||
if (position + count > _s.Length)
|
||||
{
|
||||
count = (int)(_s.Length - position);
|
||||
}
|
||||
|
||||
if (!_bufferLoaded)
|
||||
ReadSector();
|
||||
|
||||
var totalBytesRead = 0;
|
||||
while (count > 0)
|
||||
{
|
||||
var bytesToRead = Math.Min(count, _bufferSize - _bufferPos);
|
||||
|
||||
Buffer.BlockCopy(_buffer, _bufferPos, buffer, offset, bytesToRead);
|
||||
|
||||
offset += bytesToRead;
|
||||
_bufferPos += bytesToRead;
|
||||
count -= bytesToRead;
|
||||
|
||||
totalBytesRead += bytesToRead;
|
||||
|
||||
if (_bufferPos == _bufferSize)
|
||||
ReadSector();
|
||||
}
|
||||
|
||||
VirtualRead += totalBytesRead;
|
||||
|
||||
return totalBytesRead;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref name="buffer"/> to the current stream.</param>
|
||||
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes to the current stream.</param>
|
||||
/// <param name="count">The number of bytes to be written to the current stream.</param>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
while (count > 0)
|
||||
{
|
||||
if (!_bufferLoaded)
|
||||
ReadSector();
|
||||
|
||||
var bytesToWrite = Math.Min(count, _bufferSize - _bufferPos);
|
||||
|
||||
Buffer.BlockCopy(buffer, offset, _buffer, _bufferPos, bytesToWrite);
|
||||
|
||||
offset += bytesToWrite;
|
||||
_bufferPos += bytesToWrite;
|
||||
count -= bytesToWrite;
|
||||
_bufferDirty = true;
|
||||
|
||||
if (_bufferPos == _bufferSize)
|
||||
WriteSector();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a sector
|
||||
/// </summary>
|
||||
private void ReadSector()
|
||||
{
|
||||
if (_bufferLoaded && _bufferDirty)
|
||||
{
|
||||
WriteSector();
|
||||
}
|
||||
|
||||
if (_s.Position == _s.Length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var sectorPosition = _s.Position;
|
||||
|
||||
for (int i = 0; i < CacheSize; i++)
|
||||
{
|
||||
var sector = Cache[i];
|
||||
if (sector.Position == sectorPosition)
|
||||
{
|
||||
if (i != 0)
|
||||
{
|
||||
Cache.RemoveAt(i);
|
||||
Cache.Insert(0, sector);
|
||||
}
|
||||
|
||||
_buffer = sector.Data;
|
||||
|
||||
_bufferLoaded = true;
|
||||
_bufferPos = 0;
|
||||
_bufferDirty = false;
|
||||
_currentBufferSize = sector.Length;
|
||||
CacheHits++;
|
||||
_s.Position += _currentBufferSize;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var item = Cache[CacheSize - 1];
|
||||
Cache.RemoveAt(CacheSize - 1);
|
||||
_buffer = item.Data;
|
||||
|
||||
var bytesRead = _s.Read(_buffer, 0, _buffer.Length);
|
||||
PhysicalRead += bytesRead;
|
||||
|
||||
//clean the end of it
|
||||
if (bytesRead != _bufferSize)
|
||||
Array.Clear(_buffer, bytesRead, _buffer.Length - bytesRead);
|
||||
|
||||
_bufferLoaded = true;
|
||||
_bufferPos = 0;
|
||||
_bufferDirty = false;
|
||||
_currentBufferSize = bytesRead;
|
||||
|
||||
item.Position = sectorPosition;
|
||||
item.Length = bytesRead;
|
||||
Cache.Insert(0, item);
|
||||
CacheMisses++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a sector
|
||||
/// </summary>
|
||||
private void WriteSector()
|
||||
{
|
||||
if (_bufferLoaded)
|
||||
{
|
||||
//go back to beginning of the current sector
|
||||
_s.Seek(-_bufferSize, SeekOrigin.Current);
|
||||
}
|
||||
|
||||
//write it
|
||||
_s.Write(_buffer, 0, _bufferSize);
|
||||
_bufferDirty = false;
|
||||
_bufferLoaded = false;
|
||||
_bufferPos = 0;
|
||||
Array.Clear(_buffer, 0, _bufferSize);
|
||||
}
|
||||
|
||||
private class Sector
|
||||
{
|
||||
public long Position { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
public int Length { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,260 +0,0 @@
|
|||
// Copyright (c) 2010 Gareth Lennox (garethl@dwakn.com)
|
||||
// All rights reserved.
|
||||
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
|
||||
// * Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
// * Neither the name of Gareth Lennox nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from this
|
||||
// software without specific prior written permission.
|
||||
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace libhac.XTSSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Sector-based stream
|
||||
/// </summary>
|
||||
public class SectorStream : Stream
|
||||
{
|
||||
private readonly Stream _baseStream;
|
||||
private readonly long _offset;
|
||||
private ulong _currentSector;
|
||||
private bool _keepOpen;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream to read/write from</param>
|
||||
/// <param name="sectorSize">The size of the sectors to read/write</param>
|
||||
public SectorStream(Stream baseStream, int sectorSize)
|
||||
: this(baseStream, sectorSize, 0)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream to read/write from</param>
|
||||
/// <param name="sectorSize">The size of the sectors to read/write</param>
|
||||
/// <param name="offset">Offset to start counting sectors</param>
|
||||
public SectorStream(Stream baseStream, int sectorSize, long offset)
|
||||
: this(baseStream, sectorSize, offset, false)
|
||||
{
|
||||
SectorSize = sectorSize;
|
||||
_baseStream = baseStream;
|
||||
_offset = offset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream to read/write from</param>
|
||||
/// <param name="sectorSize">The size of the sectors to read/write</param>
|
||||
/// <param name="offset">Offset to start counting sectors</param>
|
||||
/// <param name="keepOpen">Should this stream leave the base stream open when disposed?</param>
|
||||
public SectorStream(Stream baseStream, int sectorSize, long offset, bool keepOpen)
|
||||
{
|
||||
SectorSize = sectorSize;
|
||||
_baseStream = baseStream;
|
||||
_offset = offset;
|
||||
_keepOpen = keepOpen;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The size of the sectors
|
||||
/// </summary>
|
||||
public int SectorSize { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports reading.
|
||||
/// </summary>
|
||||
/// <returns>true if the stream supports reading; otherwise, false.</returns>
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return _baseStream.CanRead; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports seeking.
|
||||
/// </summary>
|
||||
/// <returns>true if the stream supports seeking; otherwise, false.</returns>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return _baseStream.CanSeek; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports writing.
|
||||
/// </summary>
|
||||
/// <returns>true if the stream supports writing; otherwise, false.</returns>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return _baseStream.CanWrite; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length in bytes of the stream.
|
||||
/// </summary>
|
||||
/// <returns>A long value representing the length of the stream in bytes.</returns>
|
||||
public override long Length
|
||||
{
|
||||
get { return _baseStream.Length - _offset; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the position within the current stream.
|
||||
/// </summary>
|
||||
/// <returns>The current position within the stream.</returns>
|
||||
public override long Position
|
||||
{
|
||||
get { return _baseStream.Position - _offset; }
|
||||
set
|
||||
{
|
||||
ValidateSizeMultiple(value);
|
||||
|
||||
//base stream gets the non-tweaked value
|
||||
_baseStream.Position = value + _offset;
|
||||
_currentSector = (ulong)(value / SectorSize);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The current sector this stream is at
|
||||
/// </summary>
|
||||
protected ulong CurrentSector => _currentSector;
|
||||
|
||||
/// <summary>
|
||||
/// Validates that the size is a multiple of the sector size
|
||||
/// </summary>
|
||||
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
|
||||
protected virtual void ValidateSizeMultiple(long value)
|
||||
{
|
||||
if (value % SectorSize != 0)
|
||||
throw new ArgumentException(string.Format("Value needs to be a multiple of {0}", SectorSize));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that the size is equal to the sector size
|
||||
/// </summary>
|
||||
protected virtual void ValidateSize(long value)
|
||||
{
|
||||
if (value != SectorSize)
|
||||
throw new ArgumentException(string.Format("Value needs to be {0}", SectorSize));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that the size is equal to the sector size
|
||||
/// </summary>
|
||||
protected virtual void ValidateSize(int value)
|
||||
{
|
||||
if (value != SectorSize)
|
||||
throw new ArgumentException(string.Format("Value needs to be {0}", SectorSize));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
_baseStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the position within the current stream.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The new position within the current stream.
|
||||
/// </returns>
|
||||
/// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param>
|
||||
/// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
long newPosition;
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
newPosition = offset;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
newPosition = Length - offset;
|
||||
break;
|
||||
default:
|
||||
newPosition = Position + offset;
|
||||
break;
|
||||
}
|
||||
|
||||
Position = newPosition;
|
||||
|
||||
return newPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the length of the current stream.
|
||||
/// </summary>
|
||||
/// <param name="value">The desired length of the current stream in bytes.</param>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
ValidateSizeMultiple(value);
|
||||
|
||||
_baseStream.SetLength(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
/// </summary>
|
||||
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
|
||||
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source. </param>
|
||||
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
|
||||
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
ValidateSize(count);
|
||||
|
||||
var ret = _baseStream.Read(buffer, offset, count);
|
||||
_currentSector++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref name="buffer"/> to the current stream.</param>
|
||||
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes to the current stream.</param>
|
||||
/// <param name="count">The number of bytes to be written to the current stream.</param>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
ValidateSize(count);
|
||||
|
||||
_baseStream.Write(buffer, offset, count);
|
||||
_currentSector++;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_keepOpen)
|
||||
{
|
||||
_baseStream.Dispose();
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@
|
|||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
using System.IO;
|
||||
using libhac.Streams;
|
||||
|
||||
namespace libhac.XTSSharp
|
||||
{
|
||||
|
@ -72,7 +73,7 @@ namespace libhac.XTSSharp
|
|||
/// <param name="sectorSize">Sector size</param>
|
||||
/// <param name="offset">Offset to start counting sectors</param>
|
||||
public XtsSectorStream(Stream baseStream, Xts xts, int sectorSize, long offset)
|
||||
: base(baseStream, sectorSize, offset)
|
||||
: base(baseStream, sectorSize, 1, offset)
|
||||
{
|
||||
_xts = xts;
|
||||
_tempBuffer = new byte[sectorSize];
|
||||
|
@ -113,7 +114,7 @@ namespace libhac.XTSSharp
|
|||
_encryptor = _xts.CreateEncryptor();
|
||||
|
||||
//encrypt the sector
|
||||
int transformedCount = _encryptor.TransformBlock(buffer, offset, count, _tempBuffer, 0, currentSector);
|
||||
int transformedCount = _encryptor.TransformBlock(buffer, offset, count, _tempBuffer, 0, (ulong) currentSector);
|
||||
|
||||
//Console.WriteLine("Encrypting sector {0}", currentSector);
|
||||
|
||||
|
@ -145,7 +146,7 @@ namespace libhac.XTSSharp
|
|||
_decryptor = _xts.CreateDecryptor();
|
||||
|
||||
//decrypt the sector
|
||||
var retV = _decryptor.TransformBlock(_tempBuffer, 0, ret, buffer, offset, currentSector);
|
||||
var retV = _decryptor.TransformBlock(_tempBuffer, 0, ret, buffer, offset, (ulong) currentSector);
|
||||
|
||||
//Console.WriteLine("Decrypting sector {0} == {1} bytes", currentSector, retV);
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
using System.IO;
|
||||
using libhac.Streams;
|
||||
|
||||
namespace libhac.XTSSharp
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue