From b0fdff2d6e3a1b269f3575c4c7c32ea249fabdfa Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 26 Oct 2024 00:14:29 -0400 Subject: [PATCH] Find and fix equal size issue in streams --- SabreTools.Matching.Test/MatchUtilTests.cs | 23 ++++++++++++++++++--- SabreTools.Matching/Content/ContentMatch.cs | 4 ++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/SabreTools.Matching.Test/MatchUtilTests.cs b/SabreTools.Matching.Test/MatchUtilTests.cs index e629784..b4a1d45 100644 --- a/SabreTools.Matching.Test/MatchUtilTests.cs +++ b/SabreTools.Matching.Test/MatchUtilTests.cs @@ -1,6 +1,5 @@ -using System; using System.Collections.Generic; -using System.Linq; +using System.IO; using SabreTools.Matching.Content; using Xunit; @@ -9,7 +8,7 @@ namespace SabreTools.Matching.Test public class MatchUtilTests { [Fact] - public void ExactSizeMatch() + public void ExactSizeArrayMatch() { byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; @@ -23,5 +22,23 @@ namespace SabreTools.Matching.Test string? actual = MatchUtil.GetFirstMatch("testfile", source, matchers); Assert.Equal(expected, actual); } + + [Fact] + public void ExactSizeStreamMatch() + { + byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; + var stream = new MemoryStream(source); + + byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; + string expected = "match"; + + var matchers = new List + { + new(check, expected), + }; + + string? actual = MatchUtil.GetFirstMatch("testfile", stream, matchers); + Assert.Equal(expected, actual); + } } } \ No newline at end of file diff --git a/SabreTools.Matching/Content/ContentMatch.cs b/SabreTools.Matching/Content/ContentMatch.cs index 1adc2ee..9cc8ccf 100644 --- a/SabreTools.Matching/Content/ContentMatch.cs +++ b/SabreTools.Matching/Content/ContentMatch.cs @@ -134,6 +134,10 @@ namespace SabreTools.Matching.Content if (Needle.Length > stack.Length) return -1; + // If the needle and stack are identically sized, short-circuit + if (Needle.Length == stack.Length) + return EqualAt(stack, 0) ? 0 : -1; + // Set the default start and end values int start = Start; int end = End;