Limit the hash target size to the file size when building a hashed PFS

This commit is contained in:
Alex Barney 2024-05-13 23:07:24 -07:00
parent 71fafcac85
commit 4e0db17192

View file

@ -23,6 +23,7 @@ public enum PartitionFileSystemType
public class PartitionFileSystemBuilder public class PartitionFileSystemBuilder
{ {
private const int HeaderSize = 0x10; private const int HeaderSize = 0x10;
private const int DefaultHashTargetSize = 0x200;
private List<Entry> Entries { get; } = new List<Entry>(); private List<Entry> Entries { get; } = new List<Entry>();
private long CurrentOffset { get; set; } private long CurrentOffset { get; set; }
@ -58,7 +59,7 @@ public class PartitionFileSystemBuilder
Offset = CurrentOffset, Offset = CurrentOffset,
NameLength = Encoding.UTF8.GetByteCount(filename), NameLength = Encoding.UTF8.GetByteCount(filename),
HashOffset = 0, HashOffset = 0,
HashLength = 0x200 HashLength = (int)Math.Min(DefaultHashTargetSize, fileSize)
}; };
CurrentOffset += entry.Length; CurrentOffset += entry.Length;
@ -163,7 +164,10 @@ public class PartitionFileSystemBuilder
foreach (Entry entry in Entries) foreach (Entry entry in Entries)
{ {
if (entry.HashLength == 0) entry.HashLength = 0x200; if (entry.HashLength == 0)
{
entry.HashLength = (int)Math.Min(DefaultHashTargetSize, entry.Length);
}
byte[] data = new byte[entry.HashLength]; byte[] data = new byte[entry.HashLength];
entry.File.Read(out long bytesRead, entry.HashOffset, data); entry.File.Read(out long bytesRead, entry.HashOffset, data);
@ -180,7 +184,7 @@ public class PartitionFileSystemBuilder
sha.GetHash(entry.Hash); sha.GetHash(entry.Hash);
} }
} }
public static int GetEntrySize(PartitionFileSystemType type) public static int GetEntrySize(PartitionFileSystemType type)
{ {
switch (type) switch (type)