Debian Codegraph @

internal/magic/image.go

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