-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_compiler.go
60 lines (52 loc) · 1.87 KB
/
pattern_compiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package hypermatch
import (
"log"
"strings"
)
func compileCondition(fm *fieldMatcher, id RuleIdentifier, cond *Condition) *fieldMatcher {
if cond == nil || len(cond.Path) == 0 {
return fm
}
return compilePattern(id, cond.Path, &cond.Pattern, fm, nil)
}
func str2value(input string, prefix []byte, suffix []byte) []byte {
i := []byte(strings.ToLower(input))
r := make([]byte, 0, len(prefix)+len(i)+len(suffix)+1)
r = append(r, prefix...)
r = append(r, i...)
r = append(r, suffix...)
r = append(r, byteValueTerminator)
return r
}
func charReplace(data []byte, search byte, replace byte) []byte {
for i := 0; i < len(data); i++ {
if data[i] == search {
data[i] = replace
}
}
return data
}
func compilePattern(id RuleIdentifier, path string, pattern *Pattern, sourceFm *fieldMatcher, exitFm *fieldMatcher) *fieldMatcher {
if pattern == nil || len(path) == 0 {
return sourceFm
}
switch pattern.Type {
case PatternEquals:
return compilePatternEquals(sourceFm.GetTransition(path).Nfa, str2value(pattern.Value, nil, nil), exitFm)
case PatternPrefix:
return compilePatternWildcard(sourceFm.GetTransition(path).Nfa, str2value(pattern.Value, nil, []byte{byteWildcard}), exitFm)
case PatternSuffix:
return compilePatternWildcard(sourceFm.GetTransition(path).Nfa, str2value(pattern.Value, []byte{byteWildcard}, nil), exitFm)
case PatternWildcard:
return compilePatternWildcard(sourceFm.GetTransition(path).Nfa, charReplace(str2value(pattern.Value, nil, nil), charWildcard, byteWildcard), exitFm)
case PatternAnythingBut:
return compilePatternAnythingBut(id, path, pattern, sourceFm, exitFm)
case PatternAnyOf:
return compilePatternAnyOf(id, path, pattern, sourceFm, exitFm)
case PatternAllOf:
return compilePatternAllOf(id, path, pattern, sourceFm, exitFm)
default:
log.Printf("HyperMatch: unknown pattern type '%d', skipping!", pattern.Type)
}
return sourceFm
}