mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
ConditionalHashEquals is fun
This commit is contained in:
@@ -285,8 +285,8 @@ namespace SabreTools.Core
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Return if all hashes match according to merge rules
|
// Return if all hashes match according to merge rules
|
||||||
return ConditionalHashEquals(self.ReadString(Disk.MD5Key), other.ReadString(Disk.MD5Key))
|
return Tools.Utilities.ConditionalHashEquals(self.ReadString(Disk.MD5Key), other.ReadString(Disk.MD5Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Disk.SHA1Key), other.ReadString(Disk.SHA1Key));
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Disk.SHA1Key), other.ReadString(Disk.SHA1Key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -303,10 +303,10 @@ namespace SabreTools.Core
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Return if all hashes match according to merge rules
|
// Return if all hashes match according to merge rules
|
||||||
return ConditionalHashEquals(self.ReadString(Media.MD5Key), other.ReadString(Media.MD5Key))
|
return Tools.Utilities.ConditionalHashEquals(self.ReadString(Media.MD5Key), other.ReadString(Media.MD5Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Media.SHA1Key), other.ReadString(Media.SHA1Key))
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Media.SHA1Key), other.ReadString(Media.SHA1Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Media.SHA256Key), other.ReadString(Media.SHA256Key))
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Media.SHA256Key), other.ReadString(Media.SHA256Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Media.SpamSumKey), other.ReadString(Media.SpamSumKey));
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Media.SpamSumKey), other.ReadString(Media.SpamSumKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -323,13 +323,13 @@ namespace SabreTools.Core
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Return if all hashes match according to merge rules
|
// Return if all hashes match according to merge rules
|
||||||
return ConditionalHashEquals(self.ReadString(Rom.CRCKey), other.ReadString(Rom.CRCKey))
|
return Tools.Utilities.ConditionalHashEquals(self.ReadString(Rom.CRCKey), other.ReadString(Rom.CRCKey))
|
||||||
&& ConditionalHashEquals(self.ReadString(Rom.MD5Key), other.ReadString(Rom.MD5Key))
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Rom.MD5Key), other.ReadString(Rom.MD5Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Rom.SHA1Key), other.ReadString(Rom.SHA1Key))
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Rom.SHA1Key), other.ReadString(Rom.SHA1Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Rom.SHA256Key), other.ReadString(Rom.SHA256Key))
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Rom.SHA256Key), other.ReadString(Rom.SHA256Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Rom.SHA384Key), other.ReadString(Rom.SHA384Key))
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Rom.SHA384Key), other.ReadString(Rom.SHA384Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Rom.SHA512Key), other.ReadString(Rom.SHA512Key))
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Rom.SHA512Key), other.ReadString(Rom.SHA512Key))
|
||||||
&& ConditionalHashEquals(self.ReadString(Rom.SpamSumKey), other.ReadString(Rom.SpamSumKey));
|
&& Tools.Utilities.ConditionalHashEquals(self.ReadString(Rom.SpamSumKey), other.ReadString(Rom.SpamSumKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -435,23 +435,6 @@ namespace SabreTools.Core
|
|||||||
return crcNull && md5Null && sha1Null && sha256Null && sha384Null && sha512Null && spamsumNull;
|
return crcNull && md5Null && sha1Null && sha256Null && sha384Null && sha512Null && spamsumNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determine if two hashes are equal for the purposes of merging
|
|
||||||
/// </summary>
|
|
||||||
private static bool ConditionalHashEquals(string? firstHash, string? secondHash)
|
|
||||||
{
|
|
||||||
// If either hash is empty, we say they're equal for merging
|
|
||||||
if (string.IsNullOrWhiteSpace(firstHash) || string.IsNullOrWhiteSpace(secondHash))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// If they're different sizes, they can't match
|
|
||||||
if (firstHash!.Length != secondHash!.Length)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Otherwise, they need to match exactly
|
|
||||||
return string.Equals(firstHash, secondHash, StringComparison.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns if there are no, non-empty hashes in common
|
/// Returns if there are no, non-empty hashes in common
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.IO;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace SabreTools.Core.Tools
|
namespace SabreTools.Core.Tools
|
||||||
{
|
{
|
||||||
@@ -7,6 +9,40 @@ namespace SabreTools.Core.Tools
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Utilities
|
public static class Utilities
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Determine if two hashes are equal for the purposes of merging
|
||||||
|
/// </summary>
|
||||||
|
public static bool ConditionalHashEquals(byte[]? firstHash, byte[]? secondHash)
|
||||||
|
{
|
||||||
|
// If either hash is empty, we say they're equal for merging
|
||||||
|
if (firstHash.IsNullOrEmpty() || secondHash.IsNullOrEmpty())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// If they're different sizes, they can't match
|
||||||
|
if (firstHash!.Length != secondHash!.Length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Otherwise, they need to match exactly
|
||||||
|
return Enumerable.SequenceEqual(firstHash, secondHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determine if two hashes are equal for the purposes of merging
|
||||||
|
/// </summary>
|
||||||
|
public static bool ConditionalHashEquals(string? firstHash, string? secondHash)
|
||||||
|
{
|
||||||
|
// If either hash is empty, we say they're equal for merging
|
||||||
|
if (string.IsNullOrWhiteSpace(firstHash) || string.IsNullOrWhiteSpace(secondHash))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// If they're different sizes, they can't match
|
||||||
|
if (firstHash!.Length != secondHash!.Length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Otherwise, they need to match exactly
|
||||||
|
return string.Equals(firstHash, secondHash, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a proper romba sub path
|
/// Get a proper romba sub path
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -399,26 +399,6 @@ namespace SabreTools.DatItems
|
|||||||
|
|
||||||
#region Sorting and Merging
|
#region Sorting and Merging
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determine if two hashes are equal for the purposes of merging
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="firstHash">First hash to compare</param>
|
|
||||||
/// <param name="secondHash">Second hash to compare</param>
|
|
||||||
/// <returns>True if either is empty OR the hashes exactly match, false otherwise</returns>
|
|
||||||
public static bool ConditionalHashEquals(byte[]? firstHash, byte[]? secondHash)
|
|
||||||
{
|
|
||||||
// If either hash is empty, we say they're equal for merging
|
|
||||||
if (firstHash.IsNullOrEmpty() || secondHash.IsNullOrEmpty())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// If they're different sizes, they can't match
|
|
||||||
if (firstHash!.Length != secondHash!.Length)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Otherwise, they need to match exactly
|
|
||||||
return Enumerable.SequenceEqual(firstHash, secondHash);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Merge an arbitrary set of ROMs based on the supplied information
|
/// Merge an arbitrary set of ROMs based on the supplied information
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -317,10 +317,10 @@ namespace SabreTools.DatItems.Formats
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Return if all hashes match according to merge rules
|
// Return if all hashes match according to merge rules
|
||||||
return ConditionalHashEquals(_crc, other._crc)
|
return Utilities.ConditionalHashEquals(_crc, other._crc)
|
||||||
&& ConditionalHashEquals(_md5, other._md5)
|
&& Utilities.ConditionalHashEquals(_md5, other._md5)
|
||||||
&& ConditionalHashEquals(_sha1, other._sha1)
|
&& Utilities.ConditionalHashEquals(_sha1, other._sha1)
|
||||||
&& ConditionalHashEquals(_sha256, other._sha256);
|
&& Utilities.ConditionalHashEquals(_sha256, other._sha256);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ namespace SabreTools.Test.DatItems
|
|||||||
[InlineData(new byte[] { 0x00 }, new byte[] { 0x00 }, true)]
|
[InlineData(new byte[] { 0x00 }, new byte[] { 0x00 }, true)]
|
||||||
public void ConditionalHashEqualsTest(byte[] first, byte[] second, bool expected)
|
public void ConditionalHashEqualsTest(byte[] first, byte[] second, bool expected)
|
||||||
{
|
{
|
||||||
bool actual = DatItem.ConditionalHashEquals(first, second);
|
bool actual = SabreTools.Core.Tools.Utilities.ConditionalHashEquals(first, second);
|
||||||
Assert.Equal(expected, actual);
|
Assert.Equal(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user