Debian Codegraph @

internal/magic/zip.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5
6 "github.com/gabriel-vasile/mimetype/internal/scan"
7 )
8
9 // Odt matches an OpenDocument Text file.
10 func Odt(raw []byte, _ uint32) bool {
11 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.text"), 30)
12 }
13
14 // Ott matches an OpenDocument Text Template file.
15 func Ott(raw []byte, _ uint32) bool {
16 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.text-template"), 30)
17 }
18
19 // Ods matches an OpenDocument Spreadsheet file.
20 func Ods(raw []byte, _ uint32) bool {
21 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.spreadsheet"), 30)
22 }
23
24 // Ots matches an OpenDocument Spreadsheet Template file.
25 func Ots(raw []byte, _ uint32) bool {
26 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.spreadsheet-template"), 30)
27 }
28
29 // Odp matches an OpenDocument Presentation file.
30 func Odp(raw []byte, _ uint32) bool {
31 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.presentation"), 30)
32 }
33
34 // Otp matches an OpenDocument Presentation Template file.
35 func Otp(raw []byte, _ uint32) bool {
36 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.presentation-template"), 30)
37 }
38
39 // Odg matches an OpenDocument Drawing file.
40 func Odg(raw []byte, _ uint32) bool {
41 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.graphics"), 30)
42 }
43
44 // Otg matches an OpenDocument Drawing Template file.
45 func Otg(raw []byte, _ uint32) bool {
46 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.graphics-template"), 30)
47 }
48
49 // Odf matches an OpenDocument Formula file.
50 func Odf(raw []byte, _ uint32) bool {
51 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.formula"), 30)
52 }
53
54 // Odc matches an OpenDocument Chart file.
55 func Odc(raw []byte, _ uint32) bool {
56 return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.chart"), 30)
57 }
58
59 // Epub matches an EPUB file.
60 func Epub(raw []byte, _ uint32) bool {
61 return offset(raw, []byte("mimetypeapplication/epub+zip"), 30)
62 }
63
64 // Sxc matches an OpenOffice Spreadsheet file.
65 func Sxc(raw []byte, _ uint32) bool {
66 return offset(raw, []byte("mimetypeapplication/vnd.sun.xml.calc"), 30)
67 }
68
69 // Zip matches a zip archive.
70 func Zip(raw []byte, limit uint32) bool {
71 return len(raw) > 3 &&
72 raw[0] == 0x50 && raw[1] == 0x4B &&
73 (raw[2] == 0x3 || raw[2] == 0x5 || raw[2] == 0x7) &&
74 (raw[3] == 0x4 || raw[3] == 0x6 || raw[3] == 0x8)
75 }
76
77 // Jar matches a Java archive file. There are two types of Jar files:
78 // 1. the ones that can be opened with jexec and have 0xCAFE optional flag
79 // https://stackoverflow.com/tags/executable-jar/info
80 // 2. regular jars, same as above, just without the executable flag
81 // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=262278#c0
82 // There is an argument to only check for manifest, since it's the common nominator
83 // for both executable and non-executable versions. But the traversing zip entries
84 // is unreliable because it does linear search for signatures
85 // (instead of relying on offsets told by the file.)
86 func Jar(raw []byte, limit uint32) bool {
87 return executableJar(raw) ||
88 // First entry must be an empty META-INF directory or the manifest.
89 // There is no specification saying that, but the jar reader and writer
90 // implementations from Java do it that way.
91 // https://github.com/openjdk/jdk/blob/88c4678eed818cbe9380f35352e90883fed27d33/src/java.base/share/classes/java/util/jar/JarInputStream.java#L170-L173
92 zipHas(raw, zipEntries{{
93 name: []byte("META-INF/"),
94 }, {
95 name: []byte("META-INF/MANIFEST.MF"),
96 }}, 1)
97 }
98
99 // KMZ matches a zipped KML file, which is "doc.kml" by convention.
100 func KMZ(raw []byte, _ uint32) bool {
101 return zipHas(raw, zipEntries{{
102 name: []byte("doc.kml"),
103 }}, 100)
104 }
105
106 // An executable Jar has a 0xCAFE flag enabled in the first zip entry.
107 // The rule from file/file is:
108 // >(26.s+30) leshort 0xcafe Java archive data (JAR)
109 func executableJar(b scan.Bytes) bool {
110 b.Advance(0x1A)
111 offset, ok := b.Uint16()
112 if !ok {
113 return false
114 }
115 b.Advance(int(offset) + 2)
116
117 cafe, ok := b.Uint16()
118 return ok && cafe == 0xCAFE
119 }
120
121 // zipIterator iterates over a zip file returning the name of the zip entries
122 // in that file.
123 type zipIterator struct {
124 b scan.Bytes
125 }
126
127 type zipEntries []struct {
128 name []byte
129 dir bool // dir means checking just the prefix of the entry, not the whole path
130 }
131
132 func (z zipEntries) match(file []byte) bool {
133 for i := range z {
134 if z[i].dir {
135 if bytes.HasPrefix(file, z[i].name) {
136 return true
137 }
138 } else {
139 if bytes.Equal(file, z[i].name) {
140 return true
141 }
142 }
143 }
144 return false
145 }
146
147 func zipHas(raw scan.Bytes, searchFor zipEntries, stopAfter int) bool {
148 iter := zipIterator{raw}
149 for i := 0; i < stopAfter; i++ {
150 f := iter.next()
151 if len(f) == 0 {
152 break
153 }
154 if searchFor.match(f) {
155 return true
156 }
157 }
158
159 return false
160 }
161
162 // msoxml behaves like zipHas, but it puts restrictions on what the first zip
163 // entry can be.
164 func msoxml(raw scan.Bytes, searchFor zipEntries, stopAfter int) bool {
165 iter := zipIterator{raw}
166 for i := 0; i < stopAfter; i++ {
167 f := iter.next()
168 if len(f) == 0 {
169 break
170 }
171 if searchFor.match(f) {
172 return true
173 }
174 // If the first is not one of the next usually expected entries,
175 // then abort this check.
176 if i == 0 {
177 if !bytes.Equal(f, []byte("[Content_Types].xml")) && // this is a file
178 !bytes.HasPrefix(f, []byte("_rels/")) && // these are directories
179 !bytes.HasPrefix(f, []byte("docProps/")) &&
180 !bytes.HasPrefix(f, []byte("customXml/")) &&
181 !bytes.HasPrefix(f, []byte("[trash]/")) {
182 return false
183 }
184 }
185 }
186
187 return false
188 }
189
190 var zipLocalFileHeader = []byte("PK\003\004")
191
192 // next extracts the name of the next zip entry.
193 func (i *zipIterator) next() []byte {
194 n := bytes.Index(i.b, zipLocalFileHeader)
195 if n == -1 {
196 return nil
197 }
198 i.b.Advance(n)
199 if !i.b.Advance(0x1A) {
200 return nil
201 }
202 l, ok := i.b.Uint16()
203 if !ok {
204 return nil
205 }
206 if !i.b.Advance(0x02) {
207 return nil
208 }
209 if len(i.b) < int(l) {
210 return nil
211 }
212 return i.b[:l]
213 }
214
215 // skipZipflingerEntry tries to detect a Zipflinger virtual entry and skips it.
216 // The detection is based on the following properties:
217 // - compression method is 0
218 // - CRC32 is 0
219 // - compressed size is 0
220 // - uncompressed size is 0
221 // - file name is empty
222 // Returns true if it was found and skipped.
223 func (i *zipIterator) skipZipflingerEntry() (skipped bool) {
224 // Make a backup of the data so the inspection does not loses it.
225 b := i.b
226 defer func() {
227 // If no zipflinger was found, restore the original data.
228 if !skipped {
229 i.b = b
230 }
231 }()
232
233 n := bytes.Index(i.b, zipLocalFileHeader)
234 if n == -1 {
235 return false
236 }
237 if !i.b.Advance(0x08) {
238 return false
239 }
240
241 // Check compression method
242 if cm, ok := i.b.Uint16(); !ok || cm != 0 {
243 return false
244 }
245
246 // Advance up to the CRC32 field
247 if !i.b.Advance(0x04) {
248 return false
249 }
250
251 // Check CRC32
252 if crc32, ok := i.b.Uint32(); !ok || crc32 != 0 {
253 return false
254 }
255
256 // Check compressed size
257 if compressedSize, ok := i.b.Uint32(); !ok || compressedSize != 0 {
258 return false
259 }
260
261 // Check uncompressed size
262 if uncompressedSize, ok := i.b.Uint32(); !ok || uncompressedSize != 0 {
263 return false
264 }
265
266 // Check for empty file name
267 if l, ok := i.b.Uint16(); !ok || l != 0 {
268 return false
269 }
270
271 // Reached a zipflinger virtual entry: skip extra data
272 l, ok := i.b.Uint16()
273 if !ok {
274 return false
275 }
276
277 if !i.b.Advance(int(l)) {
278 return false
279 }
280 return true
281 }
282
283 // APK matches an Android Package Archive.
284 // The source of signatures is https://github.com/file/file/blob/1778642b8ba3d947a779a36fcd81f8e807220a19/magic/Magdir/archive#L1820-L1887
285 func APK(raw []byte, _ uint32) bool {
286 iter := zipIterator{raw}
287
288 // If a Zipflinger Virtual Entry is detected, then the data is considered APK
289 if iter.skipZipflingerEntry() {
290 return true
291 }
292
293 return zipHas(iter.b, zipEntries{{
294 name: []byte("AndroidManifest.xml"),
295 }, {
296 name: []byte("META-INF/com/android/build/gradle/app-metadata.properties"),
297 }, {
298 name: []byte("classes.dex"),
299 }, {
300 name: []byte("resources.arsc"),
301 }, {
302 name: []byte("res/drawable"),
303 }}, 100)
304 }