diff --git a/BizHawk.Client.Common/FirmwareManager.cs b/BizHawk.Client.Common/FirmwareManager.cs index 083b98f2e9..14d0d315d0 100644 --- a/BizHawk.Client.Common/FirmwareManager.cs +++ b/BizHawk.Client.Common/FirmwareManager.cs @@ -2,7 +2,7 @@ using System.IO; using System.Linq; -using BizHawk.Common; +using BizHawk.Common.BufferExtensions; using BizHawk.Emulation.Common; // IDEA: put filesizes in DB too. then scans can go real quick by only scanning filesizes that match (and then scanning filesizes that dont match, in case of an emergency) @@ -81,7 +81,7 @@ namespace BizHawk.Client.Common fs.Read(buffer, 0, (int)len); } - rff.Hash = Util.Hash_SHA1(buffer, 0, (int)len); + rff.Hash = buffer.HashSHA1(0, (int)len); dict[rff.Hash] = rff; _files.Add(rff); return rff; diff --git a/BizHawk.Client.Common/XmlGame.cs b/BizHawk.Client.Common/XmlGame.cs index 9015f5f608..d99ece5dd2 100644 --- a/BizHawk.Client.Common/XmlGame.cs +++ b/BizHawk.Client.Common/XmlGame.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Xml; using BizHawk.Common; +using BizHawk.Common.BufferExtensions; using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; @@ -94,7 +95,7 @@ namespace BizHawk.Client.Common } } - ret.GI.Hash = Util.Hash_SHA1(HashStream.GetBuffer(), 0, (int)HashStream.Length); + ret.GI.Hash = HashStream.GetBuffer().HashSHA1(0, (int)HashStream.Length); HashStream.Close(); if (OriginalIndex != null) { diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index 16fb87f486..335d2783fa 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -8,10 +8,13 @@ using System.Text; using System.Threading; using System.Windows.Forms; -using BizHawk.Client.Common; using BizHawk.Common; +using BizHawk.Common.BufferExtensions; using BizHawk.Common.IOExtensions; + +using BizHawk.Client.Common; using BizHawk.Bizware.BizwareGL; + using BizHawk.Emulation.Common; using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Emulation.Cores.Atari.Atari2600; @@ -3080,8 +3083,8 @@ namespace BizHawk.Client.EmuHawk Global.Emulator.CoreComm.RomStatusDetails = string.Format( "{0}\r\nSHA1:{1}\r\nMD5:{2}\r\n", loader.Game.Name, - Util.Hash_SHA1(loader.Rom.RomData), - Util.Hash_MD5(loader.Rom.RomData)); + loader.Rom.RomData.HashSHA1(), + loader.Rom.RomData.HashMD5()); } if (Global.Emulator.BoardName != null) diff --git a/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs b/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs index 53447950c3..2baf01b947 100644 --- a/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs +++ b/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs @@ -8,9 +8,8 @@ using System.Linq; using System.Text; using System.Windows.Forms; -using BizHawk.Common; +using BizHawk.Common.BufferExtensions; using BizHawk.Client.Common; -using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Emulation.Cores.PCEngine; using BizHawk.Emulation.Common.Components; @@ -93,7 +92,7 @@ namespace BizHawk.Client.EmuHawk foreach (var s in waveform) bw.Write(s); bw.Flush(); - string md5 = Util.Hash_MD5(waveformTemp); + string md5 = waveformTemp.HashMD5(); if (!PSGEntryTable.ContainsKey(md5)) { diff --git a/BizHawk.Common/Extensions/BufferExtensions.cs b/BizHawk.Common/Extensions/BufferExtensions.cs index 99ba6e2174..87792684f9 100644 --- a/BizHawk.Common/Extensions/BufferExtensions.cs +++ b/BizHawk.Common/Extensions/BufferExtensions.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.IO; using System.Text; +using System.Security.Cryptography; namespace BizHawk.Common.BufferExtensions { @@ -183,6 +184,34 @@ namespace BizHawk.Common.BufferExtensions return (result >= pattern.Length - 1); } + public static string HashMD5(this byte[] data, int offset, int len) + { + using (var md5 = MD5.Create()) + { + md5.ComputeHash(data, offset, len); + return md5.Hash.BytesToHexString(); + } + } + + public static string HashMD5(this byte[] data) + { + return HashMD5(data, 0, data.Length); + } + + public static string HashSHA1(this byte[] data, int offset, int len) + { + using (var sha1 = SHA1.Create()) + { + sha1.ComputeHash(data, offset, len); + return sha1.Hash.BytesToHexString(); + } + } + + public static string HashSHA1(this byte[] data) + { + return HashSHA1(data, 0, data.Length); + } + #region Helpers private static int Hex2Int(char c) diff --git a/BizHawk.Common/Util.cs b/BizHawk.Common/Util.cs index 81736125d1..1cd8db457f 100644 --- a/BizHawk.Common/Util.cs +++ b/BizHawk.Common/Util.cs @@ -20,34 +20,6 @@ namespace BizHawk.Common public static char* HexConvPtr { get; set; } - public static string Hash_MD5(byte[] data, int offset, int len) - { - using (var md5 = System.Security.Cryptography.MD5.Create()) - { - md5.ComputeHash(data, offset, len); - return md5.Hash.BytesToHexString(); - } - } - - public static string Hash_MD5(byte[] data) - { - return Hash_MD5(data, 0, data.Length); - } - - public static string Hash_SHA1(byte[] data, int offset, int len) - { - using (var sha1 = System.Security.Cryptography.SHA1.Create()) - { - sha1.ComputeHash(data, offset, len); - return sha1.Hash.BytesToHexString(); - } - } - - public static string Hash_SHA1(byte[] data) - { - return Hash_SHA1(data, 0, data.Length); - } - public static bool IsPowerOfTwo(int x) { if (x == 0 || x == 1) diff --git a/BizHawk.Emulation.Common/Database/Database.cs b/BizHawk.Emulation.Common/Database/Database.cs index 495b14f89d..8dc6669566 100644 --- a/BizHawk.Emulation.Common/Database/Database.cs +++ b/BizHawk.Emulation.Common/Database/Database.cs @@ -217,13 +217,13 @@ namespace BizHawk.Emulation.Common return new GameInfo(cgi); } - hash = Util.Hash_MD5(romData); + hash = romData.HashMD5(); if (db.TryGetValue(hash, out cgi)) { return new GameInfo(cgi); } - hash = Util.Hash_SHA1(romData); + hash = romData.HashSHA1(); if (db.TryGetValue(hash, out cgi)) { return new GameInfo(cgi); diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.Core.cs b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.Core.cs index 6a440408fd..85344800cc 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.Core.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.Core.cs @@ -2,6 +2,8 @@ using BizHawk.Common; using BizHawk.Common.NumberExtensions; +using BizHawk.Common.BufferExtensions; + using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Components.M6502; @@ -311,8 +313,8 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 string.Format( "{0}\r\nSHA1:{1}\r\nMD5:{2}\r\nMapper Impl \"{3}\"", this._game.Name, - Util.Hash_SHA1(Rom), - Util.Hash_MD5(Rom), + Rom.HashSHA1(), + Rom.HashMD5(), _mapper.GetType()); } diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs index 7dd815de7c..db96369a0e 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using BizHawk.Common; +using BizHawk.Common.BufferExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Atari.Atari2600 @@ -165,7 +166,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 Name = _game.Name, System = "A26", MetaData = "m=" + _mapper.GetType().ToString().Split('.').ToList().Last(), - Hash = Util.Hash_SHA1(Rom), + Hash = Rom.HashSHA1(), Region = _game.Region, Status = RomStatus.Unknown }; diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs index 774ff55764..8682f19f27 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs @@ -1,11 +1,12 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; -using BizHawk.Common; +using BizHawk.Common.BufferExtensions; using BizHawk.Emulation.Common; + using Newtonsoft.Json; -using System.ComponentModel; namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { @@ -145,8 +146,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy CoreComm.RomStatusDetails = string.Format("{0}\r\nSHA1:{1}\r\nMD5:{2}\r\n", game.Name, - Util.Hash_SHA1(romdata), Util.Hash_MD5(romdata) - ); + romdata.HashSHA1(), + romdata.HashMD5()); { byte[] buff = new byte[32]; diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs index bddb1abb95..9d6f8ff7b3 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs @@ -4,9 +4,11 @@ using System.IO; using System.Collections.Generic; using BizHawk.Common; -using BizHawk.Emulation.Common; -//TODO - redo all timekeeping in terms of master clock +using BizHawk.Common.BufferExtensions; +using BizHawk.Emulation.Common; + +//TODO - redo all timekeeping in terms of master clock namespace BizHawk.Emulation.Cores.Nintendo.NES { [CoreAttributes( @@ -546,9 +548,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //now that we know we have an iNES header, we can try to ignore it. - hash_sha1 = "sha1:" + Util.Hash_SHA1(file, 16, file.Length - 16); + hash_sha1 = "sha1:" + file.HashSHA1(16, file.Length - 16); hash_sha1_several.Add(hash_sha1); - hash_md5 = "md5:" + Util.Hash_MD5(file, 16, file.Length - 16); + hash_md5 = "md5:" + file.HashMD5(16, file.Length - 16); LoadWriteLine("Found iNES header:"); LoadWriteLine(iNesHeaderInfo.ToString()); @@ -572,10 +574,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES msTemp.Write(file, 16 + 16 * 1024, iNesHeaderInfo.chr_size * 1024); //add chr msTemp.Flush(); var bytes = msTemp.ToArray(); - var hash = "sha1:" + Util.Hash_SHA1(bytes, 0, bytes.Length); + var hash = "sha1:" + bytes.HashSHA1(0, bytes.Length); LoadWriteLine(" PRG (8KB) + CHR hash: {0}", hash); hash_sha1_several.Add(hash); - hash = "md5:" + Util.Hash_MD5(bytes, 0, bytes.Length); + hash = "md5:" + bytes.HashMD5(0, bytes.Length); LoadWriteLine(" PRG (8KB) + CHR hash: {0}", hash); } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs index 8b04ed13a0..b22075ab8b 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.IO; -using BizHawk.Common; +using BizHawk.Common.BufferExtensions; namespace BizHawk.Emulation.Cores.Nintendo.NES { @@ -80,13 +80,18 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } if (chunks.TryGetValue("MAPR", out tmp)) + { ci.board_type = Encoding.ASCII.GetString(tmp); + } + ci.board_type = ci.board_type.TrimEnd('\0'); ci.board_type = "UNIF_" + ci.board_type; if (chunks.TryGetValue("BATR", out tmp)) + { // apparently, this chunk just existing means battery is yes ci.wram_battery = true; + } // is there any way using System.Security.Cryptography.SHA1 to compute the hash of // prg concatentated with chr? i couldn't figure it out, so this implementation is dumb @@ -96,7 +101,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ms.Write(chrrom, 0, chrrom.Length); ms.Close(); byte[] all = ms.ToArray(); - ci.sha1 = "sha1:" + Util.Hash_SHA1(all, 0, all.Length); + ci.sha1 = "sha1:" + all.HashSHA1(0, all.Length); } // other code will expect this diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs index 59d4fadec7..7d18078c82 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs @@ -333,7 +333,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES if (game["SGB"]) { sgbRomData = CoreComm.CoreFileProvider.GetFirmware("SNES", "Rom_SGB", true, "SGB Rom is required for SGB emulation."); - game.FirmwareHash = Util.Hash_SHA1(sgbRomData); + game.FirmwareHash = sgbRomData.HashSHA1(); } ScanlineHookManager = new MyScanlineHookManager(this); diff --git a/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs b/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs index 81a5cc6beb..f1cb5c3bc8 100644 --- a/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs +++ b/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs @@ -4,8 +4,11 @@ using System.Globalization; using System.IO; using BizHawk.Common; +using BizHawk.Common.BufferExtensions; + using BizHawk.Emulation.Common; using BizHawk.Emulation.Common.Components; + using BizHawk.Emulation.Cores.Components.H6280; using BizHawk.Emulation.DiscSystem; @@ -131,7 +134,7 @@ namespace BizHawk.Emulation.Cores.PCEngine throw new Exception(); } - game.FirmwareHash = Util.Hash_SHA1(rom); + game.FirmwareHash = rom.HashSHA1(); Init(game, rom); // the default RomStatusDetails don't do anything with Disc diff --git a/BizHawk.Emulation.DiscSystem/Disc.API.cs b/BizHawk.Emulation.DiscSystem/Disc.API.cs index dc98247b2d..dee14259c6 100644 --- a/BizHawk.Emulation.DiscSystem/Disc.API.cs +++ b/BizHawk.Emulation.DiscSystem/Disc.API.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -using BizHawk.Common; +using BizHawk.Common.BufferExtensions; //main apis for emulator core routine use @@ -297,7 +297,7 @@ namespace BizHawk.Emulation.DiscSystem for (int s = 0; s < 512 && s < track.length_aba; s++) ReadABA_2352(track.Indexes[1].aba + s, buffer, s * 2352); - return Util.Hash_MD5(buffer, 0, lba_len * 2352); + return buffer.HashMD5(0, lba_len * 2352); } return "no data track found"; }