Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/DiscUtils.SquashFs/Directory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public IReadOnlyDictionary<string, DirectoryEntry> AllEntries
var reader = Context.DirectoryReader;
reader.SetPosition(_dirInode.StartBlock, _dirInode.Offset);

// For some reason, always 3 greater than actual..
// The 3 additional bytes are for ".." and "."
while (reader.DistanceFrom(_dirInode.StartBlock, _dirInode.Offset) < _dirInode.FileSize - 3)
{
var header = DirectoryHeader.ReadFrom(reader);
Expand Down
52 changes: 49 additions & 3 deletions Library/DiscUtils.SquashFs/MetablockReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,57 @@ public void SetPosition(long blockStart, int blockOffset)
_currentBlockStart = blockStart;
_currentOffset = blockOffset;
}


/// <summary>
/// Calculates the distance between the current position and the specified block position and offset.
/// </summary>
/// <remarks>
/// * BlockStart is always related to the start of the metablock in the raw data stream
/// * BlockOffset is the offset within the uncompressed data stream!
///
/// This means, that there can never be a distance calculation based on start and offset
/// across block boundaries
///
/// Therefor to calculate the distance across block boundaries we need to iteratively read
/// through all blocks from the starting block to the current block.
///
/// In most (standard) cases everything will be within the same block and the calculation is trivial.
/// </remarks>
/// <param name="blockStart">The start of the metadatablock with the raw data</param>
/// <param name="blockOffset">The offset within the uncompressed block</param>
/// <returns>The distance between </returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public long DistanceFrom(long blockStart, int blockOffset)
{
return (_currentBlockStart - blockStart) * VfsSquashFileSystemReader.MetadataBufferSize
+ (_currentOffset - blockOffset);
if (blockStart > _currentBlockStart)
{
throw new ArgumentOutOfRangeException("Block start needs to be less than or equal to current block start");
}

if (blockStart == _currentBlockStart)
{
return _currentOffset - blockOffset;
}

var block = _context.ReadMetaBlock(_start + blockStart);
long distance = Metablock.SQUASHFS_METADATA_SIZE - blockOffset;

do
{
block = _context.ReadMetaBlock(block.NextBlockStart);
blockStart = block.Position - _start;

if (blockStart == _currentBlockStart)
{
distance += _currentOffset;
}
else
{
distance += block.Data.LongLength;
}
} while (blockStart != _currentBlockStart);

return distance;
}

public void Skip(int count)
Expand Down
5 changes: 3 additions & 2 deletions Library/DiscUtils.SquashFs/VfsSquashFileSystemReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal class VfsSquashFileSystemReader : VfsReadOnlyFileSystem<DirectoryEntry,
public override bool IsCaseSensitive => true;

public const int MetadataBufferSize = 8 * 1024;
public const short BlockPrefixSize = 2;
private readonly BlockCache<Block> _blockCache;

private readonly Context _context;
Expand Down Expand Up @@ -272,7 +273,7 @@ private Metablock ReadMetaBlock(long pos)
var stream = _context.RawStream;
stream.Position = pos;

Span<byte> buffer = stackalloc byte[2];
Span<byte> buffer = stackalloc byte[BlockPrefixSize];
stream.ReadExactly(buffer);

int readLen = EndianUtilities.ToUInt16LittleEndian(buffer);
Expand All @@ -283,7 +284,7 @@ private Metablock ReadMetaBlock(long pos)
readLen = Metablock.SQUASHFS_COMPRESSED_BIT;
}

block.NextBlockStart = pos + readLen + 2;
block.NextBlockStart = pos + BlockPrefixSize + readLen;

if (isCompressed)
{
Expand Down