More cleanup of Skippers

This commit is contained in:
Matt Nadareski
2020-07-30 22:32:16 -07:00
parent a4b2a4ff17
commit b43997065c
4 changed files with 483 additions and 260 deletions

View File

@@ -78,6 +78,7 @@ namespace SabreTools.Library.Skippers
/// 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)
@@ -136,6 +137,7 @@ namespace SabreTools.Library.Skippers
/// 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)
@@ -206,6 +208,34 @@ namespace SabreTools.Library.Skippers
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
{
@@ -216,7 +246,8 @@ namespace SabreTools.Library.Skippers
Size = 0,
Operator = HeaderSkipTestFileOperator.Equal,
};
switch (subreader.Name.ToLowerInvariant())
switch (xtr.Name.ToLowerInvariant())
{
case "data":
test.Type = HeaderSkipTest.Data;
@@ -239,23 +270,23 @@ namespace SabreTools.Library.Skippers
break;
default:
subreader.Read();
xtr.Read();
break;
}
// Now populate all the parts that we can
if (subreader.GetAttribute("offset") != null)
if (xtr.GetAttribute("offset") != null)
{
string offset = subreader.GetAttribute("offset");
string offset = xtr.GetAttribute("offset");
if (offset.ToLowerInvariant() == "eof")
test.Offset = null;
else
test.Offset = Convert.ToInt64(offset, 16);
}
if (subreader.GetAttribute("value") != null)
if (xtr.GetAttribute("value") != null)
{
string value = subreader.GetAttribute("value");
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];
@@ -266,18 +297,18 @@ namespace SabreTools.Library.Skippers
}
}
if (subreader.GetAttribute("result") != null)
if (xtr.GetAttribute("result") != null)
{
string result = subreader.GetAttribute("result");
string result = xtr.GetAttribute("result");
if (!bool.TryParse(result, out bool resultBool))
resultBool = true;
test.Result = resultBool;
}
if (subreader.GetAttribute("mask") != null)
if (xtr.GetAttribute("mask") != null)
{
string mask = subreader.GetAttribute("mask");
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];
@@ -288,18 +319,18 @@ namespace SabreTools.Library.Skippers
}
}
if (subreader.GetAttribute("size") != null)
if (xtr.GetAttribute("size") != null)
{
string size = subreader.GetAttribute("size");
string size = xtr.GetAttribute("size");
if (size.ToLowerInvariant() == "po2")
test.Size = null;
else
test.Size = Convert.ToInt64(size, 16);
}
if (subreader.GetAttribute("operator") != null)
if (xtr.GetAttribute("operator") != null)
{
string oper = subreader.GetAttribute("operator");
string oper = xtr.GetAttribute("operator");
#if NET_FRAMEWORK
switch (oper.ToLowerInvariant())
{
@@ -325,13 +356,7 @@ namespace SabreTools.Library.Skippers
#endif
}
// Add the created test to the rule
rule.Tests.Add(test);
subreader.Read();
}
}
return rule;
return test;
}
catch
{
@@ -340,5 +365,55 @@ namespace SabreTools.Library.Skippers
}
#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

@@ -38,6 +38,23 @@ namespace SabreTools.Library.Skippers
#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>

View File

@@ -1,12 +1,17 @@
using System;
using System.IO;
using SabreTools.Library.Data;
namespace SabreTools.Library.Skippers
{
/// <summary>
/// Intermediate class for storing Skipper Test information
/// Individual test that applies to a SkipperRule
/// </summary>
public struct SkipperTest
public class SkipperTest
{
#region Fields
/// <summary>
/// Type of test to be run
/// </summary>
@@ -15,7 +20,8 @@ namespace SabreTools.Library.Skippers
/// <summary>
/// File offset to run the test
/// </summary>
public long? Offset { get; set; } // null is EOF
/// <remarks>null is EOF</remarks>
public long? Offset { get; set; }
/// <summary>
/// Static value to be checked at the offset
@@ -41,5 +47,259 @@ namespace SabreTools.Library.Skippers
/// 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)
{
bool result = true;
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);
}
return result;
}
#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,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
@@ -183,7 +182,6 @@ namespace SabreTools.Library.Skippers
/// </summary>
/// <param name="input">Name of the file to be checked</param>
/// <param name="skipperName">Name of the skipper to be used, blank to find a matching skipper</param>
/// <param name="logger">Logger object for file and console output</param>
/// <param name="keepOpen">True if the underlying stream should be kept open, false otherwise</param>
/// <returns>The SkipperRule that matched the file</returns>
public static SkipperRule GetMatchingRule(Stream input, string skipperName, bool keepOpen = false)
@@ -199,154 +197,27 @@ namespace SabreTools.Library.Skippers
List<SkipperFile> tempList = new List<SkipperFile>();
tempList.AddRange(List);
// Loop through all known SkipperFiles
foreach (SkipperFile skipper in tempList)
{
// If we're searching for the skipper OR we have a match to an inputted one
if (string.IsNullOrWhiteSpace(skipperName)
|| (!string.IsNullOrWhiteSpace(skipper.Name) && skipperName.ToLowerInvariant() == skipper.Name.ToLowerInvariant())
|| (!string.IsNullOrWhiteSpace(skipper.Name) && skipperName.ToLowerInvariant() == skipper.SourceFile.ToLowerInvariant()))
{
// Loop through the rules until one is found that works
BinaryReader br = new BinaryReader(input);
foreach (SkipperRule rule in skipper.Rules)
{
// Always reset the stream back to the original place
input.Seek(0, SeekOrigin.Begin);
// For each rule, make sure it passes each test
bool success = true;
foreach (SkipperTest test in rule.Tests)
{
bool result = true;
switch (test.Type)
{
case HeaderSkipTest.Data:
// First seek to the correct position
if (test.Offset == null)
input.Seek(0, SeekOrigin.End);
else if (test.Offset > 0 && test.Offset <= input.Length)
input.Seek((long)test.Offset, SeekOrigin.Begin);
else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= input.Length)
input.Seek((long)test.Offset, SeekOrigin.End);
// Then read and compare bytewise
result = true;
for (int i = 0; i < test.Value.Length; i++)
{
try
{
if (br.ReadByte() != test.Value[i])
{
result = false;
skipperRule = skipper.GetMatchingRule(input, skipperName);
if (skipperRule != null)
break;
}
}
catch
{
result = false;
break;
}
}
// Return if the expected and actual results match
success &= (result == test.Result);
break;
case HeaderSkipTest.Or:
case HeaderSkipTest.Xor:
case HeaderSkipTest.And:
// First seek to the correct position
if (test.Offset == null)
input.Seek(0, SeekOrigin.End);
else if (test.Offset > 0 && test.Offset <= input.Length)
input.Seek((long)test.Offset, SeekOrigin.Begin);
else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= input.Length)
input.Seek((long)test.Offset, SeekOrigin.End);
result = true;
try
{
// Then apply the mask if it exists
byte[] read = br.ReadBytes(test.Mask.Length);
byte[] masked = new byte[test.Mask.Length];
for (int i = 0; i < read.Length; i++)
{
masked[i] = (byte)(test.Type == HeaderSkipTest.And ? read[i] & test.Mask[i] :
(test.Type == HeaderSkipTest.Or ? read[i] | test.Mask[i] : read[i] ^ test.Mask[i])
);
}
// Finally, compare it against the value
for (int i = 0; i < test.Value.Length; i++)
{
if (masked[i] != test.Value[i])
{
result = false;
break;
}
}
}
catch
{
result = false;
}
// Return if the expected and actual results match
success &= (result == test.Result);
break;
case HeaderSkipTest.File:
// 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
result = true;
if (test.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 (test.Operator == HeaderSkipTestFileOperator.Less)
{
result = (size < test.Size);
}
else if (test.Operator == HeaderSkipTestFileOperator.Greater)
{
result = (size > test.Size);
}
else if (test.Operator == HeaderSkipTestFileOperator.Equal)
{
result = (size == test.Size);
}
// Return if the expected and actual results match
success &= (result == test.Result);
break;
}
}
// If we still have a success, then return this rule
if (success)
{
// If we're not keeping the stream open, dispose of the binary reader
if (!keepOpen)
input.Dispose();
Globals.Logger.User(" Matching rule found!");
return rule;
}
}
}
}
// If we're not keeping the stream open, dispose of the binary reader
if (!keepOpen)
input.Dispose();
// If the SkipperRule is null, make it empty
if (skipperRule == null)
skipperRule = new SkipperRule();
// If we have a blank rule, inform the user
if (skipperRule.Tests == null)
Globals.Logger.Verbose("No matching rule found!");
else
Globals.Logger.User("Matching rule found!");
return skipperRule;
}