diff --git a/BizHawk.Client.Common/movie/MovieImport.cs b/BizHawk.Client.Common/movie/MovieImport.cs index d2fe02b255..20019dad19 100644 --- a/BizHawk.Client.Common/movie/MovieImport.cs +++ b/BizHawk.Client.Common/movie/MovieImport.cs @@ -5,7 +5,9 @@ using System.Text; using System.IO; using BizHawk.Common; +using BizHawk.Common.BufferExtensions; using BizHawk.Common.IOExtensions; + using BizHawk.Emulation.Common; using BizHawk.Client.Common.MovieConversionExtensions; @@ -403,7 +405,7 @@ namespace BizHawk.Client.Common byte[] md5 = DecodeBlob(blob); if (md5 != null && md5.Length == 16) { - m.Header[MD5] = Util.BytesToHexString(md5).ToLower(); + m.Header[MD5] = md5.BytesToHexString().ToLower(); } else { @@ -596,7 +598,7 @@ namespace BizHawk.Client.Common uint firstFrameOffset = r.ReadUInt32(); // 020 16-byte md5sum of the ROM used byte[] md5 = r.ReadBytes(16); - m.Header[MD5] = Util.BytesToHexString(md5).ToLower(); + m.Header[MD5] = md5.BytesToHexString().ToLower(); // 030 4-byte little-endian unsigned int: version of the emulator used uint emuVersion = r.ReadUInt32(); m.Comments.Add(EMULATIONORIGIN + " FCEU " + emuVersion); @@ -1294,7 +1296,7 @@ namespace BizHawk.Client.Common byte[] md5 = r.ReadBytes(16); // Discard the second 16 bytes. r.ReadBytes(16); - m.Header[MD5] = Util.BytesToHexString(md5).ToLower(); + m.Header[MD5] = md5.BytesToHexString().ToLower(); // 030 64-byte Filename of the ROM used (with extension) string gameName = NullTerminated(r.ReadStringFixedAscii(64)); m.Header[HeaderKeys.GAMENAME] = gameName; @@ -1460,7 +1462,7 @@ namespace BizHawk.Client.Common m.Header[HeaderKeys.GAMENAME] = gameName; // 00e4-00f3: binary: rom MD5 digest byte[] md5 = r.ReadBytes(16); - m.Header[MD5] = string.Format("{0:x8}", Util.BytesToHexString(md5).ToLower()); + m.Header[MD5] = string.Format("{0:x8}", md5.BytesToHexString().ToLower()); var controllers = new SimpleController { Type = new ControllerDefinition { Name = "SMS Controller" }}; /* 76543210 diff --git a/BizHawk.Common/Extensions/BufferExtensions.cs b/BizHawk.Common/Extensions/BufferExtensions.cs index fe91cf9580..99ba6e2174 100644 --- a/BizHawk.Common/Extensions/BufferExtensions.cs +++ b/BizHawk.Common/Extensions/BufferExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.IO; +using System.Text; namespace BizHawk.Common.BufferExtensions { @@ -12,6 +13,7 @@ namespace BizHawk.Common.BufferExtensions { writer.Write("{0:X2}", b); } + writer.WriteLine(); } @@ -155,6 +157,32 @@ namespace BizHawk.Common.BufferExtensions } } + /// + /// Converts bytes to an uppercase string of hex numbers in upper case without any spacing or anything + /// + public static string BytesToHexString(this byte[] bytes) + { + var sb = new StringBuilder(); + foreach (var b in bytes) + { + sb.AppendFormat("{0:X2}", b); + } + + return sb.ToString(); + } + + public static bool FindBytes(this byte[] array, byte[] pattern) + { + var fidx = 0; + int result = Array.FindIndex(array, 0, array.Length, (byte b) => + { + fidx = (b == pattern[fidx]) ? fidx + 1 : 0; + return (fidx == pattern.Length); + }); + + return (result >= pattern.Length - 1); + } + #region Helpers private static int Hex2Int(char c) diff --git a/BizHawk.Common/Serializer.cs b/BizHawk.Common/Serializer.cs index 44e79decc9..845bf5cbe3 100644 --- a/BizHawk.Common/Serializer.cs +++ b/BizHawk.Common/Serializer.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.IO; using BizHawk.Common.IOExtensions; +using BizHawk.Common.BufferExtensions; namespace BizHawk.Common { @@ -226,7 +227,7 @@ namespace BizHawk.Common else { var temp = val ?? new byte[0]; - _tw.WriteLine("{0} {1}", name, Util.BytesToHexString(temp)); + _tw.WriteLine("{0} {1}", name, temp.BytesToHexString()); } } @@ -288,7 +289,7 @@ namespace BizHawk.Common else { var temp = val ?? new short[0]; - _tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.ShortBufferToByteBuffer(temp))); + _tw.WriteLine("{0} {1}", name, Util.ShortBufferToByteBuffer(temp).BytesToHexString()); } } @@ -310,7 +311,7 @@ namespace BizHawk.Common else { var temp = val ?? new ushort[0]; - _tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UshortBufferToByteBuffer(temp))); + _tw.WriteLine("{0} {1}", name, Util.UshortBufferToByteBuffer(temp).BytesToHexString()); } } @@ -352,7 +353,7 @@ namespace BizHawk.Common else { var temp = val ?? new int[0]; - _tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.IntBufferToByteBuffer(temp))); + _tw.WriteLine("{0} {1}", name, Util.IntBufferToByteBuffer(temp).BytesToHexString()); } } @@ -394,7 +395,7 @@ namespace BizHawk.Common else { var temp = val ?? new uint[0]; - _tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UintBufferToByteBuffer(temp))); + _tw.WriteLine("{0} {1}", name, Util.UintBufferToByteBuffer(temp).BytesToHexString()); } } diff --git a/BizHawk.Common/Util.cs b/BizHawk.Common/Util.cs index 9a5b0428b2..81736125d1 100644 --- a/BizHawk.Common/Util.cs +++ b/BizHawk.Common/Util.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Text; +using BizHawk.Common.BufferExtensions; + namespace BizHawk.Common { public static unsafe class Util @@ -16,18 +18,6 @@ namespace BizHawk.Common HexConvPtr = (char*)HexConvHandle.AddrOfPinnedObject().ToPointer(); } - public static bool FindBytes(byte[] array, byte[] pattern) - { - int fidx = 0; - int result = Array.FindIndex(array, 0, array.Length, (byte b) => - { - fidx = (b == pattern[fidx]) ? fidx + 1 : 0; - return (fidx == pattern.Length); - }); - - return (result >= pattern.Length - 1); - } - public static char* HexConvPtr { get; set; } public static string Hash_MD5(byte[] data, int offset, int len) @@ -35,7 +25,7 @@ namespace BizHawk.Common using (var md5 = System.Security.Cryptography.MD5.Create()) { md5.ComputeHash(data, offset, len); - return BytesToHexString(md5.Hash); + return md5.Hash.BytesToHexString(); } } @@ -49,7 +39,7 @@ namespace BizHawk.Common using (var sha1 = System.Security.Cryptography.SHA1.Create()) { sha1.ComputeHash(data, offset, len); - return BytesToHexString(sha1.Hash); + return sha1.Hash.BytesToHexString(); } } @@ -81,21 +71,6 @@ namespace BizHawk.Common return 0; } - /// - /// Converts bytes to an uppercase string of hex numbers in upper case without any spacing or anything - /// //could be extension method - /// - public static string BytesToHexString(byte[] bytes) - { - var sb = new StringBuilder(); - foreach (var b in bytes) - { - sb.AppendFormat("{0:X2}", b); - } - - return sb.ToString(); - } - // Could be extension method public static byte[] HexStringToBytes(string str) { diff --git a/BizHawk.Emulation.Common/Database/Database.cs b/BizHawk.Emulation.Common/Database/Database.cs index 01e5c62e19..495b14f89d 100644 --- a/BizHawk.Emulation.Common/Database/Database.cs +++ b/BizHawk.Emulation.Common/Database/Database.cs @@ -3,7 +3,9 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; + using BizHawk.Common; +using BizHawk.Common.BufferExtensions; namespace BizHawk.Emulation.Common { @@ -238,7 +240,7 @@ namespace BizHawk.Emulation.Common Console.WriteLine( "Game was not in DB. CRC: {0:X8} MD5: {1}", CRC32.Calculate(romData), - Util.BytesToHexString(System.Security.Cryptography.MD5.Create().ComputeHash(romData))); + System.Security.Cryptography.MD5.Create().ComputeHash(romData).BytesToHexString()); var ext = Path.GetExtension(fileName).ToUpperInvariant(); diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs index fdee3c0cd0..54a53da195 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Linq; -using BizHawk.Common; +using BizHawk.Common.BufferExtensions; namespace BizHawk.Emulation.Cores.Atari.Atari2600 { @@ -257,7 +257,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 // 3E cart bankswitching is triggered by storing the bank number // in address 3E using 'STA $3E', commonly followed by an // immediate mode LDA - return Util.FindBytes(rom, new byte[] { 0x85, 0x3E, 0xA9, 0x00 }); // STA $3E; LDA #$00 + return rom.FindBytes(new byte[] { 0x85, 0x3E, 0xA9, 0x00 }); // STA $3E; LDA #$00 } private static bool IsProbably3F(byte[] rom) @@ -443,12 +443,12 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 private static bool ContainsAny(byte[] rom, IEnumerable signatures) { - return signatures.Any(signature => Util.FindBytes(rom, signature)); + return signatures.Any(signature => rom.FindBytes(signature)); } private static bool ContainsAll(byte[] rom, IEnumerable signatures) { - return signatures.All(signature => Util.FindBytes(rom, signature)); + return signatures.All(signature => rom.FindBytes(signature)); } } }