Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of netpbm files #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/magic/image.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package magic

import "bytes"
import (
"bytes"
"regexp"
)

var (
// Png matches a Portable Network Graphics file.
Expand Down Expand Up @@ -44,6 +47,21 @@ var (
Hdr = prefix([]byte("#?RADIANCE\n"))
// Xpm matches X PixMap image data.
Xpm = prefix([]byte{0x2F, 0x2A, 0x20, 0x58, 0x50, 0x4D, 0x20, 0x2A, 0x2F})
// PbmAscii matches PBM ASCII image
// http://netpbm.sourceforge.net/doc/pbm.html
Pbm = re(regexp.MustCompile(`^P[14]\s+(?:#[^\n\r]*[\n\r])?\s*\d+\s+\d+\s`))
// PgmAscii matches PGM ASCII image
// http://netpbm.sourceforge.net/doc/pgm.html
Pgm = re(regexp.MustCompile(`^P[25]\s+(?:#[^\n\r]*[\n\r])?\s*\d+\s+\d+\s`))
// PpmAscii matches PPM ASCII image
// http://netpbm.sourceforge.net/doc/ppm.html
Ppm = re(regexp.MustCompile(`^P[36]\s+(?:#[^\n\r]*[\n\r])?\s*\d+\s+\d+\s`))
// Pam matches PAM image
// http://netpbm.sourceforge.net/doc/pam.html
Pam = re(regexp.MustCompile(`^P7[^\n]*\n(?:\s*(?:#[^\n\r]*|(?:WIDTH|HEIGHT|DEPTH|MAXVAL)\s+\d+|TUPLTYPE\s+[^\n]+)\n)+ENDHDR[\n\r]`))
// Pam matches PFM image
// http://netpbm.sourceforge.net/doc/pfm.html
Pfm = re(regexp.MustCompile(`^P[Ff]\s+\d+\s+\d+\s[+-]?(\d*[.])?\d+\s`))
)

func jpeg2k(sig []byte) Detector {
Expand Down
9 changes: 9 additions & 0 deletions internal/magic/magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package magic
import (
"bytes"
"fmt"
"regexp"
)

type (
Expand Down Expand Up @@ -237,3 +238,11 @@ func min(a, b int) int {
}
return b
}

// regex creates a Detector which return true if the provided regular expression
// matches the raw input.
func re(re *regexp.Regexp) Detector {
return func(raw []byte, limit uint32) bool {
return re.Match(raw)
}
}
13 changes: 13 additions & 0 deletions internal/magic/magic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package magic

import (
"io/ioutil"
"regexp"
"testing"
)

Expand Down Expand Up @@ -76,6 +77,18 @@ func TestMagic(t *testing.T) {
limit: 10,
res: true,
},
{
name: "regexp fail test",
detector: re(regexp.MustCompile(`qq`)),
raw: "somple string",
res: false,
},
{
name: "regexp pass test",
detector: re(regexp.MustCompile(`\ss\w+ [0-9]{2,}`)),
raw: "sample string 12345",
res: true,
},
}
for _, tt := range tCases {
t.Run(tt.name, func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,19 @@ var files = map[string]string{
"ott.ott": "application/vnd.oasis.opendocument.text-template",
"odc.odc": "application/vnd.oasis.opendocument.chart",
"owl2.owl": "application/owl+xml",
"pam.pam": "image/x-portable-arbitrarymap",
"pat.pat": "image/x-gimp-pat",
"pbm_ascii.pbm": "image/x-portable-bitmap",
"pbm_raw.pbm": "image/x-portable-bitmap",
"pdf.pdf": "application/pdf",
"pfm.pfm": "image/x-portable-floatmap",
"pgm_ascii.pgm": "image/x-portable-graymap",
"pgm_raw.pgm": "image/x-portable-graymap",
"php.php": "text/x-php",
"pl.pl": "text/x-perl",
"png.png": "image/png",
"ppm_ascii.ppm": "image/x-portable-pixmap",
"ppm_raw.ppm": "image/x-portable-pixmap",
"ppt.ppt": "application/vnd.ms-powerpoint",
"pptx.pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"ps.ps": "application/postscript",
Expand Down
7 changes: 6 additions & 1 deletion supported_mimes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 171 Supported MIME types
## 176 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type | Aliases
Expand Down Expand Up @@ -139,6 +139,11 @@ Extension | MIME type | Aliases
**.glb** | model/gltf-binary | -
**.avif** | image/avif | -
**.cab** | application/x-installshield | -
**.ppm** | image/x-portable-pixmap | image/x-portable-anymap
**.pgm** | image/x-portable-graymap | image/x-portable-anymap
**.pbm** | image/x-portable-bitmap | image/x-portable-anymap
**.pam** | image/x-portable-arbitrarymap | -
**.pfm** | image/x-portable-floatmap | -
**.txt** | text/plain | -
**.html** | text/html | -
**.svg** | image/svg+xml | -
Expand Down
Binary file added testdata/pam.pam
Binary file not shown.
4 changes: 4 additions & 0 deletions testdata/pbm_ascii.pbm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
P1
# Created by GIMP version 2.10.30 PNM plug-in
6 10
000010000010000010000010000010000010100010011100000000000000
Binary file added testdata/pbm_raw.pbm
Binary file not shown.
Binary file added testdata/pfm.pfm
Binary file not shown.
64 changes: 64 additions & 0 deletions testdata/pgm_ascii.pgm
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
P2
# Created by GIMP version 2.10.30 PNM plug-in
6 10
255
255
255
255
255
0
255
255
255
255
255
0
255
255
255
255
255
0
255
255
255
255
255
0
255
255
255
255
255
0
255
255
255
255
255
0
255
0
255
255
255
0
255
255
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
255
Binary file added testdata/pgm_raw.pgm
Binary file not shown.
184 changes: 184 additions & 0 deletions testdata/ppm_ascii.ppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
P3
# Created by GIMP version 2.10.30 PNM plug-in
6 10
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
255
255
255
0
0
0
255
255
255
255
255
255
255
255
255
0
0
0
255
255
255
255
255
255
0
0
0
0
0
0
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
Binary file added testdata/ppm_raw.ppm
Binary file not shown.
7 changes: 6 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var root = newMIME("application/octet-stream", "",
gzip, class, swf, crx, ttf, woff, woff2, otf, ttc, eot, wasm, shx, dbf, dcm, rar,
djvu, mobi, lit, bpg, sqlite3, dwg, nes, lnk, macho, qcp, icns, heic,
heicSeq, heif, heifSeq, hdr, mrc, mdb, accdb, zstd, cab, rpm, xz, lzip,
torrent, cpio, tzif, xcf, pat, gbr, glb, avif, cabIS,
torrent, cpio, tzif, xcf, pat, gbr, glb, avif, cabIS, ppm, pgm, pbm, pam, pfm,
// Keep text last because it is the slowest check
text,
)
Expand Down Expand Up @@ -254,4 +254,9 @@ var (
gbr = newMIME("image/x-gimp-gbr", ".gbr", magic.Gbr)
xfdf = newMIME("application/vnd.adobe.xfdf", ".xfdf", magic.Xfdf)
glb = newMIME("model/gltf-binary", ".glb", magic.Glb)
pbm = newMIME("image/x-portable-bitmap", ".pbm", magic.Pbm).alias("image/x-portable-anymap")
pgm = newMIME("image/x-portable-graymap", ".pgm", magic.Pgm).alias("image/x-portable-anymap")
ppm = newMIME("image/x-portable-pixmap", ".ppm", magic.Ppm).alias("image/x-portable-anymap")
pam = newMIME("image/x-portable-arbitrarymap", ".pam", magic.Pam)
pfm = newMIME("image/x-portable-floatmap", ".pfm", magic.Pfm)
)