Extract out Skippers namespace

This commit is contained in:
Matt Nadareski
2020-12-07 13:02:42 -08:00
parent e7461370af
commit 8d593c585c
21 changed files with 234 additions and 70 deletions

View File

@@ -9,7 +9,7 @@ using SabreTools.Data;
using SabreTools.Library.DatFiles;
using SabreTools.Library.FileTypes;
using SabreTools.Library.Logging;
using SabreTools.Library.Skippers;
using SabreTools.Library.Tools;
namespace SabreTools.Library.IO
{
@@ -370,7 +370,7 @@ namespace SabreTools.Library.IO
// Try to match the supplied header skipper
if (header != null)
{
SkipperRule rule = Transform.GetMatchingRule(input, Path.GetFileNameWithoutExtension(header));
var rule = Transform.GetMatchingRule(input, Path.GetFileNameWithoutExtension(header));
// If there's a match, transform the stream before getting info
if (rule.Tests != null && rule.Tests.Count != 0)

View File

@@ -4,11 +4,11 @@ using System.IO;
using SabreTools.Data;
using SabreTools.Library.DatFiles;
using SabreTools.Library.FileTypes;
using SabreTools.Library.IO;
using SabreTools.Library.Logging;
using SabreTools.Library.Skippers;
using SabreTools.Library.Tools;
namespace SabreTools.Library.Skippers
namespace SabreTools.Library.IO
{
/// <summary>
/// Class for wrapping general file transformations
@@ -32,6 +32,11 @@ namespace SabreTools.Library.Skippers
/// </summary>
private static List<SkipperFile> List;
/// <summary>
/// Local paths
/// </summary>
private static string LocalPath = Path.Combine(Globals.ExeDir, "Skippers") + Path.DirectorySeparatorChar;
#region Logging
/// <summary>
@@ -41,11 +46,6 @@ namespace SabreTools.Library.Skippers
#endregion
/// <summary>
/// Local paths
/// </summary>
public static string LocalPath = Path.Combine(Globals.ExeDir, "Skippers") + Path.DirectorySeparatorChar;
/// <summary>
/// Initialize static fields
/// </summary>

View File

@@ -13,48 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\SabreTools.Data\SabreTools.Data.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="Skippers\a7800.xml" />
<None Remove="Skippers\fds.xml" />
<None Remove="Skippers\lynx.xml" />
<None Remove="Skippers\n64.xml" />
<None Remove="Skippers\nes.xml" />
<None Remove="Skippers\pce.xml" />
<None Remove="Skippers\psid.xml" />
<None Remove="Skippers\snes.xml" />
<None Remove="Skippers\spc.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="Skippers\a7800.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\fds.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\lynx.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\n64.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\nes.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\pce.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\psid.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\snes.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\spc.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<ProjectReference Include="..\SabreTools.Skippers\SabreTools.Skippers.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,36 +0,0 @@
namespace SabreTools.Library.Skippers
{
/// <summary>
/// Determines the header skip operation
/// </summary>
public enum HeaderSkipOperation
{
None = 0,
Bitswap,
Byteswap,
Wordswap,
WordByteswap,
}
/// <summary>
/// Determines the type of test to be done
/// </summary>
public enum HeaderSkipTest
{
Data = 0,
Or,
Xor,
And,
File,
}
/// <summary>
/// Determines the operator to be used in a file test
/// </summary>
public enum HeaderSkipTestFileOperator
{
Equal = 0,
Less,
Greater,
}
}

View File

@@ -1,410 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Xml;
using SabreTools.Library.IO;
namespace SabreTools.Library.Skippers
{
public class SkipperFile
{
#region Fields
/// <summary>
/// Skipper name
/// </summary>
public string Name { get; set; } = string.Empty;
/// <summary>
/// Author names
/// </summary>
public string Author { get; set; } = string.Empty;
/// <summary>
/// File version
/// </summary>
public string Version { get; set; } = string.Empty;
/// <summary>
/// Set of all rules in the skipper
/// </summary>
public List<SkipperRule> Rules { get; set; } = new List<SkipperRule>();
/// <summary>
/// Filename the skipper lives in
/// </summary>
public string SourceFile { get; set; } = string.Empty;
#endregion
#region Constructors
/// <summary>
/// Create an empty SkipperFile object
/// </summary>
public SkipperFile() { }
/// <summary>
/// Create a SkipperFile object parsed from an input file
/// </summary>
/// <param name="filename">Name of the file to parse</param>
public SkipperFile(string filename)
{
Rules = new List<SkipperRule>();
SourceFile = Path.GetFileNameWithoutExtension(filename);
XmlReader xtr = filename.GetXmlTextReader();
bool valid = Parse(xtr);
// If we somehow have an invalid file, zero out the fields
if (!valid)
{
Name = null;
Author = null;
Version = null;
Rules = null;
SourceFile = null;
}
}
#endregion
#region Parsing Helpers
/// <summary>
/// Parse an XML document in as a SkipperFile
/// </summary>
/// <param name="xtr">XmlReader representing the document</param>
/// <returns>True if the file could be parsed, false otherwise</returns>
private bool Parse(XmlReader xtr)
{
if (xtr == null)
return false;
try
{
bool valid = false;
xtr.MoveToContent();
while (!xtr.EOF)
{
if (xtr.NodeType != XmlNodeType.Element)
xtr.Read();
switch (xtr.Name.ToLowerInvariant())
{
case "detector":
valid = true;
xtr.Read();
break;
case "name":
Name = xtr.ReadElementContentAsString();
break;
case "author":
Author = xtr.ReadElementContentAsString();
break;
case "version":
Version = xtr.ReadElementContentAsString();
break;
case "rule":
SkipperRule rule = ParseRule(xtr);
if (rule != null)
Rules.Add(rule);
xtr.Skip();
break;
default:
xtr.Read();
break;
}
}
return valid;
}
catch
{
return false;
}
}
/// <summary>
/// Parse an XML document in as a SkipperRule
/// </summary>
/// <param name="xtr">XmlReader representing the document</param>
/// <returns>Filled SkipperRule on success, null otherwise</returns>
private SkipperRule ParseRule(XmlReader xtr)
{
if (xtr == null)
return null;
try
{
// Get the information from the rule first
SkipperRule rule = new SkipperRule
{
StartOffset = null,
EndOffset = null,
Operation = HeaderSkipOperation.None,
Tests = new List<SkipperTest>(),
SourceFile = this.SourceFile,
};
if (xtr.GetAttribute("start_offset") != null)
{
string offset = xtr.GetAttribute("start_offset");
if (offset.ToLowerInvariant() == "eof")
rule.StartOffset = null;
else
rule.StartOffset = Convert.ToInt64(offset, 16);
}
if (xtr.GetAttribute("end_offset") != null)
{
string offset = xtr.GetAttribute("end_offset");
if (offset.ToLowerInvariant() == "eof")
rule.EndOffset = null;
else
rule.EndOffset = Convert.ToInt64(offset, 16);
}
if (xtr.GetAttribute("operation") != null)
{
string operation = xtr.GetAttribute("operation");
switch (operation.ToLowerInvariant())
{
case "bitswap":
rule.Operation = HeaderSkipOperation.Bitswap;
break;
case "byteswap":
rule.Operation = HeaderSkipOperation.Byteswap;
break;
case "wordswap":
rule.Operation = HeaderSkipOperation.Wordswap;
break;
}
}
// Now read the individual tests into the Rule
XmlReader subreader = xtr.ReadSubtree();
if (subreader != null)
{
while (!subreader.EOF)
{
if (subreader.NodeType != XmlNodeType.Element)
subreader.Read();
SkipperTest test = ParseTest(xtr);
// Add the created test to the rule
rule.Tests.Add(test);
subreader.Read();
}
}
return rule;
}
catch
{
return null;
}
}
/// <summary>
/// Parse an XML document in as a SkipperTest
/// </summary>
/// <param name="xtr">XmlReader representing the document</param>
/// <returns>Filled SkipperTest on success, null otherwise</returns>
private SkipperTest ParseTest(XmlReader xtr)
{
if (xtr == null)
return null;
try
{
// Get the test type
SkipperTest test = new SkipperTest
{
Offset = 0,
Value = new byte[0],
Result = true,
Mask = new byte[0],
Size = 0,
Operator = HeaderSkipTestFileOperator.Equal,
};
switch (xtr.Name.ToLowerInvariant())
{
case "data":
test.Type = HeaderSkipTest.Data;
break;
case "or":
test.Type = HeaderSkipTest.Or;
break;
case "xor":
test.Type = HeaderSkipTest.Xor;
break;
case "and":
test.Type = HeaderSkipTest.And;
break;
case "file":
test.Type = HeaderSkipTest.File;
break;
default:
xtr.Read();
break;
}
// Now populate all the parts that we can
if (xtr.GetAttribute("offset") != null)
{
string offset = xtr.GetAttribute("offset");
if (offset.ToLowerInvariant() == "eof")
test.Offset = null;
else
test.Offset = Convert.ToInt64(offset, 16);
}
if (xtr.GetAttribute("value") != null)
{
string value = xtr.GetAttribute("value");
// http://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array
test.Value = new byte[value.Length / 2];
for (int index = 0; index < test.Value.Length; index++)
{
string byteValue = value.Substring(index * 2, 2);
test.Value[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
}
if (xtr.GetAttribute("result") != null)
{
string result = xtr.GetAttribute("result");
if (!bool.TryParse(result, out bool resultBool))
resultBool = true;
test.Result = resultBool;
}
if (xtr.GetAttribute("mask") != null)
{
string mask = xtr.GetAttribute("mask");
// http://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array
test.Mask = new byte[mask.Length / 2];
for (int index = 0; index < test.Mask.Length; index++)
{
string byteValue = mask.Substring(index * 2, 2);
test.Mask[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
}
if (xtr.GetAttribute("size") != null)
{
string size = xtr.GetAttribute("size");
if (size.ToLowerInvariant() == "po2")
test.Size = null;
else
test.Size = Convert.ToInt64(size, 16);
}
if (xtr.GetAttribute("operator") != null)
{
string oper = xtr.GetAttribute("operator");
#if NET_FRAMEWORK
switch (oper.ToLowerInvariant())
{
case "less":
test.Operator = HeaderSkipTestFileOperator.Less;
break;
case "greater":
test.Operator = HeaderSkipTestFileOperator.Greater;
break;
case "equal":
default:
test.Operator = HeaderSkipTestFileOperator.Equal;
break;
}
#else
test.Operator = oper.ToLowerInvariant() switch
{
"less" => HeaderSkipTestFileOperator.Less,
"greater" => HeaderSkipTestFileOperator.Greater,
"equal" => HeaderSkipTestFileOperator.Equal,
_ => HeaderSkipTestFileOperator.Equal,
};
#endif
}
return test;
}
catch
{
return null;
}
}
#endregion
#region Matching
/// <summary>
/// Get the SkipperRule associated with a given stream
/// </summary>
/// <param name="input">Stream to be checked</param>
/// <param name="skipperName">Name of the skipper to be used, blank to find a matching skipper</param>
/// <returns>The SkipperRule that matched the stream, null otherwise</returns>
public SkipperRule GetMatchingRule(Stream input, string skipperName)
{
// If we have no name supplied, try to blindly match
if (string.IsNullOrWhiteSpace(skipperName))
return GetMatchingRule(input);
// If the name matches the internal name of the skipper
else if (string.Equals(skipperName, Name, StringComparison.OrdinalIgnoreCase))
return GetMatchingRule(input);
// If the name matches the source file name of the skipper
else if (string.Equals(skipperName, SourceFile, StringComparison.OrdinalIgnoreCase))
return GetMatchingRule(input);
// Otherwise, nothing matches by default
return null;
}
/// <summary>
/// Get the matching SkipperRule from all Rules, if possible
/// </summary>
/// <param name="input">Stream to be checked</param>
/// <returns>The SkipperRule that matched the stream, null otherwise</returns>
private SkipperRule GetMatchingRule(Stream input)
{
// Loop through the rules until one is found that works
foreach (SkipperRule rule in Rules)
{
// Always reset the stream back to the original place
input.Seek(0, SeekOrigin.Begin);
// If all tests in the rule pass, we return this rule
if (rule.PassesAllTests(input))
return rule;
}
// If nothing passed, we return null by default
return null;
}
#endregion
}
}

View File

@@ -1,242 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.Library.IO;
using SabreTools.Library.Logging;
namespace SabreTools.Library.Skippers
{
public class SkipperRule
{
#region Fields
/// <summary>
/// Starting offset for applying rule
/// </summary>
public long? StartOffset { get; set; } // null is EOF
/// <summary>
/// Ending offset for applying rule
/// </summary>
public long? EndOffset { get; set; } // null if EOF
/// <summary>
/// Byte manipulation operation
/// </summary>
public HeaderSkipOperation Operation { get; set; }
/// <summary>
/// List of matching tests in a rule
/// </summary>
public List<SkipperTest> Tests { get; set; }
/// <summary>
/// Filename the skipper rule lives in
/// </summary>
public string SourceFile { get; set; }
#endregion
#region Logging
/// <summary>
/// Logging object
/// </summary>
private readonly Logger logger;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public SkipperRule()
{
logger = new Logger(this);
}
#endregion
/// <summary>
/// Check if a Stream passes all tests in the SkipperRule
/// </summary>
/// <param name="input">Stream to check</param>
/// <returns>True if all tests passed, false otherwise</returns>
public bool PassesAllTests(Stream input)
{
bool success = true;
foreach (SkipperTest test in Tests)
{
bool result = test.Passes(input);
success &= result;
}
return success;
}
/// <summary>
/// Transform an input file using the given rule
/// </summary>
/// <param name="input">Input file name</param>
/// <param name="output">Output file name</param>
/// <returns>True if the file was transformed properly, false otherwise</returns>
public bool TransformFile(string input, string output)
{
// If the input file doesn't exist, fail
if (!File.Exists(input))
{
logger.Error($"I'm sorry but '{input}' doesn't exist!");
return false;
}
// Create the output directory if it doesn't already
DirectoryExtensions.Ensure(Path.GetDirectoryName(output));
logger.User($"Attempting to apply rule to '{input}'");
bool success = TransformStream(FileExtensions.TryOpenRead(input), FileExtensions.TryCreate(output));
// If the output file has size 0, delete it
if (new FileInfo(output).Length == 0)
{
FileExtensions.TryDelete(output);
success = false;
}
return success;
}
/// <summary>
/// Transform an input stream using the given rule
/// </summary>
/// <param name="input">Input stream</param>
/// <param name="output">Output stream</param>
/// <param name="keepReadOpen">True if the underlying read stream should be kept open, false otherwise</param>
/// <param name="keepWriteOpen">True if the underlying write stream should be kept open, false otherwise</param>
/// <returns>True if the file was transformed properly, false otherwise</returns>
public bool TransformStream(Stream input, Stream output, bool keepReadOpen = false, bool keepWriteOpen = false)
{
bool success = true;
// If the sizes are wrong for the values, fail
long extsize = input.Length;
if ((Operation > HeaderSkipOperation.Bitswap && (extsize % 2) != 0)
|| (Operation > HeaderSkipOperation.Byteswap && (extsize % 4) != 0)
|| (Operation > HeaderSkipOperation.Bitswap && (StartOffset == null || StartOffset % 2 == 0)))
{
logger.Error("The stream did not have the correct size to be transformed!");
return false;
}
// Now read the proper part of the file and apply the rule
BinaryWriter bw = null;
BinaryReader br = null;
try
{
logger.User("Applying found rule to input stream");
bw = new BinaryWriter(output);
br = new BinaryReader(input);
// Seek to the beginning offset
if (StartOffset == null)
success = false;
else if (Math.Abs((long)StartOffset) > input.Length)
success = false;
else if (StartOffset > 0)
input.Seek((long)StartOffset, SeekOrigin.Begin);
else if (StartOffset < 0)
input.Seek((long)StartOffset, SeekOrigin.End);
// Then read and apply the operation as you go
if (success)
{
byte[] buffer = new byte[4];
int pos = 0;
while (input.Position < (EndOffset ?? input.Length)
&& input.Position < input.Length)
{
byte b = br.ReadByte();
switch (Operation)
{
case HeaderSkipOperation.Bitswap:
// http://stackoverflow.com/questions/3587826/is-there-a-built-in-function-to-reverse-bit-order
uint r = b;
int s = 7;
for (b >>= 1; b != 0; b >>= 1)
{
r <<= 1;
r |= (byte)(b & 1);
s--;
}
r <<= s;
buffer[pos] = (byte)r;
break;
case HeaderSkipOperation.Byteswap:
if (pos % 2 == 1)
{
buffer[pos - 1] = b;
}
if (pos % 2 == 0)
{
buffer[pos + 1] = b;
}
break;
case HeaderSkipOperation.Wordswap:
buffer[3 - pos] = b;
break;
case HeaderSkipOperation.WordByteswap:
buffer[(pos + 2) % 4] = b;
break;
case HeaderSkipOperation.None:
default:
buffer[pos] = b;
break;
}
// Set the buffer position to default write to
pos = (pos + 1) % 4;
// If we filled a buffer, flush to the stream
if (pos == 0)
{
bw.Write(buffer);
bw.Flush();
buffer = new byte[4];
}
}
// If there's anything more in the buffer, write only the left bits
for (int i = 0; i < pos; i++)
{
bw.Write(buffer[i]);
}
}
}
catch (Exception ex)
{
logger.Error(ex);
return false;
}
finally
{
// If we're not keeping the read stream open, dispose of the binary reader
if (!keepReadOpen)
br?.Dispose();
// If we're not keeping the write stream open, dispose of the binary reader
if (!keepWriteOpen)
bw?.Dispose();
}
return success;
}
}
}

View File

@@ -1,314 +0,0 @@
using System;
using System.IO;
namespace SabreTools.Library.Skippers
{
/// <summary>
/// Individual test that applies to a SkipperRule
/// </summary>
public class SkipperTest
{
#region Fields
/// <summary>
/// Type of test to be run
/// </summary>
public HeaderSkipTest Type { get; set; }
/// <summary>
/// File offset to run the test
/// </summary>
/// <remarks>null is EOF</remarks>
public long? Offset { get; set; }
/// <summary>
/// Static value to be checked at the offset
/// </summary>
public byte[] Value { get; set; }
/// <summary>
/// Determines whether a pass or failure is expected
/// </summary>
public bool Result { get; set; }
/// <summary>
/// Byte mask to be applied to the tested bytes
/// </summary>
public byte[] Mask { get; set; }
/// <summary>
/// Expected size of the input byte array, used with the Operator
/// </summary>
public long? Size { get; set; } // null is PO2, "power of 2" filesize
/// <summary>
/// Expected range value for the input byte array size, used with Size
/// </summary>
public HeaderSkipTestFileOperator Operator { get; set; }
#endregion
/// <summary>
/// Check if a stream passes the test
/// </summary>
/// <param name="input">Stream to check rule against</param>
/// <remarks>The Stream is assumed to be in the proper position for a given test</remarks>
public bool Passes(Stream input)
{
#if NET_FRAMEWORK
switch (Type)
{
case HeaderSkipTest.And:
return CheckAnd(input);
case HeaderSkipTest.Data:
return CheckData(input);
case HeaderSkipTest.File:
return CheckFile(input);
case HeaderSkipTest.Or:
return CheckOr(input);
case HeaderSkipTest.Xor:
return CheckXor(input);
default:
return true;
}
#else
return Type switch
{
HeaderSkipTest.And => CheckAnd(input),
HeaderSkipTest.Data => CheckData(input),
HeaderSkipTest.File => CheckFile(input),
HeaderSkipTest.Or => CheckOr(input),
HeaderSkipTest.Xor => CheckXor(input),
_ => true,
};
#endif
}
#region Checking Helpers
/// <summary>
/// Run an And test against an input stream
/// </summary>
/// <param name="input">Stream to check rule against</param>
/// <returns>True if the stream passed, false otherwise</returns>
private bool CheckAnd(Stream input)
{
// First seek to the correct position
Seek(input);
bool result = true;
try
{
// Then apply the mask if it exists
byte[] read = new byte[Mask.Length];
input.Read(read, 0, Mask.Length);
byte[] masked = new byte[Mask.Length];
for (int i = 0; i < read.Length; i++)
{
masked[i] = (byte)(read[i] & Mask[i]);
}
// Finally, compare it against the value
for (int i = 0; i < Value.Length; i++)
{
if (masked[i] != Value[i])
{
result = false;
break;
}
}
}
catch
{
result = false;
}
// Return if the expected and actual results match
return result == Result;
}
/// <summary>
/// Run a Data test against an input stream
/// </summary>
/// <param name="input">Stream to check rule against</param>
/// <returns>True if the stream passed, false otherwise</returns>
private bool CheckData(Stream input)
{
// First seek to the correct position
if (!Seek(input))
return false;
// Then read and compare bytewise
bool result = true;
for (int i = 0; i < Value.Length; i++)
{
try
{
if (input.ReadByte() != Value[i])
{
result = false;
break;
}
}
catch
{
result = false;
break;
}
}
// Return if the expected and actual results match
return result == Result;
}
/// <summary>
/// Run a File test against an input stream
/// </summary>
/// <param name="input">Stream to check rule against</param>
/// <returns>True if the stream passed, false otherwise</returns>
private bool CheckFile(Stream input)
{
// First get the file size from stream
long size = input.Length;
// If we have a null size, check that the size is a power of 2
bool result = true;
if (Size == null)
{
// http://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2
result = (((ulong)size & ((ulong)size - 1)) == 0);
}
else if (Operator == HeaderSkipTestFileOperator.Less)
{
result = (size < Size);
}
else if (Operator == HeaderSkipTestFileOperator.Greater)
{
result = (size > Size);
}
else if (Operator == HeaderSkipTestFileOperator.Equal)
{
result = (size == Size);
}
// Return if the expected and actual results match
return result == Result;
}
/// <summary>
/// Run an Or test against an input stream
/// </summary>
/// <param name="input">Stream to check rule against</param>
/// <returns>True if the stream passed, false otherwise</returns>
private bool CheckOr(Stream input)
{
// First seek to the correct position
Seek(input);
bool result = true;
try
{
// Then apply the mask if it exists
byte[] read = new byte[Mask.Length];
input.Read(read, 0, Mask.Length);
byte[] masked = new byte[Mask.Length];
for (int i = 0; i < read.Length; i++)
{
masked[i] = (byte)(read[i] | Mask[i]);
}
// Finally, compare it against the value
for (int i = 0; i < Value.Length; i++)
{
if (masked[i] != Value[i])
{
result = false;
break;
}
}
}
catch
{
result = false;
}
// Return if the expected and actual results match
return result == Result;
}
/// <summary>
/// Run an Xor test against an input stream
/// </summary>
/// <param name="input">Stream to check rule against</param>
/// <returns>True if the stream passed, false otherwise</returns>
private bool CheckXor(Stream input)
{
// First seek to the correct position
Seek(input);
bool result = true;
try
{
// Then apply the mask if it exists
byte[] read = new byte[Mask.Length];
input.Read(read, 0, Mask.Length);
byte[] masked = new byte[Mask.Length];
for (int i = 0; i < read.Length; i++)
{
masked[i] = (byte)(read[i] ^ Mask[i]);
}
// Finally, compare it against the value
for (int i = 0; i < Value.Length; i++)
{
if (masked[i] != Value[i])
{
result = false;
break;
}
}
}
catch
{
result = false;
}
// Return if the expected and actual results match
return result == Result;
}
/// <summary>
/// Seek an input stream based on the test value
/// </summary>
/// <param name="input">Stream to seek</param>
/// <returns>True if the stream could seek, false on error</returns>
private bool Seek(Stream input)
{
try
{
// Null offset means EOF
if (Offset == null)
input.Seek(0, SeekOrigin.End);
// Positive offset means from beginning
else if (Offset >= 0 && Offset <= input.Length)
input.Seek(Offset.Value, SeekOrigin.Begin);
// Negative offset means from end
else if (Offset < 0 && Math.Abs(Offset.Value) <= input.Length)
input.Seek(Offset.Value, SeekOrigin.End);
return true;
}
catch
{
return false;
}
}
#endregion
}
}

View File

@@ -1,17 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>Atari 7800</name>
<author>Roman Scherzer</author>
<version>1.0</version>
<rule start_offset="80" end_offset="EOF" operation="none">
<data offset="1" value="415441524937383030" result="true"/>
</rule>
<rule start_offset="80" end_offset="EOF" operation="none">
<data offset="64" value="41435455414C20434152542044415441205354415254532048455245" result="true"/>
</rule>
</detector>

View File

@@ -1,25 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>fds</name>
<author>Yori Yoshizuki</author>
<version>1.0</version>
<rule start_offset="10">
<data offset="0" value="4644531A010000000000000000000000"/>
</rule>
<rule start_offset="10">
<data offset="0" value="4644531A020000000000000000000000"/>
</rule>
<rule start_offset="10">
<data offset="0" value="4644531A030000000000000000000000"/>
</rule>
<rule start_offset="10">
<data offset="0" value="4644531A040000000000000000000000"/>
</rule>
</detector>

View File

@@ -1,17 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>Atari Lynx</name>
<author>Roman Scherzer</author>
<version>1.0</version>
<rule start_offset="40" end_offset="EOF" operation="none">
<data offset="0" value="4C594E58" result="true"/>
</rule>
<rule start_offset="40" end_offset="EOF" operation="none">
<data offset="6" value="425339" result="true"/>
</rule>
</detector>

View File

@@ -1,24 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>Nintendo 64 - ABCD</name>
<author>CUE</author>
<version>1.1</version>
<!-- V64 format -->
<rule start_offset="0" end_offset="EOF" operation="none">
<data offset="0" value="80371240" result="true"/>
</rule>
<!-- Z64 format -->
<rule start_offset="0" end_offset="EOF" operation="byteswap">
<data offset="0" value="37804012" result="true"/>
</rule>
<!-- N64 format? -->
<rule start_offset="0" end_offset="EOF" operation="wordswap">
<data offset="0" value="40123780" result="true"/>
</rule>
</detector>

View File

@@ -1,13 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>Nintendo Famicon/NES</name>
<author>Roman Scherzer</author>
<version>1.1</version>
<rule start_offset="10" end_offset="EOF" operation="none">
<data offset="0" value="4E45531A" result="true"/>
</rule>
</detector>

View File

@@ -1,13 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>NEC TurboGrafx-16/PC-Engine</name>
<author>Matt Nadareski (darksabre76)</author>
<version>1.0</version>
<rule start_offset="200">
<data offset="0" value="4000000000000000AABB02"/>
</rule>
</detector>

View File

@@ -1,29 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>psid</name>
<author>Yori Yoshizuki</author>
<version>1.2</version>
<rule start_offset="76" end_offset="EOF" operation="none">
<data offset="0" value="5053494400010076" result="true"/>
</rule>
<rule start_offset="76" end_offset="EOF" operation="none">
<data offset="0" value="505349440003007c" result="true"/>
</rule>
<rule start_offset="7c" end_offset="EOF" operation="none">
<data offset="0" value="505349440002007c" result="true"/>
</rule>
<rule start_offset="7c" end_offset="EOF" operation="none">
<data offset="0" value="505349440001007c" result="true"/>
</rule>
<rule start_offset="7c" end_offset="EOF" operation="none">
<data offset="0" value="525349440002007c" result="true"/>
</rule>
</detector>

View File

@@ -1,24 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>Nintendo Super Famicom/SNES</name>
<author>Matt Nadareski (darksabre76)</author>
<version>1.0</version>
<!-- fig header -->
<rule start_offset="200">
<data offset="16" value="0000000000000000"/>
</rule>
<!-- smc header -->
<rule start_offset="200">
<data offset="16" value="AABB040000000000"/>
</rule>
<!-- ufo header -->
<rule start_offset="200">
<data offset="16" value="535550455255464F"/>
</rule>
</detector>

View File

@@ -1,13 +0,0 @@
<?xml version="1.0"?>
<detector>
<name>Nintendo Super Famicon SPC</name>
<author>Yori Yoshizuki</author>
<version>1.0</version>
<rule start_offset="00100" end_offset="EOF" operation="none">
<data offset="0" value="534E45532D535043" result="true"/>
</rule>
</detector>