]> git.saurik.com Git - cycript.git/blob - modules/com/saurik/substrate/MS.cy
Update the copyright year now that 2016 has begun.
[cycript.git] / modules / com / saurik / substrate / MS.cy
1 /* Cydia Substrate - Powerful Code Insertion Platform
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 (function(exports) {
6
7 var libcycript = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
8 if (libcycript == null) {
9 exports.error = dlerror();
10 return;
11 }
12
13 var CYHandleServer = dlsym(libcycript, "CYHandleServer");
14 if (CYHandleServer == null) {
15 exports.error = dlerror();
16 return;
17 }
18
19 var info = new Dl_info;
20 if (dladdr(CYHandleServer, info) == 0) {
21 exports.error = dlerror();
22 return;
23 }
24
25 var path = info->dli_fname;
26 var slash = path.lastIndexOf('/');
27 if (slash == -1)
28 return;
29
30 var libsubstrate = dlopen(path.substr(0, slash) + "/libsubstrate.dylib", RTLD_GLOBAL | RTLD_LAZY);
31 if (libsubstrate == null) {
32 exports.error = dlerror();
33 return;
34 }
35
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"));
40
41 var slice = [].slice;
42
43 exports.getImageByName = MSGetImageByName;
44 exports.findSymbol = MSFindSymbol;
45
46 exports.hookFunction = function(func, hook, old) {
47 var type = typeid(func);
48
49 var pointer;
50 if (old == null || typeof old === "undefined")
51 pointer = null;
52 else {
53 pointer = new @encode(void **);
54 *old = function() { return type(*pointer).apply(null, arguments); };
55 }
56
57 MSHookFunction(func.valueOf(), type(hook), pointer);
58 };
59
60 exports.hookMessage = function(isa, sel, imp, old) {
61 var type = sel.type(isa);
62
63 var pointer;
64 if (old == null || typeof old === "undefined")
65 pointer = null;
66 else {
67 pointer = new @encode(void **);
68 *old = function() { return type(*pointer).apply(null, [this, sel].concat(slice.call(arguments))); };
69 }
70
71 MSHookMessageEx(isa, sel, type(function(self, sel) { return imp.apply(self, slice.call(arguments, 2)); }), pointer);
72 };
73
74 })(exports);