mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-04-21 13:43:26 +00:00
Add path matching matcher
This commit is contained in:
@@ -3,7 +3,7 @@ using System.Diagnostics.Eventing.Reader;
|
||||
namespace BurnOutSharp.Matching
|
||||
{
|
||||
/// <summary>
|
||||
/// Single matching criteria
|
||||
/// Content matching criteria
|
||||
/// </summary>
|
||||
internal class ContentMatch
|
||||
{
|
||||
|
||||
@@ -14,9 +14,15 @@ namespace BurnOutSharp.Matching
|
||||
/// </summary>
|
||||
public IEnumerable<ContentMatch> ContentMatches { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Set of all path matches
|
||||
/// </summary>
|
||||
public IEnumerable<PathMatch> PathMatches { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Function to get a version for this Matcher
|
||||
/// </summary>
|
||||
/// TODO: Can this be made more generic?
|
||||
public Func<string, byte[], int, string> GetVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
@@ -24,7 +30,7 @@ namespace BurnOutSharp.Matching
|
||||
/// </summary>
|
||||
public string ProtectionName { get; set; }
|
||||
|
||||
#region Constructors
|
||||
#region ContentMatch Constructors
|
||||
|
||||
public Matcher(byte?[] needle, string protectionName)
|
||||
: this(new List<byte?[]> { needle }, null, protectionName) { }
|
||||
@@ -56,10 +62,42 @@ namespace BurnOutSharp.Matching
|
||||
|
||||
#endregion
|
||||
|
||||
#region PathMatch Constructors
|
||||
|
||||
public Matcher(string needle, string protectionName)
|
||||
: this(new List<string> { needle }, null, protectionName) { }
|
||||
|
||||
public Matcher(List<string> needles, string protectionName)
|
||||
: this(needles, null, protectionName) { }
|
||||
|
||||
public Matcher(string needle, Func<string, byte[], int, string> getVersion, string protectionName)
|
||||
: this(new List<string> { needle }, getVersion, protectionName) { }
|
||||
|
||||
public Matcher(List<string> needles, Func<string, byte[], int, string> getVersion, string protectionName)
|
||||
: this(needles.Select(n => new PathMatch(n)).ToList(), getVersion, protectionName) { }
|
||||
|
||||
public Matcher(PathMatch needle, string protectionName)
|
||||
: this(new List<PathMatch>() { needle }, null, protectionName) { }
|
||||
|
||||
public Matcher(List<PathMatch> needles, string protectionName)
|
||||
: this(needles, null, protectionName) { }
|
||||
|
||||
public Matcher(PathMatch needle, Func<string, byte[], int, string> getVersion, string protectionName)
|
||||
: this(new List<PathMatch>() { needle }, getVersion, protectionName) { }
|
||||
|
||||
public Matcher(List<PathMatch> needles, Func<string, byte[], int, string> getVersion, string protectionName)
|
||||
{
|
||||
PathMatches = needles;
|
||||
GetVersion = getVersion;
|
||||
ProtectionName = protectionName;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Matching
|
||||
|
||||
/// <summary>
|
||||
/// Determine wheter all content matches pass
|
||||
/// Determine whether all content matches pass
|
||||
/// </summary>
|
||||
/// <param name="fileContent">Byte array representing the file contents</param>
|
||||
/// <returns>Tuole of passing status and matching positions</returns>
|
||||
@@ -85,6 +123,55 @@ namespace BurnOutSharp.Matching
|
||||
return (true, positions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine whether all content matches pass
|
||||
/// </summary>
|
||||
/// <param name="stack">List of strings to try to match</param>
|
||||
/// <returns>Tuole of passing status and matching values</returns>
|
||||
public (bool, List<string>) MatchesAll(List<string> stack)
|
||||
{
|
||||
// If no path matches are defined, we fail out
|
||||
if (PathMatches == null || !PathMatches.Any())
|
||||
return (false, null);
|
||||
|
||||
// Initialize the value list
|
||||
List<string> values = new List<string>();
|
||||
|
||||
// Loop through all path matches and make sure all pass
|
||||
foreach (var pathMatch in PathMatches)
|
||||
{
|
||||
(bool match, string value) = pathMatch.Match(stack);
|
||||
if (!match)
|
||||
return (false, null);
|
||||
else
|
||||
values.Add(value);
|
||||
}
|
||||
|
||||
return (true, values);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine whether any content matches pass
|
||||
/// </summary>
|
||||
/// <param name="stack">List of strings to try to match</param>
|
||||
/// <returns>Tuole of passing status and matching values</returns>
|
||||
public (bool, string) MatchesAny(List<string> stack)
|
||||
{
|
||||
// If no path matches are defined, we fail out
|
||||
if (PathMatches == null || !PathMatches.Any())
|
||||
return (false, null);
|
||||
|
||||
// Loop through all path matches and make sure all pass
|
||||
foreach (var pathMatch in PathMatches)
|
||||
{
|
||||
(bool match, string value) = pathMatch.Match(stack);
|
||||
if (match)
|
||||
return (true, value);
|
||||
}
|
||||
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
43
BurnOutSharp/Matching/PathMatch.cs
Normal file
43
BurnOutSharp/Matching/PathMatch.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BurnOutSharp.Matching
|
||||
{
|
||||
/// <summary>
|
||||
/// Path matching criteria
|
||||
/// </summary>
|
||||
internal class PathMatch
|
||||
{
|
||||
/// <summary>
|
||||
/// String to match
|
||||
/// </summary>
|
||||
public string Needle { get; set; }
|
||||
|
||||
public PathMatch(string needle)
|
||||
{
|
||||
Needle = needle;
|
||||
}
|
||||
|
||||
#region Matching
|
||||
|
||||
/// <summary>
|
||||
/// Get if this match can be found in a stack
|
||||
/// </summary>
|
||||
/// <param name="stack">List of strings to search for the given content</param>
|
||||
public (bool, string) Match(List<string> stack)
|
||||
{
|
||||
// If either array is null or empty, we can't do anything
|
||||
if (stack == null || stack.Count == 0 || Needle == null || Needle.Length == 0)
|
||||
return (false, null);
|
||||
|
||||
foreach (string stackItem in stack)
|
||||
{
|
||||
if (stackItem.Contains(Needle))
|
||||
return (true, stackItem);
|
||||
}
|
||||
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user