]> git.saurik.com Git - fiveicondock.git/blob - Library.mm
2d09b23c44467b87c8f4f454fb5a1144c93c3e14
[fiveicondock.git] / Library.mm
1 #import <Foundation/Foundation.h>
2
3 extern "C" {
4 #include <mach-o/nlist.h>
5 }
6
7 #include <unistd.h>
8 #include <dlfcn.h>
9
10 #include <mach/mach_init.h>
11 #include <mach/vm_map.h>
12
13 extern "C" void __clear_cache (char *beg, char *end);
14
15 extern "C" CFStringRef CPPhoneNumberCopyNetworkCountryCode();
16
17 static void ASReplace(void *symbol, void *replace) {
18 //NSLog(@"AS:Notice: Remapping %p to %p", symbol, replace);
19 if (symbol == NULL)
20 return;
21
22 int page = getpagesize();
23 uintptr_t base = (uintptr_t) symbol / page * page;
24
25 mach_port_t self = mach_task_self();
26
27 if (kern_return_t error = vm_protect(self, base, page, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY)) {
28 NSLog(@"AS:Error:vm_protect():%d", error);
29 return;
30 }
31
32 uint32_t *code = (uint32_t *) symbol;
33
34 code[0] = 0xe51ff004; // ldr pc, [pc, #-4]
35 code[1] = (uint32_t) replace;
36
37 __clear_cache(reinterpret_cast<char *>(code), reinterpret_cast<char *>(code + 2));
38
39 if (kern_return_t error = vm_protect(self, base, page, FALSE, VM_PROT_READ | VM_PROT_EXECUTE))
40 NSLog(@"AS:Error:vm_protect():%d", error);
41 }
42
43 /*static CFStringRef UIDefaultCountryCode_() {
44 return CPPhoneNumberCopyNetworkCountryCode();
45 }*/
46
47 #define UIKit "/System/Library/Frameworks/UIKit.framework/UIKit"
48
49 extern "C" void ASInitialize() {
50 if (dlopen(UIKit, RTLD_LAZY | RTLD_NOLOAD) == NULL)
51 return;
52 //NSLog(@"AS:Notice: Installing AppSupport...");
53
54 struct nlist nl[2];
55 memset(nl, 0, sizeof(nl));
56 nl[0].n_un.n_name = (char *) "_UIDefaultCountryCode";
57 nlist(UIKit, nl);
58 CFStringRef (*UIDefaultCountryCode)() = reinterpret_cast<CFStringRef (*)()>(nl[0].n_value);
59 ASReplace(reinterpret_cast<void *>(UIDefaultCountryCode), reinterpret_cast<void *>(&CPPhoneNumberCopyNetworkCountryCode));
60
61 //NSLog(@"AS:Notice: Done");
62 }