refactor: remove length argument in matchers

This commit is contained in:
Tomas Aparicio
2015-09-27 10:05:46 +01:00
parent f2acc883e5
commit 08b47a1d80
9 changed files with 152 additions and 143 deletions
+2 -2
View File
@@ -44,7 +44,7 @@ func IsType(buf []byte, kind types.Type) bool {
if matcher == nil {
return false
}
return matcher(buf, len(buf)) != types.Unknown
return matcher(buf) != types.Unknown
}
// Checks if a given buffer matches with the given MIME type
@@ -52,7 +52,7 @@ func IsMIME(buf []byte, mime string) bool {
for _, kind := range types.Types {
if kind.MIME.Value == mime {
matcher := matchers.Matchers[kind]
return matcher(buf, len(buf)) != types.Unknown
return matcher(buf) != types.Unknown
}
}
return false
+2 -3
View File
@@ -19,7 +19,7 @@ func Match(buf []byte) (types.Type, error) {
}
for _, checker := range Matchers {
match := checker(buf, length)
match := checker(buf)
if match != types.Unknown && match.Extension != "" {
return match, nil
}
@@ -46,9 +46,8 @@ func Matches(buf []byte) bool {
// Perform a file matching againts a map of match functions
func MatchMap(buf []byte, matchers matchers.Map) types.Type {
length := len(buf)
for kind, matcher := range matchers {
if matcher(buf, length) {
if matcher(buf) {
return kind
}
}
+2 -2
View File
@@ -48,8 +48,8 @@ func TestMatches(t *testing.T) {
func TestAddMatcher(t *testing.T) {
fileType := AddType("foo", "foo/foo")
AddMatcher(fileType, func(buf []byte, l int) bool {
return l == 2 && buf[0] == 0x00 && buf[1] == 0x00
AddMatcher(fileType, func(buf []byte) bool {
return len(buf) == 2 && buf[0] == 0x00 && buf[1] == 0x00
})
if !Is([]byte{0x00, 0x00}, "foo") {
+47 -45
View File
@@ -1,20 +1,22 @@
package matchers
var TypeEpub = newType("epub", "application/epub+zip")
var TypeZip = newType("zip", "application/zip")
var TypeTar = newType("tar", "application/x-tar")
var TypeRar = newType("rar", "application/x-rar-compressed")
var TypeGz = newType("gz", "application/gzip")
var TypeBz2 = newType("bz2", "application/x-bzip2")
var Type7z = newType("7z", "application/x-7z-compressed")
var TypeXz = newType("xz", "application/x-xz")
var TypePdf = newType("pdf", "application/pdf")
var TypeExe = newType("exe", "application/x-msdownload")
var TypeSwf = newType("swf", "application/x-shockwave-flash")
var TypeRtf = newType("rtf", "application/rtf")
var TypeEot = newType("eot", "application/octet-stream")
var TypePs = newType("ps", "application/postscript")
var TypeSqlite = newType("sqlite", "application/x-sqlite3")
var (
TypeEpub = newType("epub", "application/epub+zip")
TypeZip = newType("zip", "application/zip")
TypeTar = newType("tar", "application/x-tar")
TypeRar = newType("rar", "application/x-rar-compressed")
TypeGz = newType("gz", "application/gzip")
TypeBz2 = newType("bz2", "application/x-bzip2")
Type7z = newType("7z", "application/x-7z-compressed")
TypeXz = newType("xz", "application/x-xz")
TypePdf = newType("pdf", "application/pdf")
TypeExe = newType("exe", "application/x-msdownload")
TypeSwf = newType("swf", "application/x-shockwave-flash")
TypeRtf = newType("rtf", "application/rtf")
TypeEot = newType("eot", "application/octet-stream")
TypePs = newType("ps", "application/postscript")
TypeSqlite = newType("sqlite", "application/x-sqlite3")
)
var Archive = Map{
TypeEpub: Epub,
@@ -33,8 +35,8 @@ var Archive = Map{
TypeSqlite: Sqlite,
}
func Epub(buf []byte, length int) bool {
return length > 57 &&
func Epub(buf []byte) bool {
return len(buf) > 57 &&
buf[0] == 0x50 && buf[1] == 0x4B && buf[2] == 0x3 && buf[3] == 0x4 &&
buf[30] == 0x6D && buf[31] == 0x69 && buf[32] == 0x6D && buf[33] == 0x65 &&
buf[34] == 0x74 && buf[35] == 0x79 && buf[36] == 0x70 && buf[37] == 0x65 &&
@@ -45,69 +47,69 @@ func Epub(buf []byte, length int) bool {
buf[54] == 0x2B && buf[55] == 0x7A && buf[56] == 0x69 && buf[57] == 0x70
}
func Zip(buf []byte, length int) bool {
return length > 3 &&
func Zip(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x50 && buf[1] == 0x4B &&
(buf[2] == 0x3 || buf[2] == 0x5 || buf[2] == 0x7) &&
(buf[3] == 0x4 || buf[3] == 0x6 || buf[3] == 0x8)
}
func Tar(buf []byte, length int) bool {
return length > 261 &&
func Tar(buf []byte) bool {
return len(buf) > 261 &&
buf[257] == 0x75 && buf[258] == 0x73 &&
buf[259] == 0x74 && buf[260] == 0x61 &&
buf[261] == 0x72
}
func Rar(buf []byte, length int) bool {
return length > 6 &&
func Rar(buf []byte) bool {
return len(buf) > 6 &&
buf[0] == 0x52 && buf[1] == 0x61 && buf[2] == 0x72 &&
buf[3] == 0x21 && buf[4] == 0x1A && buf[5] == 0x7 &&
(buf[6] == 0x0 || buf[6] == 0x1)
}
func Gz(buf []byte, length int) bool {
return length > 2 &&
func Gz(buf []byte) bool {
return len(buf) > 2 &&
buf[0] == 0x1F && buf[1] == 0x8B && buf[2] == 0x8
}
func Bz2(buf []byte, length int) bool {
return length > 2 &&
func Bz2(buf []byte) bool {
return len(buf) > 2 &&
buf[0] == 0x42 && buf[1] == 0x5A && buf[2] == 0x68
}
func SevenZ(buf []byte, length int) bool {
return length > 5 &&
func SevenZ(buf []byte) bool {
return len(buf) > 5 &&
buf[0] == 0x37 && buf[1] == 0x7A && buf[2] == 0xBC &&
buf[3] == 0xAF && buf[4] == 0x27 && buf[5] == 0x1C
}
func Pdf(buf []byte, length int) bool {
return length > 3 &&
func Pdf(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x25 && buf[1] == 0x50 &&
buf[2] == 0x44 && buf[3] == 0x46
}
func Exe(buf []byte, length int) bool {
return length > 1 &&
func Exe(buf []byte) bool {
return len(buf) > 1 &&
buf[0] == 0x4D && buf[1] == 0x5A
}
func Swf(buf []byte, length int) bool {
return length > 2 &&
func Swf(buf []byte) bool {
return len(buf) > 2 &&
(buf[0] == 0x43 || buf[0] == 0x46) &&
buf[1] == 0x57 && buf[2] == 0x53
}
func Rtf(buf []byte, length int) bool {
return length > 4 &&
func Rtf(buf []byte) bool {
return len(buf) > 4 &&
buf[0] == 0x7B && buf[1] == 0x5C &&
buf[2] == 0x72 && buf[3] == 0x74 &&
buf[4] == 0x66
}
func Eot(buf []byte, length int) bool {
return length > 10 &&
func Eot(buf []byte) bool {
return len(buf) > 10 &&
buf[34] == 0x4C && buf[35] == 0x50 &&
((buf[8] == 0x02 && buf[9] == 0x00 &&
buf[10] == 0x01) || (buf[8] == 0x01 &&
@@ -115,20 +117,20 @@ func Eot(buf []byte, length int) bool {
(buf[8] == 0x02 && buf[9] == 0x00 && buf[10] == 0x02))
}
func Ps(buf []byte, length int) bool {
return length > 1 &&
func Ps(buf []byte) bool {
return len(buf) > 1 &&
buf[0] == 0x25 && buf[1] == 0x21
}
func Xz(buf []byte, length int) bool {
return length > 5 &&
func Xz(buf []byte) bool {
return len(buf) > 5 &&
buf[0] == 0xFD && buf[1] == 0x37 &&
buf[2] == 0x7A && buf[3] == 0x58 &&
buf[4] == 0x5A && buf[5] == 0x00
}
func Sqlite(buf []byte, length int) bool {
return length > 3 &&
func Sqlite(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x53 && buf[1] == 0x51 &&
buf[2] == 0x4C && buf[3] == 0x69
}
+20 -18
View File
@@ -1,11 +1,13 @@
package matchers
var TypeMidi = newType("mid", "audio/midi")
var TypeMp3 = newType("mp3", "audio/mpeg")
var TypeM4a = newType("m4a", "audio/m4a")
var TypeOgg = newType("ogg", "audio/ogg")
var TypeFlac = newType("flac", "audio/x-flac")
var TypeWav = newType("wav", "audio/x-wav")
var (
TypeMidi = newType("mid", "audio/midi")
TypeMp3 = newType("mp3", "audio/mpeg")
TypeM4a = newType("m4a", "audio/m4a")
TypeOgg = newType("ogg", "audio/ogg")
TypeFlac = newType("flac", "audio/x-flac")
TypeWav = newType("wav", "audio/x-wav")
)
var Audio = Map{
TypeMidi: Midi,
@@ -16,39 +18,39 @@ var Audio = Map{
TypeWav: Wav,
}
func Midi(buf []byte, length int) bool {
return length > 3 &&
func Midi(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x4D && buf[1] == 0x54 &&
buf[2] == 0x68 && buf[3] == 0x64
}
func Mp3(buf []byte, length int) bool {
return length > 2 &&
func Mp3(buf []byte) bool {
return len(buf) > 2 &&
(buf[0] == 0x49 && buf[1] == 0x44 && buf[2] == 0x33) ||
(buf[0] == 0xFF && buf[1] == 0xfb)
}
func M4a(buf []byte, length int) bool {
return length > 10 &&
func M4a(buf []byte) bool {
return len(buf) > 10 &&
(buf[4] == 0x66 && buf[5] == 0x74 && buf[6] == 0x79 &&
buf[7] == 0x70 && buf[8] == 0x4D && buf[9] == 0x34 && buf[10] == 0x41) ||
(buf[0] == 0x4D && buf[1] == 0x34 && buf[2] == 0x41 && buf[3] == 0x20)
}
func Ogg(buf []byte, length int) bool {
return length > 3 &&
func Ogg(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x4F && buf[1] == 0x67 &&
buf[2] == 0x67 && buf[3] == 0x53
}
func Flac(buf []byte, length int) bool {
return length > 3 &&
func Flac(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x66 && buf[1] == 0x4C &&
buf[2] == 0x61 && buf[3] == 0x43
}
func Wav(buf []byte, length int) bool {
return length > 11 &&
func Wav(buf []byte) bool {
return len(buf) > 11 &&
buf[0] == 0x52 && buf[1] == 0x49 && buf[2] == 0x46 && buf[3] == 0x46 &&
buf[8] == 0x57 && buf[9] == 0x41 && buf[10] == 0x56 && buf[11] == 0x45
}
+14 -12
View File
@@ -1,9 +1,11 @@
package matchers
var TypeWoff = newType("woff", "application/font-woff")
var TypeWoff2 = newType("woff2", "application/font-woff")
var TypeTtf = newType("ttf", "application/font-sfnt")
var TypeOtf = newType("otf", "application/font-sfnt")
var (
TypeWoff = newType("woff", "application/font-woff")
TypeWoff2 = newType("woff2", "application/font-woff")
TypeTtf = newType("ttf", "application/font-sfnt")
TypeOtf = newType("otf", "application/font-sfnt")
)
var Font = Map{
TypeWoff: Woff,
@@ -12,27 +14,27 @@ var Font = Map{
TypeOtf: Otf,
}
func Woff(buf []byte, length int) bool {
return length > 7 &&
func Woff(buf []byte) bool {
return len(buf) > 7 &&
buf[0] == 0x77 && buf[1] == 0x4F && buf[2] == 0x46 && buf[3] == 0x46 &&
buf[4] == 0x00 && buf[5] == 0x01 && buf[6] == 0x00 && buf[7] == 0x00
}
func Woff2(buf []byte, length int) bool {
return length > 7 &&
func Woff2(buf []byte) bool {
return len(buf) > 7 &&
buf[0] == 0x77 && buf[1] == 0x4F && buf[2] == 0x46 && buf[3] == 0x32 &&
buf[4] == 0x00 && buf[5] == 0x01 && buf[6] == 0x00 && buf[7] == 0x00
}
func Ttf(buf []byte, length int) bool {
return length > 4 &&
func Ttf(buf []byte) bool {
return len(buf) > 4 &&
buf[0] == 0x00 && buf[1] == 0x01 &&
buf[2] == 0x00 && buf[3] == 0x00 &&
buf[4] == 0x00
}
func Otf(buf []byte, length int) bool {
return length > 4 &&
func Otf(buf []byte) bool {
return len(buf) > 4 &&
buf[0] == 0x4F && buf[1] == 0x54 &&
buf[2] == 0x54 && buf[3] == 0x4F &&
buf[4] == 0x00
+32 -30
View File
@@ -1,15 +1,17 @@
package matchers
var TypeJpeg = newType("jpg", "image/jpeg")
var TypePng = newType("png", "image/png")
var TypeGif = newType("gif", "image/gif")
var TypeWebp = newType("webp", "image/webp")
var TypeCR2 = newType("cr2", "image/x-canon-cr2")
var TypeTiff = newType("tif", "image/tiff")
var TypeBmp = newType("bmp", "image/bmp")
var TypeJxr = newType("jxr", "image/vnd.ms-photo")
var TypePsd = newType("psd", "image/vnd.adobe.photoshop")
var TypeIco = newType("ico", "image/x-icon")
var (
TypeJpeg = newType("jpg", "image/jpeg")
TypePng = newType("png", "image/png")
TypeGif = newType("gif", "image/gif")
TypeWebp = newType("webp", "image/webp")
TypeCR2 = newType("cr2", "image/x-canon-cr2")
TypeTiff = newType("tif", "image/tiff")
TypeBmp = newType("bmp", "image/bmp")
TypeJxr = newType("jxr", "image/vnd.ms-photo")
TypePsd = newType("psd", "image/vnd.adobe.photoshop")
TypeIco = newType("ico", "image/x-icon")
)
var Image = Map{
TypeJpeg: Jpeg,
@@ -24,59 +26,59 @@ var Image = Map{
TypeIco: Ico,
}
func Jpeg(buf []byte, length int) bool {
return length > 2 &&
func Jpeg(buf []byte) bool {
return len(buf) > 2 &&
buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF
}
func Png(buf []byte, length int) bool {
return length > 3 &&
func Png(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x89 && buf[1] == 0x50 &&
buf[2] == 0x4E && buf[3] == 0x47
}
func Gif(buf []byte, length int) bool {
return length > 2 &&
func Gif(buf []byte) bool {
return len(buf) > 2 &&
buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46
}
func Webp(buf []byte, length int) bool {
return length > 11 &&
func Webp(buf []byte) bool {
return len(buf) > 11 &&
buf[8] == 0x57 && buf[9] == 0x45 &&
buf[10] == 0x42 && buf[11] == 0x50
}
func CR2(buf []byte, length int) bool {
return length > 9 &&
func CR2(buf []byte) bool {
return len(buf) > 9 &&
((buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0x2A && buf[3] == 0x0) ||
(buf[0] == 0x4D && buf[1] == 0x4D && buf[2] == 0x0 && buf[3] == 0x2A)) &&
buf[8] == 0x43 && buf[9] == 0x52
}
func Tiff(buf []byte, length int) bool {
return length > 3 &&
func Tiff(buf []byte) bool {
return len(buf) > 3 &&
(buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0x2A && buf[3] == 0x0) ||
(buf[0] == 0x4D && buf[1] == 0x4D && buf[2] == 0x0 && buf[3] == 0x2A)
}
func Bmp(buf []byte, length int) bool {
return length > 1 &&
func Bmp(buf []byte) bool {
return len(buf) > 1 &&
buf[0] == 0x42 && buf[1] == 0x4D
}
func Jxr(buf []byte, length int) bool {
return length > 2 &&
func Jxr(buf []byte) bool {
return len(buf) > 2 &&
buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0xBC
}
func Psd(buf []byte, length int) bool {
return length > 3 &&
func Psd(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x38 && buf[1] == 0x42 &&
buf[2] == 0x50 && buf[3] == 0x53
}
func Ico(buf []byte, length int) bool {
return length > 3 &&
func Ico(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x00 && buf[1] == 0x00 &&
buf[2] == 0x01 && buf[3] == 0x00
}
+4 -4
View File
@@ -6,21 +6,21 @@ import "gopkg.in/h2non/filetype.v0/types"
var newType = types.NewType
// Matcher function interface as type alias
type Matcher func([]byte, int) bool
type Matcher func([]byte) bool
// Type interface to store pairs of type with its matcher function
type Map map[types.Type]Matcher
// Type specific matcher function interface
type TypeMatcher func([]byte, int) types.Type
type TypeMatcher func([]byte) types.Type
// Store registered file type matchers
var Matchers = make(map[types.Type]TypeMatcher)
// Create and register a new type matcher function
func NewMatcher(kind types.Type, fn Matcher) TypeMatcher {
matcher := func(buf []byte, length int) types.Type {
if fn(buf, length) {
matcher := func(buf []byte) types.Type {
if fn(buf) {
return kind
}
return types.Unknown
+29 -27
View File
@@ -1,14 +1,16 @@
package matchers
var TypeMp4 = newType("mp4", "video/mp4")
var TypeM4v = newType("m4v", "video/x-m4v")
var TypeMkv = newType("mkv", "video/x-matroska")
var TypeWebm = newType("webm", "video/webm")
var TypeMov = newType("mov", "video/quicktime")
var TypeAvi = newType("avi", "video/x-msvideo")
var TypeWmv = newType("wmv", "video/x-ms-wmv")
var TypeMpeg = newType("mpg", "video/mpeg")
var TypeFlv = newType("flv", "video/x-flv")
var (
TypeMp4 = newType("mp4", "video/mp4")
TypeM4v = newType("m4v", "video/x-m4v")
TypeMkv = newType("mkv", "video/x-matroska")
TypeWebm = newType("webm", "video/webm")
TypeMov = newType("mov", "video/quicktime")
TypeAvi = newType("avi", "video/x-msvideo")
TypeWmv = newType("wmv", "video/x-ms-wmv")
TypeMpeg = newType("mpg", "video/mpeg")
TypeFlv = newType("flv", "video/x-flv")
)
var Video = Map{
TypeMp4: Mp4,
@@ -21,8 +23,8 @@ var Video = Map{
TypeFlv: Flv,
}
func M4v(buf []byte, length int) bool {
return length > 10 &&
func M4v(buf []byte) bool {
return len(buf) > 10 &&
buf[0] == 0x0 && buf[1] == 0x0 &&
buf[2] == 0x0 && buf[3] == 0x1C &&
buf[4] == 0x66 && buf[5] == 0x74 &&
@@ -31,38 +33,38 @@ func M4v(buf []byte, length int) bool {
buf[10] == 0x56
}
func Mkv(buf []byte, length int) bool {
return length > 38 &&
func Mkv(buf []byte) bool {
return len(buf) > 38 &&
buf[31] == 0x6D && buf[32] == 0x61 &&
buf[33] == 0x74 && buf[34] == 0x72 &&
buf[35] == 0x6f && buf[36] == 0x73 &&
buf[37] == 0x6B && buf[38] == 0x61
}
func Webm(buf []byte, length int) bool {
return length > 3 &&
func Webm(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x1A && buf[1] == 0x45 &&
buf[2] == 0xDF && buf[3] == 0xA3
}
func Mov(buf []byte, length int) bool {
return length > 7 &&
func Mov(buf []byte) bool {
return len(buf) > 7 &&
buf[0] == 0x0 && buf[1] == 0x0 &&
buf[2] == 0x0 && buf[3] == 0x14 &&
buf[4] == 0x66 && buf[5] == 0x74 &&
buf[6] == 0x79 && buf[7] == 0x70
}
func Avi(buf []byte, length int) bool {
return length > 10 &&
func Avi(buf []byte) bool {
return len(buf) > 10 &&
buf[0] == 0x52 && buf[1] == 0x49 &&
buf[2] == 0x46 && buf[3] == 0x46 &&
buf[8] == 0x41 && buf[9] == 0x56 &&
buf[10] == 0x49
}
func Wmv(buf []byte, length int) bool {
return length > 9 &&
func Wmv(buf []byte) bool {
return len(buf) > 9 &&
buf[0] == 0x30 && buf[1] == 0x26 &&
buf[2] == 0xB2 && buf[3] == 0x75 &&
buf[4] == 0x8E && buf[5] == 0x66 &&
@@ -70,20 +72,20 @@ func Wmv(buf []byte, length int) bool {
buf[8] == 0xA6 && buf[9] == 0xD9
}
func Mpeg(buf []byte, length int) bool {
return length > 3 &&
func Mpeg(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x1 &&
buf[3] >= 0xb0 && buf[3] <= 0xbf
}
func Flv(buf []byte, length int) bool {
return length > 3 &&
func Flv(buf []byte) bool {
return len(buf) > 3 &&
buf[0] == 0x46 && buf[1] == 0x4C &&
buf[2] == 0x56 && buf[3] == 0x01
}
func Mp4(buf []byte, length int) bool {
return length > 27 &&
func Mp4(buf []byte) bool {
return len(buf) > 27 &&
(buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x0 &&
(buf[3] == 0x18 || buf[3] == 0x20) && buf[4] == 0x66 &&
buf[5] == 0x74 && buf[6] == 0x79 && buf[7] == 0x70) ||