Debian Codegraph @

internal/magic/video.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 )
6
7 // Flv matches a Flash video file.
8 func Flv(raw []byte, _ uint32) bool {
9 return bytes.HasPrefix(raw, []byte("\x46\x4C\x56\x01"))
10 }
11
12 // Asf matches an Advanced Systems Format file.
13 func Asf(raw []byte, _ uint32) bool {
14 return bytes.HasPrefix(raw, []byte{
15 0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11,
16 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C,
17 })
18 }
19
20 // Rmvb matches a RealMedia Variable Bitrate file.
21 func Rmvb(raw []byte, _ uint32) bool {
22 return bytes.HasPrefix(raw, []byte{0x2E, 0x52, 0x4D, 0x46})
23 }
24
25 // WebM matches a WebM file.
26 func WebM(raw []byte, limit uint32) bool {
27 return isMatroskaFileTypeMatched(raw, "webm")
28 }
29
30 // Mkv matches a mkv file.
31 func Mkv(raw []byte, limit uint32) bool {
32 return isMatroskaFileTypeMatched(raw, "matroska")
33 }
34
35 // isMatroskaFileTypeMatched is used for webm and mkv file matching.
36 // It checks for .Eߣ sequence. If the sequence is found,
37 // then it means it is Matroska media container, including WebM.
38 // Then it verifies which of the file type it is representing by matching the
39 // file specific string.
40 func isMatroskaFileTypeMatched(in []byte, flType string) bool {
41 if bytes.HasPrefix(in, []byte("\x1A\x45\xDF\xA3")) {
42 return isFileTypeNamePresent(in, flType)
43 }
44 return false
45 }
46
47 // isFileTypeNamePresent accepts the matroska input data stream and searches
48 // for the given file type in the stream. Return whether a match is found.
49 // The logic of search is: find first instance of \x42\x82 and then
50 // search for given string after n bytes of above instance.
51 func isFileTypeNamePresent(in []byte, flType string) bool {
52 ind, maxInd, lenIn := 0, 4096, len(in)
53 if lenIn < maxInd { // restricting length to 4096
54 maxInd = lenIn
55 }
56 ind = bytes.Index(in[:maxInd], []byte("\x42\x82"))
57 if ind > 0 && lenIn > ind+2 {
58 ind += 2
59
60 // filetype name will be present exactly
61 // n bytes after the match of the two bytes "\x42\x82"
62 n := vintWidth(int(in[ind]))
63 if lenIn > ind+n {
64 return bytes.HasPrefix(in[ind+n:], []byte(flType))
65 }
66 }
67 return false
68 }
69
70 // vintWidth parses the variable-integer width in matroska containers
71 func vintWidth(v int) int {
72 mask, nTimes, num := 128, 8, 1
73 for num < nTimes && v&mask == 0 {
74 mask >>= 1
75 num++
76 }
77 return num
78 }
79
80 // Mpeg matches a Moving Picture Experts Group file.
81 func Mpeg(raw []byte, limit uint32) bool {
82 return len(raw) > 3 && bytes.HasPrefix(raw, []byte{0x00, 0x00, 0x01}) &&
83 raw[3] >= 0xB0 && raw[3] <= 0xBF
84 }
85
86 // Avi matches an Audio Video Interleaved file.
87 func Avi(raw []byte, limit uint32) bool {
88 return len(raw) > 16 &&
89 bytes.Equal(raw[:4], []byte("RIFF")) &&
90 bytes.Equal(raw[8:16], []byte("AVI LIST"))
91 }