From 4e0db1719237385fb210ddc24dc76c4b5e38fb75 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 13 May 2024 23:07:24 -0700 Subject: [PATCH] Limit the hash target size to the file size when building a hashed PFS --- .../Tools/FsSystem/PartitionFileSystemBuilder.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs b/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs index b9858d21..2e8ec276 100644 --- a/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs +++ b/src/LibHac/Tools/FsSystem/PartitionFileSystemBuilder.cs @@ -23,6 +23,7 @@ public enum PartitionFileSystemType public class PartitionFileSystemBuilder { private const int HeaderSize = 0x10; + private const int DefaultHashTargetSize = 0x200; private List Entries { get; } = new List(); private long CurrentOffset { get; set; } @@ -58,7 +59,7 @@ public class PartitionFileSystemBuilder Offset = CurrentOffset, NameLength = Encoding.UTF8.GetByteCount(filename), HashOffset = 0, - HashLength = 0x200 + HashLength = (int)Math.Min(DefaultHashTargetSize, fileSize) }; CurrentOffset += entry.Length; @@ -163,7 +164,10 @@ public class PartitionFileSystemBuilder 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]; entry.File.Read(out long bytesRead, entry.HashOffset, data); @@ -180,7 +184,7 @@ public class PartitionFileSystemBuilder sha.GetHash(entry.Hash); } } - + public static int GetEntrySize(PartitionFileSystemType type) { switch (type)