diff --git a/src/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs b/src/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs index baa70fc4f1..e50098fb59 100644 --- a/src/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs +++ b/src/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs @@ -39,13 +39,16 @@ namespace BizHawk.Emulation.Common string desc, string additionalInfo = "", bool isBad = false) - => filesByHash[hash] = new( - hash: hash, - size: size, - recommendedName: recommendedName, - desc: desc, - additionalInfo: additionalInfo, - isBad: isBad); + { + FirmwareFile ff = new( + hash: hash, + size: size, + recommendedName: recommendedName, + desc: desc, + additionalInfo: additionalInfo, + isBad: isBad); + return filesByHash[ff.Hash/*may have been reformatted*/] = ff; + } void Option(string systemId, string id, in FirmwareFile ff, FirmwareOptionStatus status = FirmwareOptionStatus.Acceptable) { diff --git a/src/BizHawk.Emulation.Common/Database/FirmwareFile.cs b/src/BizHawk.Emulation.Common/Database/FirmwareFile.cs index a855962b5c..4dd59396ec 100644 --- a/src/BizHawk.Emulation.Common/Database/FirmwareFile.cs +++ b/src/BizHawk.Emulation.Common/Database/FirmwareFile.cs @@ -1,7 +1,24 @@ +using System; +using System.Linq; + +using BizHawk.Common.StringExtensions; + namespace BizHawk.Emulation.Common { public readonly struct FirmwareFile { + internal static void CheckChecksumStrIsHex(ref string digest) + { + static bool IsAllowedCharacter(char c) + => c is (>= '0' and <= '9') or (>= 'A' and <= 'F'); //TODO SearchValues? + if (digest.All(IsAllowedCharacter)) return; + if (!digest.IsHex()) throw new ArgumentOutOfRangeException(paramName: nameof(digest), actualValue: digest, message: "malformed checksum digest: must match /[0-9A-F]+/ (no lowercase please)"); + // but if it is hex, let's be lenient + Console.Write("interpreting as hex checksum digest and uppercasing (please fix in source): "); + Console.WriteLine(digest); + digest = digest.ToUpperInvariant(); + } + public readonly string Description; public readonly string Hash; @@ -22,6 +39,7 @@ namespace BizHawk.Emulation.Common string additionalInfo = "", bool isBad = false) { + CheckChecksumStrIsHex(ref hash); Description = desc; Hash = hash; Info = additionalInfo; diff --git a/src/BizHawk.Emulation.Common/Database/FirmwareOption.cs b/src/BizHawk.Emulation.Common/Database/FirmwareOption.cs index fa5ec1dc1e..3fb72926b6 100644 --- a/src/BizHawk.Emulation.Common/Database/FirmwareOption.cs +++ b/src/BizHawk.Emulation.Common/Database/FirmwareOption.cs @@ -22,6 +22,7 @@ namespace BizHawk.Emulation.Common public FirmwareOption(FirmwareID id, string hash, long size, FirmwareOptionStatus status) { + FirmwareFile.CheckChecksumStrIsHex(ref hash); Hash = hash; ID = id; Size = size; diff --git a/src/BizHawk.Emulation.Common/Database/FirmwarePatchOption.cs b/src/BizHawk.Emulation.Common/Database/FirmwarePatchOption.cs index 6cd4d69876..66197c0a44 100644 --- a/src/BizHawk.Emulation.Common/Database/FirmwarePatchOption.cs +++ b/src/BizHawk.Emulation.Common/Database/FirmwarePatchOption.cs @@ -15,6 +15,8 @@ namespace BizHawk.Emulation.Common public FirmwarePatchOption(string baseHash, IReadOnlyList patches, string targetHash) { + FirmwareFile.CheckChecksumStrIsHex(ref baseHash); + FirmwareFile.CheckChecksumStrIsHex(ref targetHash); BaseHash = baseHash; Patches = patches; TargetHash = targetHash;