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