%hookf
can be confusing at first, but it is pretty easy to understand once you get into it.
While %hook
is used to hook Objective-C classes, %hookf
is used to hook C functions. Its syntax is also different from %hook
.
%hookf(return_type, symbol_name, arguments...) {...}
return_type
- The return type of the function.symbol_name
- This is the name of the function being hooked.arguments
- These are the arguments that are passed into the function.
Let's say we want to hook CGFontRef CGFontCreateWithFontName(CFStringRef name);
. This would be done like so:
%hookf(CGFontRef, CGFontCreateWithFontName, CFStringRef name) {
// code
return %orig;
}
Below is the Substrate version of the above code, if needed.
CGFontRef (*orig_CGFontCreateWithFontName)(CFStringRef);
CGFontRef new_CGFontCreateWithFontName(CFStringRef name) {
return orig_CGFontCreateWithFontName(name);
}
__attribute__((constructor)) static void initialize() {
MSHookFunction(((void *)MSFindSymbol(NULL, "CGFontCreateWithFontName")), (void *)new_CGFontCreateWithFontName, (void **)&orig_CGFontCreateWithFontName);
}
For further information about %hookf
, please go here.