diff --git a/NandReaderGui/ViewModel/NandViewModel.cs b/NandReaderGui/ViewModel/NandViewModel.cs index 5250af3c..c94bb8dd 100644 --- a/NandReaderGui/ViewModel/NandViewModel.cs +++ b/NandReaderGui/ViewModel/NandViewModel.cs @@ -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 { diff --git a/hactoolnet/Program.cs b/hactoolnet/Program.cs index 1b6a7609..90eb20ac 100644 --- a/hactoolnet/Program.cs +++ b/hactoolnet/Program.cs @@ -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) { diff --git a/libhac.Nand/Nand.cs b/libhac.Nand/Nand.cs index b9ca5804..48a76182 100644 --- a/libhac.Nand/Nand.cs +++ b/libhac.Nand/Nand.cs @@ -4,6 +4,7 @@ using DiscUtils; using DiscUtils.Fat; using DiscUtils.Partitions; using DiscUtils.Streams; +using libhac.Streams; using libhac.XTSSharp; namespace libhac.Nand diff --git a/libhac/Aes128CtrStream.cs b/libhac/Aes128CtrStream.cs index bf45e0c6..bc0a5546 100644 --- a/libhac/Aes128CtrStream.cs +++ b/libhac/Aes128CtrStream.cs @@ -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 /// The decryption key /// The intial counter 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 /// Offset to add to the counter /// The value of the upper 64 bits of the counter 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}"); - } } } diff --git a/libhac/BktrCryptoStream.cs b/libhac/BktrCryptoStream.cs index 51ad28bd..1bc647a0 100644 --- a/libhac/BktrCryptoStream.cs +++ b/libhac/BktrCryptoStream.cs @@ -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) { } } } diff --git a/libhac/Crypto.cs b/libhac/Crypto.cs index f9e35273..ed3e35d6 100644 --- a/libhac/Crypto.cs +++ b/libhac/Crypto.cs @@ -2,7 +2,7 @@ using System.IO; using System.Numerics; using System.Security.Cryptography; -using libhac.XTSSharp; +using libhac.Streams; namespace libhac { diff --git a/libhac/Nax0.cs b/libhac/Nax0.cs index 438d1a71..428ce27d 100644 --- a/libhac/Nax0.cs +++ b/libhac/Nax0.cs @@ -2,6 +2,7 @@ using System.IO; using System.Security.Cryptography; using System.Text; +using libhac.Streams; using libhac.XTSSharp; namespace libhac diff --git a/libhac/Streams/RandomAccessSectorStream.cs b/libhac/Streams/RandomAccessSectorStream.cs new file mode 100644 index 00000000..5b716d9e --- /dev/null +++ b/libhac/Streams/RandomAccessSectorStream.cs @@ -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(); + } + } +} diff --git a/libhac/Streams/SectorStream.cs b/libhac/Streams/SectorStream.cs new file mode 100644 index 00000000..7ca785e8 --- /dev/null +++ b/libhac/Streams/SectorStream.cs @@ -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; + + /// + /// The size of the sectors. + /// + public int SectorSize { get; } + + /// + /// The maximum number of sectors that can be read or written in a single operation. + /// + public int MaxSectors { get; } + + /// + /// The current sector this stream is at + /// + protected long CurrentSector { get; private set; } + + /// + /// Creates a new stream + /// + /// The base stream to read/write from + /// The size of the sectors to read/write + public SectorStream(Stream baseStream, int sectorSize) + : this(baseStream, sectorSize, 1, 0) + { + } + + /// + /// Creates a new stream + /// + /// The base stream to read/write from + /// The size of the sectors to read/write + /// The maximum number of sectors to read/write at once + /// Offset to start counting sectors + public SectorStream(Stream baseStream, int sectorSize, int maxSectors, long offset) + : this(baseStream, sectorSize, maxSectors, offset, false) + { + } + + /// + /// Creates a new stream + /// + /// The base stream to read/write from + /// The size of the sectors to read/write + /// The maximum number of sectors to read/write at once + /// Offset to start counting sectors + /// Should this stream leave the base stream open when disposed? + 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; + } + } + + /// + /// Validates that the size is a multiple of the sector size and smaller than the max buffer size + /// + protected void ValidateSize(long value) + { + ValidateSizeMultiple(value); + + if (value > _maxBufferSize) + throw new ArgumentException($"Value cannot be greater than {_maxBufferSize}"); + } + + /// + /// Validates that the size is a multiple of the sector size + /// + 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); + } + } +} diff --git a/libhac/Util.cs b/libhac/Util.cs index 9cefc139..ee02ece6 100644 --- a/libhac/Util.cs +++ b/libhac/Util.cs @@ -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"; } } diff --git a/libhac/XTSSharp/RandomAccessSectorStream.cs b/libhac/XTSSharp/RandomAccessSectorStream.cs deleted file mode 100644 index b01f6c9c..00000000 --- a/libhac/XTSSharp/RandomAccessSectorStream.cs +++ /dev/null @@ -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 -{ - /// - /// A wraps a sector based stream and provides random access to it - /// - 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 Cache { get; } - private const int CacheSize = 4; - - /// - /// Creates a new stream - /// - /// Base stream - public RandomAccessSectorStream(SectorStream s) - : this(s, true) - { - } - - /// - /// Creates a new stream - /// - /// Base stream - /// Should this stream leave the base stream open when disposed? - public RandomAccessSectorStream(SectorStream s, bool keepOpen) - { - _s = s; - _keepOpen = keepOpen; - _buffer = new byte[s.SectorSize]; - _bufferSize = s.SectorSize; - - Cache = new List(CacheSize); - - for (int i = 0; i < CacheSize; i++) - { - Cache.Add(new Sector - { - Position = -1, - Data = new byte[_bufferSize] - }); - } - } - - /// - /// Gets a value indicating whether the current stream supports reading. - /// - /// true if the stream supports reading; otherwise, false. - public override bool CanRead - { - get { return _s.CanRead; } - } - - /// - /// Gets a value indicating whether the current stream supports seeking. - /// - /// true if the stream supports seeking; otherwise, false. - public override bool CanSeek - { - get { return _s.CanSeek; } - } - - /// - /// Gets a value indicating whether the current stream supports writing. - /// - /// true if the stream supports writing; otherwise, false. - public override bool CanWrite - { - get { return _s.CanWrite; } - } - - /// - /// Gets the length in bytes of the stream. - /// - /// A long value representing the length of the stream in bytes. - public override long Length - { - get { return _s.Length; } - } - - /// - /// Gets or sets the position within the current stream. - /// - /// The current position within the stream. - 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; - } - } - - /// - /// Releases the unmanaged resources used by the and optionally releases the managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected override void Dispose(bool disposing) - { - Flush(); - - base.Dispose(disposing); - - if (!_keepOpen) - _s.Dispose(); - } - - /// - /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. - /// - public override void Flush() - { - if (_bufferDirty) - WriteSector(); - } - - /// - /// Sets the position within the current stream. - /// - /// - /// The new position within the current stream. - /// - /// A byte offset relative to the parameter. - /// A value of type indicating the reference point used to obtain the new position. - 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; - } - - /// - /// Sets the length of the current stream. - /// - /// The desired length of the current stream in bytes. - public override void SetLength(long value) - { - var remainder = value % _s.SectorSize; - - if (remainder > 0) - { - value = (value - remainder) + _bufferSize; - } - - _s.SetLength(value); - } - - /// - /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. - /// - /// 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. - /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. - /// The zero-based byte offset in at which to begin storing the data read from the current stream. - /// The maximum number of bytes to be read from the current stream. - 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; - } - - /// - /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. - /// - /// An array of bytes. This method copies bytes from to the current stream. - /// The zero-based byte offset in at which to begin copying bytes to the current stream. - /// The number of bytes to be written to the current stream. - 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(); - } - } - - /// - /// Reads a sector - /// - 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++; - } - - /// - /// Writes a sector - /// - 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; } - } - } -} \ No newline at end of file diff --git a/libhac/XTSSharp/SectorStream.cs b/libhac/XTSSharp/SectorStream.cs deleted file mode 100644 index 1b450a60..00000000 --- a/libhac/XTSSharp/SectorStream.cs +++ /dev/null @@ -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 -{ - /// - /// Sector-based stream - /// - public class SectorStream : Stream - { - private readonly Stream _baseStream; - private readonly long _offset; - private ulong _currentSector; - private bool _keepOpen; - - /// - /// Creates a new stream - /// - /// The base stream to read/write from - /// The size of the sectors to read/write - public SectorStream(Stream baseStream, int sectorSize) - : this(baseStream, sectorSize, 0) - { - } - - /// - /// Creates a new stream - /// - /// The base stream to read/write from - /// The size of the sectors to read/write - /// Offset to start counting sectors - public SectorStream(Stream baseStream, int sectorSize, long offset) - : this(baseStream, sectorSize, offset, false) - { - SectorSize = sectorSize; - _baseStream = baseStream; - _offset = offset; - } - - /// - /// Creates a new stream - /// - /// The base stream to read/write from - /// The size of the sectors to read/write - /// Offset to start counting sectors - /// Should this stream leave the base stream open when disposed? - public SectorStream(Stream baseStream, int sectorSize, long offset, bool keepOpen) - { - SectorSize = sectorSize; - _baseStream = baseStream; - _offset = offset; - _keepOpen = keepOpen; - } - - /// - /// The size of the sectors - /// - public int SectorSize { get; private set; } - - /// - /// Gets a value indicating whether the current stream supports reading. - /// - /// true if the stream supports reading; otherwise, false. - public override bool CanRead - { - get { return _baseStream.CanRead; } - } - - /// - /// Gets a value indicating whether the current stream supports seeking. - /// - /// true if the stream supports seeking; otherwise, false. - public override bool CanSeek - { - get { return _baseStream.CanSeek; } - } - - /// - /// Gets a value indicating whether the current stream supports writing. - /// - /// true if the stream supports writing; otherwise, false. - public override bool CanWrite - { - get { return _baseStream.CanWrite; } - } - - /// - /// Gets the length in bytes of the stream. - /// - /// A long value representing the length of the stream in bytes. - public override long Length - { - get { return _baseStream.Length - _offset; } - } - - /// - /// Gets or sets the position within the current stream. - /// - /// The current position within the stream. - 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); - } - } - - /// - /// The current sector this stream is at - /// - protected ulong CurrentSector => _currentSector; - - /// - /// Validates that the size is a multiple of the sector size - /// - // 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)); - } - - /// - /// Validates that the size is equal to the sector size - /// - protected virtual void ValidateSize(long value) - { - if (value != SectorSize) - throw new ArgumentException(string.Format("Value needs to be {0}", SectorSize)); - } - - /// - /// Validates that the size is equal to the sector size - /// - protected virtual void ValidateSize(int value) - { - if (value != SectorSize) - throw new ArgumentException(string.Format("Value needs to be {0}", SectorSize)); - } - - /// - /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. - /// - public override void Flush() - { - _baseStream.Flush(); - } - - /// - /// Sets the position within the current stream. - /// - /// - /// The new position within the current stream. - /// - /// A byte offset relative to the parameter. - /// A value of type indicating the reference point used to obtain the new position. - 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; - } - - /// - /// Sets the length of the current stream. - /// - /// The desired length of the current stream in bytes. - public override void SetLength(long value) - { - ValidateSizeMultiple(value); - - _baseStream.SetLength(value); - } - - /// - /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. - /// - /// 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. - /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. - /// The zero-based byte offset in at which to begin storing the data read from the current stream. - /// The maximum number of bytes to be read from the current stream. - public override int Read(byte[] buffer, int offset, int count) - { - ValidateSize(count); - - var ret = _baseStream.Read(buffer, offset, count); - _currentSector++; - return ret; - } - - /// - /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. - /// - /// An array of bytes. This method copies bytes from to the current stream. - /// The zero-based byte offset in at which to begin copying bytes to the current stream. - /// The number of bytes to be written to the current stream. - 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); - } - } -} \ No newline at end of file diff --git a/libhac/XTSSharp/XtsSectorStream.cs b/libhac/XTSSharp/XtsSectorStream.cs index 661ac3c5..e2c3f94e 100644 --- a/libhac/XTSSharp/XtsSectorStream.cs +++ b/libhac/XTSSharp/XtsSectorStream.cs @@ -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 /// Sector size /// Offset to start counting sectors 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); diff --git a/libhac/XTSSharp/XtsStream.cs b/libhac/XTSSharp/XtsStream.cs index f5117a71..562710e3 100644 --- a/libhac/XTSSharp/XtsStream.cs +++ b/libhac/XTSSharp/XtsStream.cs @@ -25,6 +25,7 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using System.IO; +using libhac.Streams; namespace libhac.XTSSharp {