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 // next extracts the name of the next zip entry.
191 func (i *zipIterator) next() []byte {
192 pk := []byte("PK\003\004")
193
194 n := bytes.Index(i.b, pk)
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 // APK matches an Android Package Archive.
216 // The source of signatures is https://github.com/file/file/blob/1778642b8ba3d947a779a36fcd81f8e807220a19/magic/Magdir/archive#L1820-L1887
217 func APK(raw []byte, _ uint32) bool {
218 return zipHas(raw, zipEntries{{
219 name: []byte("AndroidManifest.xml"),
220 }, {
221 name: []byte("META-INF/com/android/build/gradle/app-metadata.properties"),
222 }, {
223 name: []byte("classes.dex"),
224 }, {
225 name: []byte("resources.arsc"),
226 }, {
227 name: []byte("res/drawable"),
228 }}, 100)
229 }