1 /* Cydia Substrate - Powerful Code Insertion Platform
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
7 var libcycript = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
8 if (libcycript == null) {
9 exports.error = dlerror();
13 var CYHandleServer = dlsym(libcycript, "CYHandleServer");
14 if (CYHandleServer == null) {
15 exports.error = dlerror();
19 var info = new Dl_info;
20 if (dladdr(CYHandleServer, info) == 0) {
21 exports.error = dlerror();
25 var path = info->dli_fname;
26 var slash = path.lastIndexOf('/');
30 var libsubstrate = dlopen(path.substr(0, slash) + "/libsubstrate.dylib", RTLD_GLOBAL | RTLD_LAZY);
31 if (libsubstrate == null) {
32 exports.error = dlerror();
36 MSGetImageByName = @encode(void *(const char *))(dlsym(libsubstrate, "MSGetImageByName"));
37 MSFindSymbol = @encode(void *(void *, const char *))(dlsym(libsubstrate, "MSFindSymbol"));
38 MSHookFunction = @encode(void(void *, void *, void **))(dlsym(libsubstrate, "MSHookFunction"));
39 MSHookMessageEx = @encode(void(Class, SEL, void *, void **))(dlsym(libsubstrate, "MSHookMessageEx"));
43 exports.getImageByName = MSGetImageByName;
44 exports.findSymbol = MSFindSymbol;
46 exports.hookFunction = function(func, hook, old) {
47 var type = typeid(func);
50 if (old == null || typeof old === "undefined")
53 pointer = new @encode(void **);
54 *old = function() { return type(*pointer).apply(null, arguments); };
57 MSHookFunction(func.valueOf(), type(hook), pointer);
60 exports.hookMessage = function(isa, sel, imp, old) {
61 var type = sel.type(isa);
64 if (old == null || typeof old === "undefined")
67 pointer = new @encode(void **);
68 *old = function() { return type(*pointer).apply(null, [this, sel].concat(slice.call(arguments))); };
71 MSHookMessageEx(isa, sel, type(function(self, sel) { return imp.apply(self, slice.call(arguments, 2)); }), pointer);