Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Know a transport stream by three packets, and broadcast's form

Țugui Dragoș56cb0c49e7d6
diff --git a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
index c314645f..f4b5cb6b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
@@ -308,14 +308,18 @@ public enum FileSearch {
}
/// macOS gives TypeScript's `.ts` and `.mts` the type of an MPEG-2 transport stream, a movie, so such a file is one
- /// only when it begins as one: packets that each start with the byte 0x47, every 188 bytes, or every 192 after the
- /// 4-byte header Blu-ray adds (ISO/IEC 13818-1). Nil when it can't be read.
+ /// only when it begins as one: three packets that each start with the byte 0x47, every 188 bytes (ISO/IEC 13818-1),
+ /// every 192 after the 4-byte header Blu-ray adds, or every 204 with the check bytes broadcast adds to each
+ /// (ETSI EN 300 744). Nil when it can't be read.
static func beginsAsATransportStream(_ url: URL) -> Bool? {
- guard let bytes = BoundedRead.prefix(of: url, count: 197).map(Array.init) else { return nil }
+ guard let bytes = BoundedRead.prefix(of: url, count: 409).map(Array.init) else { return nil }
func packets(from start: Int, every length: Int) -> Bool {
- bytes.count > start + length && bytes[start] == 0x47 && bytes[start + length] == 0x47
+ (0..<3).allSatisfy { packet in
+ let place = start + packet * length
+ return place < bytes.count && bytes[place] == 0x47
+ }
}
- return packets(from: 0, every: 188) || packets(from: 4, every: 192)
+ return packets(from: 0, every: 188) || packets(from: 4, every: 192) || packets(from: 0, every: 204)
}
static func isATransportStream(type identifier: String) -> Bool {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
index f4c67c62..95da0410 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
@@ -271,6 +271,27 @@ struct FileSearchTests {
#expect(!FileSearch.isATransportStream(type: "public.mpeg-4"))
}
+ @Test func aTransportStreamBeginsWithThreePacketsInARow() throws {
+ let directory = try TemporaryDirectory()
+ func file(_ name: String, syncAt places: [Int], bytes: Int = 1_000) throws -> URL {
+ var contents = [UInt8](repeating: 0xFF, count: bytes)
+ for place in places { contents[place] = 0x47 }
+ return try directory.file(name, contents: Data(contents))
+ }
+
+ let plain = try file("plain.ts", syncAt: [0, 188, 376])
+ let blueRay = try file("blueray.ts", syncAt: [4, 196, 388])
+ let broadcast = try file("broadcast.ts", syncAt: [0, 204, 408])
+ let textWithTwoLetters = try file("note.ts", syncAt: [0, 188])
+ let tooShort = try file("short.ts", syncAt: [0, 188], bytes: 300)
+
+ #expect(FileSearch.beginsAsATransportStream(plain) == true)
+ #expect(FileSearch.beginsAsATransportStream(blueRay) == true)
+ #expect(FileSearch.beginsAsATransportStream(broadcast) == true)
+ #expect(FileSearch.beginsAsATransportStream(textWithTwoLetters) == false)
+ #expect(FileSearch.beginsAsATransportStream(tooShort) == false)
+ }
+
@Test func goesByTheTypeSpotlightIndexedAndNotTheExtension() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)