package magic import ( "bytes" "time" "github.com/gabriel-vasile/mimetype/internal/charset" "github.com/gabriel-vasile/mimetype/internal/json" mkup "github.com/gabriel-vasile/mimetype/internal/markup" "github.com/gabriel-vasile/mimetype/internal/scan" ) // HTML matches a Hypertext Markup Language file. func HTML(raw []byte, _ uint32) bool { return markup(raw, []byte(" 0 && firstNonWS == '[') hasTargetTok = hasTargetTok || (t&json.TokObject > 0 && firstNonWS == '{') } if !hasTargetTok { return false } lraw := len(raw) parsed, inspected, _, querySatisfied := json.Parse(q, raw) if !querySatisfied { return false } // If the full file content was provided, check that the whole input was parsed. if limit == 0 || lraw < int(limit) { return parsed == lraw } // If a section of the file was provided, check if all of it was inspected. // In other words, check that if there was a problem parsing, that problem // occurred after the last byte in the input. return inspected == lraw && lraw > 0 } // NdJSON matches a Newline delimited JSON file. All complete lines from raw // must be valid JSON documents meaning they contain one of the valid JSON data // types. func NdJSON(raw []byte, limit uint32) bool { lCount, objOrArr := 0, 0 s := scan.Bytes(raw) var l scan.Bytes for len(s) != 0 { l = s.Line() _, inspected, firstToken, _ := json.Parse(json.QueryNone, l) if len(l) != inspected { return false } if firstToken == json.TokArray || firstToken == json.TokObject { objOrArr++ } lCount++ } return lCount > 1 && objOrArr > 0 } // Svg matches a SVG file. func Svg(raw []byte, limit uint32) bool { return svgWithoutXMLDeclaration(raw) || svgWithXMLDeclaration(raw) } // svgWithoutXMLDeclaration matches a SVG image that does not have an XML header. // Example: // // // // // func svgWithoutXMLDeclaration(s scan.Bytes) bool { for scan.ByteIsWS(s.Peek()) { s.Advance(1) } for mkup.SkipAComment(&s) { } if !bytes.HasPrefix(s, []byte(" // // // func svgWithXMLDeclaration(s scan.Bytes) bool { for scan.ByteIsWS(s.Peek()) { s.Advance(1) } if !bytes.HasPrefix(s, []byte(" 4096 { s = s[:4096] } return hasVersion && bytes.Contains(s, []byte(" 00:02:19,376) limits second line // length to exactly 29 characters. if len(line) != 29 { return false } // Decimal separator of fractional seconds in the timestamps must be a // comma, not a period. if bytes.IndexByte(line, '.') != -1 { return false } sep := []byte(" --> ") i := bytes.Index(line, sep) if i == -1 { return false } const layout = "15:04:05,000" t0, err := time.Parse(layout, string(line[:i])) if err != nil { return false } t1, err := time.Parse(layout, string(line[i+len(sep):])) if err != nil { return false } if t0.After(t1) { return false } line = s.Line() // A third line must exist and not be empty. This is the actual subtitle text. return len(line) != 0 } // Vtt matches a Web Video Text Tracks (WebVTT) file. See // https://www.iana.org/assignments/media-types/text/vtt. func Vtt(raw []byte, limit uint32) bool { // Prefix match. prefixes := [][]byte{ {0xEF, 0xBB, 0xBF, 0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x0A}, // UTF-8 BOM, "WEBVTT" and a line feed {0xEF, 0xBB, 0xBF, 0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x0D}, // UTF-8 BOM, "WEBVTT" and a carriage return {0xEF, 0xBB, 0xBF, 0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x20}, // UTF-8 BOM, "WEBVTT" and a space {0xEF, 0xBB, 0xBF, 0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x09}, // UTF-8 BOM, "WEBVTT" and a horizontal tab {0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x0A}, // "WEBVTT" and a line feed {0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x0D}, // "WEBVTT" and a carriage return {0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x20}, // "WEBVTT" and a space {0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x09}, // "WEBVTT" and a horizontal tab } for _, p := range prefixes { if bytes.HasPrefix(raw, p) { return true } } // Exact match. return bytes.Equal(raw, []byte{0xEF, 0xBB, 0xBF, 0x57, 0x45, 0x42, 0x56, 0x54, 0x54}) || // UTF-8 BOM and "WEBVTT" bytes.Equal(raw, []byte{0x57, 0x45, 0x42, 0x56, 0x54, 0x54}) // "WEBVTT" } type rfc822Hint struct { h []byte matchFlags scan.Flags } // The hints come from libmagic, but the implementation is bit different. libmagic // only checks if the file starts with the hint, while we additionally look for // a secondary hint in the first few lines of input. func RFC822(raw []byte, limit uint32) bool { b := scan.Bytes(raw) // Keep hints here to avoid instantiating them several times in lineHasRFC822Hint. // The alternative is to make them a package level var, but then they'd go // on the heap. // Some of the hints are IgnoreCase, some not. I selected based on what libmagic // does and based on personal observations from sample files. hints := []rfc822Hint{ {[]byte("From: "), 0}, {[]byte("To: "), 0}, {[]byte("CC: "), scan.IgnoreCase}, {[]byte("Date: "), 0}, {[]byte("Subject: "), 0}, {[]byte("Received: "), 0}, {[]byte("Relay-Version: "), 0}, {[]byte("#! rnews"), 0}, {[]byte("N#! rnews"), 0}, {[]byte("Forward to"), 0}, {[]byte("Pipe to"), 0}, {[]byte("DELIVERED-TO: "), scan.IgnoreCase}, {[]byte("RETURN-PATH: "), scan.IgnoreCase}, {[]byte("Content-Type: "), 0}, {[]byte("Content-Transfer-Encoding: "), 0}, } if !lineHasRFC822Hint(b.Line(), hints) { return false } for i := 0; i < 20; i++ { if lineHasRFC822Hint(b.Line(), hints) { return true } } return false } func lineHasRFC822Hint(b scan.Bytes, hints []rfc822Hint) bool { for _, h := range hints { if b.Match(h.h, h.matchFlags) > -1 { return true } } return false }