Add some common file signatures to magic.

Change-Id: I428933fdb5bb987113ae1bbd1161eca4c477b426
This commit is contained in:
Tamás Gulácsi 2014-01-23 21:55:37 +01:00
parent c35f1d1afe
commit 9feb32a329
5 changed files with 36 additions and 0 deletions

View File

@ -30,6 +30,8 @@ type prefixEntry struct {
mtype string
}
// usable source: http://www.garykessler.net/library/file_sigs.html
// mime types: http://www.iana.org/assignments/media-types/media-types.xhtml
var prefixTable = []prefixEntry{
{[]byte("GIF87a"), "image/gif"},
{[]byte("GIF89a"), "image/gif"}, // TODO: Others?
@ -39,8 +41,33 @@ var prefixTable = []prefixEntry{
{[]byte("\xff\xd8\xff\xdb"), "image/jpeg"},
{[]byte("\x49\x49\x2a\x00\x10\x00\x00\x00\x43\x52\x02"), "image/cr2"},
{[]byte{137, 'P', 'N', 'G', '\r', '\n', 26, 10}, "image/png"},
{[]byte{0x49, 0x20, 0x49}, "image/tiff"},
{[]byte{0x49, 0x49, 0x2A, 0}, "image/tiff"},
{[]byte{0x4D, 0x4D, 0, 0x2A}, "image/tiff"},
{[]byte{0x4D, 0x4D, 0, 0x2B}, "image/tiff"},
{[]byte("8BPS"), "image/vnd.adobe.photoshop"},
{[]byte("gimp xcf "), "image/xcf"},
{[]byte("-----BEGIN PGP PUBLIC KEY BLOCK---"), "text/x-openpgp-public-key"},
{[]byte("fLaC\x00\x00\x00"), "audio/flac"},
{[]byte{'I', 'D', '3'}, "audio/mpeg"},
{[]byte{0, 0, 1, 0xB7}, "video/mpeg"},
{[]byte{0, 0, 0, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20}, "video/quicktime"},
{[]byte{0, 0x6E, 0x1E, 0xF0}, "application/vnd.ms-powerpoint"},
{[]byte{0x1A, 0x45, 0xDF, 0xA3}, "video/webm"},
{[]byte("FLV\x01"), "application/vnd.adobe.flash.video"},
{[]byte{0x1F, 0x8B, 0x08}, "application/gzip"},
{[]byte{0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C}, "application/x-7z-compressed"},
{[]byte("BZh"), "application/bzip2"},
{[]byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0}, "application/x-xz"},
{[]byte{'P', 'K', 3, 4, 0x0A, 0, 2, 0}, "application/epub+zip"},
{[]byte{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1}, "application/vnd.ms-word"},
{[]byte{'P', 'K', 3, 4, 0x0A, 0x14, 0, 6, 0}, "application/vnd.openxmlformats-officedocument.custom-properties+xml"},
{[]byte{'P', 'K', 3, 4}, "application/zip"},
{[]byte("%PDF"), "application/pdf"},
{[]byte("{rtf"), "text/rtf1"},
{[]byte("BEGIN:VCARD\x0D\x0A"), "text/vcard"},
{[]byte("Return-Path: "), "message/rfc822"},
// TODO(bradfitz): popular audio & video formats at least
}

View File

@ -31,6 +31,15 @@ type magicTest struct {
var tests = []magicTest{
{fileName: "smile.jpg", want: "image/jpeg"},
{fileName: "smile.png", want: "image/png"},
{fileName: "smile.psd", want: "image/vnd.adobe.photoshop"},
{fileName: "smile.tiff", want: "image/tiff"},
{fileName: "smile.xcf", want: "image/xcf"},
{fileName: "smile.gif", want: "image/gif"},
{fileName: "foo.tar.gz", want: "application/gzip"},
{fileName: "foo.tar.xz", want: "application/x-xz"},
{fileName: "foo.tbz2", want: "application/bzip2"},
{fileName: "foo.zip", want: "application/zip"},
{fileName: "magic.pdf", want: "application/pdf"},
{data: "<html>foo</html>", want: "text/html"},
{data: "\xff", want: ""},
}

BIN
pkg/magic/testdata/foo.tar.xz vendored Normal file

Binary file not shown.

BIN
pkg/magic/testdata/foo.tbz2 vendored Normal file

Binary file not shown.

BIN
pkg/magic/testdata/magic.pdf vendored Normal file

Binary file not shown.