write a swift function for macos that, given the url of a file, parses its mach-o header…
- User
write a swift function for macos that, given the url of a file, parses its mach-o header and returns the architectures it was compiled for
Assistant
Here is a Swift function that parses the Mach-O header of a file URL and returns the architectures it was compiled for on macOS:
import Darwin func getArchitectures(forFile url: URL) -> [CPUType]? { guard let data = try? Data(contentsOf: url) else { return nil } var header = mach_header() data.withUnsafeBytes { pointer in memcpy(&header, pointer.baseAddress!, MemoryLayout<mach_header>.size) } guard header.magic == MH_MAGIC || header.magic == MH_CIGAM else { return nil } var architectures = [CPUType]() var offset = MemoryLayout<mach_header>.size for _ in 0..<header.ncmds { var loadCommand = load_command() memcpy(&loadCommand, data.advanced(by: offset).bindMemory(to: load_command.self).baseAddress!, MemoryLayout<load_command>.size) if loadCommand.cmd == LC_BUILD_VERSION { let buildVersion = data.advanced(by: offset).bindMemory(to: build_version_command.self).pointee architectures = buildVersion.platform } offset += MemoryLayout<load_command>.size } return architectures }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.