ssed. func XHTML(raw []byte, limit uint32) bool { raw = raw[:min(len(raw), 1024)] b := scan.Bytes(raw) i, _ := b.Search([]byte(" 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) s.DropLastLine(limit) 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" }