]> git.saurik.com Git - cycript.git/blob - modules/com/saurik/substrate/MS.cy
f3ee4d8b3b4cae255b72092cf0714b1bdb627ac9
[cycript.git] / modules / com / saurik / substrate / MS.cy
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 (function(exports) {
23
24 var libcycript = dlopen("/usr/lib/libcycript.dylib", RTLD_NOLOAD);
25 if (libcycript == null)
26 return;
27
28 var CYHandleServer = dlsym(libcycript, "CYHandleServer");
29 if (CYHandleServer == null)
30 return;
31
32 var info = new Dl_info;
33 if (dladdr(CYHandleServer, info) == 0) {
34 free(info);
35 return;
36 }
37
38 var path = info->dli_fname;
39 free(info);
40
41 var slash = path.lastIndexOf('/');
42 if (slash == -1)
43 return;
44 var libsubstrate = dlopen(path.substr(0, slash) + "/libsubstrate.dylib", RTLD_GLOBAL | RTLD_LAZY);
45 if (libsubstrate == null)
46 return;
47
48 MSGetImageByName = @encode(void *(const char *))(dlsym(libsubstrate, "MSGetImageByName"));
49 MSFindSymbol = @encode(void *(void *, const char *))(dlsym(libsubstrate, "MSFindSymbol"));
50 MSHookFunction = @encode(void(void *, void *, void **))(dlsym(libsubstrate, "MSHookFunction"));
51 MSHookMessageEx = @encode(void(Class, SEL, void *, void **))(dlsym(libsubstrate, "MSHookMessageEx"));
52
53 var slice = [].slice;
54
55 exports.getImageByName = MSGetImageByName;
56 exports.findSymbol = MSFindSymbol;
57
58 exports.hookFunction = function(func, hook, old) {
59 var type = func.type;
60
61 var pointer;
62 if (old == null || typeof old === "undefined")
63 pointer = null;
64 else {
65 pointer = new @encode(void **);
66 *old = function() { return type(*pointer).apply(null, arguments); };
67 }
68
69 MSHookFunction(func.valueOf(), type(hook), pointer);
70 };
71
72 exports.hookMessage = function(isa, sel, imp, old) {
73 var type = sel.type(isa);
74
75 var pointer;
76 if (old == null || typeof old === "undefined")
77 pointer = null;
78 else {
79 pointer = new @encode(void **);
80 *old = function() { return type(*pointer).apply(null, [this, sel].concat(slice.call(arguments))); };
81 }
82
83 MSHookMessageEx(isa, sel, type(function(self, sel) { return imp.apply(self, slice.call(arguments, 2)); }), pointer);
84 };
85
86 })(exports);