Debian Codegraph @

internal/magic/audio.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6 )
7
8 // Flac matches a Free Lossless Audio Codec file.
9 func Flac(raw []byte, _ uint32) bool {
10 return bytes.HasPrefix(raw, []byte("\x66\x4C\x61\x43\x00\x00\x00\x22"))
11 }
12
13 // Midi matches a Musical Instrument Digital Interface file.
14 func Midi(raw []byte, _ uint32) bool {
15 return bytes.HasPrefix(raw, []byte("\x4D\x54\x68\x64"))
16 }
17
18 // Ape matches a Monkey's Audio file.
19 func Ape(raw []byte, _ uint32) bool {
20 return bytes.HasPrefix(raw, []byte("\x4D\x41\x43\x20\x96\x0F\x00\x00\x34\x00\x00\x00\x18\x00\x00\x00\x90\xE3"))
21 }
22
23 // MusePack matches a Musepack file.
24 func MusePack(raw []byte, _ uint32) bool {
25 return bytes.HasPrefix(raw, []byte("MPCK"))
26 }
27
28 // Au matches a Sun Microsystems au file.
29 func Au(raw []byte, _ uint32) bool {
30 return bytes.HasPrefix(raw, []byte("\x2E\x73\x6E\x64"))
31 }
32
33 // Amr matches an Adaptive Multi-Rate file.
34 func Amr(raw []byte, _ uint32) bool {
35 return bytes.HasPrefix(raw, []byte("\x23\x21\x41\x4D\x52"))
36 }
37
38 // Voc matches a Creative Voice file.
39 func Voc(raw []byte, _ uint32) bool {
40 return bytes.HasPrefix(raw, []byte("Creative Voice File"))
41 }
42
43 // M3U matches a Playlist file.
44 func M3U(raw []byte, _ uint32) bool {
45 return bytes.HasPrefix(raw, []byte("#EXTM3U\n")) ||
46 bytes.HasPrefix(raw, []byte("#EXTM3U\r\n"))
47 }
48
49 // AAC matches an Advanced Audio Coding file.
50 func AAC(raw []byte, _ uint32) bool {
51 return len(raw) > 1 && ((raw[0] == 0xFF && raw[1] == 0xF1) || (raw[0] == 0xFF && raw[1] == 0xF9))
52 }
53
54 // Mp3 matches an mp3 file.
55 func Mp3(raw []byte, limit uint32) bool {
56 if len(raw) < 3 {
57 return false
58 }
59
60 if bytes.HasPrefix(raw, []byte("ID3")) {
61 // MP3s with an ID3v2 tag will start with "ID3"
62 // ID3v1 tags, however appear at the end of the file.
63 return true
64 }
65
66 // Match MP3 files without tags
67 switch binary.BigEndian.Uint16(raw[:2]) & 0xFFFE {
68 case 0xFFFA:
69 // MPEG ADTS, layer III, v1
70 return true
71 case 0xFFF2:
72 // MPEG ADTS, layer III, v2
73 return true
74 case 0xFFE2:
75 // MPEG ADTS, layer III, v2.5
76 return true
77 }
78
79 return false
80 }
81
82 // Wav matches a Waveform Audio File Format file.
83 func Wav(raw []byte, limit uint32) bool {
84 return len(raw) > 12 &&
85 bytes.Equal(raw[:4], []byte("RIFF")) &&
86 bytes.Equal(raw[8:12], []byte{0x57, 0x41, 0x56, 0x45})
87 }
88
89 // Aiff matches Audio Interchange File Format file.
90 func Aiff(raw []byte, limit uint32) bool {
91 return len(raw) > 12 &&
92 bytes.Equal(raw[:4], []byte{0x46, 0x4F, 0x52, 0x4D}) &&
93 bytes.Equal(raw[8:12], []byte{0x41, 0x49, 0x46, 0x46})
94 }
95
96 // Qcp matches a Qualcomm Pure Voice file.
97 func Qcp(raw []byte, limit uint32) bool {
98 return len(raw) > 12 &&
99 bytes.Equal(raw[:4], []byte("RIFF")) &&
100 bytes.Equal(raw[8:12], []byte("QLCM"))
101 }