MCPcopy Index your code
hub / github.com/crowdsecurity/crowdsec / RegexpInFile

Function RegexpInFile

pkg/exprhelpers/helpers.go:472–522  ·  view source on GitHub ↗

func RegexpInFile(data string, filename string) bool {

(params ...any)

Source from the content-addressed store, hash-verified

470
471// func RegexpInFile(data string, filename string) bool {
472func RegexpInFile(params ...any) (any, error) {
473 data := params[0].(string)
474 filename := params[1].(string)
475
476 var hash uint64
477
478 hasCache := false
479 matched := false
480
481 if _, ok := dataFileRegexCache[filename]; ok {
482 hasCache = true
483 hash = xxhash.Sum64String(data)
484
485 if val, err := dataFileRegexCache[filename].Get(hash); err == nil {
486 return val.(bool), nil
487 }
488 }
489
490 switch fflag.Re2RegexpInfileSupport.IsEnabled() {
491 case true:
492 if _, ok := dataFileRe2[filename]; ok {
493 for _, re := range dataFileRe2[filename] {
494 if re.MatchString(data) {
495 matched = true
496 break
497 }
498 }
499 } else {
500 log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
501 log.Errorf("expr library : %s", spew.Sdump(dataFileRe2))
502 }
503 case false:
504 if _, ok := dataFileRegex[filename]; ok {
505 for _, re := range dataFileRegex[filename] {
506 if re.MatchString(data) {
507 matched = true
508 break
509 }
510 }
511 } else {
512 log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
513 log.Errorf("expr library : %s", spew.Sdump(dataFileRegex))
514 }
515 }
516
517 if hasCache {
518 dataFileRegexCache[filename].Set(hash, matched)
519 }
520
521 return matched, nil
522}
523
524// func IpInRange(ip string, ipRange string) bool {
525func IpInRange(params ...any) (any, error) {

Callers 1

TestRegexpCacheBehaviorFunction · 0.85

Calls 3

GetMethod · 0.45
IsEnabledMethod · 0.45
SetMethod · 0.45

Tested by 1

TestRegexpCacheBehaviorFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…