]> git.saurik.com Git - cycript.git/blame - modules/com/saurik/substrate/MS.cy
Update the copyright year now that 2016 has begun.
[cycript.git] / modules / com / saurik / substrate / MS.cy
CommitLineData
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
7var libcycript = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
b97dddae
JF
8if (libcycript == null) {
9 exports.error = dlerror();
afc574ad 10 return;
b97dddae 11}
afc574ad
JF
12
13var CYHandleServer = dlsym(libcycript, "CYHandleServer");
b97dddae
JF
14if (CYHandleServer == null) {
15 exports.error = dlerror();
afc574ad 16 return;
b97dddae 17}
afc574ad
JF
18
19var info = new Dl_info;
20if (dladdr(CYHandleServer, info) == 0) {
b97dddae 21 exports.error = dlerror();
afc574ad
JF
22 return;
23}
24
25var path = info->dli_fname;
afc574ad
JF
26var slash = path.lastIndexOf('/');
27if (slash == -1)
28 return;
b97dddae 29
afc574ad 30var libsubstrate = dlopen(path.substr(0, slash) + "/libsubstrate.dylib", RTLD_GLOBAL | RTLD_LAZY);
b97dddae
JF
31if (libsubstrate == null) {
32 exports.error = dlerror();
afc574ad 33 return;
b97dddae 34}
afc574ad
JF
35
36MSGetImageByName = @encode(void *(const char *))(dlsym(libsubstrate, "MSGetImageByName"));
37MSFindSymbol = @encode(void *(void *, const char *))(dlsym(libsubstrate, "MSFindSymbol"));
38MSHookFunction = @encode(void(void *, void *, void **))(dlsym(libsubstrate, "MSHookFunction"));
39MSHookMessageEx = @encode(void(Class, SEL, void *, void **))(dlsym(libsubstrate, "MSHookMessageEx"));
40
41var slice = [].slice;
42
43exports.getImageByName = MSGetImageByName;
44exports.findSymbol = MSFindSymbol;
45
46exports.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
60exports.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);