2020-07-30 22:32:16 -07:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2023-04-04 11:23:59 -04:00
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Serialization;
|
2020-07-30 22:32:16 -07:00
|
|
|
|
2020-12-08 00:13:22 -08:00
|
|
|
namespace SabreTools.Skippers
|
2020-07-30 21:07:25 -07:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-07-30 22:32:16 -07:00
|
|
|
/// Individual test that applies to a SkipperRule
|
2020-07-30 21:07:25 -07:00
|
|
|
/// </summary>
|
2023-04-04 11:23:59 -04:00
|
|
|
public abstract class SkipperTest
|
2020-07-30 21:07:25 -07:00
|
|
|
{
|
2020-07-30 22:32:16 -07:00
|
|
|
#region Fields
|
|
|
|
|
|
2020-07-30 21:07:25 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// File offset to run the test
|
|
|
|
|
/// </summary>
|
2020-07-30 22:32:16 -07:00
|
|
|
/// <remarks>null is EOF</remarks>
|
2023-04-04 11:23:59 -04:00
|
|
|
[XmlAttribute("offset")]
|
2020-07-30 22:32:16 -07:00
|
|
|
public long? Offset { get; set; }
|
2020-07-30 21:07:25 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Static value to be checked at the offset
|
|
|
|
|
/// </summary>
|
2023-04-04 11:23:59 -04:00
|
|
|
[XmlAttribute("value")]
|
2020-07-30 21:07:25 -07:00
|
|
|
public byte[] Value { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether a pass or failure is expected
|
|
|
|
|
/// </summary>
|
2023-04-04 11:23:59 -04:00
|
|
|
[XmlAttribute("result")]
|
2020-07-30 21:07:25 -07:00
|
|
|
public bool Result { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Byte mask to be applied to the tested bytes
|
|
|
|
|
/// </summary>
|
2023-04-04 11:23:59 -04:00
|
|
|
[XmlAttribute("mask")]
|
2020-07-30 21:07:25 -07:00
|
|
|
public byte[] Mask { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Expected size of the input byte array, used with the Operator
|
|
|
|
|
/// </summary>
|
2023-04-04 11:23:59 -04:00
|
|
|
[XmlAttribute("size")]
|
2020-07-30 21:07:25 -07:00
|
|
|
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>
|
2023-04-04 11:23:59 -04:00
|
|
|
[XmlAttribute("operator")]
|
2020-07-30 21:07:25 -07:00
|
|
|
public HeaderSkipTestFileOperator Operator { get; set; }
|
2020-07-30 22:32:16 -07:00
|
|
|
|
|
|
|
|
#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>
|
2023-04-04 11:23:59 -04:00
|
|
|
public abstract bool Passes(Stream input);
|
2020-07-30 22:32:16 -07:00
|
|
|
|
|
|
|
|
#region Checking Helpers
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-04-04 11:23:59 -04:00
|
|
|
/// Seek an input stream based on the test value
|
2020-07-30 22:32:16 -07:00
|
|
|
/// </summary>
|
2023-04-04 11:23:59 -04:00
|
|
|
/// <param name="input">Stream to seek</param>
|
|
|
|
|
/// <returns>True if the stream could seek, false on error</returns>
|
|
|
|
|
protected 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Skipper test using AND
|
|
|
|
|
/// </summary>
|
2023-04-04 11:38:46 -04:00
|
|
|
[XmlType("and")]
|
2023-04-04 11:23:59 -04:00
|
|
|
public class AndSkipperTest : SkipperTest
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool Passes(Stream input)
|
2020-07-30 22:32:16 -07:00
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2023-04-04 11:23:59 -04:00
|
|
|
}
|
2020-07-30 22:32:16 -07:00
|
|
|
|
2023-04-04 11:23:59 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Skipper test using DATA
|
|
|
|
|
/// </summary>
|
2023-04-04 11:38:46 -04:00
|
|
|
[XmlType("data")]
|
2023-04-04 11:23:59 -04:00
|
|
|
public class DataSkipperTest : SkipperTest
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool Passes(Stream input)
|
2020-07-30 22:32:16 -07:00
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2023-04-04 11:23:59 -04:00
|
|
|
}
|
2020-07-30 22:32:16 -07:00
|
|
|
|
2023-04-04 11:23:59 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Skipper test using FILE
|
|
|
|
|
/// </summary>
|
2023-04-04 11:38:46 -04:00
|
|
|
[XmlType("file")]
|
2023-04-04 11:23:59 -04:00
|
|
|
public class FileSkipperTest : SkipperTest
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool Passes(Stream input)
|
2020-07-30 22:32:16 -07:00
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2023-04-04 11:23:59 -04:00
|
|
|
}
|
2020-07-30 22:32:16 -07:00
|
|
|
|
2023-04-04 11:23:59 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Skipper test using OR
|
|
|
|
|
/// </summary>
|
2023-04-04 11:38:46 -04:00
|
|
|
[XmlType("or")]
|
2023-04-04 11:23:59 -04:00
|
|
|
public class OrSkipperTest : SkipperTest
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool Passes(Stream input)
|
2020-07-30 22:32:16 -07:00
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2023-04-04 11:23:59 -04:00
|
|
|
}
|
2020-07-30 22:32:16 -07:00
|
|
|
|
2023-04-04 11:23:59 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Skipper test using XOR
|
|
|
|
|
/// </summary>
|
2023-04-04 11:38:46 -04:00
|
|
|
[XmlType("xor")]
|
2023-04-04 11:23:59 -04:00
|
|
|
public class XorSkipperTest : SkipperTest
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool Passes(Stream input)
|
2020-07-30 22:32:16 -07:00
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2020-07-30 21:07:25 -07:00
|
|
|
}
|
|
|
|
|
}
|