Debian Codegraph @

internal/magic/audio.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6
7 "github.com/gabriel-vasile/mimetype/internal/mp3"
8 )
9
10 // Flac matches a Free Lossless Audio Codec file.
11 func Flac(raw []byte, _ uint32) bool {
12 return bytes.HasPrefix(raw, []byte("\x66\x4C\x61\x43\x00\x00\x00\x22"))
13 }
14
15 // Midi matches a Musical Instrument Digital Interface file.
16 func Midi(raw []byte, _ uint32) bool {
17 return bytes.HasPrefix(raw, []byte("\x4D\x54\x68\x64"))
18 }
19
20 // Ape matches a Monkey's Audio file.
21 func Ape(raw []byte, _ uint32) bool {
22 return bytes.HasPrefix(raw, []byte("\x4D\x41\x43\x20\x96\x0F\x00\x00\x34\x00\x00\x00\x18\x00\x00\x00\x90\xE3"))
23 }
24
25 // MusePack matches a Musepack file.
26 func MusePack(raw []byte, _ uint32) bool {
27 return bytes.HasPrefix(raw, []byte("MPCK"))
28 }
29
30 // Au matches a Sun Microsystems au file.
31 func Au(raw []byte, _ uint32) bool {
32 return bytes.HasPrefix(raw, []byte("\x2E\x73\x6E\x64"))
33 }
34
35 // Amr matches an Adaptive Multi-Rate file.
36 func Amr(raw []byte, _ uint32) bool {
37 return bytes.HasPrefix(raw, []byte("\x23\x21\x41\x4D\x52"))
38 }
39
40 // Voc matches a Creative Voice file.
41 func Voc(raw []byte, _ uint32) bool {
42 return bytes.HasPrefix(raw, []byte("Creative Voice File"))
43 }
44
45 // M3U matches a Playlist file.
46 func M3U(raw []byte, _ uint32) bool {
47 return bytes.HasPrefix(raw, []byte("#EXTM3U\n")) ||
48 bytes.HasPrefix(raw, []byte("#EXTM3U\r\n"))
49 }
50
51 // AAC matches an Advanced Audio Coding file.
52 func AAC(raw []byte, _ uint32) bool {
53 return len(raw) > 1 && ((raw[0] == 0xFF && raw[1] == 0xF1) || (raw[0] == 0xFF && raw[1] == 0xF9))
54 }
55
56 // MP3 matches a .mp3 file.
57 func MP3(raw []byte, limit uint32) bool {
58 if len(raw) < 3 {
59 return false
60 }
61
62 // Any ID3v2 is reported as MP3. Not entirely correct, but the mimesniff
63 // standard says so. https://mimesniff.spec.whatwg.org/#matching-an-audio-or-video-type-pattern
64 // Despite the standard only checking for "ID3", we do more validations to
65 // avoid false positives.
66 if id3v2(raw) {
67 return true
68 }
69
70 // If no ID3v2 tag found, then we will look for MP3 frames, but:
71 // a. Layer III files are a lot more prevalent than Layer I and II.
72 // b. Layer I frame header has looser constraints than the others: many files
73 // with regularly repeating 0xFFFF bytes can be misidentified as MP3.
74 // c. MP3 files are composed of individual frames and those frames can have
75 // leading garbage bytes: if we want to find all valid MP3s, we have to do a
76 // linear search. #775, #310
77 // d. There are file formats that contain MP3s inside: .mo3 and .swa
78 //
79 // Given a, b, c and d, this code:
80 // - initially tries to match by first two bytes in header
81 // - checks for .mo3 and .swa and disqualifies them
82 // - does linear search for Layer III
83 switch binary.BigEndian.Uint16(raw[:2]) & 0xFFFE {
84 case 0xFFFA, 0xFFF2, 0xFFE2, // layer III: v1, v2, v2.5
85 0xFFFC, 0xFFF4, // layer II: v1, v2
86 0xFFF5: // layer I: v2
87 return true
88 }
89 // http://lclevy.free.fr/mo3/
90 if bytes.HasPrefix(raw, []byte("MO3")) {
91 return false
92 }
93
94 // From PRONOM:
95 // Macromedia licensed the MP3 technology in 1995 to use in their Shockwave
96 // product. .swa or Shockwave Audio was originally added as a free plugin
97 // (Xtras) to SoundEdit 16 to export AIFF files to .swa.
98 // There is no media type assigned for .swa.
99 if bytes.HasPrefix(raw, []byte{0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00}) {
100 return false
101 }
102
103 _, size := mp3.ExtractFrame(raw)
104 return size > 0
105 }
106
107 // Based on https://id3.org/Developer%20Information.
108 func id3v2(raw []byte) bool {
109 if len(raw) < 10 || !bytes.HasPrefix(raw, []byte("ID3")) {
110 return false
111 }
112 if raw[3] < 2 || raw[3] > 4 { // Version: ID3v2.2 - ID3v2.4.
113 return false
114 }
115 if raw[4] != 0 { // Revision is 0 for all versions.
116 return false
117 }
118
119 // v2.2 uses 2 bits, v2.3 uses 3 bits and v2.4 uses 4.
120 // For all versions least significant 4 bits should be 0
121 if raw[5]&0b1111 != 0 {
122 return false
123 }
124
125 // Size bytes are synchsafe: most significant bit always 0.
126 if raw[6]&0x80 != 0 || raw[7]&0x80 != 0 || raw[8]&0x80 != 0 || raw[9]&0x80 != 0 {
127 return false
128 }
129
130 size := uint32(raw[6])<<21 | uint32(raw[7])<<14 | uint32(raw[8])<<7 | uint32(raw[9])
131 // Disallow too big frames, let's say 10MB.
132 return size > 0 && size < 10*1024*1024
133 }
134
135 // Wav matches a Waveform Audio File Format file.
136 func Wav(raw []byte, limit uint32) bool {
137 return len(raw) > 12 &&
138 bytes.Equal(raw[:4], []byte("RIFF")) &&
139 bytes.Equal(raw[8:12], []byte{0x57, 0x41, 0x56, 0x45})
140 }
141
142 // Aiff matches Audio Interchange File Format file.
143 func Aiff(raw []byte, limit uint32) bool {
144 return len(raw) > 12 &&
145 bytes.Equal(raw[:4], []byte{0x46, 0x4F, 0x52, 0x4D}) &&
146 bytes.Equal(raw[8:12], []byte{0x41, 0x49, 0x46, 0x46})
147 }
148
149 // Qcp matches a Qualcomm Pure Voice file.
150 func Qcp(raw []byte, limit uint32) bool {
151 return len(raw) > 12 &&
152 bytes.Equal(raw[:4], []byte("RIFF")) &&
153 bytes.Equal(raw[8:12], []byte("QLCM"))
154 }