Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Jul 2, 2018
0 parents commit 1b28fdf
Show file tree
Hide file tree
Showing 35 changed files with 680 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Gabriel Vasile

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h1 align="center">
mimetype
</h1>

<h4 align="center">
A library for detecting mime types and extensions based on magic numbers
</h4>

## Installation
```bash
go get github.com/gabriel-vasile/mimetype


## Usage
The library exposes three functions you can use in order to detect a file type.
```go
func Detect(in []byte) (mime, extension string) {...}
func DetectReader(r io.Reader) (mime, extension string, err error) {...}
func DetectFile(file string) (mime, extension string, err error) {...}
```
See [Godoc](https://godoc.org/github.com/gabriel-vasile/mimetype) for full reference.

## Structure
<b>mimetype</b> uses an hierarchical structure to keep the matching functions.
This reduces the number of calls needed for detecting the file type. The reason
behind this choice is that there are file formats used as containers for other
file formats. For example, Microsoft office files are just zip archives,
containing specific metadata files.
<div align="center">
<img alt="Header" src="mimetype.gif" width="88%">
</div>
49 changes: 49 additions & 0 deletions matchers/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Package matchers holds the matching functions used to find mime types
package matchers

import (
"archive/zip"
"bytes"
)

func Zip(in []byte) bool {
return len(in) > 3 &&
in[0] == 0x50 && in[1] == 0x4B &&
(in[2] == 0x3 || in[2] == 0x5 || in[2] == 0x7) &&
(in[3] == 0x4 || in[3] == 0x6 || in[3] == 0x8)
}

func SevenZ(in []byte) bool {
return bytes.Equal(in[:6], []byte{0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C})
}

func Epub(in []byte) bool {
if len(in) < 58 {
return false
}
in = in[30:58]

return bytes.Equal(in, []byte("mimetypeapplication/epub+zip"))
}

func Jar(in []byte) bool {
reader := bytes.NewReader(in)
zipr, err := zip.NewReader(reader, reader.Size())
if err != nil {
return false
}

return zipHasFile(zipr, "META-INF/MANIFEST.MF")
}

func Apk(in []byte) bool {
reader := bytes.NewReader(in)
zipr, err := zip.NewReader(reader, reader.Size())
if err != nil {
return false
}

return zipHasFile(zipr, "AndroidManifest.xml") &&
zipHasFile(zipr, "classes.dex") &&
zipHasFile(zipr, "resources.arsc")
}
31 changes: 31 additions & 0 deletions matchers/audio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package matchers

import "bytes"

func Mp3(in []byte) bool {
return bytes.HasPrefix(in, []byte("\x49\x44\x33"))
}

func Flac(in []byte) bool {
return false
}

func Midi(in []byte) bool {
return false
}

func Ape(in []byte) bool {
return false
}

func MusePack(in []byte) bool {
return false
}

func Wav(in []byte) bool {
return false
}

func Aiff(in []byte) bool {
return false
}
79 changes: 79 additions & 0 deletions matchers/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package matchers

import "bytes"

type htmlSignature []byte
type xmlSignature []byte

var htmlSignatures = []htmlSignature{
htmlSignature("<!DOCTYPE HTML"),
htmlSignature("<HTML"),
htmlSignature("<HEAD"),
htmlSignature("<SCRIPT"),
htmlSignature("<IFRAME"),
htmlSignature("<H1"),
htmlSignature("<DIV"),
htmlSignature("<FONT"),
htmlSignature("<TABLE"),
htmlSignature("<A"),
htmlSignature("<STYLE"),
htmlSignature("<TITLE"),
htmlSignature("<B"),
htmlSignature("<BODY"),
htmlSignature("<BR"),
htmlSignature("<P"),
htmlSignature("<!--"),
}

func Txt(in []byte) bool {
in = trimWS(in)
for _, b := range in {
if b <= 0x08 ||
b == 0x0B ||
0x0E <= b && b <= 0x1A ||
0x1C <= b && b <= 0x1F {
return false
}
}

return true
}

func Html(in []byte) bool {
in = trimWS(in)

for _, hSig := range htmlSignatures {
if detectHtml(in, hSig) {
return true
}
}

return false
}

func Xml(in []byte) bool {
in = trimWS(in)
return bytes.HasPrefix(in, []byte("<?xml"))
}

func detectHtml(in []byte, hSig htmlSignature) bool {
if len(in) < len(hSig)+1 {
return false
}

for i, b := range hSig {
db := in[i]
if 'A' <= b && b <= 'Z' {
db &= 0xDF
}
if b != db {
return false
}
}
// Next byte must be space or right angle bracket.
if db := in[len(hSig)]; db != ' ' && db != '>' {
return false
}

return true
}
46 changes: 46 additions & 0 deletions matchers/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package matchers

import "bytes"

func Png(in []byte) bool {
return bytes.Equal(in[:8], []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A})
}

func Jpg(in []byte) bool {
return bytes.Equal(in[:3], []byte{0xFF, 0xD8, 0xFF})
}

func Gif(in []byte) bool {
return bytes.HasPrefix(in, []byte("GIF87a")) ||
bytes.HasPrefix(in, []byte("GIF89a"))
}

func Webp(in []byte) bool {
return len(in) > 11 &&
in[8] == 0x57 && in[9] == 0x45 &&
in[10] == 0x42 && in[11] == 0x50
}

func Bmp(in []byte) bool {
return len(in) > 1 &&
in[0] == 0x42 &&
in[1] == 0x4D
}

func Ps(in []byte) bool {
return bytes.HasPrefix(in, []byte("%!PS-Adobe-"))
}

func Psd(in []byte) bool {
return bytes.HasPrefix(in, []byte("8BPS"))
}

func Ico(in []byte) bool {
return len(in) > 3 &&
in[0] == 0x00 && in[1] == 0x00 &&
in[2] == 0x01 && in[3] == 0x00
}

func Tiff(in []byte) bool {
return false
}
26 changes: 26 additions & 0 deletions matchers/matchers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package matchers

import "bytes"

func Dummy(_ []byte) bool {
return true
}

func Pdf(in []byte) bool {
return bytes.Equal(in[:4], []byte{0x25, 0x50, 0x44, 0x46})
}

func trimWS(in []byte) []byte {
firstNonWS := 0
for ; firstNonWS < len(in) && isWS(in[firstNonWS]); firstNonWS++ {
}
return in[firstNonWS:]
}

func isWS(b byte) bool {
switch b {
case '\t', '\n', '\x0c', '\r', ' ':
return true
}
return false
}
61 changes: 61 additions & 0 deletions matchers/ms_office.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package matchers

import (
"archive/zip"
"bytes"
"path/filepath"
)

func Xlsx(in []byte) bool {
return checkMsOfficex(in, "xl")
}

func Docx(in []byte) bool {
return checkMsOfficex(in, "word")
}

func Pptx(in []byte) bool {
return checkMsOfficex(in, "ppt")
}

func Doc(in []byte) bool {
return false
}

func Ppt(in []byte) bool {
return false
}

func Xls(in []byte) bool {
return false
}

func checkMsOfficex(in []byte, folder string) bool {
reader := bytes.NewReader(in)
zipr, err := zip.NewReader(reader, reader.Size())
if err != nil {
return false
}

return zipHasFile(zipr, "[Content_Types].xml") && zipHasFolder(zipr, folder)
}

func zipHasFolder(r *zip.Reader, folder string) bool {
for _, f := range r.File {
if filepath.Dir(f.Name) == folder {
return true
}
}

return false
}

func zipHasFile(r *zip.Reader, file string) bool {
for _, f := range r.File {
if f.Name == file {
return true
}
}

return false
}
Loading

0 comments on commit 1b28fdf

Please sign in to comment.