privatefuncisSpaceEnough(header: mach_header, offset: Int, is64bit: Bool) ->Bool { let pathSize = (dylibPath.count &~(pathPadding -1)) + pathPadding let injectSpace =MemoryLayout<dylib_command>.size + pathSize let headerSize = is64bit ?MemoryLayout<mach_header_64>.size : MemoryLayout<mach_header>.size
var segOffset = offset for_in0..< header.ncmds { let segData = machOData.subdata(in: segOffset..<segOffset+MemoryLayout<segment_command_64>.size) guardlet segCmd = getSegmentCommand(data: segData) else { print("[ERROR] Failed to get segment command pointer.") returnfalse }
var segName = segCmd.segname if (strncmp(&segName.0, "__TEXT", 15) ==0) { for i in0..< segCmd.nsects { let sectOffset = segOffset +MemoryLayout<segment_command_64>.size +MemoryLayout<section_64>.size *Int(i) let sectData = machOData.subdata(in: sectOffset..<sectOffset+MemoryLayout<section_64>.size) guardlet sectCmd = getSectionCommand(data: sectData) else { print("[ERROR] Failed to get section pointer.") returnfalse }
var sectName = sectCmd.sectname if (strncmp(§Name.0, "__text", 15) ==0) { let space = sectCmd.offset - header.sizeofcmds -UInt32(headerSize) print("[INFO] Available space is \(space/8) bytes.") return space > injectSpace } } } else { segOffset = segOffset +Int(segCmd.cmdsize) } } returnfalse }