Skip to content

Commit

Permalink
improve webm/matroska detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Kycklingar committed Sep 9, 2021
1 parent a040a9f commit 90b7576
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions internal/magic/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,36 @@ func isMatroskaFileTypeMatched(in []byte, flType string) bool {
// isFileTypeNamePresent accepts the matroska input data stream and searches
// for the given file type in the stream. Return whether a match is found.
// The logic of search is: find first instance of \x42\x82 and then
// search for given string after one byte of above instance.
// search for given string after n bytes of above instance.
func isFileTypeNamePresent(in []byte, flType string) bool {
ind, maxInd, lenIn := 0, 4096, len(in)
if lenIn < maxInd { // restricting length to 4096
maxInd = lenIn
}
ind = bytes.Index(in[:maxInd], []byte("\x42\x82"))
if ind > 0 && lenIn > ind+3 {
if ind > 0 && lenIn > ind+2 {
ind += 2

// filetype name will be present exactly
// one byte after the match of the two bytes "\x42\x82"
return bytes.HasPrefix(in[ind+3:], []byte(flType))
// n bytes after the match of the two bytes "\x42\x82"
n := vint(int(in[ind]))
if lenIn > ind+n {
return bytes.HasPrefix(in[ind+n:], []byte(flType))
}
}
return false
}

// vint parses the offset of the media signature in matroska containers
func vint(v int) int {
mask, max, num := 128, 8, 1
for num < max && v&mask == 0 {
mask = mask >> 1
num++
}
return num
}

// Mpeg matches a Moving Picture Experts Group file.
func Mpeg(raw []byte, limit uint32) bool {
return len(raw) > 3 && bytes.HasPrefix(raw, []byte{0x00, 0x00, 0x01}) &&
Expand Down

0 comments on commit 90b7576

Please sign in to comment.