From c6a596b37fa26960b15ecabe843754344637f7e2 Mon Sep 17 00:00:00 2001 From: Yuriy Syrota Date: Wed, 4 May 2022 18:00:50 +0300 Subject: [PATCH] Added support of netpbm files --- internal/magic/image.go | 20 +++- internal/magic/magic.go | 9 ++ internal/magic/magic_test.go | 13 +++ mimetype_test.go | 8 ++ supported_mimes.md | 7 +- testdata/pam.pam | Bin 0 -> 288 bytes testdata/pbm_ascii.pbm | 4 + testdata/pbm_raw.pbm | Bin 0 -> 64 bytes testdata/pfm.pfm | Bin 0 -> 737 bytes testdata/pgm_ascii.pgm | 64 ++++++++++++ testdata/pgm_raw.pgm | Bin 0 -> 118 bytes testdata/ppm_ascii.ppm | 184 +++++++++++++++++++++++++++++++++++ testdata/ppm_raw.ppm | Bin 0 -> 238 bytes tree.go | 7 +- 14 files changed, 313 insertions(+), 3 deletions(-) create mode 100644 testdata/pam.pam create mode 100644 testdata/pbm_ascii.pbm create mode 100644 testdata/pbm_raw.pbm create mode 100644 testdata/pfm.pfm create mode 100644 testdata/pgm_ascii.pgm create mode 100644 testdata/pgm_raw.pgm create mode 100644 testdata/ppm_ascii.ppm create mode 100644 testdata/ppm_raw.ppm diff --git a/internal/magic/image.go b/internal/magic/image.go index 355ad944..6686f764 100644 --- a/internal/magic/image.go +++ b/internal/magic/image.go @@ -1,6 +1,9 @@ package magic -import "bytes" +import ( + "bytes" + "regexp" +) var ( // Png matches a Portable Network Graphics file. @@ -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 { diff --git a/internal/magic/magic.go b/internal/magic/magic.go index 466058fb..c3c36115 100644 --- a/internal/magic/magic.go +++ b/internal/magic/magic.go @@ -4,6 +4,7 @@ package magic import ( "bytes" "fmt" + "regexp" ) type ( @@ -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) + } +} diff --git a/internal/magic/magic_test.go b/internal/magic/magic_test.go index abf5c559..620a7998 100644 --- a/internal/magic/magic_test.go +++ b/internal/magic/magic_test.go @@ -2,6 +2,7 @@ package magic import ( "io/ioutil" + "regexp" "testing" ) @@ -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) { diff --git a/mimetype_test.go b/mimetype_test.go index f057162b..e88e7d5b 100644 --- a/mimetype_test.go +++ b/mimetype_test.go @@ -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", diff --git a/supported_mimes.md b/supported_mimes.md index 63f6d096..808339db 100644 --- a/supported_mimes.md +++ b/supported_mimes.md @@ -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 @@ -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 | - diff --git a/testdata/pam.pam b/testdata/pam.pam new file mode 100644 index 0000000000000000000000000000000000000000..d1f424e4fb6df54056fac076a0ff5770cc26a27c GIT binary patch literal 288 zcmWGA=TcU1E=o--Nlj5ms#I|I^bJrbOD!tS%+FIW(la#BGd55N@bgtD$SFWWVa)pEj_=H3TxGDs>J8`-C hxp=q)as5XE3=9lNJP?z*BFLt}*+2#apjnNF3jpxbuE+oY literal 0 HcmV?d00001 diff --git a/testdata/pbm_ascii.pbm b/testdata/pbm_ascii.pbm new file mode 100644 index 00000000..1b96ca96 --- /dev/null +++ b/testdata/pbm_ascii.pbm @@ -0,0 +1,4 @@ +P1 +# Created by GIMP version 2.10.30 PNM plug-in +6 10 +000010000010000010000010000010000010100010011100000000000000 \ No newline at end of file diff --git a/testdata/pbm_raw.pbm b/testdata/pbm_raw.pbm new file mode 100644 index 0000000000000000000000000000000000000000..a13316a50b324cbc7cabecf0a62b1fbcaf80ff81 GIT binary patch literal 64 zcmWGA;Zjy`E=o--Nlj5ms#I|I^bJrbOD!tS%+FIW(la#BGd55N@bgtD$SF