| 1 |
package magic |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
|
| 6 |
"github.com/gabriel-vasile/mimetype/internal/scan" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
func Odt(raw []byte, _ uint32) bool { |
| 11 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.text"), 30) |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
func Ott(raw []byte, _ uint32) bool { |
| 16 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.text-template"), 30) |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func Ods(raw []byte, _ uint32) bool { |
| 21 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.spreadsheet"), 30) |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func Ots(raw []byte, _ uint32) bool { |
| 26 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.spreadsheet-template"), 30) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func Odp(raw []byte, _ uint32) bool { |
| 31 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.presentation"), 30) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func Otp(raw []byte, _ uint32) bool { |
| 36 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.presentation-template"), 30) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func Odg(raw []byte, _ uint32) bool { |
| 41 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.graphics"), 30) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func Otg(raw []byte, _ uint32) bool { |
| 46 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.graphics-template"), 30) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func Odf(raw []byte, _ uint32) bool { |
| 51 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.formula"), 30) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
func Odc(raw []byte, _ uint32) bool { |
| 56 |
return offset(raw, []byte("mimetypeapplication/vnd.oasis.opendocument.chart"), 30) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func Epub(raw []byte, _ uint32) bool { |
| 61 |
return offset(raw, []byte("mimetypeapplication/epub+zip"), 30) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func Sxc(raw []byte, _ uint32) bool { |
| 66 |
return offset(raw, []byte("mimetypeapplication/vnd.sun.xml.calc"), 30) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
func Jar(raw []byte, limit uint32) bool { |
| 87 |
return executableJar(raw) || |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
zipHas(raw, zipEntries{{ |
| 93 |
name: []byte("META-INF/"), |
| 94 |
}, { |
| 95 |
name: []byte("META-INF/MANIFEST.MF"), |
| 96 |
}}, 1) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
func KMZ(raw []byte, _ uint32) bool { |
| 101 |
return zipHas(raw, zipEntries{{ |
| 102 |
name: []byte("doc.kml"), |
| 103 |
}}, 100) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 122 |
|
| 123 |
type zipIterator struct { |
| 124 |
b scan.Bytes |
| 125 |
} |
| 126 |
|
| 127 |
type zipEntries []struct { |
| 128 |
name []byte |
| 129 |
dir bool |
| 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 |
|
| 163 |
|
| 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 |
|
| 175 |
|
| 176 |
if i == 0 { |
| 177 |
if !bytes.Equal(f, []byte("[Content_Types].xml")) && |
| 178 |
!bytes.HasPrefix(f, []byte("_rels/")) && |
| 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 |
|
| 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 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
func (i *zipIterator) skipZipflingerEntry() (skipped bool) { |
| 224 |
|
| 225 |
b := i.b |
| 226 |
defer func() { |
| 227 |
|
| 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 |
|
| 242 |
if cm, ok := i.b.Uint16(); !ok || cm != 0 { |
| 243 |
return false |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if !i.b.Advance(0x04) { |
| 248 |
return false |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
if crc32, ok := i.b.Uint32(); !ok || crc32 != 0 { |
| 253 |
return false |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
if compressedSize, ok := i.b.Uint32(); !ok || compressedSize != 0 { |
| 258 |
return false |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
if uncompressedSize, ok := i.b.Uint32(); !ok || uncompressedSize != 0 { |
| 263 |
return false |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
if l, ok := i.b.Uint16(); !ok || l != 0 { |
| 268 |
return false |
| 269 |
} |
| 270 |
|
| 271 |
|
| 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 |
|
| 284 |
|
| 285 |
func APK(raw []byte, _ uint32) bool { |
| 286 |
iter := zipIterator{raw} |
| 287 |
|
| 288 |
|
| 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 |
} |