Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid detecting other files as SVG #594

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion internal/magic/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package magic

import (
"bytes"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -295,9 +296,23 @@ func HAR(raw []byte, limit uint32) bool {
return false
}

var (
xmlPrefix = []byte("<?xml version=")
xmlVersionRegex = regexp.MustCompile(`['"\ \t]*[0-9.]+['"\ \t]*`)
svgDocumentPrefix = []byte("<!DOCTYPE svg PUBLIC")
svgPrefix = []byte("<svg")
)

// Svg matches a SVG file.
func Svg(raw []byte, limit uint32) bool {
return bytes.Contains(raw, []byte("<svg"))
if bytes.HasPrefix(raw, xmlPrefix) {
mlen := min(len(raw), 10+len(xmlPrefix))
return xmlVersionRegex.Match(raw[len(xmlPrefix):mlen]) && bytes.Contains(raw, []byte("<svg"))
}
if bytes.HasPrefix(raw, svgDocumentPrefix) {
return true
}
return bytes.HasPrefix(raw, svgPrefix)
}

// Srt matches a SubRip file.
Expand Down