h2non / filetype

Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature
https://pkg.go.dev/github.com/h2non/filetype?tab=doc
MIT License
2.05k stars 178 forks source link

Introduce bytePrefixMatcher helper #99

Closed cfergeau closed 3 years ago

cfergeau commented 3 years ago

Many matchers check for a known magic prefix at the beginning of the data being sniffed. They currently all reimplement length check + byte by byte comparison to achieve this. This commit adds a generic helper which can be passed the magic pattern and returns a

The matchers were exported (Gz(), Bz2(), SevenZ(), ...), this commit removes them as I'm not sure if this was intentional. They can be easily readded as 'var Xz = bytePrefixMatcher(xzMagic)' if needed.

I've only made changes to matchers/archive.go for now, but other matchers could benefit from the same change.

h2non commented 3 years ago

Thank you!