Skip to content

Commit e867149

Browse files
committed
Resolve #42
1 parent 7e26237 commit e867149

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

objfile/patterns.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,12 @@ func RegexpPatternFromYaraPattern(pattern string) (*RegexAndNeedle, error) {
227227
func FindRegex(data []byte, regexInfo *RegexAndNeedle) []int {
228228
data_len := len(data)
229229
matches := make([]int, 0)
230+
231+
// use an optimized memscan to find some candidates chunks from the much large haystack
230232
needleMatches := findAllOccurrences(data, [][]byte{regexInfo.needle})
231233
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
232236
data_start := needleMatch - regexInfo.len
233237
data_end := needleMatch + regexInfo.len
234238
if data_start >= data_len {
@@ -241,8 +245,11 @@ func FindRegex(data []byte, regexInfo *RegexAndNeedle) []int {
241245
data_end = data_len - 1
242246
}
243247

248+
// do the full regex scan on a very small chunk
244249
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+
246253
//end := reMatch[1]
247254
matches = append(matches, start)
248255
}

0 commit comments

Comments
 (0)