mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[DatTools, RomTools] Create method for cleaning file hashes
This commit is contained in:
@@ -15,6 +15,11 @@ namespace SabreTools.Helper
|
||||
public static string MD5Zero = "d41d8cd98f00b204e9800998ecf8427e";
|
||||
public static string SHA1Zero = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
|
||||
|
||||
// Hash string length constants
|
||||
public static int CRCLength = 8;
|
||||
public static int MD5Length = 32;
|
||||
public static int SHA1Length = 40;
|
||||
|
||||
// Regex File Name Patterns
|
||||
public static string DefaultPattern = @"^(.+?) - (.+?) \((.*) (.*)\)\.dat$";
|
||||
public static string DefaultSpecialPattern = @"^(.+?) - (.+?) \((.*) (.*)\)\.xml$";
|
||||
|
||||
@@ -279,36 +279,9 @@ namespace SabreTools.Helper
|
||||
}
|
||||
|
||||
// Sanitize the hashes from null, hex sizes, and "true blank" strings
|
||||
if (rom.CRC != null)
|
||||
{
|
||||
rom.CRC = (rom.CRC.StartsWith("0x") ? rom.CRC.Remove(0, 2) : rom.CRC);
|
||||
rom.CRC = (rom.CRC == "-" ? "" : rom.CRC);
|
||||
rom.CRC = (rom.CRC == "" ? "" : rom.CRC.PadLeft(8, '0'));
|
||||
}
|
||||
else
|
||||
{
|
||||
rom.CRC = "";
|
||||
}
|
||||
if (rom.MD5 != null)
|
||||
{
|
||||
rom.MD5 = (rom.MD5.StartsWith("0x") ? rom.MD5.Remove(0, 2) : rom.MD5);
|
||||
rom.MD5 = (rom.MD5 == "-" ? "" : rom.MD5);
|
||||
rom.MD5 = (rom.MD5 == "" ? "" : rom.MD5.PadLeft(32, '0'));
|
||||
}
|
||||
else
|
||||
{
|
||||
rom.MD5 = "";
|
||||
}
|
||||
if (rom.SHA1 != null)
|
||||
{
|
||||
rom.SHA1 = (rom.SHA1.StartsWith("0x") ? rom.SHA1.Remove(0, 2) : rom.SHA1);
|
||||
rom.SHA1 = (rom.SHA1 == "-" ? "" : rom.SHA1);
|
||||
rom.SHA1 = (rom.SHA1 == "" ? "" : rom.SHA1.PadLeft(40, '0'));
|
||||
}
|
||||
else
|
||||
{
|
||||
rom.SHA1 = "";
|
||||
}
|
||||
rom.CRC = RomTools.CleanHashData(rom.CRC, Constants.CRCLength);
|
||||
rom.MD5 = RomTools.CleanHashData(rom.MD5, Constants.MD5Length);
|
||||
rom.SHA1 = RomTools.CleanHashData(rom.SHA1, Constants.SHA1Length);
|
||||
|
||||
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
|
||||
if (rom.Type == "rom" && (rom.Size == 0 || rom.Size == -1) && ((rom.CRC == Constants.CRCZero || rom.CRC == "") || rom.MD5 == Constants.MD5Zero || rom.SHA1 == Constants.SHA1Zero))
|
||||
@@ -580,36 +553,9 @@ namespace SabreTools.Helper
|
||||
};
|
||||
|
||||
// Sanitize the hashes from null, hex sizes, and "true blank" strings
|
||||
if (rom.CRC != null)
|
||||
{
|
||||
rom.CRC = (rom.CRC.StartsWith("0x") ? rom.CRC.Remove(0, 2) : rom.CRC);
|
||||
rom.CRC = (rom.CRC == "-" ? "" : rom.CRC);
|
||||
rom.CRC = (rom.CRC == "" ? "" : rom.CRC.PadLeft(8, '0'));
|
||||
}
|
||||
else
|
||||
{
|
||||
rom.CRC = "";
|
||||
}
|
||||
if (rom.MD5 != null)
|
||||
{
|
||||
rom.MD5 = (rom.MD5.StartsWith("0x") ? rom.MD5.Remove(0, 2) : rom.MD5);
|
||||
rom.MD5 = (rom.MD5 == "-" ? "" : rom.MD5);
|
||||
rom.MD5 = (rom.MD5 == "" ? "" : rom.MD5.PadLeft(32, '0'));
|
||||
}
|
||||
else
|
||||
{
|
||||
rom.MD5 = "";
|
||||
}
|
||||
if (rom.SHA1 != null)
|
||||
{
|
||||
rom.SHA1 = (rom.SHA1.StartsWith("0x") ? rom.SHA1.Remove(0, 2) : rom.SHA1);
|
||||
rom.SHA1 = (rom.SHA1 == "-" ? "" : rom.SHA1);
|
||||
rom.SHA1 = (rom.SHA1 == "" ? "" : rom.SHA1.PadLeft(40, '0'));
|
||||
}
|
||||
else
|
||||
{
|
||||
rom.SHA1 = "";
|
||||
}
|
||||
rom.CRC = RomTools.CleanHashData(rom.CRC, Constants.CRCLength);
|
||||
rom.MD5 = RomTools.CleanHashData(rom.MD5, Constants.MD5Length);
|
||||
rom.SHA1 = RomTools.CleanHashData(rom.SHA1, Constants.SHA1Length);
|
||||
|
||||
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
|
||||
if (rom.Type == "rom" && (rom.Size == 0 || rom.Size == -1) && ((rom.CRC == Constants.CRCZero || rom.CRC == "") || rom.MD5 == Constants.MD5Zero || rom.SHA1 == Constants.SHA1Zero))
|
||||
@@ -1098,18 +1044,9 @@ namespace SabreTools.Helper
|
||||
}
|
||||
|
||||
// Sanitize the hashes from null, hex sizes, and "true blank" strings
|
||||
crc = (subreader.GetAttribute("crc") != null ? subreader.GetAttribute("crc").ToLowerInvariant().Trim() : "");
|
||||
crc = (crc.StartsWith("0x") ? crc.Remove(0, 2) : crc);
|
||||
crc = (crc == "-" ? "" : crc);
|
||||
crc = (crc == "" ? "" : crc.PadLeft(8, '0'));
|
||||
md5 = (subreader.GetAttribute("md5") != null ? subreader.GetAttribute("md5").ToLowerInvariant().Trim() : "");
|
||||
md5 = (md5.StartsWith("0x") ? md5.Remove(0, 2) : md5);
|
||||
md5 = (md5 == "-" ? "" : md5);
|
||||
md5 = (md5 == "" ? "" : md5.PadLeft(32, '0'));
|
||||
sha1 = (subreader.GetAttribute("sha1") != null ? subreader.GetAttribute("sha1").ToLowerInvariant().Trim() : "");
|
||||
sha1 = (sha1.StartsWith("0x") ? sha1.Remove(0, 2) : sha1);
|
||||
sha1 = (sha1 == "-" ? "" : sha1);
|
||||
sha1 = (sha1 == "" ? "" : sha1.PadLeft(40, '0'));
|
||||
crc = RomTools.CleanHashData(subreader.GetAttribute("crc"), Constants.CRCLength);
|
||||
md5 = RomTools.CleanHashData(subreader.GetAttribute("md5"), Constants.MD5Length);
|
||||
sha1 = RomTools.CleanHashData(subreader.GetAttribute("sha1"), Constants.SHA1Length);
|
||||
|
||||
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
|
||||
if (subreader.Name == "rom" && (size == 0 || size == -1) &&
|
||||
@@ -1324,18 +1261,9 @@ namespace SabreTools.Helper
|
||||
}
|
||||
|
||||
// Sanitize the hashes from null, hex sizes, and "true blank" strings
|
||||
crc = (xtr.GetAttribute("crc") != null ? xtr.GetAttribute("crc").ToLowerInvariant().Trim() : "");
|
||||
crc = (crc.StartsWith("0x") ? crc.Remove(0, 2) : crc);
|
||||
crc = (crc == "-" ? "" : crc);
|
||||
crc = (crc == "" ? "" : crc.PadLeft(8, '0'));
|
||||
md5 = (xtr.GetAttribute("md5") != null ? xtr.GetAttribute("md5").ToLowerInvariant().Trim() : "");
|
||||
md5 = (md5.StartsWith("0x") ? md5.Remove(0, 2) : md5);
|
||||
md5 = (md5 == "-" ? "" : md5);
|
||||
md5 = (md5 == "" ? "" : md5.PadLeft(32, '0'));
|
||||
sha1 = (xtr.GetAttribute("sha1") != null ? xtr.GetAttribute("sha1").ToLowerInvariant().Trim() : "");
|
||||
sha1 = (sha1.StartsWith("0x") ? sha1.Remove(0, 2) : sha1);
|
||||
sha1 = (sha1 == "-" ? "" : sha1);
|
||||
sha1 = (sha1 == "" ? "" : sha1.PadLeft(40, '0'));
|
||||
crc = RomTools.CleanHashData(xtr.GetAttribute("crc"), Constants.CRCLength);
|
||||
md5 = RomTools.CleanHashData(xtr.GetAttribute("md5"), Constants.MD5Length);
|
||||
sha1 = RomTools.CleanHashData(xtr.GetAttribute("sha1"), Constants.SHA1Length);
|
||||
|
||||
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
|
||||
if (xtr.GetAttribute("type") == "rom" && (size == 0 || size == -1) && ((crc == Constants.CRCZero || crc == "") || md5 == Constants.MD5Zero || sha1 == Constants.SHA1Zero))
|
||||
|
||||
@@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SabreTools.Helper
|
||||
{
|
||||
@@ -317,5 +318,28 @@ namespace SabreTools.Helper
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clean a hash string and pad to the correct size
|
||||
/// </summary>
|
||||
/// <param name="hash">Hash string to sanitize</param>
|
||||
/// <param name="padding">Amount of characters to pad to</param>
|
||||
/// <returns>Cleaned string</returns>
|
||||
public static string CleanHashData(string hash, int padding)
|
||||
{
|
||||
// First get the hash to the correct length
|
||||
hash = (String.IsNullOrEmpty(hash) ? "" : hash.Trim());
|
||||
hash = (hash.StartsWith("0x") ? hash.Remove(0, 2) : hash);
|
||||
hash = (hash == "-" ? "" : hash);
|
||||
hash = (String.IsNullOrEmpty(hash) ? "" : hash.PadLeft(padding, '0'));
|
||||
|
||||
// Then make sure that it has the correct characters
|
||||
if (!Regex.IsMatch(hash.ToLowerInvariant(), "[0-9a-f]{" + padding + "}"))
|
||||
{
|
||||
hash = "";
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user