Debian Codegraph @

internal/magic/image.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "slices"
7
8 "github.com/gabriel-vasile/mimetype/internal/scan"
9 )
10
11 // Png matches a Portable Network Graphics file.
12 // https://www.w3.org/TR/PNG/
13 func Png(raw []byte, _ uint32) bool {
14 return bytes.HasPrefix(raw, []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A})
15 }
16
17 // Apng matches an Animated Portable Network Graphics file.
18 // https://wiki.mozilla.org/APNG_Specification
19 func Apng(raw []byte, _ uint32) bool {
20 b := scan.Bytes(raw)
21 b.Advance(8) // the first 8 bytes matched by regular png
22
23 // PNG chunks are composed of:
24 // 4 bytes: length in big endian
25 // 4 bytes: chunk type
26 // length bytes: chunk data
27 // 4 bytes: CRC
28 //
29 // Limit to 32, so we don't waste time on huge inputs.
30 // acTL chunk must come before any IDAT chunks.
31 // https://www.w3.org/TR/png-3/#structure
32 for i := 0; i < 32 && len(b) > 0; i++ {
33 sz, _ := b.Uint32be()
34 if bytes.HasPrefix(b, []byte("acTL")) {
35 return true
36 }
37 if bytes.HasPrefix(b, []byte("IDAT")) {
38 return false
39 }
40 if !b.Advance(int(sz + 8)) {
41 return false
42 }
43 }
44 return false
45 }
46
47 // Jpg matches a Joint Photographic Experts Group file.
48 func Jpg(raw []byte, _ uint32) bool {
49 return bytes.HasPrefix(raw, []byte{0xFF, 0xD8, 0xFF})
50 }
51
52 // Jp2 matches a JPEG 2000 Image file (ISO 15444-1).
53 func Jp2(raw []byte, _ uint32) bool {
54 return jpeg2k(raw, []byte{0x6a, 0x70, 0x32, 0x20})
55 }
56
57 // Jpx matches a JPEG 2000 Image file (ISO 15444-2).
58 func Jpx(raw []byte, _ uint32) bool {
59 return jpeg2k(raw, []byte{0x6a, 0x70, 0x78, 0x20})
60 }
61
62 // Jpm matches a JPEG 2000 Image file (ISO 15444-6).
63 func Jpm(raw []byte, _ uint32) bool {
64 return jpeg2k(raw, []byte{0x6a, 0x70, 0x6D, 0x20})
65 }
66
67 // Gif matches a Graphics Interchange Format file.
68 func Gif(raw []byte, _ uint32) bool {
69 return bytes.HasPrefix(raw, []byte("GIF87a")) ||
70 bytes.HasPrefix(raw, []byte("GIF89a"))
71 }
72
73 // Bmp matches a bitmap image file.
74 func Bmp(raw []byte, _ uint32) bool {
75 if len(raw) < 18 {
76 return false
77 }
78 if raw[0] != 'B' || raw[1] != 'M' {
79 return false
80 }
81
82 bmpFormat := binary.LittleEndian.Uint32(raw[14:])
83 // sourced from libmagic Magdir/images
84 possibleFormats := []uint32{
85 48, // PC bitmap, OS/2 2.x format (DIB header size=48)
86 24, // PC bitmap, OS/2 2.x format (DIB header size=24)
87 16, // PC bitmap, OS/2 2.x format (DIB header size=16)
88 64, // PC bitmap, OS/2 2.x format
89 52, // PC bitmap, Adobe Photoshop
90 56, // PC bitmap, Adobe Photoshop with alpha channel mask
91 40, // PC bitmap, Windows 3.x format
92 124, // PC bitmap, Windows 98/2000 and newer format
93 108, // PC bitmap, Windows 95/NT4 and newer format
94 }
95
96 return slices.Contains(possibleFormats, bmpFormat)
97 }
98
99 // Ps matches a PostScript file.
100 func Ps(raw []byte, _ uint32) bool {
101 return bytes.HasPrefix(raw, []byte("%!PS-Adobe-"))
102 }
103
104 // Psd matches a Photoshop Document file.
105 func Psd(raw []byte, _ uint32) bool {
106 return bytes.HasPrefix(raw, []byte("8BPS"))
107 }
108
109 // Ico matches an ICO file.
110 func Ico(raw []byte, _ uint32) bool {
111 return bytes.HasPrefix(raw, []byte{0x00, 0x00, 0x01, 0x00}) ||
112 bytes.HasPrefix(raw, []byte{0x00, 0x00, 0x02, 0x00})
113 }
114
115 // Icns matches an ICNS (Apple Icon Image format) file.
116 func Icns(raw []byte, _ uint32) bool {
117 return bytes.HasPrefix(raw, []byte("icns"))
118 }
119
120 // Tiff matches a Tagged Image File Format file.
121 func Tiff(raw []byte, _ uint32) bool {
122 return bytes.HasPrefix(raw, []byte{0x49, 0x49, 0x2A, 0x00}) ||
123 bytes.HasPrefix(raw, []byte{0x4D, 0x4D, 0x00, 0x2A})
124 }
125
126 // Bpg matches a Better Portable Graphics file.
127 func Bpg(raw []byte, _ uint32) bool {
128 return bytes.HasPrefix(raw, []byte{0x42, 0x50, 0x47, 0xFB})
129 }
130
131 // Xcf matches GIMP image data.
132 func Xcf(raw []byte, _ uint32) bool {
133 return bytes.HasPrefix(raw, []byte("gimp xcf"))
134 }
135
136 // Pat matches GIMP pattern data.
137 func Pat(raw []byte, _ uint32) bool {
138 return offset(raw, []byte("GPAT"), 20)
139 }
140
141 // Gbr matches GIMP brush data.
142 func Gbr(raw []byte, _ uint32) bool {
143 return offset(raw, []byte("GIMP"), 20)
144 }
145
146 // Hdr matches Radiance HDR image.
147 // https://web.archive.org/web/20060913152809/http://local.wasp.uwa.edu.au/~pbourke/dataformats/pic/
148 func Hdr(raw []byte, _ uint32) bool {
149 return bytes.HasPrefix(raw, []byte("#?RADIANCE\n"))
150 }
151
152 // Xpm matches X PixMap image data.
153 func Xpm(raw []byte, _ uint32) bool {
154 return bytes.HasPrefix(raw, []byte{0x2F, 0x2A, 0x20, 0x58, 0x50, 0x4D, 0x20, 0x2A, 0x2F})
155 }
156
157 // Jxs matches a JPEG XS coded image file (ISO/IEC 21122-3).
158 func Jxs(raw []byte, _ uint32) bool {
159 return bytes.HasPrefix(raw, []byte{0x00, 0x00, 0x00, 0x0C, 0x4A, 0x58, 0x53, 0x20, 0x0D, 0x0A, 0x87, 0x0A})
160 }
161
162 // Jxr matches Microsoft HD JXR photo file.
163 func Jxr(raw []byte, _ uint32) bool {
164 return bytes.HasPrefix(raw, []byte{0x49, 0x49, 0xBC, 0x01})
165 }
166
167 func jpeg2k(raw []byte, sig []byte) bool {
168 if len(raw) < 24 {
169 return false
170 }
171
172 if !bytes.Equal(raw[4:8], []byte{0x6A, 0x50, 0x20, 0x20}) &&
173 !bytes.Equal(raw[4:8], []byte{0x6A, 0x50, 0x32, 0x20}) {
174 return false
175 }
176 return bytes.Equal(raw[20:24], sig)
177 }
178
179 // Webp matches a WebP file.
180 func Webp(raw []byte, _ uint32) bool {
181 return len(raw) > 12 &&
182 bytes.Equal(raw[0:4], []byte("RIFF")) &&
183 bytes.Equal(raw[8:12], []byte{0x57, 0x45, 0x42, 0x50})
184 }
185
186 // Dwg matches a CAD drawing file.
187 func Dwg(raw []byte, _ uint32) bool {
188 if len(raw) < 6 || raw[0] != 0x41 || raw[1] != 0x43 {
189 return false
190 }
191 dwgVersions := [][]byte{
192 {0x31, 0x2E, 0x34, 0x30},
193 {0x31, 0x2E, 0x35, 0x30},
194 {0x32, 0x2E, 0x31, 0x30},
195 {0x31, 0x30, 0x30, 0x32},
196 {0x31, 0x30, 0x30, 0x33},
197 {0x31, 0x30, 0x30, 0x34},
198 {0x31, 0x30, 0x30, 0x36},
199 {0x31, 0x30, 0x30, 0x39},
200 {0x31, 0x30, 0x31, 0x32},
201 {0x31, 0x30, 0x31, 0x34},
202 {0x31, 0x30, 0x31, 0x35},
203 {0x31, 0x30, 0x31, 0x38},
204 {0x31, 0x30, 0x32, 0x31},
205 {0x31, 0x30, 0x32, 0x34},
206 {0x31, 0x30, 0x33, 0x32},
207 }
208
209 for _, d := range dwgVersions {
210 if bytes.Equal(raw[2:6], d) {
211 return true
212 }
213 }
214
215 return false
216 }
217
218 // Jxl matches JPEG XL image file.
219 func Jxl(raw []byte, _ uint32) bool {
220 return bytes.HasPrefix(raw, []byte{0xFF, 0x0A}) ||
221 bytes.HasPrefix(raw, []byte("\x00\x00\x00\x0cJXL\x20\x0d\x0a\x87\x0a"))
222 }
223
224 // DXF matches Drawing Exchange Format AutoCAD file.
225 // There does not seem to be a clear specification and the files in the wild
226 // differ wildly.
227 // https://images.autodesk.com/adsk/files/autocad_2012_pdf_dxf-reference_enu.pdf
228 //
229 // I collected these signatures by downloading a few dozen files from
230 // http://cd.textfiles.com/amigaenv/DXF/OBJEKTE/ and
231 // https://sembiance.com/fileFormatSamples/poly/dxf/ and then
232 // xxd -l 16 {} | sort | uniq.
233 // These signatures are only for the ASCII version of DXF. There is a binary version too.
234 func DXF(raw []byte, _ uint32) bool {
235 return bytes.HasPrefix(raw, []byte(" 0\x0ASECTION\x0A")) ||
236 bytes.HasPrefix(raw, []byte(" 0\x0D\x0ASECTION\x0D\x0A")) ||
237 bytes.HasPrefix(raw, []byte("0\x0ASECTION\x0A")) ||
238 bytes.HasPrefix(raw, []byte("0\x0D\x0ASECTION\x0D\x0A"))
239 }