Skip to content

Commit

Permalink
fm2 hash parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
adelikat committed Nov 11, 2024
1 parent 50360a7 commit eb3ab42
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
39 changes: 38 additions & 1 deletion TASVideos.Parsers/Parsers/Fm2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TASVideos.MovieParsers.Parsers;
using System.Text;

namespace TASVideos.MovieParsers.Parsers;

[FileExtension("fm2")]
internal class Fm2 : Parser, IParser
Expand Down Expand Up @@ -55,9 +57,43 @@ public async Task<IParseResult> Parse(Stream file, long length)
result.StartType = MovieStartType.Savestate;
}

var hashLine = header.GetValueFor(Keys.RomChecksum);
if (!string.IsNullOrWhiteSpace(hashLine))
{
var hashSplit = hashLine.Split(':');
var base64Line = hashSplit.Length == 2 ? hashSplit[1] : "";
if (!string.IsNullOrWhiteSpace(base64Line))
{
try
{
byte[] data = Convert.FromBase64String(base64Line);
string hash = BytesToHexString(data.AsSpan());
if (hash.Length == 32)
{
result.Hashes.Add(HashType.Md5, hash.ToLower());
}
}
catch
{
// Treat an invalid base64 hash as a missing hash
}
}
}

return result;
}

private static string BytesToHexString(ReadOnlySpan<byte> bytes)
{
StringBuilder sb = new(capacity: 2 * bytes.Length, maxCapacity: 2 * bytes.Length);
foreach (var b in bytes)
{
sb.Append($"{b:X2}");
}

return sb.ToString();
}

private static class Keys
{
public const string RerecordCount = "rerecordcount";
Expand All @@ -66,5 +102,6 @@ private static class Keys
public const string Length = "length";
public const string Fds = "fds";
public const string StartsFromSavestate = "savestate";
public const string RomChecksum = "romChecksum";
}
}
16 changes: 16 additions & 0 deletions tests/TASVideos.MovieParsers.Tests/Fm2ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ public async Task BinaryWithoutFrameCount()
AssertNoWarnings(result);
Assert.AreEqual(1, result.Errors.Count());
}

[TestMethod]
public async Task Hash()
{
var result = await _fm2Parser.Parse(Embedded("hash.fm2"), EmbeddedLength("hash.fm2"));
Assert.AreEqual(1, result.Hashes.Count);
Assert.AreEqual(HashType.Md5, result.Hashes.First().Key);
Assert.AreEqual("e9d82f825725c616b0be66ac85dc1b7a", result.Hashes.First().Value);
}

[TestMethod]
public async Task InvalidHash()
{
var result = await _fm2Parser.Parse(Embedded("hash-invalid.fm2"), EmbeddedLength("hash-invalid.fm2"));
Assert.AreEqual(0, result.Hashes.Count);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
romChecksum base64:ThisIsNotBase64

Empty file.
1 change: 1 addition & 0 deletions tests/TASVideos.MovieParsers.Tests/Fm2SampleFiles/hash.fm2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
romChecksum base64:6DgvglcLxhawvMAshDwbeQ==

0 comments on commit eb3ab42

Please sign in to comment.