FunctionHook

public typealias FunctionHook = FunctionHookProtocol & _FunctionHookClass

The base function hook type. All function hooks must subclass this.

In order to make a function hook, create a class which conforms to this type. Satisfy the protocol requirement by providing a static target property, and declare a function named function with the signature of the function that is being hooked.

The original function implementation can be accessed via orig.function.

Example

The following example hooks the C standard library function atoi such that atoi("1234") returns 4321 and all other calls behave normally.

class AtoiHook: FunctionHook {
    static let target = Function.symbol("atoi", image: nil)

    func function(_ string: UnsafePointer<Int8>) -> Int32 {
        if String(cString: string) == "1234" {
            return 4321
        } else {
            return orig.function(string)
        }
    }
}