Hi, I found the Boyer-Moore search algorithm below to search for a byte pattern in byte arrays. The byte arrays are between 10MB and 50MB. The code works fine, but now I need to search for a byte pattern where certain bytes are always different.
Does anybody know if there's a way to modify this code, so it will skip certain bytes in the byte pattern and still finds it? It should find the byte pattern below no matter what byte 3, 8, 10, 11 (red) are.
Does anybody know if there's a way to modify this code, so it will skip certain bytes in the byte pattern and still finds it? It should find the byte pattern below no matter what byte 3, 8, 10, 11 (red) are.
Code:
E5 87 4A 3B 32 9B 61 40 84 C6 C3 B1 A7 30 4A 10vb.net Code:
Private needle() As Byte = {&HE5, &H87, &H4A, &H3B, &H32, &H9B, &H61, &H40, &H84, &HC6, &HC3, &HB1, &HA7, &H30, &H4A, &H10} Private Function SimpleBoyerMooreSearch(ByVal haystack As Byte(), ByVal needle As Byte()) As Integer Dim lookup As Integer() = New Integer(255) {} For i As Integer = 0 To lookup.Length - 1 lookup(i) = needle.Length Next For i As Integer = 0 To needle.Length - 1 lookup(needle(i)) = needle.Length - i - 1 Next Dim index As Integer = needle.Length - 1 Dim lastByte As Byte = needle(needle.Length - 1) While index < haystack.Length Dim checkByte As Byte = haystack(index) If haystack(index) = lastByte Then Dim found As Boolean = True For j As Integer = needle.Length - 2 To 0 Step -1 If haystack(index - needle.Length + j + 1) <> needle(j) Then found = False Exit For End If Next If found Then Return index - needle.Length + 1 Else index += 1 End If Else index += lookup(checkByte) End If End While Return -1 End Function