File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -227,8 +227,12 @@ func RegexpPatternFromYaraPattern(pattern string) (*RegexAndNeedle, error) {
227
227
func FindRegex (data []byte , regexInfo * RegexAndNeedle ) []int {
228
228
data_len := len (data )
229
229
matches := make ([]int , 0 )
230
+
231
+ // use an optimized memscan to find some candidates chunks from the much large haystack
230
232
needleMatches := findAllOccurrences (data , [][]byte {regexInfo .needle })
231
233
for _ , needleMatch := range needleMatches {
234
+ // we might have found a needle beginning at the very end of our regex
235
+ // widen the window to regex scan from the [-regexLen:regexLen] so we scan the front too
232
236
data_start := needleMatch - regexInfo .len
233
237
data_end := needleMatch + regexInfo .len
234
238
if data_start >= data_len {
@@ -241,8 +245,11 @@ func FindRegex(data []byte, regexInfo *RegexAndNeedle) []int {
241
245
data_end = data_len - 1
242
246
}
243
247
248
+ // do the full regex scan on a very small chunk
244
249
for _ , reMatch := range regexInfo .re .FindAllIndex (data [data_start :data_end ], - 1 ) {
245
- start := reMatch [0 ]
250
+ // the match offset is the start index of the chunk + reMatch index
251
+ start := reMatch [0 ] + data_start
252
+
246
253
//end := reMatch[1]
247
254
matches = append (matches , start )
248
255
}
You can’t perform that action at this time.
0 commit comments