mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Prep Skippers for XML serialization
This commit is contained in:
@@ -12,18 +12,6 @@
|
||||
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>
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Schema;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SabreTools.Skippers
|
||||
{
|
||||
@@ -20,26 +21,31 @@ namespace SabreTools.Skippers
|
||||
/// <summary>
|
||||
/// Skipper name
|
||||
/// </summary>
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Author names
|
||||
/// </summary>
|
||||
[XmlElement("author")]
|
||||
public string Author { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// File version
|
||||
/// </summary>
|
||||
[XmlElement("version")]
|
||||
public string Version { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Set of all rules in the skipper
|
||||
/// </summary>
|
||||
[XmlArray("rule")]
|
||||
public List<SkipperRule> Rules { get; set; } = new List<SkipperRule>();
|
||||
|
||||
/// <summary>
|
||||
/// Filename the skipper lives in
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public string SourceFile { get; set; } = string.Empty;
|
||||
|
||||
#endregion
|
||||
@@ -256,41 +262,27 @@ namespace SabreTools.Skippers
|
||||
try
|
||||
{
|
||||
// Get the test type
|
||||
SkipperTest test = new SkipperTest
|
||||
SkipperTest test = xtr.Name.ToLowerInvariant() switch
|
||||
{
|
||||
Offset = 0,
|
||||
Value = new byte[0],
|
||||
Result = true,
|
||||
Mask = new byte[0],
|
||||
Size = 0,
|
||||
Operator = HeaderSkipTestFileOperator.Equal,
|
||||
"and" => new AndSkipperTest(),
|
||||
"data" => new DataSkipperTest(),
|
||||
"file" => new FileSkipperTest(),
|
||||
"or" => new OrSkipperTest(),
|
||||
"xor" => new XorSkipperTest(),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
switch (xtr.Name.ToLowerInvariant())
|
||||
{
|
||||
case "data":
|
||||
test.Type = HeaderSkipTest.Data;
|
||||
break;
|
||||
// If we had an invalid test type
|
||||
if (test == null)
|
||||
return null;
|
||||
|
||||
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:
|
||||
return null;
|
||||
}
|
||||
// Set the default values
|
||||
test.Offset = 0;
|
||||
test.Value = Array.Empty<byte>();
|
||||
test.Result = true;
|
||||
test.Mask = Array.Empty<byte>();
|
||||
test.Size = 0;
|
||||
test.Operator = HeaderSkipTestFileOperator.Equal;
|
||||
|
||||
// Now populate all the parts that we can
|
||||
if (xtr.GetAttribute("offset") != null)
|
||||
|
||||
@@ -11,17 +11,15 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public Atari7800()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x01,
|
||||
Value = new byte[] { 0x41, 0x54, 0x41, 0x52, 0x49, 0x37, 0x38, 0x30, 0x30 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule2Test1 = new SkipperTest
|
||||
var rule2Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x64,
|
||||
Value = new byte[] { 0x41, 0x43, 0x54, 0x55, 0x41, 0x4C, 0x20, 0x43, 0x41, 0x52, 0x54, 0x20, 0x44, 0x41, 0x54, 0x41, 0x20, 0x53, 0x54, 0x41, 0x52, 0x54, 0x53, 0x20, 0x48, 0x45, 0x52, 0x45 },
|
||||
Result = true,
|
||||
|
||||
@@ -11,17 +11,15 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public AtariLynx()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x4C, 0x59, 0x4E, 0x58 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule2Test1 = new SkipperTest
|
||||
var rule2Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x06,
|
||||
Value = new byte[] { 0x42, 0x53, 0x39 },
|
||||
Result = true,
|
||||
|
||||
@@ -11,41 +11,36 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public CommodorePSID()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x01, 0x00, 0x76 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule2Test1 = new SkipperTest
|
||||
var rule2Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x03, 0x00, 0x7c },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule3Test1 = new SkipperTest
|
||||
var rule3Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x02, 0x00, 0x7c },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule4Test1 = new SkipperTest
|
||||
var rule4Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x01, 0x00, 0x7c },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule5Test1 = new SkipperTest
|
||||
var rule5Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x52, 0x53, 0x49, 0x44, 0x00, 0x02, 0x00, 0x7c },
|
||||
Result = true,
|
||||
|
||||
@@ -11,9 +11,8 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public NECPCEngine()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xBB, 0x02 },
|
||||
Result = true,
|
||||
|
||||
@@ -11,25 +11,22 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public Nintendo64()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x80, 0x37, 0x12, 0x40 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule2Test1 = new SkipperTest
|
||||
var rule2Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x37, 0x80, 0x40, 0x12 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule3Test1 = new SkipperTest
|
||||
var rule3Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x40, 0x12, 0x37, 0x80 },
|
||||
Result = true,
|
||||
|
||||
@@ -11,9 +11,8 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public NintendoEntertainmentSystem()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x4E, 0x45, 0x53, 0x1A },
|
||||
Result = true,
|
||||
|
||||
@@ -11,33 +11,29 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public NintendoFamicomDiskSystem()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule2Test1 = new SkipperTest
|
||||
var rule2Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule3Test1 = new SkipperTest
|
||||
var rule3Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule4Test1 = new SkipperTest
|
||||
var rule4Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
Result = true,
|
||||
|
||||
@@ -11,9 +11,8 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public SuperFamicomSPC()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x00,
|
||||
Value = new byte[] { 0x53, 0x4E, 0x45, 0x53, 0x2D, 0x53, 0x50, 0x43 },
|
||||
Result = true,
|
||||
|
||||
@@ -11,25 +11,22 @@ namespace SabreTools.Skippers.SkipperFiles
|
||||
public SuperNintendoEntertainmentSystem()
|
||||
{
|
||||
// Create tests
|
||||
var rule1Test1 = new SkipperTest
|
||||
var rule1Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x16,
|
||||
Value = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule2Test1 = new SkipperTest
|
||||
var rule2Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x16,
|
||||
Value = new byte[] { 0xAA, 0xBB, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
Result = true,
|
||||
};
|
||||
|
||||
var rule3Test1 = new SkipperTest
|
||||
var rule3Test1 = new DataSkipperTest
|
||||
{
|
||||
Type = HeaderSkipTest.Data,
|
||||
Offset = 0x16,
|
||||
Value = new byte[] { 0x53, 0x55, 0x50, 0x45, 0x52, 0x55, 0x46, 0x4F },
|
||||
Result = true,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using SabreTools.Logging;
|
||||
|
||||
@@ -13,26 +14,36 @@ namespace SabreTools.Skippers
|
||||
/// <summary>
|
||||
/// Starting offset for applying rule
|
||||
/// </summary>
|
||||
[XmlAttribute("start_offset")]
|
||||
public long? StartOffset { get; set; } // null is EOF
|
||||
|
||||
/// <summary>
|
||||
/// Ending offset for applying rule
|
||||
/// </summary>
|
||||
[XmlAttribute("end_offset")]
|
||||
public long? EndOffset { get; set; } // null if EOF
|
||||
|
||||
/// <summary>
|
||||
/// Byte manipulation operation
|
||||
/// </summary>
|
||||
[XmlAttribute("operation")]
|
||||
public HeaderSkipOperation Operation { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// List of matching tests in a rule
|
||||
/// </summary>
|
||||
[XmlArray]
|
||||
[XmlArrayItem("data")]
|
||||
[XmlArrayItem("or")]
|
||||
[XmlArrayItem("xor")]
|
||||
[XmlArrayItem("and")]
|
||||
[XmlArrayItem("file")]
|
||||
public List<SkipperTest> Tests { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Filename the skipper rule lives in
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public string SourceFile { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,49 +1,52 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SabreTools.Skippers
|
||||
{
|
||||
/// <summary>
|
||||
/// Individual test that applies to a SkipperRule
|
||||
/// </summary>
|
||||
public class SkipperTest
|
||||
public abstract 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>
|
||||
[XmlAttribute("offset")]
|
||||
public long? Offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Static value to be checked at the offset
|
||||
/// </summary>
|
||||
[XmlAttribute("value")]
|
||||
public byte[] Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether a pass or failure is expected
|
||||
/// </summary>
|
||||
[XmlAttribute("result")]
|
||||
public bool Result { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Byte mask to be applied to the tested bytes
|
||||
/// </summary>
|
||||
[XmlAttribute("mask")]
|
||||
public byte[] Mask { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Expected size of the input byte array, used with the Operator
|
||||
/// </summary>
|
||||
[XmlAttribute("size")]
|
||||
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>
|
||||
[XmlAttribute("operator")]
|
||||
public HeaderSkipTestFileOperator Operator { get; set; }
|
||||
|
||||
#endregion
|
||||
@@ -53,27 +56,50 @@ namespace SabreTools.Skippers
|
||||
/// </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)
|
||||
{
|
||||
return Type switch
|
||||
{
|
||||
HeaderSkipTest.And => CheckAnd(input),
|
||||
HeaderSkipTest.Data => CheckData(input),
|
||||
HeaderSkipTest.File => CheckFile(input),
|
||||
HeaderSkipTest.Or => CheckOr(input),
|
||||
HeaderSkipTest.Xor => CheckXor(input),
|
||||
_ => true,
|
||||
};
|
||||
}
|
||||
public abstract bool Passes(Stream input);
|
||||
|
||||
#region Checking Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Run an And test against an input stream
|
||||
/// Seek an input stream based on the test value
|
||||
/// </summary>
|
||||
/// <param name="input">Stream to check rule against</param>
|
||||
/// <returns>True if the stream passed, false otherwise</returns>
|
||||
private bool CheckAnd(Stream input)
|
||||
/// <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>
|
||||
[XmlRoot("and")]
|
||||
public class AndSkipperTest : SkipperTest
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool Passes(Stream input)
|
||||
{
|
||||
// First seek to the correct position
|
||||
Seek(input);
|
||||
@@ -109,13 +135,16 @@ namespace SabreTools.Skippers
|
||||
// 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)
|
||||
/// <summary>
|
||||
/// Skipper test using DATA
|
||||
/// </summary>
|
||||
[XmlRoot("data")]
|
||||
public class DataSkipperTest : SkipperTest
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool Passes(Stream input)
|
||||
{
|
||||
// First seek to the correct position
|
||||
if (!Seek(input))
|
||||
@@ -143,13 +172,16 @@ namespace SabreTools.Skippers
|
||||
// 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)
|
||||
/// <summary>
|
||||
/// Skipper test using FILE
|
||||
/// </summary>
|
||||
[XmlRoot("file")]
|
||||
public class FileSkipperTest : SkipperTest
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool Passes(Stream input)
|
||||
{
|
||||
// First get the file size from stream
|
||||
long size = input.Length;
|
||||
@@ -177,13 +209,16 @@ namespace SabreTools.Skippers
|
||||
// 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)
|
||||
/// <summary>
|
||||
/// Skipper test using OR
|
||||
/// </summary>
|
||||
[XmlRoot("or")]
|
||||
public class OrSkipperTest : SkipperTest
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool Passes(Stream input)
|
||||
{
|
||||
// First seek to the correct position
|
||||
Seek(input);
|
||||
@@ -219,13 +254,16 @@ namespace SabreTools.Skippers
|
||||
// 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)
|
||||
/// <summary>
|
||||
/// Skipper test using XOR
|
||||
/// </summary>
|
||||
[XmlRoot("xor")]
|
||||
public class XorSkipperTest : SkipperTest
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool Passes(Stream input)
|
||||
{
|
||||
// First seek to the correct position
|
||||
Seek(input);
|
||||
@@ -261,36 +299,5 @@ namespace SabreTools.Skippers
|
||||
// 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user