forked from TASEmulators/BizHawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRomHasher.cs
262 lines (233 loc) · 6.2 KB
/
RomHasher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.DiscSystem;
namespace BizHawk.Client.DBMan
{
public class InitialRomInfo
{
public string FileName;
public string Name;
public string VersionTags;
public string GuessedSystem;
public string GuessedRegion;
public string CRC32;
public string MD5;
public string SHA1;
public long Size;
public override string ToString()
{
return FileName;
}
}
public static class RomHasher
{
public static InitialRomInfo Generate(string file)
{
//if (isDiscImage(file))
// return HashDiscImage(file);
return GenerateRomHashDirect(file, File.ReadAllBytes(file));
}
static char[] modifierStartChars = { '(', '[' };
static InitialRomInfo GenerateRomHashDirect(string file, byte[] filebytes)
{
var info = new InitialRomInfo();
var fileInfo = new FileInfo(file);
string ext = fileInfo.Extension.ToLowerInvariant().Replace(".", "");
info.FileName = fileInfo.Name;
// Parse the filename to guess things about the rom
var name = Path.GetFileNameWithoutExtension(fileInfo.Name);
if (name.StartsWith("[BIOS] "))
name = name.Replace("[BIOS] ","") + " [BIOS]";
string modifiers = "";
int modIndex = name.IndexOfAny(modifierStartChars);
if (modIndex > 0)
{
modifiers = name.Substring(modIndex);
name = name.Substring(0, modIndex);
}
info.Name = name.Trim();
// parse out modifiers
var mods = new List<string>();
modifiers = modifiers.Replace(")", ";").Replace("]",";");
modifiers = modifiers.Replace("(", "").Replace("[", "");
var m_ = modifiers.Split(';');
foreach (var mi in m_)
{
var m = mi.Trim();
if (m.Length == 0) continue;
mods.Add(m);
}
info.VersionTags = "";
foreach (var mi in mods)
{
if (info.VersionTags.Length != 0)
info.VersionTags += ";";
switch (mi.ToLower())
{
case "j":
case "jp":
case "jpn":
case "japan":
info.GuessedRegion = "Japan";
break;
case "usa":
case "us":
case "u":
info.GuessedRegion = "USA";
break;
case "europe":
case "eur":
case "e":
info.GuessedRegion = "Europe";
break;
case "world":
case "w":
info.GuessedRegion = "World";
break;
case "korea":
case "kr":
case "k":
info.GuessedRegion = "Korea";
break;
case "brazil":
case "br":
info.GuessedRegion = "Brazil";
break;
case "taiwan":
case "tw":
info.GuessedRegion = "Taiwan";
break;
case "usa, europe":
info.GuessedRegion = "USA;Europe";
break;
case "japan, europe":
info.GuessedRegion = "Europe;Japan";
break;
case "japan, usa":
info.GuessedRegion = "USA;Japan";
break;
default:
info.VersionTags += mi;
break;
}
}
// transform binary to canonical binary representation (de-header/de-stripe/de-swap)
byte[] romBytes = filebytes;
switch (ext)
{
case "sms":
case "gg":
case "sg":
case "pce":
case "sgx":
romBytes = MaybeStripHeader512(filebytes);
break;
case "smd":
if (filebytes.Length % 1024 == 512)
System.Windows.Forms.MessageBox.Show("derp");
romBytes = DeInterleaveSMD(filebytes);
break;
case "z64":
case "n64":
case "v64":
throw new NotImplementedException("n64 demutate not done");
}
// guess system
switch (ext)
{
case "sms": info.GuessedSystem = "SMS"; break;
case "gg": info.GuessedSystem = "GG"; break;
case "sg": info.GuessedSystem = "SG"; break;
case "pce": info.GuessedSystem = "PCE"; break;
case "sgx": info.GuessedSystem = "SGX"; break;
case "smd":
case "gen": info.GuessedSystem = "GEN"; break;
case "nes": info.GuessedSystem = "NES"; break;
default: info.GuessedSystem = "Unknown"; break;
}
// Perform hashing
info.CRC32 = Hash_CRC32(romBytes);
info.MD5 = Hash_MD5(romBytes);
info.SHA1 = Hash_SHA1(romBytes);
info.Size = romBytes.Length;
return info;
}
static string HashDiscImage(string file)
{
try
{
string ext = new FileInfo(file).Extension.ToLowerInvariant();
using (var disc = Disc.LoadAutomagic(file))
{
return disc.GetHash();
}
}
catch
{
return "Error Hashing Disc";
}
}
static string Hash_CRC32(byte[] data)
{
return string.Format("{0:X8}", CRC32.Calculate(data));
}
static string Hash_SHA1(byte[] data)
{
using (var sha1 = System.Security.Cryptography.SHA1.Create())
{
sha1.TransformFinalBlock(data, 0, data.Length);
return BytesToHexString(sha1.Hash);
}
}
static string Hash_MD5(byte[] data)
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
md5.TransformFinalBlock(data, 0, data.Length);
return BytesToHexString(md5.Hash);
}
}
static string BytesToHexString(byte[] bytes)
{
var sb = new StringBuilder();
foreach (var b in bytes)
sb.AppendFormat("{0:X2}", b);
return sb.ToString();
}
static byte[] MaybeStripHeader512(byte[] fileBytes)
{
if (fileBytes.Length % 1024 != 512)
return fileBytes;
var romBytes = new byte[fileBytes.Length - 512];
Array.Copy(fileBytes, 512, romBytes, 0, fileBytes.Length - 512);
return romBytes;
}
static byte[] DeInterleaveSMD(byte[] source)
{
int size = source.Length;
if (size > 0x400000)
size = 0x400000;
int pages = size / 0x4000;
var output = new byte[size];
for (int page = 0; page < pages; page++)
{
for (int i = 0; i < 0x2000; i++)
{
output[(page * 0x4000) + (i * 2) + 0] = source[(page * 0x4000) + 0x2000 + i];
output[(page * 0x4000) + (i * 2) + 1] = source[(page * 0x4000) + 0x0000 + i];
}
}
return output;
}
static bool isDiscImage(string file)
{
var ext = new FileInfo(file).Extension.ToLowerInvariant();
if (ext == ".cue" || ext == ".iso")
return true;
return false;
}
}
}