Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
44f1278
add chimera library
flier Sep 23, 2021
79be099
Merge branch 'master' into features/chimera
flier Sep 23, 2021
fad9e25
force to use C++ compiler
flier Sep 23, 2021
95450aa
refactor internal and add chimera
flier Sep 24, 2021
8213a65
test chiema API
flier Sep 24, 2021
310fe63
add section to build chimera
flier Sep 25, 2021
5a1f40c
build with docker
flier Sep 26, 2021
48ed8bd
install hyperscan with flier/install-hyperscan@main
flier Sep 26, 2021
c23bd7f
compile patterns with database builder
flier Sep 27, 2021
47d5018
improve examples
flier Sep 27, 2021
5b8fdcd
make go test -race happy
flier Sep 30, 2021
7a9f702
upgrade dependencies
flier Sep 30, 2021
445256f
run and upload coverage to Codecov
flier Sep 30, 2021
46b29b2
include <stdint.h>
flier Sep 30, 2021
c4079fa
use CODECOV_TOKEN from secrets
flier Sep 30, 2021
ab80419
run coverage base on matrix setting
flier Sep 30, 2021
ce63ea8
add codecov badge
flier Sep 30, 2021
98b061a
Merge remote-tracking branch 'origin/master' into features/chimera
flier Nov 23, 2021
3dc6614
Merge remote-tracking branch 'origin/master' into features/chimera
flier Nov 24, 2021
5a93585
replace Expression with string for back compatibility
flier Nov 24, 2021
f123508
add benchmark for chimera
flier Nov 24, 2021
fce3c0e
ignore scan flags in MatchRecorder.Handle
flier Nov 24, 2021
f7edc13
fix lint warning
flier Nov 24, 2021
84539c6
Merge remote-tracking branch 'origin/master' into features/chimera
flier Nov 24, 2021
6abc2e8
Merge remote-tracking branch 'origin/master' into features/chimera
flier Nov 24, 2021
445e752
remove unused package
flier Nov 24, 2021
1c6de84
use chimera build tag to enable chimera support
flier Apr 10, 2022
22ead3e
Merge branch 'features/chimera' of https://github.com/flier/gohs into…
flier Apr 10, 2022
db4a452
upgrade dependencies
flier Apr 10, 2022
372c7ae
add chimera build tag
flier Apr 10, 2022
b3167f7
use build tag `hyperscan_v52` for literal support
flier Apr 10, 2022
6a9dcf4
fix spell error
flier Apr 10, 2022
2c64a98
format code
flier Apr 10, 2022
f14fec4
upgrade to actions/cache@v3
flier Apr 11, 2022
29e2638
use flier/install-hyperscan action to cache hyperscan
flier Apr 11, 2022
234a807
fix test cases for docker
flier Apr 11, 2022
16a18a2
upgrade to golangci/golangci-lint-action@v3
flier Apr 11, 2022
42300bc
fix lint warning
flier Apr 11, 2022
f7e2e6c
make golangci-lint happy
flier Apr 11, 2022
1412847
add chimera tag for ubuntu 20.04 and hyperscan 5.1.1
flier Apr 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve examples
  • Loading branch information
flier committed Sep 27, 2021
commit 47d50184998a97baffc91bc6a3772a9b7a8baf50
2 changes: 1 addition & 1 deletion chimera/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (m *blockMatcher) OnMatch(id uint, from, to uint64, flags uint,
return Continue
}

if m.n < len(m.Events) {
if m.n <= len(m.Events) {
m.Events = m.Events[:m.n]

return Terminate
Expand Down
13 changes: 4 additions & 9 deletions chimera/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@ const (
ErrScratchInUse Error = ch.ErrScratchInUse
)

// ModeFlag represents the compile mode flags.
type ModeFlag = hyperscan.ModeFlag

// BlockMode for the block scan (non-streaming) database.
const BlockMode = hyperscan.BlockMode

// DbInfo identify the version and platform information for the supplied database.
type DbInfo string // nolint: stylecheck
// nolint: stylecheck
type DbInfo string

// parse `Chimera Version: 5.4.0 Features: AVX2 Mode: BLOCK`.
var regexDBInfo = regexp.MustCompile(`^Chimera Version: (\d+\.\d+\.\d+) Features: ([\w\s]+)? Mode: (\w+)$`)
Expand Down Expand Up @@ -76,7 +71,7 @@ func (i DbInfo) Version() (string, error) {
}

// Mode is the scanning mode for the supplied database.
func (i DbInfo) Mode() (ModeFlag, error) {
func (i DbInfo) Mode() (hyperscan.ModeFlag, error) {
_, _, mode, err := i.Parse()
if err != nil {
return 0, err
Expand All @@ -85,7 +80,7 @@ func (i DbInfo) Mode() (ModeFlag, error) {
return hyperscan.ParseModeFlag(mode) // nolint: wrapcheck
}

// Database is an immutable database that can be used by the Hyperscan scanning API.
// Database is an immutable database that can be used by the Chimera scanning API.
type Database interface {
// Provides information about a database.
Info() (DbInfo, error)
Expand Down
7 changes: 4 additions & 3 deletions chimera/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
. "github.com/smartystreets/goconvey/convey"

"github.com/flier/gohs/chimera"
"github.com/flier/gohs/hyperscan"
)

func TestChimera(t *testing.T) {
Expand All @@ -16,7 +17,7 @@ func TestChimera(t *testing.T) {

func TestBaseDatabase(t *testing.T) {
Convey("Given a block database", t, func() {
So(chimera.ValidPlatform(), ShouldBeNil)
So(hyperscan.ValidPlatform(), ShouldBeNil)

bdb, err := chimera.NewBlockDatabase(&chimera.Pattern{Expression: "test"})

Expand Down Expand Up @@ -50,7 +51,7 @@ func TestBaseDatabase(t *testing.T) {
mode, err := info.Mode()

So(err, ShouldBeNil)
So(mode, ShouldEqual, chimera.BlockMode)
So(mode, ShouldEqual, hyperscan.BlockMode)
})
})

Expand Down Expand Up @@ -78,7 +79,7 @@ func TestBlockDatabase(t *testing.T) {
mode, err := info.Mode()

So(err, ShouldBeNil)
So(mode, ShouldEqual, chimera.BlockMode)
So(mode, ShouldEqual, hyperscan.BlockMode)
})
})

Expand Down
18 changes: 13 additions & 5 deletions chimera/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"
"strconv"

"github.com/flier/gohs/hyperscan"
"github.com/flier/gohs/internal/ch"
"github.com/flier/gohs/internal/hs"
)
Expand Down Expand Up @@ -67,31 +68,38 @@ const (
Groups CompileMode = ch.Groups
)

// Builder creates a database with the given mode and target platform.
type Builder interface {
// Build the database with the given mode.
Build(mode CompileMode) (Database, error)

ForPlatform(mode CompileMode, platform Platform) (Database, error)
// ForPlatform determine the target platform for the database
ForPlatform(mode CompileMode, platform hyperscan.Platform) (Database, error)
}

// Build the database with the given mode.
func (p *Pattern) Build(mode CompileMode) (Database, error) {
return p.ForPlatform(mode, nil)
}

func (p *Pattern) ForPlatform(mode CompileMode, platform Platform) (Database, error) {
// ForPlatform determine the target platform for the database.
func (p *Pattern) ForPlatform(mode CompileMode, platform hyperscan.Platform) (Database, error) {
b := DatabaseBuilder{Patterns: Patterns{p}, Mode: mode, Platform: platform}
return b.Build()
}

// Build the database with the given mode.
func (p Patterns) Build(mode CompileMode) (Database, error) {
return p.ForPlatform(mode, nil)
}

func (p Patterns) ForPlatform(mode CompileMode, platform Platform) (Database, error) {
// ForPlatform determine the target platform for the database.
func (p Patterns) ForPlatform(mode CompileMode, platform hyperscan.Platform) (Database, error) {
b := DatabaseBuilder{Patterns: p, Mode: mode, Platform: platform}
return b.Build()
}

// DatabaseBuilder to help to build up a database.
// DatabaseBuilder creates a database that will be used to matching the patterns.
type DatabaseBuilder struct {
// Array of patterns to compile.
Patterns
Expand All @@ -101,7 +109,7 @@ type DatabaseBuilder struct {

// If not nil, the platform structure is used to determine the target platform for the database.
// If nil, a database suitable for running on the current host platform is produced.
Platform
hyperscan.Platform

// A limit from pcre_extra on the amount of match function called in PCRE to limit backtracking that can take place.
MatchLimit uint
Expand Down
2 changes: 1 addition & 1 deletion chimera/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestDatabaseBuilder(t *testing.T) {
mode, err := info.Mode()

So(err, ShouldBeNil)
So(mode, ShouldEqual, chimera.BlockMode)
So(mode, ShouldEqual, hyperscan.BlockMode)

So(db.Close(), ShouldBeNil)
})
Expand Down
2 changes: 1 addition & 1 deletion chimera/example_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ExampleBlockScanner() {

var matches []Match

handler := chimera.MatchHandlerFunc(func(id uint, from, to uint64, flags uint,
handler := chimera.HandlerFunc(func(id uint, from, to uint64, flags uint,
captured []*chimera.Capture, ctx interface{}) chimera.Callback {
matches = append(matches, Match{from, to})
return chimera.Continue
Expand Down
18 changes: 8 additions & 10 deletions chimera/runtime.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package chimera

import (
"github.com/flier/gohs/hyperscan"
"github.com/flier/gohs/internal/ch"
)

// Platform is a type containing information on the target platform.
type Platform = hyperscan.Platform

// ValidPlatform test the current system architecture.
var ValidPlatform = hyperscan.ValidPlatform

// Callback return value used to tell the Chimera matcher what to do after processing this match.
type Callback = ch.Callback

Expand Down Expand Up @@ -43,12 +36,17 @@ type Handler interface {
OnError(event ErrorEvent, id uint, info, context interface{}) Callback
}

type MatchHandlerFunc func(id uint, from, to uint64, flags uint, captured []*Capture, context interface{}) Callback
// HandlerFunc type is an adapter to allow the use of ordinary functions as Chimera handlers.
// If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.
type HandlerFunc func(id uint, from, to uint64, flags uint, captured []*Capture, context interface{}) Callback

func (f MatchHandlerFunc) OnMatch(id uint, from, to uint64, flags uint, captured []*Capture, ctx interface{}) Callback {
// OnMatch will be invoked whenever a match is located in the target data during the execution of a scan.
func (f HandlerFunc) OnMatch(id uint, from, to uint64, flags uint, captured []*Capture, ctx interface{}) Callback {
return f(id, from, to, flags, captured, ctx)
}

func (f MatchHandlerFunc) OnError(event ErrorEvent, id uint, info, context interface{}) Callback {
// OnError will be invoked when an error event occurs during matching;
// this indicates that some matches for a given expression may not be reported.
func (f HandlerFunc) OnError(event ErrorEvent, id uint, info, context interface{}) Callback {
return Terminate
}
2 changes: 1 addition & 1 deletion chimera/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBlockScanner(t *testing.T) {
return chimera.Continue
}

err = bdb.Scan([]byte("abc123def456"), nil, chimera.MatchHandlerFunc(matched), nil)
err = bdb.Scan([]byte("abc123def456"), nil, chimera.HandlerFunc(matched), nil)

So(err, ShouldBeNil)
So(matches, ShouldResemble, [][]uint64{{3, 6}, {9, 12}})
Expand Down
4 changes: 3 additions & 1 deletion examples/pcapscan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"io/ioutil"
"net"
"os"
"path/filepath"
"runtime/pprof"
"strconv"
"strings"
Expand Down Expand Up @@ -412,7 +413,8 @@ func main() {
flag.Parse()

if flag.NArg() != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [-n repeats] <pattern file> <pcap file>\n", os.Args[0])
_, prog := filepath.Split(os.Args[0])
fmt.Fprintf(os.Stderr, "Usage: %s [-n repeats] <pattern file> <pcap file>\n", prog)
os.Exit(-1)
}

Expand Down
Loading