]> git.saurik.com Git - cycript.git/blob - modules/com/saurik/substrate/MS.cy
Add struct definitions and namespace struct names.
[cycript.git] / modules / com / saurik / substrate / MS.cy
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 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 exports.error = dlerror();
27 return;
28 }
29
30 var CYHandleServer = dlsym(libcycript, "CYHandleServer");
31 if (CYHandleServer == null) {
32 exports.error = dlerror();
33 return;
34 }
35
36 var info = new Dl_info;
37 if (dladdr(CYHandleServer, info) == 0) {
38 exports.error = dlerror();
39 return;
40 }
41
42 var path = info->dli_fname;
43 var slash = path.lastIndexOf('/');
44 if (slash == -1)
45 return;
46
47 var libsubstrate = dlopen(path.substr(0, slash) + "/libsubstrate.dylib", RTLD_GLOBAL | RTLD_LAZY);
48 if (libsubstrate == null) {
49 exports.error = dlerror();
50 return;
51 }
52
53 MSGetImageByName = @encode(void *(const char *))(dlsym(libsubstrate, "MSGetImageByName"));
54 MSFindSymbol = @encode(void *(void *, const char *))(dlsym(libsubstrate, "MSFindSymbol"));
55 MSHookFunction = @encode(void(void *, void *, void **))(dlsym(libsubstrate, "MSHookFunction"));
56 MSHookMessageEx = @encode(void(Class, SEL, void *, void **))(dlsym(libsubstrate, "MSHookMessageEx"));
57
58 var slice = [].slice;
59
60 exports.getImageByName = MSGetImageByName;
61 exports.findSymbol = MSFindSymbol;
62
63 exports.hookFunction = function(func, hook, old) {
64 var type = func.type;
65
66 var pointer;
67 if (old == null || typeof old === "undefined")
68 pointer = null;
69 else {
70 pointer = new @encode(void **);
71 *old = function() { return type(*pointer).apply(null, arguments); };
72 }
73
74 MSHookFunction(func.valueOf(), type(hook), pointer);
75 };
76
77 exports.hookMessage = function(isa, sel, imp, old) {
78 var type = sel.type(isa);
79
80 var pointer;
81 if (old == null || typeof old === "undefined")
82 pointer = null;
83 else {
84 pointer = new @encode(void **);
85 *old = function() { return type(*pointer).apply(null, [this, sel].concat(slice.call(arguments))); };
86 }
87
88 MSHookMessageEx(isa, sel, type(function(self, sel) { return imp.apply(self, slice.call(arguments, 2)); }), pointer);
89 };
90
91 })(exports);