patterns

package
v1.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package patterns describes the Pattern interface. Standard patterns are also defined in this package: Sequence (as well as BMH and reverse BMH Sequence), Choice, List and Not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Index

func Index(a, b Pattern) int

Index reports the offset of one pattern within another (or -1 if not contained)

func Overlap added in v1.8.0

func Overlap(p Pattern) int

func Overlap calculates the max distance before a possible overlap with multiple matches of the same Pattern e.g. 0xAABBAA has a length of 3 but returns 2

func OverlapR added in v1.8.0

func OverlapR(p Pattern) int

func OverlapR calculates the max distance before a possible overlap with multiple matches of the same Pattern, matching in reverse e.g. EOFE has a length of 4 but returns 3

func Register

func Register(id byte, l Loader)

Register a new Loader (provide an id higher than 16).

func Stringify

func Stringify(b []byte) string

Stringify returns a string version of a byte slice. If all bytes are UTF8, an ASCII string is returned Otherwise a hex string is returned.

Types

type AnyMask

type AnyMask byte

func (AnyMask) Equals

func (am AnyMask) Equals(pat Pattern) bool

func (AnyMask) Length

func (am AnyMask) Length() (int, int)

func (AnyMask) NumSequences

func (am AnyMask) NumSequences() int

func (AnyMask) Save

func (am AnyMask) Save(ls *persist.LoadSaver)

func (AnyMask) Sequences

func (am AnyMask) Sequences() []Sequence

func (AnyMask) String

func (am AnyMask) String() string

func (AnyMask) Test

func (am AnyMask) Test(b []byte) ([]int, int)

func (AnyMask) TestR

func (am AnyMask) TestR(b []byte) ([]int, int)

type BMHSequence

type BMHSequence struct {
	Seq Sequence

	Shift [256]int
	// contains filtered or unexported fields
}

BMHSequence is an optimised version of the regular Sequence pattern. It is used behind the scenes in the Bytematcher package to speed up matching and should not be used directly in other packages (use the plain Sequence instead).

func NewBMHSequence

func NewBMHSequence(s Sequence) *BMHSequence

NewBMHSequence turns a Sequence into a BMHSequence.

func (*BMHSequence) Equals

func (s *BMHSequence) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (*BMHSequence) Length

func (s *BMHSequence) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (*BMHSequence) NumSequences

func (s *BMHSequence) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (*BMHSequence) Save

func (s *BMHSequence) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (*BMHSequence) Sequences

func (s *BMHSequence) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (*BMHSequence) String

func (s *BMHSequence) String() string

func (*BMHSequence) Test

func (s *BMHSequence) Test(b []byte) ([]int, int)

Test bytes against the pattern.

func (*BMHSequence) TestR

func (s *BMHSequence) TestR(b []byte) ([]int, int)

Test bytes against the pattern in reverse.

type Choice

type Choice []Pattern

Choice is a slice of patterns, any of which can test successfully for the pattern to succeed. For advance, returns shortest

func (Choice) Equals

func (c Choice) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (Choice) Length

func (c Choice) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (Choice) NumSequences

func (c Choice) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (Choice) Save

func (c Choice) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (Choice) Sequences

func (c Choice) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (Choice) String

func (c Choice) String() string

func (Choice) Test

func (c Choice) Test(b []byte) ([]int, int)

Test bytes against the pattern.

func (Choice) TestR

func (c Choice) TestR(b []byte) ([]int, int)

Test bytes against the pattern in reverse.

type List

type List []Pattern

List is a slice of patterns, all of which must test true sequentially in order for the pattern to succeed.

func (List) Equals

func (l List) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (List) Length

func (l List) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (List) NumSequences

func (l List) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (List) Save

func (l List) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (List) Sequences

func (l List) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (List) String

func (l List) String() string

func (List) Test

func (l List) Test(b []byte) ([]int, int)

Test bytes against the pattern.

func (List) TestR

func (l List) TestR(b []byte) ([]int, int)

Test bytes against the pattern in reverse.

type Loader

type Loader func(*persist.LoadSaver) Pattern

Loader loads a Pattern.

type Mask

type Mask byte

func (Mask) Equals

func (m Mask) Equals(pat Pattern) bool

func (Mask) Length

func (m Mask) Length() (int, int)

func (Mask) NumSequences

func (m Mask) NumSequences() int

func (Mask) Save

func (m Mask) Save(ls *persist.LoadSaver)

func (Mask) Sequences

func (m Mask) Sequences() []Sequence

func (Mask) String

func (m Mask) String() string

func (Mask) Test

func (m Mask) Test(b []byte) ([]int, int)

func (Mask) TestR

func (m Mask) TestR(b []byte) ([]int, int)

type Not

type Not struct{ Pattern }

Not contains a pattern and reports the opposite of that pattern's result when testing.

func (Not) Equals

func (n Not) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (Not) Length

func (n Not) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (Not) NumSequences

func (n Not) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (Not) Save

func (n Not) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (Not) Sequences

func (n Not) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (Not) String

func (n Not) String() string

func (Not) Test

func (n Not) Test(b []byte) ([]int, int)

Test bytes against the pattern.

func (Not) TestR

func (n Not) TestR(b []byte) ([]int, int)

Test bytes against the pattern in reverse.

type Pattern

type Pattern interface {
	Test([]byte) ([]int, int)  // For a positive match, returns slice of lengths of the match and bytes to advance for a subsequent test. For a negative match, returns nil or empty slice and the bytes to advance for subsequent test (or 0 if the length of the pattern is longer than the length of the slice).
	TestR([]byte) ([]int, int) // Same as Test but for testing in reverse (from the right-most position of the byte slice).
	Equals(Pattern) bool       // Test equality with another pattern
	Length() (int, int)        // Minimum and maximum lengths of the pattern
	NumSequences() int         // Number of simple sequences represented by a pattern. Return 0 if the pattern cannot be represented by a defined number of simple sequence (e.g. for an indirect offset pattern) or, if in your opinion, the number of sequences is unreasonably large.
	Sequences() []Sequence     // Convert the pattern to a slice of sequences. Return an empty slice if the pattern cannot be represented by a defined number of simple sequences.
	String() string
	Save(*persist.LoadSaver) // encode the pattern into bytes for saving in a persist file
}

Patterns are the smallest building blocks of a format signature. Exact byte sequence matches are a type of pattern, as are byte ranges, non-sequence matches etc. You can define custom patterns (e.g. for W3C date type) by implementing this interface.

func BMH

func BMH(p Pattern, rev bool) Pattern

BMH turns patterns into BMH sequences if possible.

func Load

func Load(ls *persist.LoadSaver) Pattern

Load loads the Pattern, choosing the correct Loader by the leading id byte.

type RBMHSequence

type RBMHSequence struct {
	Seq Sequence

	Shift [256]int
	// contains filtered or unexported fields
}

RBMHSequence is a variant of the BMH sequence designed for reverse (R-L) matching. It is used behind the scenes in the Bytematcher package to speed up matching and should not be used directly in other packages (use the plain Sequence instead).

func NewRBMHSequence

func NewRBMHSequence(s Sequence) *RBMHSequence

NewRBMHSequence create a reverse matching BMH sequence (apply the BMH optimisation to TestR rather than Test).

func (*RBMHSequence) Equals

func (s *RBMHSequence) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (*RBMHSequence) Length

func (s *RBMHSequence) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (*RBMHSequence) NumSequences

func (s *RBMHSequence) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (*RBMHSequence) Save

func (s *RBMHSequence) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (*RBMHSequence) Sequences

func (s *RBMHSequence) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (*RBMHSequence) String

func (s *RBMHSequence) String() string

func (*RBMHSequence) Test

func (s *RBMHSequence) Test(b []byte) ([]int, int)

Test bytes against the pattern.

func (*RBMHSequence) TestR

func (s *RBMHSequence) TestR(b []byte) ([]int, int)

Test bytes against the pattern in reverse.

type Sequence

type Sequence []byte

Sequence is a matching sequence of bytes.

func (Sequence) Equals

func (s Sequence) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (Sequence) Length

func (s Sequence) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (Sequence) NumSequences

func (s Sequence) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (Sequence) Reverse

func (s Sequence) Reverse() Sequence

The Reverse method is unique to this pattern. It is used for the EOF byte sequence set

func (Sequence) Save

func (s Sequence) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (Sequence) Sequences

func (s Sequence) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (Sequence) String

func (s Sequence) String() string

func (Sequence) Test

func (s Sequence) Test(b []byte) ([]int, int)

Test bytes against the pattern.

func (Sequence) TestR

func (s Sequence) TestR(b []byte) ([]int, int)

Test bytes against the pattern in reverse.

Directories

Path Synopsis
Package tests exports shared patterns for use by the other bytematcher packages
Package tests exports shared patterns for use by the other bytematcher packages

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL