Skip to content

Commit

Permalink
Resolve #42
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemk14ebr committed Nov 10, 2023
1 parent 7e26237 commit e867149
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion objfile/patterns.go
Expand Up @@ -227,8 +227,12 @@ func RegexpPatternFromYaraPattern(pattern string) (*RegexAndNeedle, error) {
func FindRegex(data []byte, regexInfo *RegexAndNeedle) []int {
data_len := len(data)
matches := make([]int, 0)

// use an optimized memscan to find some candidates chunks from the much large haystack
needleMatches := findAllOccurrences(data, [][]byte{regexInfo.needle})
for _, needleMatch := range needleMatches {
// we might have found a needle beginning at the very end of our regex
// widen the window to regex scan from the [-regexLen:regexLen] so we scan the front too
data_start := needleMatch - regexInfo.len
data_end := needleMatch + regexInfo.len
if data_start >= data_len {
Expand All @@ -241,8 +245,11 @@ func FindRegex(data []byte, regexInfo *RegexAndNeedle) []int {
data_end = data_len - 1
}

// do the full regex scan on a very small chunk
for _, reMatch := range regexInfo.re.FindAllIndex(data[data_start:data_end], -1) {
start := reMatch[0]
// the match offset is the start index of the chunk + reMatch index
start := reMatch[0] + data_start

//end := reMatch[1]
matches = append(matches, start)
}
Expand Down

0 comments on commit e867149

Please sign in to comment.