|
| 1 | +import Foundation |
| 2 | + |
| 3 | +private let manifestTextEncodings: [String.Encoding] = [ |
| 4 | + .utf8, |
| 5 | + .utf16LittleEndian, |
| 6 | + .utf16BigEndian, |
| 7 | +] |
| 8 | + |
| 9 | +private let manifestLeadingCharacters = |
| 10 | + CharacterSet.whitespacesAndNewlines.union(CharacterSet(charactersIn: "\u{FEFF}")) |
| 11 | + |
| 12 | +func matchM3U(_ data: Data) -> Bool { |
| 13 | + guard |
| 14 | + let manifestText = manifestText(from: data)? |
| 15 | + .trimmingCharacters(in: manifestLeadingCharacters) |
| 16 | + .uppercased(), |
| 17 | + manifestText.hasPrefix("#EXTM3U") |
| 18 | + else { |
| 19 | + return false |
| 20 | + } |
| 21 | + |
| 22 | + // Require an HLS-specific tag so generic M3U playlists do not match. |
| 23 | + return manifestText.contains("#EXT-X-") |
| 24 | +} |
| 25 | + |
| 26 | +func matchMPD(_ data: Data) -> Bool { |
| 27 | + matchXMLManifest(data, rootElement: "MPD") |
| 28 | +} |
| 29 | + |
| 30 | +func matchISM(_ data: Data) -> Bool { |
| 31 | + matchXMLManifest(data, rootElement: "SmoothStreamingMedia") |
| 32 | +} |
| 33 | + |
| 34 | +private func matchXMLManifest(_ data: Data, rootElement: String) -> Bool { |
| 35 | + guard |
| 36 | + let manifestText = manifestText(from: data), |
| 37 | + let startTag = firstXMLStartTag(in: manifestText) |
| 38 | + else { |
| 39 | + return false |
| 40 | + } |
| 41 | + |
| 42 | + return startTag == rootElement |
| 43 | +} |
| 44 | + |
| 45 | +private func manifestText(from data: Data) -> String? { |
| 46 | + for encoding in manifestTextEncodings { |
| 47 | + if let manifestText = String(data: data, encoding: encoding) { |
| 48 | + return manifestText |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +private func firstXMLStartTag(in text: String) -> String? { |
| 56 | + var remaining = text[...] |
| 57 | + |
| 58 | + while true { |
| 59 | + remaining = remaining.drop(while: isManifestLeadingCharacter) |
| 60 | + |
| 61 | + guard remaining.first == "<" else { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + if remaining.hasPrefix("<?") { |
| 66 | + guard let terminator = remaining.range(of: "?>") else { |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + remaining = remaining[terminator.upperBound...] |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + if remaining.hasPrefix("<!--") { |
| 75 | + guard let terminator = remaining.range(of: "-->") else { |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + remaining = remaining[terminator.upperBound...] |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + if remaining.hasPrefix("<!") { |
| 84 | + guard let terminator = remaining.firstIndex(of: ">") else { |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + remaining = remaining[remaining.index(after: terminator)...] |
| 89 | + continue |
| 90 | + } |
| 91 | + |
| 92 | + let rawTagName = |
| 93 | + remaining |
| 94 | + .dropFirst() |
| 95 | + .prefix { character in |
| 96 | + !character.isWhitespace && character != ">" && character != "/" |
| 97 | + } |
| 98 | + |
| 99 | + guard !rawTagName.isEmpty else { |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + return String(rawTagName.split(separator: ":").last ?? rawTagName[...]) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +private func isManifestLeadingCharacter(_ character: Character) -> Bool { |
| 108 | + character.unicodeScalars.allSatisfy(manifestLeadingCharacters.contains) |
| 109 | +} |
0 commit comments