Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Floyd's algorithm for FAT cycles #247

Merged
merged 2 commits into from
Nov 20, 2024
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
21 changes: 8 additions & 13 deletions OpenMcdf/FatChainEnumerator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

namespace OpenMcdf;

Expand All @@ -12,12 +11,12 @@ internal sealed class FatChainEnumerator : IEnumerator<uint>
{
private readonly Fat fat;
private readonly FatEnumerator fatEnumerator;
readonly HashSet<uint> visited = new();
private uint startId;
private bool start = true;
private uint index = uint.MaxValue;
private uint current = uint.MaxValue;
private long length = -1;
private uint slow = uint.MaxValue; // Floyd's cycle-finding algorithm

public FatChainEnumerator(Fat fat, uint startSectorId)
{
Expand Down Expand Up @@ -65,7 +64,7 @@ public bool MoveNext()
index = 0;
current = startId;
start = false;
visited.Add(current);
slow = startId;
return true;
}

Expand All @@ -83,23 +82,19 @@ public bool MoveNext()
index++;
if (index >= fat.Context.SectorCount)
{
// If the index is greater than the maximum, then the chain must contain a loop
index = uint.MaxValue;
current = uint.MaxValue;
throw new FileFormatException("FAT chain index is greater than the sector count.");
}

if (visited.Contains(value))
if (index % 2 == 0 && !SectorType.IsFreeOrEndOfChain(slow))
{
// If the sector has already been visited, then the chain must contain a loop
index = uint.MaxValue;
current = uint.MaxValue;
throw new FileFormatException("FAT chain contains a loop.");
// Slow might become free or end of chain while shrinking
slow = fat[slow];
if (slow == value)
throw new FileFormatException("FAT chain contains a loop.");
}

if (BitOperations.IsPow2(index))
visited.Add(value);

current = value;
return true;
}
Expand Down Expand Up @@ -246,7 +241,7 @@ public void Reset(uint startSectorId)
start = true;
index = uint.MaxValue;
current = uint.MaxValue;
visited.Clear();
slow = uint.MaxValue;
}

[ExcludeFromCodeCoverage]
Expand Down
18 changes: 14 additions & 4 deletions OpenMcdf/MiniFatChainEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal sealed class MiniFatChainEnumerator : ContextBase, IEnumerator<uint>
uint index = uint.MaxValue;
private uint current = uint.MaxValue;
private long length = -1;
private uint slow = uint.MaxValue; // Floyd's cycle-finding algorithm

public MiniFatChainEnumerator(RootContextSite rootContextSite, uint startSectorId)
: base(rootContextSite)
Expand Down Expand Up @@ -60,8 +61,8 @@ public bool MoveNext()
}
else if (!SectorType.IsFreeOrEndOfChain(current))
{
uint sectorId = Context.MiniFat[current];
if (sectorId == SectorType.EndOfChain)
uint value = Context.MiniFat[current];
if (value == SectorType.EndOfChain)
{
index = uint.MaxValue;
current = uint.MaxValue;
Expand All @@ -70,10 +71,18 @@ public bool MoveNext()

uint nextIndex = index + 1;
if (nextIndex > SectorType.Maximum)
throw new FileFormatException("Mini FAT chain is corrupt.");
throw new FileFormatException("Mini FAT chain length is greater than the maximum.");

if (nextIndex % 2 == 0 && !SectorType.IsFreeOrEndOfChain(slow))
{
// Slow might become free or end of chain while shrinking
slow = Context.MiniFat[slow];
if (slow == value)
throw new FileFormatException("Mini FAT chain contains a loop.");
}

index = nextIndex;
current = sectorId;
current = value;
return true;
}

Expand Down Expand Up @@ -203,6 +212,7 @@ public void Reset()
start = true;
index = uint.MaxValue;
current = uint.MaxValue;
slow = uint.MaxValue;
}

[ExcludeFromCodeCoverage]
Expand Down
6 changes: 0 additions & 6 deletions OpenMcdf/System/BitOperations.cs

This file was deleted.