Skip to content

Commit

Permalink
Add test for zero length input and refactor some funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Jul 24, 2019
1 parent 80f938c commit 34a3ae5
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion internal/matchers/archive.go
Expand Up @@ -33,7 +33,7 @@ func Gzip(in []byte) bool {

// Crx matches a Chrome extension file: a zip archive prepended by "Cr24".
func Crx(in []byte) bool {
return len(in) > 4 && bytes.Equal(in[:4], []byte("Cr24"))
return bytes.HasPrefix(in, []byte("Cr24"))
}

// Tar matches a (t)ape (ar)chive file.
Expand Down
2 changes: 1 addition & 1 deletion internal/matchers/audio.go
Expand Up @@ -26,7 +26,7 @@ func Ape(in []byte) bool {

// MusePack matches a Musepack file.
func MusePack(in []byte) bool {
return len(in) > 4 && bytes.Equal(in[:4], []byte("MPCK"))
return bytes.HasPrefix(in, []byte("MPCK"))
}

// Wav matches a Waveform Audio File Format file.
Expand Down
11 changes: 5 additions & 6 deletions internal/matchers/binary.go
Expand Up @@ -6,20 +6,19 @@ import (

// Class matches an java class file.
func Class(in []byte) bool {
return len(in) > 4 && bytes.Equal(in[:4], []byte{0xCA, 0xFE, 0xBA, 0xBE})
return bytes.HasPrefix(in, []byte{0xCA, 0xFE, 0xBA, 0xBE})
}

// Swf matches an Adobe Flash swf file.
func Swf(in []byte) bool {
return len(in) > 3 &&
bytes.Equal(in[:3], []byte("CWS")) ||
bytes.Equal(in[:3], []byte("FWS")) ||
bytes.Equal(in[:3], []byte("ZWS"))
return bytes.HasPrefix(in, []byte("CWS")) ||
bytes.HasPrefix(in, []byte("FWS")) ||
bytes.HasPrefix(in, []byte("ZWS"))
}

// Wasm matches a web assembly File Format file.
func Wasm(in []byte) bool {
return len(in) > 4 && bytes.Equal(in[:4], []byte{0x00, 0x61, 0x73, 0x6D})
return bytes.HasPrefix(in, []byte{0x00, 0x61, 0x73, 0x6D})
}

// Dbf matches a dBase file.
Expand Down
7 changes: 5 additions & 2 deletions internal/matchers/document.go
Expand Up @@ -4,7 +4,7 @@ import "bytes"

// Pdf matches a Portable Document Format file.
func Pdf(in []byte) bool {
return len(in) > 4 && bytes.Equal(in[:4], []byte{0x25, 0x50, 0x44, 0x46})
return bytes.HasPrefix(in, []byte{0x25, 0x50, 0x44, 0x46})
}

// DjVu matches a DjVu file
Expand All @@ -15,7 +15,10 @@ func DjVu(in []byte) bool {
if len(in) < 15 {
return false
}
return bytes.HasPrefix(in[12:], []byte("DJVM")) || bytes.HasPrefix(in[12:], []byte("DJVU")) || bytes.HasPrefix(in[12:], []byte("DJVI")) || bytes.HasPrefix(in[12:], []byte("THUM"))
return bytes.HasPrefix(in[12:], []byte("DJVM")) ||
bytes.HasPrefix(in[12:], []byte("DJVU")) ||
bytes.HasPrefix(in[12:], []byte("DJVI")) ||
bytes.HasPrefix(in[12:], []byte("THUM"))
}

// Mobi matches a Mobi file
Expand Down
2 changes: 1 addition & 1 deletion internal/matchers/font.go
Expand Up @@ -4,7 +4,7 @@ import "bytes"

// Woff matches a Web Open Font Format file.
func Woff(in []byte) bool {
return len(in) > 4 && bytes.Equal(in[:4], []byte("wOFF"))
return bytes.HasPrefix(in, []byte("wOFF"))
}

// Woff2 matches a Web Open Font Format version 2 file.
Expand Down
2 changes: 1 addition & 1 deletion internal/matchers/geo.go
Expand Up @@ -40,5 +40,5 @@ func Shp(in []byte) bool {
// Shx matches a shape index format file.
// https://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
func Shx(in []byte) bool {
return len(in) > 4 && bytes.Equal(in[:4], []byte{0x00, 0x00, 0x27, 0x0A})
return bytes.HasPrefix(in, []byte{0x00, 0x00, 0x27, 0x0A})
}
12 changes: 5 additions & 7 deletions internal/matchers/image.go
Expand Up @@ -4,13 +4,12 @@ import "bytes"

// Png matches a Portable Network Graphics file.
func Png(in []byte) bool {
return len(in) > 8 &&
bytes.Equal(in[:8], []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A})
return bytes.HasPrefix(in, []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A})
}

// Jpg matches a Joint Photographic Experts Group file.
func Jpg(in []byte) bool {
return len(in) > 3 && bytes.Equal(in[:3], []byte{0xFF, 0xD8, 0xFF})
return bytes.HasPrefix(in, []byte{0xFF, 0xD8, 0xFF})
}

// Gif matches a Graphics Interchange Format file.
Expand Down Expand Up @@ -50,9 +49,8 @@ func Ico(in []byte) bool {

// Tiff matches a Tagged Image File Format file.
func Tiff(in []byte) bool {
return len(in) > 4 &&
(bytes.Equal(in[:4], []byte{0x49, 0x49, 0x2A, 0x00}) ||
bytes.Equal(in[:4], []byte{0x4D, 0x4D, 0x00, 0x2A}))
return bytes.HasPrefix(in, []byte{0x49, 0x49, 0x2A, 0x00}) ||
bytes.HasPrefix(in, []byte{0x4D, 0x4D, 0x00, 0x2A})
}

// Bpg matches a Better Portable Graphics file.
Expand All @@ -62,7 +60,7 @@ func Bpg(in []byte) bool {

// Dwg matches a CAD drawing file.
func Dwg(in []byte) bool {
if in[0] != 0x41 || in[1] != 0x43 || len(in) < 6 {
if len(in) < 6 || in[0] != 0x41 || in[1] != 0x43 {
return false
}
dwgVersions := [][]byte{
Expand Down
5 changes: 4 additions & 1 deletion internal/matchers/text.go
Expand Up @@ -158,6 +158,9 @@ func Json(in []byte) bool {
//
// BUG(gabriel-vasile): The "type" key should be searched for in the root object.
func GeoJson(in []byte) bool {
if len(in) == 0 {
return false
}
in = trimLWS(in)
// geojson is always an object
if in[0] != '{' {
Expand Down Expand Up @@ -227,7 +230,7 @@ func Tcl(in []byte) bool {

// Rtf matches a Rich Text Format file.
func Rtf(in []byte) bool {
return len(in) > 6 && bytes.Equal(in[:6], []byte("{\\rtf1"))
return bytes.HasPrefix(in, []byte("{\\rtf1"))
}

// Svg matches a SVG file.
Expand Down
2 changes: 1 addition & 1 deletion internal/matchers/video.go
Expand Up @@ -52,7 +52,7 @@ func Flv(in []byte) bool {

// Mpeg matches a Moving Picture Experts Group file.
func Mpeg(in []byte) bool {
return len(in) > 3 && bytes.Equal(in[:3], []byte("\x00\x00\x01")) &&
return bytes.HasPrefix(in, []byte{0x00, 0x00, 0x01}) &&
in[3] >= 0xB0 && in[3] <= 0xBF
}

Expand Down
6 changes: 6 additions & 0 deletions mime_test.go
Expand Up @@ -233,6 +233,12 @@ Extension | MIME type
}
}

func TestIndexOutOfRange(t *testing.T) {
for _, n := range root.flatten() {
_ = n.matchFunc(nil)
}
}

func BenchmarkMatchDetect(b *testing.B) {
files := []string{"png.png", "jpg.jpg", "pdf.pdf", "zip.zip", "docx.docx", "doc.doc"}
data, fLen := [][matchers.ReadLimit]byte{}, len(files)
Expand Down

0 comments on commit 34a3ae5

Please sign in to comment.