-
Notifications
You must be signed in to change notification settings - Fork 22
/
xpc_type.c
545 lines (433 loc) · 9.84 KB
/
xpc_type.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* Copyright 2014-2015 iXsystems, Inc.
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <time.h>
#include "xpc/xpc.h"
#include "xpc_internal.h"
struct _xpc_type_s {
};
typedef const struct _xpc_type_s xt;
xt _xpc_type_array;
xt _xpc_type_bool;
xt _xpc_type_connection;
xt _xpc_type_data;
xt _xpc_type_date;
xt _xpc_type_dictionary;
xt _xpc_type_endpoint;
xt _xpc_type_null;
xt _xpc_type_error;
xt _xpc_type_fd;
xt _xpc_type_int64;
xt _xpc_type_uint64;
xt _xpc_type_shmem;
xt _xpc_type_string;
xt _xpc_type_uuid;
xt _xpc_type_double;
struct _xpc_bool_s {
};
typedef const struct _xpc_bool_s xb;
xb _xpc_bool_true;
xb _xpc_bool_false;
struct _xpc_dictionary_s {
};
typedef const struct _xpc_dictionary_s xs;
xs _xpc_error_connection_interrupted;
xs _xpc_error_connection_invalid;
xs _xpc_error_connection_imminent;
static size_t xpc_data_hash(const uint8_t *data, size_t length);
static xpc_type_t xpc_typemap[] = {
NULL,
XPC_TYPE_DICTIONARY,
XPC_TYPE_ARRAY,
XPC_TYPE_BOOL,
XPC_TYPE_CONNECTION,
XPC_TYPE_ENDPOINT,
XPC_TYPE_NULL,
NULL,
XPC_TYPE_INT64,
XPC_TYPE_UINT64,
XPC_TYPE_DATE,
XPC_TYPE_DATA,
XPC_TYPE_STRING,
XPC_TYPE_UUID,
XPC_TYPE_FD,
XPC_TYPE_SHMEM,
XPC_TYPE_ERROR,
XPC_TYPE_DOUBLE
};
static const char *xpc_typestr[] = {
"invalid",
"dictionary",
"array",
"bool",
"connection",
"endpoint",
"null",
"invalid",
"int64",
"uint64",
"date",
"data",
"string",
"uuid",
"fd",
"shmem",
"error",
"double"
};
__private_extern__ struct xpc_object *
_xpc_prim_create(int type, xpc_u value, size_t size)
{
return (_xpc_prim_create_flags(type, value, size, 0));
}
__private_extern__ struct xpc_object *
_xpc_prim_create_flags(int type, xpc_u value, size_t size, uint16_t flags)
{
struct xpc_object *xo;
if ((xo = malloc(sizeof(*xo))) == NULL)
return (NULL);
xo->xo_size = size;
xo->xo_xpc_type = type;
xo->xo_flags = flags;
xo->xo_u = value;
xo->xo_refcnt = 1;
#if MACH
xo->xo_audit_token = NULL;
#endif
if (type == _XPC_TYPE_DICTIONARY)
TAILQ_INIT(&xo->xo_dict);
if (type == _XPC_TYPE_ARRAY)
TAILQ_INIT(&xo->xo_array);
return (xo);
}
xpc_object_t
xpc_null_create(void)
{
xpc_u val;
return _xpc_prim_create(_XPC_TYPE_NULL, val, 0);
}
xpc_object_t
xpc_bool_create(bool value)
{
xpc_u val;
val.b = value;
return _xpc_prim_create(_XPC_TYPE_BOOL, val, 1);
}
bool
xpc_bool_get_value(xpc_object_t xbool)
{
struct xpc_object *xo;
xo = xbool;
if (xo == NULL)
return (0);
if (xo->xo_xpc_type == _XPC_TYPE_BOOL)
return (xo->xo_bool);
return (false);
}
xpc_object_t
xpc_int64_create(int64_t value)
{
xpc_u val;
val.i = value;
return _xpc_prim_create(_XPC_TYPE_INT64, val, 1);
}
int64_t
xpc_int64_get_value(xpc_object_t xint)
{
struct xpc_object *xo;
xo = xint;
if (xo == NULL)
return (0);
if (xo->xo_xpc_type == _XPC_TYPE_INT64)
return (xo->xo_int);
return (0);
}
xpc_object_t
xpc_uint64_create(uint64_t value)
{
xpc_u val;
val.ui = value;
return _xpc_prim_create(_XPC_TYPE_UINT64, val, 1);
}
uint64_t
xpc_uint64_get_value(xpc_object_t xuint)
{
struct xpc_object *xo;
xo = xuint;
if (xo == NULL)
return (0);
if (xo->xo_xpc_type == _XPC_TYPE_UINT64)
return (xo->xo_uint);
return (0);
}
xpc_object_t
xpc_double_create(double value)
{
xpc_u val;
val.d = value;
return _xpc_prim_create(_XPC_TYPE_DOUBLE, val, 1);
}
double
xpc_double_get_value(xpc_object_t xdouble)
{
struct xpc_object *xo = xdouble;
if (xo->xo_xpc_type == _XPC_TYPE_DOUBLE)
return (xo->xo_d);
return (0);
}
xpc_object_t
xpc_date_create(int64_t interval)
{
xpc_u val;
val.i = interval;
return _xpc_prim_create(_XPC_TYPE_DATE, val, 1);
}
xpc_object_t
xpc_date_create_from_current(void)
{
xpc_u val;
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
val.ui = *(uint64_t *)&tp;
return _xpc_prim_create(_XPC_TYPE_DATE, val, 1);
}
int64_t
xpc_date_get_value(xpc_object_t xdate)
{
struct xpc_object *xo = xdate;
if (xo == NULL)
return (0);
if (xo->xo_xpc_type == _XPC_TYPE_DATE)
return (xo->xo_int);
return (0);
}
xpc_object_t
xpc_data_create(const void *bytes, size_t length)
{
xpc_u val;
val.ptr = (uintptr_t)bytes;
return _xpc_prim_create(_XPC_TYPE_DATA, val, length);
}
#ifdef MACH
xpc_object_t
xpc_data_create_with_dispatch_data(dispatch_data_t ddata)
{
}
#endif
size_t
xpc_data_get_length(xpc_object_t xdata)
{
struct xpc_object *xo = xdata;
if (xo == NULL)
return (0);
if (xo->xo_xpc_type == _XPC_TYPE_DATA)
return (xo->xo_size);
return (0);
}
const void *
xpc_data_get_bytes_ptr(xpc_object_t xdata)
{
struct xpc_object *xo = xdata;
if (xo == NULL)
return (NULL);
if (xo->xo_xpc_type == _XPC_TYPE_DATA)
return ((const void *)xo->xo_ptr);
return (0);
}
size_t
xpc_data_get_bytes(xpc_object_t xdata, void *buffer, size_t off, size_t length)
{
/* XXX */
return (0);
}
xpc_object_t
xpc_string_create(const char *string)
{
xpc_u val;
val.str = __DECONST(char *, string);
return _xpc_prim_create(_XPC_TYPE_STRING, val, strlen(string));
}
xpc_object_t
xpc_string_create_with_format(const char *fmt, ...)
{
va_list ap;
xpc_u val;
va_start(ap, fmt);
vasprintf(&val.str, fmt, ap);
va_end(ap);
return _xpc_prim_create(_XPC_TYPE_STRING, val, strlen(val.str));
}
xpc_object_t
xpc_string_create_with_format_and_arguments(const char *fmt, va_list ap)
{
xpc_u val;
vasprintf(&val.str, fmt, ap);
return _xpc_prim_create(_XPC_TYPE_STRING, val, strlen(val.str));
}
size_t
xpc_string_get_length(xpc_object_t xstring)
{
struct xpc_object *xo = xstring;
if (xo == NULL)
return (0);
if (xo->xo_xpc_type == _XPC_TYPE_STRING)
return (xo->xo_size);
return (0);
}
const char *
xpc_string_get_string_ptr(xpc_object_t xstring)
{
struct xpc_object *xo = xstring;
if (xo == NULL)
return (NULL);
if (xo->xo_xpc_type == _XPC_TYPE_STRING)
return (xo->xo_str);
return (NULL);
}
xpc_object_t
xpc_uuid_create(const uuid_t uuid)
{
xpc_u val;
memcpy(val.uuid, uuid, sizeof(uuid_t));
return _xpc_prim_create(_XPC_TYPE_UUID, val, 1);
}
const uint8_t *
xpc_uuid_get_bytes(xpc_object_t xuuid)
{
struct xpc_object *xo;
xo = xuuid;
if (xo == NULL)
return (NULL);
if (xo->xo_xpc_type == _XPC_TYPE_UUID)
return ((uint8_t*)&xo->xo_uuid);
return (NULL);
}
xpc_type_t
xpc_get_type(xpc_object_t obj)
{
struct xpc_object *xo;
xo = obj;
return (xpc_typemap[xo->xo_xpc_type]);
}
bool
xpc_equal(xpc_object_t x1, xpc_object_t x2)
{
struct xpc_object *xo1, *xo2;
xo1 = x1;
xo2 = x2;
/* FIXME */
return (false);
}
xpc_object_t
xpc_copy(xpc_object_t obj)
{
struct xpc_object *xo, *xotmp;
const void *newdata;
xo = obj;
switch (xo->xo_xpc_type) {
case _XPC_TYPE_BOOL:
case _XPC_TYPE_INT64:
case _XPC_TYPE_UINT64:
case _XPC_TYPE_DATE:
case _XPC_TYPE_ENDPOINT:
return _xpc_prim_create(xo->xo_xpc_type, xo->xo_u, 1);
case _XPC_TYPE_STRING:
return xpc_string_create(strdup(
xpc_string_get_string_ptr(xo)));
case _XPC_TYPE_DATA:
newdata = xpc_data_get_bytes_ptr(obj);
return (xpc_data_create(newdata,
xpc_data_get_length(obj)));
case _XPC_TYPE_DICTIONARY:
xotmp = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_apply(obj, ^(const char *k, xpc_object_t v) {
xpc_dictionary_set_value(xotmp, k, xpc_copy(v));
return (bool)true;
});
return (xotmp);
case _XPC_TYPE_ARRAY:
xotmp = xpc_array_create(NULL, 0);
xpc_array_apply(obj, ^(size_t idx, xpc_object_t v) {
xpc_array_set_value(xotmp, idx, xpc_copy(v));
return ((bool)true);
});
return (xotmp);
}
return (0);
}
static size_t
xpc_data_hash(const uint8_t *data, size_t length)
{
size_t hash = 5381;
while (length--)
hash = ((hash << 5) + hash) + data[length];
return (hash);
}
size_t
xpc_hash(xpc_object_t obj)
{
struct xpc_object *xo;
__block size_t hash = 0;
xo = obj;
switch (xo->xo_xpc_type) {
case _XPC_TYPE_BOOL:
case _XPC_TYPE_INT64:
case _XPC_TYPE_UINT64:
case _XPC_TYPE_DATE:
case _XPC_TYPE_ENDPOINT:
return ((size_t)xo->xo_u.ui);
case _XPC_TYPE_STRING:
return (xpc_data_hash(
(const uint8_t *)xpc_string_get_string_ptr(obj),
xpc_string_get_length(obj)));
case _XPC_TYPE_DATA:
return (xpc_data_hash(
xpc_data_get_bytes_ptr(obj),
xpc_data_get_length(obj)));
case _XPC_TYPE_DICTIONARY:
xpc_dictionary_apply(obj, ^(const char *k, xpc_object_t v) {
hash ^= xpc_data_hash((const uint8_t *)k, strlen(k));
hash ^= xpc_hash(v);
return ((bool)true);
});
return (hash);
case _XPC_TYPE_ARRAY:
xpc_array_apply(obj, ^(size_t idx, xpc_object_t v) {
hash ^= xpc_hash(v);
return ((bool)true);
});
return (hash);
}
return (0);
}
__private_extern__ const char *
_xpc_get_type_name(xpc_object_t obj)
{
struct xpc_object *xo;
xo = obj;
return (xpc_typestr[xo->xo_xpc_type]);
}