2016-03-29 12:11:58 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-06-16 22:17:58 -07:00
|
|
|
|
using System.Globalization;
|
2016-03-29 12:11:58 -07:00
|
|
|
|
using System.Xml;
|
2016-10-24 13:51:39 -07:00
|
|
|
|
|
2017-05-04 02:41:11 -07:00
|
|
|
|
using SabreTools.Library.Data;
|
|
|
|
|
|
using SabreTools.Library.Tools;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-10-28 21:49:29 -07:00
|
|
|
|
#if MONO
|
2016-10-27 11:35:17 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
#else
|
2016-10-26 22:10:47 -07:00
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
|
|
|
|
|
|
|
|
|
|
|
using BinaryReader = System.IO.BinaryReader;
|
|
|
|
|
|
using SearchOption = System.IO.SearchOption;
|
|
|
|
|
|
using SeekOrigin = System.IO.SeekOrigin;
|
|
|
|
|
|
using Stream = System.IO.Stream;
|
2016-10-30 21:15:33 -07:00
|
|
|
|
#endif
|
2016-10-26 22:10:47 -07:00
|
|
|
|
|
2017-05-04 02:41:11 -07:00
|
|
|
|
namespace SabreTools.Library.Skippers
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-10-03 21:16:59 -07:00
|
|
|
|
public class Skipper
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-10-03 21:16:59 -07:00
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
|
|
public string Name;
|
|
|
|
|
|
public string Author;
|
|
|
|
|
|
public string Version;
|
|
|
|
|
|
public List<SkipperRule> Rules;
|
|
|
|
|
|
public string SourceFile;
|
|
|
|
|
|
|
2016-06-16 22:17:58 -07:00
|
|
|
|
// Local paths
|
2017-03-29 12:49:34 -07:00
|
|
|
|
public static string LocalPath = Path.Combine(Globals.ExeDir, "Skippers") + Path.DirectorySeparatorChar;
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
|
|
|
|
|
// Header skippers represented by a list of skipper objects
|
|
|
|
|
|
private static List<Skipper> _list;
|
|
|
|
|
|
public static List<Skipper> List
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_list == null || _list.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
PopulateSkippers();
|
|
|
|
|
|
}
|
|
|
|
|
|
return _list;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-03 21:16:59 -07:00
|
|
|
|
#endregion
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
2016-10-03 21:16:59 -07:00
|
|
|
|
#region Constructors
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
2016-10-24 12:58:57 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create an empty Skipper object
|
|
|
|
|
|
/// </summary>
|
2016-10-03 21:16:59 -07:00
|
|
|
|
public Skipper()
|
2016-06-16 22:17:58 -07:00
|
|
|
|
{
|
2017-01-27 16:53:29 -08:00
|
|
|
|
Name = "";
|
|
|
|
|
|
Author = "";
|
|
|
|
|
|
Version = "";
|
2016-10-03 21:16:59 -07:00
|
|
|
|
Rules = new List<SkipperRule>();
|
2017-01-27 16:53:29 -08:00
|
|
|
|
SourceFile = "";
|
2016-10-03 21:16:59 -07:00
|
|
|
|
}
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
2016-10-24 12:58:57 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a Skipper object parsed from an input file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to parse</param>
|
2016-10-03 21:16:59 -07:00
|
|
|
|
public Skipper(string filename)
|
|
|
|
|
|
{
|
|
|
|
|
|
Rules = new List<SkipperRule>();
|
|
|
|
|
|
SourceFile = Path.GetFileNameWithoutExtension(filename);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
2016-10-24 12:58:57 -07:00
|
|
|
|
Logger logger = new Logger();
|
2017-11-08 00:27:00 -08:00
|
|
|
|
XmlReader xtr = Utilities.GetXmlTextReader(filename);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
|
|
|
|
|
if (xtr == null)
|
|
|
|
|
|
{
|
2016-10-03 21:16:59 -07:00
|
|
|
|
return;
|
2016-06-16 22:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool valid = false;
|
|
|
|
|
|
xtr.MoveToContent();
|
|
|
|
|
|
while (!xtr.EOF)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (xtr.NodeType != XmlNodeType.Element)
|
|
|
|
|
|
{
|
|
|
|
|
|
xtr.Read();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (xtr.Name.ToLowerInvariant())
|
|
|
|
|
|
{
|
|
|
|
|
|
case "detector":
|
|
|
|
|
|
valid = true;
|
2016-06-17 11:02:38 -07:00
|
|
|
|
xtr.Read();
|
2016-06-16 22:17:58 -07:00
|
|
|
|
break;
|
|
|
|
|
|
case "name":
|
2016-10-03 21:16:59 -07:00
|
|
|
|
Name = xtr.ReadElementContentAsString();
|
2016-06-16 22:17:58 -07:00
|
|
|
|
break;
|
|
|
|
|
|
case "author":
|
2016-10-03 21:16:59 -07:00
|
|
|
|
Author = xtr.ReadElementContentAsString();
|
2016-06-16 22:17:58 -07:00
|
|
|
|
break;
|
|
|
|
|
|
case "version":
|
2016-10-03 21:16:59 -07:00
|
|
|
|
Version = xtr.ReadElementContentAsString();
|
2016-06-16 22:17:58 -07:00
|
|
|
|
break;
|
|
|
|
|
|
case "rule":
|
|
|
|
|
|
// Get the information from the rule first
|
|
|
|
|
|
SkipperRule rule = new SkipperRule
|
|
|
|
|
|
{
|
|
|
|
|
|
StartOffset = 0,
|
|
|
|
|
|
EndOffset = 0,
|
|
|
|
|
|
Operation = HeaderSkipOperation.None,
|
|
|
|
|
|
Tests = new List<SkipperTest>(),
|
2016-10-03 21:16:59 -07:00
|
|
|
|
SourceFile = Path.GetFileNameWithoutExtension(filename),
|
2016-06-16 22:17:58 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (xtr.GetAttribute("start_offset") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string offset = xtr.GetAttribute("start_offset");
|
|
|
|
|
|
if (offset.ToLowerInvariant() == "eof")
|
|
|
|
|
|
{
|
|
|
|
|
|
rule.StartOffset = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
rule.StartOffset = Convert.ToInt64(offset, 16);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (xtr.GetAttribute("end_offset") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string offset = xtr.GetAttribute("end_offset");
|
|
|
|
|
|
if (offset.ToLowerInvariant() == "eof")
|
|
|
|
|
|
{
|
|
|
|
|
|
rule.EndOffset = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
rule.EndOffset = Convert.ToInt64(offset, 16);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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 (subreader.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:
|
|
|
|
|
|
subreader.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now populate all the parts that we can
|
|
|
|
|
|
if (subreader.GetAttribute("offset") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string offset = subreader.GetAttribute("offset");
|
|
|
|
|
|
if (offset.ToLowerInvariant() == "eof")
|
|
|
|
|
|
{
|
|
|
|
|
|
test.Offset = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
test.Offset = Convert.ToInt64(offset, 16);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (subreader.GetAttribute("value") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string value = subreader.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 (subreader.GetAttribute("result") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string result = subreader.GetAttribute("result");
|
|
|
|
|
|
switch (result.ToLowerInvariant())
|
|
|
|
|
|
{
|
|
|
|
|
|
case "false":
|
|
|
|
|
|
test.Result = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "true":
|
|
|
|
|
|
default:
|
|
|
|
|
|
test.Result = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (subreader.GetAttribute("mask") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string mask = subreader.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 (subreader.GetAttribute("size") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string size = subreader.GetAttribute("size");
|
|
|
|
|
|
if (size.ToLowerInvariant() == "po2")
|
|
|
|
|
|
{
|
|
|
|
|
|
test.Size = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
test.Size = Convert.ToInt64(size, 16);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (subreader.GetAttribute("operator") != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string oper = subreader.GetAttribute("operator");
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add the created test to the rule
|
|
|
|
|
|
rule.Tests.Add(test);
|
|
|
|
|
|
subreader.Read();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add the created rule to the skipper
|
2016-10-03 21:16:59 -07:00
|
|
|
|
Rules.Add(rule);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
xtr.Skip();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
xtr.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-03 21:16:59 -07:00
|
|
|
|
// 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 Static Methods
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Populate the entire list of header Skippers
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// http://mamedev.emulab.it/clrmamepro/docs/xmlheaders.txt
|
|
|
|
|
|
/// http://www.emulab.it/forum/index.php?topic=127.0
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
private static void PopulateSkippers()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_list == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_list = new List<Skipper>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (string skipperFile in Directory.EnumerateFiles(LocalPath, "*", SearchOption.AllDirectories))
|
|
|
|
|
|
{
|
|
|
|
|
|
_list.Add(new Skipper(Path.GetFullPath(skipperFile)));
|
|
|
|
|
|
}
|
2016-06-16 22:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-17 01:22:22 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the SkipperRule associated with a given file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Name of the file to be checked</param>
|
2016-10-03 21:16:59 -07:00
|
|
|
|
/// <param name="skipperName">Name of the skipper to be used, blank to find a matching skipper</param>
|
2016-06-17 01:22:22 -07:00
|
|
|
|
/// <param name="logger">Logger object for file and console output</param>
|
|
|
|
|
|
/// <returns>The SkipperRule that matched the file</returns>
|
2017-03-01 21:26:27 -08:00
|
|
|
|
public static SkipperRule GetMatchingRule(string input, string skipperName)
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If the file doesn't exist, return a blank skipper rule
|
2016-08-29 16:33:07 -07:00
|
|
|
|
if (!File.Exists(input))
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2017-08-26 14:11:10 -07:00
|
|
|
|
Globals.Logger.Error("The file '{0}' does not exist so it cannot be tested", input);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
return new SkipperRule();
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-08 00:27:00 -08:00
|
|
|
|
return GetMatchingRule(Utilities.TryOpenRead(input), skipperName);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the SkipperRule associated with a given stream
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Name of the file to be checked</param>
|
2016-10-03 21:16:59 -07:00
|
|
|
|
/// <param name="skipperName">Name of the skipper to be used, blank to find a matching skipper</param>
|
2016-09-17 17:30:25 -07:00
|
|
|
|
/// <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>
|
2017-03-01 21:26:27 -08:00
|
|
|
|
public static SkipperRule GetMatchingRule(Stream input, string skipperName, bool keepOpen = false)
|
2016-09-17 17:30:25 -07:00
|
|
|
|
{
|
|
|
|
|
|
SkipperRule skipperRule = new SkipperRule();
|
|
|
|
|
|
|
2016-10-25 10:38:26 -07:00
|
|
|
|
// If we have a null skipper name, we return since we're not matching skippers
|
|
|
|
|
|
if (skipperName == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return skipperRule;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-17 01:22:22 -07:00
|
|
|
|
// Loop through and find a Skipper that has the right name
|
2017-03-01 21:26:27 -08:00
|
|
|
|
Globals.Logger.Verbose("Beginning search for matching header skip rules");
|
2016-10-03 15:05:07 -07:00
|
|
|
|
List<Skipper> tempList = new List<Skipper>();
|
|
|
|
|
|
tempList.AddRange(List);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (Skipper skipper in tempList)
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-10-03 21:16:59 -07:00
|
|
|
|
// If we're searching for the skipper OR we have a match to an inputted one
|
|
|
|
|
|
if (String.IsNullOrEmpty(skipperName)
|
|
|
|
|
|
|| (!String.IsNullOrEmpty(skipper.Name) && skipperName.ToLowerInvariant() == skipper.Name.ToLowerInvariant())
|
|
|
|
|
|
|| (!String.IsNullOrEmpty(skipper.Name) && skipperName.ToLowerInvariant() == skipper.SourceFile.ToLowerInvariant()))
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
// Loop through the rules until one is found that works
|
2016-09-17 17:30:25 -07:00
|
|
|
|
BinaryReader br = new BinaryReader(input);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (SkipperRule rule in skipper.Rules)
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// Always reset the stream back to the original place
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek(0, SeekOrigin.Begin);
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// For each rule, make sure it passes each test
|
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
foreach (SkipperTest test in rule.Tests)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool result = true;
|
|
|
|
|
|
switch (test.Type)
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
case HeaderSkipTest.Data:
|
|
|
|
|
|
// First seek to the correct position
|
|
|
|
|
|
if (test.Offset == null)
|
|
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek(0, SeekOrigin.End);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
2016-09-18 12:29:33 -07:00
|
|
|
|
else if (test.Offset > 0 && test.Offset <= input.Length)
|
2016-09-17 17:30:25 -07:00
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek((long)test.Offset, SeekOrigin.Begin);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
2016-09-18 12:29:33 -07:00
|
|
|
|
else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= input.Length)
|
2016-09-17 17:30:25 -07:00
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek((long)test.Offset, SeekOrigin.End);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// Then read and compare bytewise
|
|
|
|
|
|
result = true;
|
|
|
|
|
|
for (int i = 0; i < test.Value.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
if (br.ReadByte() != test.Value[i])
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
|
|
|
|
|
result = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-09-17 17:30:25 -07:00
|
|
|
|
catch
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
result = false;
|
|
|
|
|
|
break;
|
2016-06-17 11:02:38 -07:00
|
|
|
|
}
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek(0, SeekOrigin.End);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
2016-09-18 12:29:33 -07:00
|
|
|
|
else if (test.Offset > 0 && test.Offset <= input.Length)
|
2016-09-17 17:30:25 -07:00
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek((long)test.Offset, SeekOrigin.Begin);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
2016-09-18 12:29:33 -07:00
|
|
|
|
else if (test.Offset < 0 && Math.Abs((long)test.Offset) <= input.Length)
|
2016-09-17 17:30:25 -07:00
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek((long)test.Offset, SeekOrigin.End);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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++)
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
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])
|
|
|
|
|
|
);
|
2016-06-17 11:02:38 -07:00
|
|
|
|
}
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// Finally, compare it against the value
|
|
|
|
|
|
for (int i = 0; i < test.Value.Length; i++)
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
if (masked[i] != test.Value[i])
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
result = false;
|
|
|
|
|
|
break;
|
2016-06-17 11:02:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
result = false;
|
|
|
|
|
|
}
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// Return if the expected and actual results match
|
|
|
|
|
|
success &= (result == test.Result);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case HeaderSkipTest.File:
|
|
|
|
|
|
// First get the file size from stream
|
2016-09-18 12:29:33 -07:00
|
|
|
|
long size = input.Length;
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// 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);
|
|
|
|
|
|
}
|
2016-06-17 11:02:38 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// Return if the expected and actual results match
|
|
|
|
|
|
success &= (result == test.Result);
|
|
|
|
|
|
break;
|
2016-06-17 11:02:38 -07:00
|
|
|
|
}
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// If we still have a success, then return this rule
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
// If we're not keeping the stream open, dispose of the binary reader
|
|
|
|
|
|
if (!keepOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
input.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-01 21:26:27 -08:00
|
|
|
|
Globals.Logger.User(" Matching rule found!");
|
2016-09-17 17:30:25 -07:00
|
|
|
|
return rule;
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-18 12:29:33 -07:00
|
|
|
|
// If we're not keeping the stream open, dispose of the binary reader
|
|
|
|
|
|
if (!keepOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
input.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-17 01:22:22 -07:00
|
|
|
|
// If we have a blank rule, inform the user
|
|
|
|
|
|
if (skipperRule.Tests == null)
|
|
|
|
|
|
{
|
2017-03-01 21:26:27 -08:00
|
|
|
|
Globals.Logger.Verbose("No matching rule found!");
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return skipperRule;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-03 21:16:59 -07:00
|
|
|
|
#endregion
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|