using System; using System.Collections.Generic; using System.Linq; namespace BurnOutSharp.Matching { /// /// A set of content matches that work together /// public class ContentMatchSet : MatchSet { /// /// Function to get a content version /// /// /// A content version method takes the file path, the file contents, /// and a list of found positions and returns a single string. That /// string is either a version string, in which case it will be appended /// to the protection name, or `null`, in which case it will cause /// the protection to be omitted. /// public Func, string> GetVersion { get; set; } #region Constructors public ContentMatchSet(byte?[] needle, string protectionName) : this(new List { needle }, null, protectionName) { } public ContentMatchSet(List needles, string protectionName) : this(needles, null, protectionName) { } public ContentMatchSet(byte?[] needle, Func, string> getVersion, string protectionName) : this(new List { needle }, getVersion, protectionName) { } public ContentMatchSet(List needles, Func, string> getVersion, string protectionName) : this(needles.Select(n => new ContentMatch(n)).ToList(), getVersion, protectionName) { } public ContentMatchSet(ContentMatch needle, string protectionName) : this(new List() { needle }, null, protectionName) { } public ContentMatchSet(List needles, string protectionName) : this(needles, null, protectionName) { } public ContentMatchSet(ContentMatch needle, Func, string> getVersion, string protectionName) : this(new List() { needle }, getVersion, protectionName) { } public ContentMatchSet(List needles, Func, string> getVersion, string protectionName) { Matchers = needles; GetVersion = getVersion; ProtectionName = protectionName; } #endregion #region Matching /// /// Determine whether all content matches pass /// /// Byte array representing the file contents /// Tuple of passing status and matching positions public (bool, List) MatchesAll(byte[] fileContent) { // If no content matches are defined, we fail out if (Matchers == null || !Matchers.Any()) return (false, new List()); // Initialize the position list List positions = new List(); // Loop through all content matches and make sure all pass foreach (var contentMatch in Matchers) { (bool match, int position) = contentMatch.Match(fileContent); if (!match) return (false, new List()); else positions.Add(position); } return (true, positions); } /// /// Determine whether any content matches pass /// /// Byte array representing the file contents /// Tuple of passing status and first matching position public (bool, int) MatchesAny(byte[] fileContent) { // If no content matches are defined, we fail out if (Matchers == null || !Matchers.Any()) return (false, -1); // Loop through all content matches and make sure all pass foreach (var contentMatch in Matchers) { (bool match, int position) = contentMatch.Match(fileContent); if (match) return (true, position); } return (false, -1); } #endregion } }