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.

Find an Intel program whose file starts with its i386 slice

Țugui Dragoș7387490ea769
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/MachOHeader.swift b/Packages/PeelCore/Sources/PeelCore/Apps/MachOHeader.swift
index cf4ab894..1403a9a5 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/MachOHeader.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/MachOHeader.swift
@@ -5,6 +5,8 @@ enum MachOHeader {
private static let fatMagic: UInt32 = 0xCAFE_BABE
private static let fatMagic64: UInt32 = 0xCAFE_BABF
private static let machMagic64Swapped: UInt32 = 0xCFFA_EDFE
+ /// `MH_CIGAM`: a 32 bit header, as an i386 slice has, which keeps its file type at the same place.
+ private static let machMagicSwapped: UInt32 = 0xCEFA_EDFE
private static let cpuTypeARM64: UInt32 = 0x0100_000C
private static let cpuTypeX86_64: UInt32 = 0x0100_0007
private static let maximumFatArchitectures = 32
@@ -39,7 +41,8 @@ enum MachOHeader {
return read > 0 ? Array(buffer[0..<read]) : []
}
func isAProgram(_ header: [UInt8]) -> Bool {
- bigEndianUInt32(header, at: 0) == machMagic64Swapped
+ let magic = bigEndianUInt32(header, at: 0)
+ return (magic == machMagic64Swapped || magic == machMagicSwapped)
&& littleEndianUInt32(header, at: 12) == programFileType
}
let header = bytes(at: 0, count: 32)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
index f8ae59f1..44b5b5d3 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/IntelInspectorTests.swift
@@ -31,6 +31,29 @@ struct IntelInspectorTests {
private let intel: UInt32 = 0x0100_0007
private let appleSilicon: UInt32 = 0x0100_000C
+ @Test func findsAnIntelProgramWhoseFileStartsWithItsI386Slice() throws {
+ let directory = try TemporaryDirectory()
+ var bytes = Data([0xCA, 0xFE, 0xBA, 0xBE, 0x00, 0x00, 0x00, 0x02])
+ for (cpuType, offset) in [(UInt32(7), UInt32(4_096)), (intel, UInt32(8_192))] {
+ for value in [cpuType, 3, offset, 4_096, 12] {
+ bytes += [24, 16, 8, 0].map { UInt8(value >> $0 & 0xFF) }
+ }
+ }
+ bytes += Data(count: 4_096 - bytes.count)
+ bytes += Data([0xCE, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00])
+ bytes += Data(count: 8_192 - bytes.count)
+ bytes += intelHeader(fileType: 2)
+ bytes += Data(count: 12_288 - bytes.count)
+ let filter = try directory.file("Printers/Vendor/filter", contents: bytes)
+ try FileManager.default.setAttributes(
+ [.posixPermissions: 0o755], ofItemAtPath: filter.path(percentEncoded: false)
+ )
+ let printers = directory.url.appending(path: "Printers").path(percentEncoded: false)
+
+ #expect(MachOHeader.isProgram(at: filter))
+ #expect(IntelInspector.programs(in: [printers]).map(\.lastPathComponent) == ["filter"])
+ }
+
/// The drivers, the background items, and the tools are scanned after the apps, and those later steps must
/// stop as well.
@Test func aStoppedScanStops() async throws {