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.IO;
|
|
|
|
|
|
using System.Xml;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-29 13:48:10 -07:00
|
|
|
|
namespace SabreTools.Helper
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-06-17 01:22:22 -07:00
|
|
|
|
public class Skippers
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-06-16 22:17:58 -07:00
|
|
|
|
// Local paths
|
2016-09-19 18:04:24 -07:00
|
|
|
|
public const string LocalPath = "Skippers";
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Populate the entire list of header Skippers
|
|
|
|
|
|
/// </summary>
|
2016-06-16 22:20:32 -07:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// http://mamedev.emulab.it/clrmamepro/docs/xmlheaders.txt
|
|
|
|
|
|
/// http://www.emulab.it/forum/index.php?topic=127.0
|
|
|
|
|
|
/// </remarks>
|
2016-06-16 22:17:58 -07:00
|
|
|
|
private static void PopulateSkippers()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_list == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_list = new List<Skipper>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-19 18:04:24 -07:00
|
|
|
|
foreach (string skipperFile in Directory.EnumerateFiles(LocalPath, "*", SearchOption.AllDirectories))
|
2016-06-16 22:17:58 -07:00
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
_list.Add(PopulateSkippersHelper(Path.GetFullPath(skipperFile)));
|
2016-06-16 22:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Populate an individual Skipper from file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to be read from</param>
|
|
|
|
|
|
/// <returns>The Skipper object associated with the file</returns>
|
|
|
|
|
|
private static Skipper PopulateSkippersHelper(string filename)
|
|
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
Skipper skipper = new Skipper
|
|
|
|
|
|
{
|
|
|
|
|
|
Rules = new List<SkipperRule>(),
|
2016-09-22 21:39:38 -07:00
|
|
|
|
SourceFile = Path.GetFileName(filename),
|
2016-06-17 11:02:38 -07:00
|
|
|
|
};
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
2016-08-29 16:33:07 -07:00
|
|
|
|
if (!File.Exists(filename))
|
2016-06-16 22:17:58 -07:00
|
|
|
|
{
|
|
|
|
|
|
return skipper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Logger logger = new Logger(false, "");
|
2016-09-22 17:14:23 -07:00
|
|
|
|
XmlTextReader xtr = FileTools.GetXmlTextReader(filename, logger);
|
2016-06-16 22:17:58 -07:00
|
|
|
|
|
|
|
|
|
|
if (xtr == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return skipper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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":
|
|
|
|
|
|
skipper.Name = xtr.ReadElementContentAsString();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "author":
|
|
|
|
|
|
skipper.Author = xtr.ReadElementContentAsString();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "version":
|
|
|
|
|
|
skipper.Version = xtr.ReadElementContentAsString();
|
|
|
|
|
|
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-09-22 21:39:38 -07:00
|
|
|
|
SourceFile = Path.GetFileName(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
|
|
|
|
|
|
skipper.Rules.Add(rule);
|
|
|
|
|
|
xtr.Skip();
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
xtr.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (valid ? skipper : new Skipper());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-09-17 17:30:25 -07:00
|
|
|
|
/// <param name="skipperName">Name of the skipper to be used</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>
|
2016-09-17 17:30:25 -07:00
|
|
|
|
public static SkipperRule MatchesSkipper(string input, string skipperName, Logger logger)
|
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
|
|
|
|
{
|
|
|
|
|
|
logger.Error("The file '" + input + "' does not exist so it cannot be tested");
|
2016-09-17 17:30:25 -07:00
|
|
|
|
return new SkipperRule();
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
return MatchesSkipper(File.OpenRead(input), skipperName, logger);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the SkipperRule associated with a given stream
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Name of the file to be checked</param>
|
|
|
|
|
|
/// <param name="skipperName">Name of the skipper to be used</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 MatchesSkipper(Stream input, string skipperName, Logger logger, bool keepOpen = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
SkipperRule skipperRule = new SkipperRule();
|
|
|
|
|
|
|
2016-06-17 01:22:22 -07:00
|
|
|
|
// Loop through and find a Skipper that has the right name
|
2016-06-21 23:15:59 -07:00
|
|
|
|
logger.Log("Beginning search for matching header skip rules");
|
2016-06-17 01:22:22 -07:00
|
|
|
|
foreach (Skipper skipper in List)
|
|
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
if (String.IsNullOrEmpty(skipperName) || (!String.IsNullOrEmpty(skipper.Name) && skipperName.ToLowerInvariant() == skipper.Name.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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
logger.User(" Matching rule found!");
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2016-06-21 23:16:36 -07:00
|
|
|
|
logger.Log("No matching rule found!");
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return skipperRule;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transform an input file using the given rule
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Input file name</param>
|
|
|
|
|
|
/// <param name="output">Output file name</param>
|
|
|
|
|
|
/// <param name="rule">SkipperRule to apply to the file</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file and console output</param>
|
2016-09-17 17:30:25 -07:00
|
|
|
|
/// <returns>True if the file was transformed properly, false otherwise</returns>
|
2016-06-17 01:22:22 -07:00
|
|
|
|
public static bool TransformFile(string input, string output, SkipperRule rule, Logger logger)
|
|
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
2016-06-17 01:22:22 -07:00
|
|
|
|
// If the input file doesn't exist, fail
|
2016-08-29 16:33:07 -07:00
|
|
|
|
if (!File.Exists(input))
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
|
|
|
|
|
logger.Error("I'm sorry but '" + input + "' doesn't exist!");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create the output directory if it doesn't already
|
2016-09-19 18:04:24 -07:00
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(output)))
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(output));
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
logger.User("Attempting to apply rule to '" + input + "'");
|
2016-09-18 12:29:33 -07:00
|
|
|
|
success = TransformStream(File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), File.OpenWrite(output), rule, logger);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
|
|
|
|
|
|
// If the output file has size 0, delete it
|
|
|
|
|
|
if (new FileInfo(output).Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(output);
|
2016-09-18 12:29:33 -07:00
|
|
|
|
success = false;
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// Don't log this file deletion error
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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="rule">SkipperRule to apply to the stream</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file and console output</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 static bool TransformStream(Stream input, Stream output, SkipperRule rule, Logger logger, bool keepReadOpen = false, bool keepWriteOpen = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
2016-06-17 01:22:22 -07:00
|
|
|
|
// If the sizes are wrong for the values, fail
|
2016-09-17 17:30:25 -07:00
|
|
|
|
long extsize = input.Length;
|
2016-06-17 01:22:22 -07:00
|
|
|
|
if ((rule.Operation > HeaderSkipOperation.Bitswap && (extsize % 2) != 0)
|
|
|
|
|
|
|| (rule.Operation > HeaderSkipOperation.Byteswap && (extsize % 4) != 0)
|
|
|
|
|
|
|| (rule.Operation > HeaderSkipOperation.Bitswap && (rule.StartOffset == null || rule.StartOffset % 2 == 0)))
|
|
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
logger.Error("The stream did not have the correct size to be transformed!");
|
2016-06-17 01:22:22 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now read the proper part of the file and apply the rule
|
2016-09-17 17:30:25 -07:00
|
|
|
|
BinaryWriter bw = null;
|
|
|
|
|
|
BinaryReader br = null;
|
2016-06-17 01:22:22 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
logger.User("Applying found rule to input stream");
|
|
|
|
|
|
bw = new BinaryWriter(output);
|
|
|
|
|
|
br = new BinaryReader(input);
|
|
|
|
|
|
|
|
|
|
|
|
// Seek to the beginning offset
|
|
|
|
|
|
if (rule.StartOffset == null)
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
success = false;
|
|
|
|
|
|
}
|
2016-09-18 12:29:33 -07:00
|
|
|
|
else if (Math.Abs((long)rule.StartOffset) > input.Length)
|
2016-09-17 17:30:25 -07:00
|
|
|
|
{
|
|
|
|
|
|
success = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (rule.StartOffset > 0)
|
|
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek((long)rule.StartOffset, SeekOrigin.Begin);
|
2016-09-17 17:30:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
else if (rule.StartOffset < 0)
|
|
|
|
|
|
{
|
2016-09-18 12:29:33 -07:00
|
|
|
|
input.Seek((long)rule.StartOffset, 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 apply the operation as you go
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] buffer = new byte[4];
|
|
|
|
|
|
int pos = 0;
|
2016-09-18 12:29:33 -07:00
|
|
|
|
while (input.Position < (rule.EndOffset != null ? rule.EndOffset : input.Length)
|
|
|
|
|
|
&& input.Position < input.Length)
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
byte b = br.ReadByte();
|
|
|
|
|
|
switch (rule.Operation)
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// Set the buffer position to default write to
|
|
|
|
|
|
pos = (pos + 1) % 4;
|
2016-06-17 01:22:22 -07:00
|
|
|
|
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// If we filled a buffer, flush to the stream
|
|
|
|
|
|
if (pos == 0)
|
2016-06-17 01:22:22 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
bw.Write(buffer);
|
|
|
|
|
|
bw.Flush();
|
|
|
|
|
|
buffer = new byte[4];
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// If there's anything more in the buffer, write only the left bits
|
|
|
|
|
|
for (int i = 0; i < pos; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
bw.Write(buffer[i]);
|
|
|
|
|
|
}
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-09-17 17:30:25 -07:00
|
|
|
|
finally
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
// If we're not keeping the read stream open, dispose of the binary reader
|
|
|
|
|
|
if (!keepReadOpen)
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
br?.Dispose();
|
2016-06-17 11:02:38 -07:00
|
|
|
|
}
|
2016-09-17 17:30:25 -07:00
|
|
|
|
|
|
|
|
|
|
// If we're not keeping the write stream open, dispose of the binary reader
|
|
|
|
|
|
if (!keepWriteOpen)
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
2016-09-17 17:30:25 -07:00
|
|
|
|
bw?.Dispose();
|
2016-06-17 11:02:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return success;
|
2016-06-17 01:22:22 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|