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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
class native_ptr {
// constructor(addy, size = 4, buf_to_obj = u8x4_to_u32) {
constructor() {
this.addy = arguments[1];
this.count = arguments[0];
var our_proto = Object.getPrototypeOf(this);
if (our_proto.predef == true) {
this.size = our_proto.size;
this.buf_to_obj = our_proto.buf_to_obj;
this.obj_to_buf = our_proto.obj_to_buf;
} else {
this.size = arguments[2];
this.buf_to_obj = arguments[3];
this.obj_to_buf = arguments[4];
}
if (this.count === undefined) {
this.count = 1;
}
if (this.size === undefined) {
this.size = 4;
}
if (this.buf_to_obj === undefined) {
this.buf_to_obj = u8x4_to_u32;
}
if (this.obj_to_buf === undefined) {
this.obj_to_buf = u32_to_u8x4;
}
if (Object.getPrototypeOf(this).deref_all != undefined) {
this.size *= this.count;
}
if (this.addy === undefined) {
this.addy = shit_heap(this.size * this.count);
}
}
deref() {
var n = arguments[0];
if (n === undefined) {
n = 0;
}
return this.buf_to_obj(read_buf(this.addy + (n * this.size), this.size));
}
write() {
var v = arguments[0];
var n = arguments[1];
if (n === undefined) {
n = 0;
}
write_buf(this.addy + (n * this.size), this.obj_to_buf(v), this.size);
}
}
function native_ptr_type(size, buf_to_obj, obj_to_buf) {
class ret extends native_ptr {
//
}
ret.prototype.predef = true;
ret.prototype.size = size;
ret.prototype.buf_to_obj = buf_to_obj;
ret.prototype.obj_to_buf = obj_to_buf;
return ret;
}
/*
typedef struct{
void* address;
mach_msg_size_t count;
boolean_t deallocate: 8;
mach_msg_copy_options_t copy: 8;
mach_msg_type_name_t disposition : 8;
mach_msg_descriptor_type_t type : 8;
} mach_msg_ool_ports_descriptor_t;
*/
function mach_msg_ool_ports_descriptor_t_buf_to_obj(buf) {
var ret = {};
ret.address = u8x4_to_u32(buf);
ret.count = u8x4_to_u32([buf[4], buf[5], buf[6], buf[7]]);
ret.deallocate = buf[8];
ret.copy = buf[9];
ret.disposition = buf[10];
ret.type = buf[11];
return ret;
}
function mach_msg_ool_ports_descriptor_t_obj_to_buf(obj) {
var ret = new Uint8Array(12);
var tmp;
tmp = u32_to_u8x4(obj.address);
ret[0] = tmp[0];
ret[1] = tmp[1];
ret[2] = tmp[2];
ret[3] = tmp[3];
tmp = u32_to_u8x4(obj.count);
ret[0 + 4] = tmp[0];
ret[1 + 4] = tmp[1];
ret[2 + 4] = tmp[2];
ret[3 + 4] = tmp[3];
if (typeof obj.deallocate === 'boolean') {
ret[8] = obj.deallocate ? 1 : 0;
} else {
ret[8] = obj.deallocate;
}
ret[9] = obj.copy;
ret[10] = obj.disposition;
ret[11] = obj.type;
return ret;
}
function u32xn_to_u8xn(buf) {
var ret = new Uint8Array(buf.length * 4);
for (var i = 0; i < buf.length; i++) {
var tmp = u32_to_u8x4(buf[i]);
ret[(i << 2) + 0] = tmp[0];
ret[(i << 2) + 1] = tmp[1];
ret[(i << 2) + 2] = tmp[2];
ret[(i << 2) + 3] = tmp[3];
}
return ret;
}
function u8xn_to_u32xn(buf) {
var ret = new Uint32Array(buf.length >>> 2);
for (var i = 0; i < buf.length; i += 4) {
var tmp = [buf[(i) + 0],
buf[(i) + 1],
buf[(i) + 2],
buf[(i) + 3]];
ret[i >> 2] = u8x4_to_u32(tmp);
}
return ret;
}
/*
typedef struct{
mach_msg_bits_t msgh_bits;
mach_msg_size_t msgh_size;
mach_port_t msgh_remote_port;
mach_port_t msgh_local_port;
mach_port_name_t msgh_voucher_port;
mach_msg_id_t msgh_id;
} mach_msg_header_t;
*/
function mach_msg_header_t_buf_to_obj(buf) {
var arr = u8xn_to_u32xn(buf);
var ret = {};
ret.msgh_bits = arr[0];
ret.msgh_size = arr[1];
ret.msgh_remote_port = arr[2];
ret.msgh_local_port = arr[3];
ret.msgh_voucher_port = arr[4];
ret.msgh_id = arr[5];
return ret;
}
function mach_msg_header_t_obj_to_buf(obj) {
var arr = [obj.msgh_bits,
obj.msgh_size,
obj.msgh_remote_port,
obj.msgh_local_port,
obj.msgh_voucher_port,
obj.msgh_id];
var ret = u32xn_to_u8xn(arr);
return ret;
}
function mach_msg_body_t_buf_to_obj(buf) {
var ret = {};
ret.msgh_descriptor_count = u8x4_to_u32(buf);
return ret;
}
function mach_msg_body_t_obj_to_buf(obj) {
var ret = u32_to_u8x4(obj.msgh_descriptor_count);
return ret;
}
function Request_sp_buf_to_obj(buf) {
var ret = {};
var Head_buf = buf.subarray(0, 24);
var msgh_body_buf = buf.subarray(24, 28);
var init_port_set_buf = buf.subarray(28);
ret.Head = mach_msg_header_t_buf_to_obj(Head_buf);
ret.msgh_body = mach_msg_body_t_buf_to_obj(msgh_body_buf);
ret.init_port_set = new Array();
for (var i = 0; i < (buf.length - 28) / 28; i++) {
var init_port_set_buf = buf.subarray((i * 28) + 28);
ret.init_port_set.push(mach_msg_ool_ports_descriptor_t_buf_to_obj(init_port_set_buf));
}
return ret;
}
function Request_sp_obj_to_buf(obj) {
var ret = new Uint8Array(this.size * this.count);
var tmp = mach_msg_header_t_obj_to_buf(obj.Head);
var begin = 0;
var i = 0;
begin = i;
for (; i < 24; i++) {
ret[i] = tmp[i - begin];
}
begin = i;
var tmp = mach_msg_body_t_obj_to_buf(obj.msgh_body);
for (; i < 28; i++) {
ret[i] = tmp[i - begin];
}
begin = i;
for (var i = 0; i < obj.init_port_set.length; i++) {
var tmp = mach_msg_ool_ports_descriptor_t_obj_to_buf(obj.init_port_set[i]);
for (var j = 0; j < tmp.length; j++) {
ret[begin + (i * 12) + j] = tmp[j];
}
}
return ret;
}
function Request_r3_buf_to_obj(buf) {
var ret = {};
var Head_buf = buf.subarray(0, 24);
var msgh_body_buf = buf.subarray(24, 28);
var init_port_set_buf = buf.subarray(28, 40);
var NDR_buf = buf.subarray(40, 48);
var init_port_setCnt_buf = buf.subarray(48, 52);
ret.Head = mach_msg_header_t_buf_to_obj(Head_buf);
ret.msgh_body = mach_msg_body_t_buf_to_obj(msgh_body_buf);
ret.init_port_set = mach_msg_ool_ports_descriptor_t_buf_to_obj(init_port_set_buf);
ret.NDR = buf_ret(NDR_buf);
ret.init_port_setCnt = u8x4_to_u32(init_port_setCnt_buf);
return ret;
}
function Request_r3_obj_to_buf(obj) {
var ret = new Uint8Array(this.size);
var tmp = mach_msg_header_t_obj_to_buf(obj.Head);
var begin = 0;
var i = 0;
begin = i;
for (; i < 24; i++) {
ret[i] = tmp[i - begin];
}
begin = i;
var tmp = mach_msg_body_t_obj_to_buf(obj.msgh_body);
for (; i < 28; i++) {
ret[i] = tmp[i - begin];
}
var tmp = mach_msg_ool_ports_descriptor_t_obj_to_buf(obj.init_port_set);
begin = i;
for (; i < 40; i++) {
ret[i] = tmp[i - begin];
}
var tmp = buf_ret(obj.NDR);
begin = i;
for (; i < 48; i++) {
ret[i] = tmp[i - begin];
}
var tmp = u32_to_u8x4(obj.init_port_setCnt);
begin = i;
for (; i < 52; i++) {
ret[i] = tmp[i - begin];
}
return ret;
}
function buf_ret(buf) {
return buf;
}
var mach_msg_header_t = native_ptr_type(24,
mach_msg_header_t_buf_to_obj,
mach_msg_header_t_obj_to_buf);
var mach_msg_body_t = native_ptr_type(4,
mach_msg_body_t_buf_to_obj,
mach_msg_body_t_obj_to_buf);
var mach_msg_ool_ports_descriptor_t = native_ptr_type(12,
mach_msg_ool_ports_descriptor_t_buf_to_obj,
mach_msg_ool_ports_descriptor_t_obj_to_buf);
var Request_sp = native_ptr_type(24 + 4 + 12,
Request_sp_buf_to_obj,
Request_sp_obj_to_buf);
Request_sp.prototype.deref_all = true;
var uint32_t = native_ptr_type(4);
var mach_port_t = uint32_t;
var io_master_t = mach_port_t;
var uint8_t = native_ptr_type(1);
var NDR_record_t = native_ptr_type(8, buf_ret, buf_ret);
var kern_return_t = uint32_t;
var mach_msg_type_number_t = uint32_t;
var Request_r3 = native_ptr_type(24 + 4 + 12 + 8 + 4,
Request_r3_buf_to_obj,
Request_r3_obj_to_buf);
/*var Reply_r3 = native_ptr_type(24 + 4 + 12 + 8 + 4,
Reply_r3_buf_to_obj,
Reply_r3_obj_to_buf);*/
var mach_msg_trailer_type_t = uint32_t;
var mach_msg_trailer_size_t = uint32_t;
|