-
Notifications
You must be signed in to change notification settings - Fork 57
/
osobject.c
168 lines (124 loc) · 5.11 KB
/
osobject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#import <stdlib.h>
#import "kexecute.h"
#import "kernel_utils.h"
#import "patchfinder64.h"
#import "osobject.h"
// offsets in vtable:
static uint32_t off_OSDictionary_SetObjectWithCharP = sizeof(void*) * 0x1F;
static uint32_t off_OSDictionary_GetObjectWithCharP = sizeof(void*) * 0x26;
static uint32_t off_OSDictionary_Merge = sizeof(void*) * 0x23;
static uint32_t off_OSArray_Merge = sizeof(void*) * 0x1E;
static uint32_t off_OSArray_RemoveObject = sizeof(void*) * 0x20;
static uint32_t off_OSArray_GetObject = sizeof(void*) * 0x22;
static uint32_t off_OSObject_Release = sizeof(void*) * 0x05;
static uint32_t off_OSObject_GetRetainCount = sizeof(void*) * 0x03;
static uint32_t off_OSObject_Retain = sizeof(void*) * 0x04;
static uint32_t off_OSString_GetLength = sizeof(void*) * 0x11;
// 1 on success, 0 on error
int OSDictionary_SetItem(uint64_t dict, const char *key, uint64_t val) {
size_t len = strlen(key) + 1;
uint64_t ks = Kernel_alloc(len);
KernelWrite(ks, key, len);
uint64_t vtab = KernelRead_64bits(dict);
uint64_t f = KernelRead_64bits(vtab + off_OSDictionary_SetObjectWithCharP);
int rv = (int) Kernel_Execute(f, dict, ks, val, 0, 0, 0, 0);
Kernel_free(ks, len);
return rv;
}
// XXX it can return 0 in lower 32 bits but still be valid
// fix addr of returned value and check if KernelRead_64bits gives ptr
// to vtable addr saved before
// address if exists, 0 if not
uint64_t _OSDictionary_GetItem(uint64_t dict, const char *key) {
size_t len = strlen(key) + 1;
uint64_t ks = Kernel_alloc(len);
KernelWrite(ks, key, len);
uint64_t vtab = KernelRead_64bits(dict);
uint64_t f = KernelRead_64bits(vtab + off_OSDictionary_GetObjectWithCharP);
int rv = (int) Kernel_Execute(f, dict, ks, 0, 0, 0, 0, 0);
Kernel_free(ks, len);
return rv;
}
uint64_t OSDictionary_GetItem(uint64_t dict, const char *key) {
uint64_t ret = _OSDictionary_GetItem(dict, key);
if (ret != 0) {
// XXX can it be not in zalloc?..
ret = ZmFixAddr(ret);
}
return ret;
}
// 1 on success, 0 on error
int OSDictionary_Merge(uint64_t dict, uint64_t aDict) {
uint64_t vtab = KernelRead_64bits(dict);
uint64_t f = KernelRead_64bits(vtab + off_OSDictionary_Merge);
return (int) Kernel_Execute(f, dict, aDict, 0, 0, 0, 0, 0);
}
// 1 on success, 0 on error
int OSArray_Merge(uint64_t array, uint64_t aArray) {
uint64_t vtab = KernelRead_64bits(array);
uint64_t f = KernelRead_64bits(vtab + off_OSArray_Merge);
return (int) Kernel_Execute(f, array, aArray, 0, 0, 0, 0, 0);
}
uint64_t _OSArray_GetObject(uint64_t array, unsigned int idx){
uint64_t vtab = KernelRead_64bits(array);
uint64_t f = KernelRead_64bits(vtab + off_OSArray_GetObject);
return Kernel_Execute(f, array, idx, 0, 0, 0, 0, 0);
}
uint64_t OSArray_GetObject(uint64_t array, unsigned int idx){
uint64_t ret = _OSArray_GetObject(array, idx);
if (ret != 0){
// XXX can it be not in zalloc?..
ret = ZmFixAddr(ret);
}
return ret;
}
void OSArray_RemoveObject(uint64_t array, unsigned int idx){
uint64_t vtab = KernelRead_64bits(array);
uint64_t f = KernelRead_64bits(vtab + off_OSArray_RemoveObject);
(void)Kernel_Execute(f, array, idx, 0, 0, 0, 0, 0);
}
// XXX error handling just for fun? :)
uint64_t _OSUnserializeXML(const char* buffer) {
size_t len = strlen(buffer) + 1;
uint64_t ks = Kernel_alloc(len);
KernelWrite(ks, buffer, len);
uint64_t errorptr = 0;
uint64_t rv = Kernel_Execute(Find_osunserializexml(), ks, errorptr, 0, 0, 0, 0, 0);
Kernel_free(ks, len);
return rv;
}
uint64_t OSUnserializeXML(const char* buffer) {
uint64_t ret = _OSUnserializeXML(buffer);
if (ret != 0) {
// XXX can it be not in zalloc?..
ret = ZmFixAddr(ret);
}
return ret;
}
void OSObject_Release(uint64_t osobject) {
uint64_t vtab = KernelRead_64bits(osobject);
uint64_t f = KernelRead_64bits(vtab + off_OSObject_Release);
(void) Kernel_Execute(f, osobject, 0, 0, 0, 0, 0, 0);
}
void OSObject_Retain(uint64_t osobject) {
uint64_t vtab = KernelRead_64bits(osobject);
uint64_t f = KernelRead_64bits(vtab + off_OSObject_Retain);
(void) Kernel_Execute(f, osobject, 0, 0, 0, 0, 0, 0);
}
uint32_t OSObject_GetRetainCount(uint64_t osobject) {
uint64_t vtab = KernelRead_64bits(osobject);
uint64_t f = KernelRead_64bits(vtab + off_OSObject_GetRetainCount);
return (uint32_t) Kernel_Execute(f, osobject, 0, 0, 0, 0, 0, 0);
}
unsigned int OSString_GetLength(uint64_t osstring){
uint64_t vtab = KernelRead_64bits(osstring);
uint64_t f = KernelRead_64bits(vtab + off_OSString_GetLength);
return (unsigned int)Kernel_Execute(f, osstring, 0, 0, 0, 0, 0, 0);
}
char *OSString_CopyString(uint64_t osstring){
unsigned int length = OSString_GetLength(osstring);
char *str = malloc(length + 1);
str[length] = 0;
KernelRead(OSString_CStringPtr(osstring), str, length);
return str;
}