Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

Boyer-Moore byte array search

$
0
0
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.


Code:

E5 87 4A 3B 32 9B 61 40 84 C6 C3 B1 A7 30 4A 10

vb.net Code:
  1. Private needle() As Byte = {&HE5, &H87, &H4A, &H3B, &H32, &H9B, &H61, &H40, &H84, &HC6, &HC3, &HB1, &HA7, &H30, &H4A, &H10}
  2.  
  3.     Private Function SimpleBoyerMooreSearch(ByVal haystack As Byte(), ByVal needle As Byte()) As Integer
  4.         Dim lookup As Integer() = New Integer(255) {}
  5.         For i As Integer = 0 To lookup.Length - 1
  6.             lookup(i) = needle.Length
  7.         Next
  8.  
  9.         For i As Integer = 0 To needle.Length - 1
  10.             lookup(needle(i)) = needle.Length - i - 1
  11.         Next
  12.  
  13.         Dim index As Integer = needle.Length - 1
  14.         Dim lastByte As Byte = needle(needle.Length - 1)
  15.         While index < haystack.Length
  16.             Dim checkByte As Byte = haystack(index)
  17.             If haystack(index) = lastByte Then
  18.                 Dim found As Boolean = True
  19.                 For j As Integer = needle.Length - 2 To 0 Step -1
  20.                     If haystack(index - needle.Length + j + 1) <> needle(j) Then
  21.                         found = False
  22.                         Exit For
  23.                     End If
  24.                 Next
  25.  
  26.                 If found Then
  27.                     Return index - needle.Length + 1
  28.                 Else
  29.                     index += 1
  30.                 End If
  31.             Else
  32.                 index += lookup(checkByte)
  33.             End If
  34.         End While
  35.         Return -1
  36.     End Function

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>