syntax parser: reintroduce header regex in .hdr files

Replacing header patterns with signature patterns was a mistake, since
both have their own uses. So restore support for header regex, while
keeping support for signature regex as well.
This commit is contained in:
Dmytro Maluka 2024-03-23 18:26:22 +01:00
parent 2b8d925925
commit 3f4942cedb
2 changed files with 24 additions and 3 deletions

View file

@ -39,6 +39,7 @@ type Def struct {
type Header struct {
FileType string
FileNameRegex *regexp.Regexp
HeaderRegex *regexp.Regexp
SignatureRegex *regexp.Regexp
}
@ -46,6 +47,7 @@ type HeaderYaml struct {
FileType string `yaml:"filetype"`
Detect struct {
FNameRegexStr string `yaml:"filename"`
HeaderRegexStr string `yaml:"header"`
SignatureRegexStr string `yaml:"signature"`
} `yaml:"detect"`
}
@ -96,18 +98,22 @@ func init() {
// A yaml file might take ~400us to parse while a header file only takes ~20us
func MakeHeader(data []byte) (*Header, error) {
lines := bytes.Split(data, []byte{'\n'})
if len(lines) < 3 {
if len(lines) < 4 {
return nil, errors.New("Header file has incorrect format")
}
header := new(Header)
var err error
header.FileType = string(lines[0])
fnameRegexStr := string(lines[1])
signatureRegexStr := string(lines[2])
headerRegexStr := string(lines[2])
signatureRegexStr := string(lines[3])
if fnameRegexStr != "" {
header.FileNameRegex, err = regexp.Compile(fnameRegexStr)
}
if err == nil && headerRegexStr != "" {
header.HeaderRegex, err = regexp.Compile(headerRegexStr)
}
if err == nil && signatureRegexStr != "" {
header.SignatureRegex, err = regexp.Compile(signatureRegexStr)
}
@ -134,6 +140,9 @@ func MakeHeaderYaml(data []byte) (*Header, error) {
if hdrYaml.Detect.FNameRegexStr != "" {
header.FileNameRegex, err = regexp.Compile(hdrYaml.Detect.FNameRegexStr)
}
if err == nil && hdrYaml.Detect.HeaderRegexStr != "" {
header.HeaderRegex, err = regexp.Compile(hdrYaml.Detect.HeaderRegexStr)
}
if err == nil && hdrYaml.Detect.SignatureRegexStr != "" {
header.SignatureRegex, err = regexp.Compile(hdrYaml.Detect.SignatureRegexStr)
}
@ -154,6 +163,14 @@ func (header *Header) MatchFileName(filename string) bool {
return false
}
func (header *Header) MatchFileHeader(firstLine []byte) bool {
if header.HeaderRegex != nil {
return header.HeaderRegex.Match(firstLine)
}
return false
}
// HasFileSignature checks the presence of a stored signature
func (header *Header) HasFileSignature() bool {
return header.SignatureRegex != nil

View file

@ -18,6 +18,7 @@ type HeaderYaml struct {
FileType string `yaml:"filetype"`
Detect struct {
FNameRgx string `yaml:"filename"`
HeaderRgx string `yaml:"header"`
SignatureRgx string `yaml:"signature"`
} `yaml:"detect"`
}
@ -25,6 +26,7 @@ type HeaderYaml struct {
type Header struct {
FileType string
FNameRgx string
HeaderRgx string
SignatureRgx string
}
@ -59,6 +61,7 @@ func encode(name string, c HeaderYaml) {
f, _ := os.Create(name + ".hdr")
f.WriteString(c.FileType + "\n")
f.WriteString(c.Detect.FNameRgx + "\n")
f.WriteString(c.Detect.HeaderRgx + "\n")
f.WriteString(c.Detect.SignatureRgx + "\n")
f.Close()
}
@ -70,7 +73,8 @@ func decode(name string) Header {
var hdr Header
hdr.FileType = string(strs[0])
hdr.FNameRgx = string(strs[1])
hdr.SignatureRgx = string(strs[2])
hdr.HeaderRgx = string(strs[2])
hdr.SignatureRgx = string(strs[3])
fmt.Printf("took %v\n", time.Since(start))
return hdr