| 1 |
package matchers |
| 2 |
|
| 3 |
import "github.com/h2non/filetype/matchers/isobmff" |
| 4 |
|
| 5 |
var ( |
| 6 |
TypeJpeg = newType("jpg", "image/jpeg") |
| 7 |
TypeJpeg2000 = newType("jp2", "image/jp2") |
| 8 |
TypePng = newType("png", "image/png") |
| 9 |
TypeGif = newType("gif", "image/gif") |
| 10 |
TypeWebp = newType("webp", "image/webp") |
| 11 |
TypeCR2 = newType("cr2", "image/x-canon-cr2") |
| 12 |
TypeTiff = newType("tif", "image/tiff") |
| 13 |
TypeBmp = newType("bmp", "image/bmp") |
| 14 |
TypeJxr = newType("jxr", "image/vnd.ms-photo") |
| 15 |
TypePsd = newType("psd", "image/vnd.adobe.photoshop") |
| 16 |
TypeIco = newType("ico", "image/vnd.microsoft.icon") |
| 17 |
TypeHeif = newType("heif", "image/heif") |
| 18 |
TypeDwg = newType("dwg", "image/vnd.dwg") |
| 19 |
) |
| 20 |
|
| 21 |
var Image = Map{ |
| 22 |
TypeJpeg: Jpeg, |
| 23 |
TypeJpeg2000: Jpeg2000, |
| 24 |
TypePng: Png, |
| 25 |
TypeGif: Gif, |
| 26 |
TypeWebp: Webp, |
| 27 |
TypeCR2: CR2, |
| 28 |
TypeTiff: Tiff, |
| 29 |
TypeBmp: Bmp, |
| 30 |
TypeJxr: Jxr, |
| 31 |
TypePsd: Psd, |
| 32 |
TypeIco: Ico, |
| 33 |
TypeHeif: Heif, |
| 34 |
TypeDwg: Dwg, |
| 35 |
} |
| 36 |
|
| 37 |
func Jpeg(buf []byte) bool { |
| 38 |
return len(buf) > 2 && |
| 39 |
buf[0] == 0xFF && |
| 40 |
buf[1] == 0xD8 && |
| 41 |
buf[2] == 0xFF |
| 42 |
} |
| 43 |
|
| 44 |
func Jpeg2000(buf []byte) bool { |
| 45 |
return len(buf) > 12 && |
| 46 |
buf[0] == 0x0 && |
| 47 |
buf[1] == 0x0 && |
| 48 |
buf[2] == 0x0 && |
| 49 |
buf[3] == 0xC && |
| 50 |
buf[4] == 0x6A && |
| 51 |
buf[5] == 0x50 && |
| 52 |
buf[6] == 0x20 && |
| 53 |
buf[7] == 0x20 && |
| 54 |
buf[8] == 0xD && |
| 55 |
buf[9] == 0xA && |
| 56 |
buf[10] == 0x87 && |
| 57 |
buf[11] == 0xA && |
| 58 |
buf[12] == 0x0 |
| 59 |
} |
| 60 |
|
| 61 |
func Png(buf []byte) bool { |
| 62 |
return len(buf) > 3 && |
| 63 |
buf[0] == 0x89 && buf[1] == 0x50 && |
| 64 |
buf[2] == 0x4E && buf[3] == 0x47 |
| 65 |
} |
| 66 |
|
| 67 |
func Gif(buf []byte) bool { |
| 68 |
return len(buf) > 2 && |
| 69 |
buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46 |
| 70 |
} |
| 71 |
|
| 72 |
func Webp(buf []byte) bool { |
| 73 |
return len(buf) > 11 && |
| 74 |
buf[8] == 0x57 && buf[9] == 0x45 && |
| 75 |
buf[10] == 0x42 && buf[11] == 0x50 |
| 76 |
} |
| 77 |
|
| 78 |
func CR2(buf []byte) bool { |
| 79 |
return len(buf) > 10 && |
| 80 |
((buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0x2A && buf[3] == 0x0) || |
| 81 |
(buf[0] == 0x4D && buf[1] == 0x4D && buf[2] == 0x0 && buf[3] == 0x2A)) && |
| 82 |
buf[8] == 0x43 && buf[9] == 0x52 && |
| 83 |
buf[10] == 0x02 |
| 84 |
} |
| 85 |
|
| 86 |
func Tiff(buf []byte) bool { |
| 87 |
return len(buf) > 10 && |
| 88 |
((buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0x2A && buf[3] == 0x0) || |
| 89 |
(buf[0] == 0x4D && buf[1] == 0x4D && buf[2] == 0x0 && buf[3] == 0x2A)) && |
| 90 |
!CR2(buf) |
| 91 |
} |
| 92 |
|
| 93 |
func Bmp(buf []byte) bool { |
| 94 |
return len(buf) > 1 && |
| 95 |
buf[0] == 0x42 && |
| 96 |
buf[1] == 0x4D |
| 97 |
} |
| 98 |
|
| 99 |
func Jxr(buf []byte) bool { |
| 100 |
return len(buf) > 2 && |
| 101 |
buf[0] == 0x49 && |
| 102 |
buf[1] == 0x49 && |
| 103 |
buf[2] == 0xBC |
| 104 |
} |
| 105 |
|
| 106 |
func Psd(buf []byte) bool { |
| 107 |
return len(buf) > 3 && |
| 108 |
buf[0] == 0x38 && buf[1] == 0x42 && |
| 109 |
buf[2] == 0x50 && buf[3] == 0x53 |
| 110 |
} |
| 111 |
|
| 112 |
func Ico(buf []byte) bool { |
| 113 |
return len(buf) > 3 && |
| 114 |
buf[0] == 0x00 && buf[1] == 0x00 && |
| 115 |
buf[2] == 0x01 && buf[3] == 0x00 |
| 116 |
} |
| 117 |
|
| 118 |
func Heif(buf []byte) bool { |
| 119 |
if !isobmff.IsISOBMFF(buf) { |
| 120 |
return false |
| 121 |
} |
| 122 |
|
| 123 |
majorBrand, _, compatibleBrands := isobmff.GetFtyp(buf) |
| 124 |
if majorBrand == "heic" { |
| 125 |
return true |
| 126 |
} |
| 127 |
|
| 128 |
if majorBrand == "mif1" || majorBrand == "msf1" { |
| 129 |
for _, compatibleBrand := range compatibleBrands { |
| 130 |
if compatibleBrand == "heic" { |
| 131 |
return true |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return false |
| 137 |
} |
| 138 |
|
| 139 |
func Dwg(buf []byte) bool { |
| 140 |
return len(buf) > 3 && |
| 141 |
buf[0] == 0x41 && buf[1] == 0x43 && |
| 142 |
buf[2] == 0x31 && buf[3] == 0x30 |
| 143 |
} |