Permalink
Browse files

Fix issue 12: Misses match after partial match

Added bug fix
Added test case

The beginning of an actual match could have been found in a partial match.

For example 001 in 0001. The code attempts to match the 00 but hits the 3rd
zero and drops the partial match. However, it didn't reset itself to the next 0
which is a valid start point for a match and thus 001 is missed in 0001.

See also Issue 12 on github:
#12
  • Loading branch information...
1 parent fb6ffd0 commit e276860e193e1abfa02f59a46005715d8bcfe94d @loarabia committed Jun 5, 2011
Showing with 21 additions and 0 deletions.
  1. +5 −0 MediaParsers.Desktop/BitTools.cs
  2. +16 −0 MediaParsersTests/BitToolsTests/FindBitPatternTests.cs
View
5 MediaParsers.Desktop/BitTools.cs
@@ -277,6 +277,11 @@ public static int FindBitPattern(byte[] data, byte[] pattern, byte[] mask, int s
}
else if (pattern[pati] != (data[di] & mask[pati]))
{
+ // If we were in the middle of matching a pattern, move back in case the pattern occurred within this section
+ if (pati > 0)
+ {
+ di -= pati;
+ }
pati = 0;
}
else
View
16 MediaParsersTests/BitToolsTests/FindBitPatternTests.cs
@@ -111,6 +111,22 @@ public void PatternIsData()
}
[TestMethod]
+ public void PartialPattern()
+ {
+ byte[] localDataArray = new byte[20]
+ {
+ 0, 0, 0, 1, 0,
+ 48, 101, 45, 97, 1,
+ 88, 53, 97, 58, 45,
+ 0, 0, 0, 1, 109
+ };
+ this.mask = new byte[3] { 255, 255, 255 };
+ this.pattern = new byte[3] { 0, 0, 1 };
+ int result = BitTools.FindBitPattern(localDataArray, this.pattern, this.mask);
+ Assert.AreEqual(1, result);
+ }
+
+ [TestMethod]
public void PatternAtEnd()
{
this.mask = new byte[1] { 255 };

0 comments on commit e276860

Please sign in to comment.