Debian Codegraph @

internal/magic/ftyp.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 )
6
7 // AVIF matches an AV1 Image File Format still or animated.
8 // Wikipedia page seems outdated listing image/avif-sequence for animations.
9 // https://github.com/AOMediaCodec/av1-avif/issues/59
10 func AVIF(raw []byte, _ uint32) bool {
11 return ftyp(raw, []byte("avif"), []byte("avis"))
12 }
13
14 // ThreeGP matches a 3GPP file.
15 func ThreeGP(raw []byte, _ uint32) bool {
16 return ftyp(raw,
17 []byte("3gp1"), []byte("3gp2"), []byte("3gp3"), []byte("3gp4"),
18 []byte("3gp5"), []byte("3gp6"), []byte("3gp7"), []byte("3gs7"),
19 []byte("3ge6"), []byte("3ge7"), []byte("3gg6"),
20 )
21 }
22
23 // ThreeG2 matches a 3GPP2 file.
24 func ThreeG2(raw []byte, _ uint32) bool {
25 return ftyp(raw,
26 []byte("3g24"), []byte("3g25"), []byte("3g26"), []byte("3g2a"),
27 []byte("3g2b"), []byte("3g2c"), []byte("KDDI"),
28 )
29 }
30
31 // AMp4 matches an audio MP4 file.
32 func AMp4(raw []byte, _ uint32) bool {
33 return ftyp(raw,
34 // audio for Adobe Flash Player 9+
35 []byte("F4A "), []byte("F4B "),
36 // Apple iTunes AAC-LC (.M4A) Audio
37 []byte("M4B "), []byte("M4P "),
38 // MPEG-4 (.MP4) for SonyPSP
39 []byte("MSNV"),
40 // Nero Digital AAC Audio
41 []byte("NDAS"),
42 )
43 }
44
45 // Mqv matches a Sony / Mobile QuickTime file.
46 func Mqv(raw []byte, _ uint32) bool {
47 return ftyp(raw, []byte("mqt "))
48 }
49
50 // M4a matches an audio M4A file.
51 func M4a(raw []byte, _ uint32) bool {
52 return ftyp(raw, []byte("M4A "))
53 }
54
55 // M4v matches an Appl4 M4V video file.
56 func M4v(raw []byte, _ uint32) bool {
57 return ftyp(raw, []byte("M4V "), []byte("M4VH"), []byte("M4VP"))
58 }
59
60 // Heic matches a High Efficiency Image Coding (HEIC) file.
61 func Heic(raw []byte, _ uint32) bool {
62 return ftyp(raw, []byte("heic"), []byte("heix"))
63 }
64
65 // HeicSequence matches a High Efficiency Image Coding (HEIC) file sequence.
66 func HeicSequence(raw []byte, _ uint32) bool {
67 return ftyp(raw, []byte("hevc"), []byte("hevx"))
68 }
69
70 // Heif matches a High Efficiency Image File Format (HEIF) file.
71 func Heif(raw []byte, _ uint32) bool {
72 return ftyp(raw, []byte("mif1"), []byte("heim"), []byte("heis"), []byte("avic"))
73 }
74
75 // HeifSequence matches a High Efficiency Image File Format (HEIF) file sequence.
76 func HeifSequence(raw []byte, _ uint32) bool {
77 return ftyp(raw, []byte("msf1"), []byte("hevm"), []byte("hevs"), []byte("avcs"))
78 }
79
80 // Mj2 matches a Motion JPEG 2000 file: https://en.wikipedia.org/wiki/Motion_JPEG_2000.
81 func Mj2(raw []byte, _ uint32) bool {
82 return ftyp(raw, []byte("mj2s"), []byte("mjp2"), []byte("MFSM"), []byte("MGSV"))
83 }
84
85 // Dvb matches a Digital Video Broadcasting file: https://dvb.org.
86 // https://cconcolato.github.io/mp4ra/filetype.html
87 // https://github.com/file/file/blob/512840337ead1076519332d24fefcaa8fac36e06/magic/Magdir/animation#L135-L154
88 func Dvb(raw []byte, _ uint32) bool {
89 return ftyp(raw,
90 []byte("dby1"), []byte("dsms"), []byte("dts1"), []byte("dts2"),
91 []byte("dts3"), []byte("dxo "), []byte("dmb1"), []byte("dmpf"),
92 []byte("drc1"), []byte("dv1a"), []byte("dv1b"), []byte("dv2a"),
93 []byte("dv2b"), []byte("dv3a"), []byte("dv3b"), []byte("dvr1"),
94 []byte("dvt1"), []byte("emsg"))
95 }
96
97 // TODO: add support for remaining video formats at ftyps.com.
98
99 // QuickTime matches a QuickTime File Format file.
100 // https://www.loc.gov/preservation/digital/formats/fdd/fdd000052.shtml
101 // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap1/qtff1.html#//apple_ref/doc/uid/TP40000939-CH203-38190
102 // https://github.com/apache/tika/blob/0f5570691133c75ac4472c3340354a6c4080b104/tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml#L7758-L7777
103 func QuickTime(raw []byte, _ uint32) bool {
104 if len(raw) < 12 {
105 return false
106 }
107 // First 4 bytes represent the size of the atom as unsigned int.
108 // Next 4 bytes are the type of the atom.
109 // For `ftyp` atoms check if first byte in size is 0, otherwise, a text file
110 // which happens to contain 'ftypqt ' at index 4 will trigger a false positive.
111 if bytes.Equal(raw[4:12], []byte("ftypqt ")) ||
112 bytes.Equal(raw[4:12], []byte("ftypmoov")) {
113 return raw[0] == 0x00
114 }
115 basicAtomTypes := [][]byte{
116 []byte("moov\x00"),
117 []byte("mdat\x00"),
118 []byte("free\x00"),
119 []byte("skip\x00"),
120 []byte("pnot\x00"),
121 }
122 for _, a := range basicAtomTypes {
123 if bytes.Equal(raw[4:9], a) {
124 return true
125 }
126 }
127 return bytes.Equal(raw[:8], []byte("\x00\x00\x00\x08wide"))
128 }
129
130 // Mp4 detects an .mp4 file. Mp4 detections only does a basic ftyp check.
131 // Mp4 has many registered and unregistered code points so it's hard to keep track
132 // of all. Detection will default on video/mp4 for all ftyp files.
133 // ISO_IEC_14496-12 is the specification for the iso container.
134 func Mp4(raw []byte, _ uint32) bool {
135 if len(raw) < 12 {
136 return false
137 }
138 // ftyps are made out of boxes. The first 4 bytes of the box represent
139 // its size in big-endian uint32. First box is the ftyp box and it is small
140 // in size. Check most significant byte is 0 to filter out false positive
141 // text files that happen to contain the string "ftyp" at index 4.
142 if raw[0] != 0 {
143 return false
144 }
145 return bytes.Equal(raw[4:8], []byte("ftyp"))
146 }