]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cydia Substrate - Powerful Code Insertion Platform |
2 | * Copyright (C) 2008-2015 Jay Freeman (saurik) | |
afc574ad JF |
3 | */ |
4 | ||
afc574ad JF |
5 | (function(exports) { |
6 | ||
7 | var libcycript = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD); | |
b97dddae JF |
8 | if (libcycript == null) { |
9 | exports.error = dlerror(); | |
afc574ad | 10 | return; |
b97dddae | 11 | } |
afc574ad JF |
12 | |
13 | var CYHandleServer = dlsym(libcycript, "CYHandleServer"); | |
b97dddae JF |
14 | if (CYHandleServer == null) { |
15 | exports.error = dlerror(); | |
afc574ad | 16 | return; |
b97dddae | 17 | } |
afc574ad JF |
18 | |
19 | var info = new Dl_info; | |
20 | if (dladdr(CYHandleServer, info) == 0) { | |
b97dddae | 21 | exports.error = dlerror(); |
afc574ad JF |
22 | return; |
23 | } | |
24 | ||
25 | var path = info->dli_fname; | |
afc574ad JF |
26 | var slash = path.lastIndexOf('/'); |
27 | if (slash == -1) | |
28 | return; | |
b97dddae | 29 | |
afc574ad | 30 | var libsubstrate = dlopen(path.substr(0, slash) + "/libsubstrate.dylib", RTLD_GLOBAL | RTLD_LAZY); |
b97dddae JF |
31 | if (libsubstrate == null) { |
32 | exports.error = dlerror(); | |
afc574ad | 33 | return; |
b97dddae | 34 | } |
afc574ad JF |
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) { | |
574d4720 | 47 | var type = typeid(func); |
afc574ad JF |
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); |