Skip to content

Commit cd85f0a

Browse files
Improve mov detection (#304)
* Improve mov detection; closes #302 Include other atom types, besides ftyp, inside mov detection.
1 parent 80fe766 commit cd85f0a

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

internal/magic/ftyp.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package magic
22

3+
import "bytes"
4+
35
var (
46
// AVIF matches an AV1 Image File Format still or animated.
57
// Wikipedia page seems outdated listing image/avif-sequence for animations.
@@ -37,8 +39,6 @@ var (
3739
// Nero Digital AAC Audio
3840
[]byte("NDAS"),
3941
)
40-
// QuickTime matches a QuickTime File Format file.
41-
QuickTime = ftyp([]byte("qt "), []byte("moov"))
4242
// Mqv matches a Sony / Mobile QuickTime file.
4343
Mqv = ftyp([]byte("mqt "))
4444
// M4a matches an audio M4A file.
@@ -55,3 +55,34 @@ var (
5555
HeifSequence = ftyp([]byte("msf1"), []byte("hevm"), []byte("hevs"), []byte("avcs"))
5656
// TODO: add support for remaining video formats at ftyps.com.
5757
)
58+
59+
// QuickTime matches a QuickTime File Format file.
60+
// https://www.loc.gov/preservation/digital/formats/fdd/fdd000052.shtml
61+
// https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap1/qtff1.html#//apple_ref/doc/uid/TP40000939-CH203-38190
62+
// https://github.com/apache/tika/blob/0f5570691133c75ac4472c3340354a6c4080b104/tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml#L7758-L7777
63+
func QuickTime(raw []byte, _ uint32) bool {
64+
if len(raw) < 12 {
65+
return false
66+
}
67+
// First 4 bytes represent the size of the atom as unsigned int.
68+
// Next 4 bytes are the type of the atom.
69+
// For `ftyp` atoms check if first byte in size is 0, otherwise, a text file
70+
// which happens to contain 'ftypqt ' at index 4 will trigger a false positive.
71+
if bytes.Equal(raw[4:12], []byte("ftypqt ")) ||
72+
bytes.Equal(raw[4:12], []byte("ftypmoov")) {
73+
return raw[0] == 0x00
74+
}
75+
basicAtomTypes := [][]byte{
76+
[]byte("moov\x00"),
77+
[]byte("mdat\x00"),
78+
[]byte("free\x00"),
79+
[]byte("skip\x00"),
80+
[]byte("pnot\x00"),
81+
}
82+
for _, a := range basicAtomTypes {
83+
if bytes.Equal(raw[4:9], a) {
84+
return true
85+
}
86+
}
87+
return bytes.Equal(raw[:8], []byte("\x00\x00\x00\x08wide"))
88+
}

0 commit comments

Comments
 (0)