-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathxpc_dictionary.c
More file actions
386 lines (313 loc) · 8.3 KB
/
xpc_dictionary.c
File metadata and controls
386 lines (313 loc) · 8.3 KB
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
/*
* 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 "xpc/xpc.h"
#include "xpc_internal.h"
#include "mpack.h"
static struct xpc_object *
mpack2xpc_extension(int8_t type, const char *data)
{
}
struct xpc_object *
mpack2xpc(const mpack_node_t node)
{
xpc_object_t xotmp;
size_t i;
xpc_u val;
switch (mpack_node_type(node)) {
case mpack_type_nil:
xotmp = xpc_null_create();
break;
case mpack_type_int:
val.i = mpack_node_i64(node);
xotmp = xpc_int64_create(val.i);
break;
case mpack_type_uint:
val.ui = mpack_node_u64(node);
xotmp = xpc_uint64_create(val.ui);
break;
case mpack_type_bool:
val.b = mpack_node_bool(node);
xotmp = xpc_bool_create(val.b);
break;
case mpack_type_double:
val.d = mpack_node_double(node);
xotmp = xpc_double_create(val.d);
break;
case mpack_type_str:
val.str = mpack_node_cstr_alloc(node, 65536);
xotmp = xpc_string_create(val.str);
break;
case mpack_type_bin:
break;
case mpack_type_array:
xotmp = xpc_array_create(NULL, 0);
for (i = 0; i < mpack_node_array_length(node); i++) {
xpc_object_t item = mpack2xpc(
mpack_node_array_at(node, i));
xpc_array_append_value(item, xotmp);
}
break;
case mpack_type_map:
xotmp = xpc_dictionary_create(NULL, NULL, 0);
for (i = 0; i < mpack_node_map_count(node); i++) {
char *key = mpack_node_cstr_alloc(
mpack_node_map_key_at(node, i), 1024);
xpc_object_t value = mpack2xpc(
mpack_node_map_value_at(node, i));
xpc_dictionary_set_value(xotmp, key, value);
}
break;
case mpack_type_ext:
xotmp = mpack2xpc_extension(mpack_node_exttype(node),
mpack_node_data(node));
break;
default:
xotmp = NULL;
break;
}
return (xotmp);
}
void
xpc2mpack(mpack_writer_t *writer, xpc_object_t obj)
{
struct xpc_object *xotmp = obj;
switch (xotmp->xo_xpc_type) {
case _XPC_TYPE_DICTIONARY:
mpack_start_map(writer, xpc_dictionary_get_count(obj));
xpc_dictionary_apply(obj, ^(const char *k, xpc_object_t v) {
mpack_write_cstr(writer, k);
xpc2mpack(writer, v);
return ((bool)true);
});
mpack_finish_map(writer);
break;
case _XPC_TYPE_ARRAY:
mpack_start_array(writer, xpc_array_get_count(obj));
xpc_array_apply(obj, ^(size_t index __unused, xpc_object_t v) {
xpc2mpack(writer, v);
return ((bool)true);
});
mpack_finish_map(writer);
break;
case _XPC_TYPE_NULL:
mpack_write_nil(writer);
break;
case _XPC_TYPE_BOOL:
mpack_write_bool(writer, xpc_bool_get_value(obj));
break;
case _XPC_TYPE_INT64:
mpack_write_i64(writer, xpc_int64_get_value(obj));
break;
case _XPC_TYPE_DOUBLE:
mpack_write_double(writer, xpc_double_get_value(obj));
case _XPC_TYPE_UINT64:
mpack_write_u64(writer, xpc_uint64_get_value(obj));
break;
case _XPC_TYPE_STRING:
mpack_write_cstr(writer, xpc_string_get_string_ptr(obj));
break;
case _XPC_TYPE_UUID:
break;
}
}
xpc_object_t
xpc_dictionary_create(const char * const *keys, const xpc_object_t *values,
size_t count)
{
struct xpc_object *xo;
size_t i;
xpc_u val;
xo = _xpc_prim_create(_XPC_TYPE_DICTIONARY, val, count);
for (i = 0; i < count; i++)
xpc_dictionary_set_value(xo, keys[i], values[i]);
return (xo);
}
xpc_object_t
xpc_dictionary_create_reply(xpc_object_t original)
{
struct xpc_object *xo_orig;
xo_orig = original;
if ((xo_orig->xo_flags & _XPC_FROM_WIRE) == 0)
return (NULL);
return xpc_dictionary_create(NULL, NULL, 0);
}
#ifdef MACH
void
xpc_dictionary_get_audit_token(xpc_object_t xdict, audit_token_t *token)
{
struct xpc_object *xo;
xo = xdict;
if (xo->xo_audit_token != NULL)
memcpy(token, xo->xo_audit_token, sizeof(*token));
}
void
xpc_dictionary_set_mach_recv(xpc_object_t xdict, const char *key,
mach_port_t port)
{
struct xpc_object *xo = xdict;
struct xpc_object *xotmp;
xpc_u val;
val.port = port;
xotmp = _xpc_prim_create(_XPC_TYPE_ENDPOINT, val, 0);
xpc_dictionary_set_value(xdict, key, xotmp);
}
void
xpc_dictionary_set_mach_send(xpc_object_t xdict, const char *key,
mach_port_t port)
{
struct xpc_object *xotmp;
xpc_u val;
val.port = port;
xotmp = _xpc_prim_create(_XPC_TYPE_ENDPOINT, val, 0);
xpc_dictionary_set_value(xdict, key, xotmp);
}
mach_port_t
xpc_dictionary_copy_mach_send(xpc_object_t xdict, const char *key)
{
struct xpc_object *xo;
const struct xpc_object *xotmp;
}
#endif
void
xpc_dictionary_set_value(xpc_object_t xdict, const char *key,
xpc_object_t value)
{
struct xpc_object *xo;
struct xpc_dict_head *head;
struct xpc_dict_pair *pair;
xo = xdict;
head = &xo->xo_dict;
TAILQ_FOREACH(pair, head, xo_link) {
if (!strcmp(pair->key, key)) {
pair->value = value;
return;
}
}
xo->xo_size++;
pair = malloc(sizeof(struct xpc_dict_pair));
pair->key = key;
pair->value = value;
TAILQ_INSERT_TAIL(&xo->xo_dict, pair, xo_link);
xpc_retain(value);
}
xpc_object_t
xpc_dictionary_get_value(xpc_object_t xdict, const char *key)
{
struct xpc_object *xo;
struct xpc_dict_head *head;
struct xpc_dict_pair *pair;
xo = xdict;
head = &xo->xo_dict;
TAILQ_FOREACH(pair, head, xo_link) {
if (!strcmp(pair->key, key))
return (pair->value);
}
return (NULL);
}
size_t
xpc_dictionary_get_count(xpc_object_t xdict)
{
struct xpc_object *xo;
xo = xdict;
return (xo->xo_size);
}
void
xpc_dictionary_set_bool(xpc_object_t xdict, const char *key, bool value)
{;
struct xpc_object *xo, *xotmp;
xo = xdict;
xotmp = xpc_bool_create(value);
xpc_dictionary_set_value(xdict, key, xotmp);
}
void
xpc_dictionary_set_int64(xpc_object_t xdict, const char *key, int64_t value)
{
struct xpc_object *xo, *xotmp;
xo = xdict;
xotmp = xpc_int64_create(value);
xpc_dictionary_set_value(xdict, key, xotmp);
}
void
xpc_dictionary_set_uint64(xpc_object_t xdict, const char *key, uint64_t value)
{
struct xpc_object *xo, *xotmp;
xo = xdict;
xotmp = xpc_uint64_create(value);
xpc_dictionary_set_value(xdict, key, xotmp);
}
void
xpc_dictionary_set_string(xpc_object_t xdict, const char *key,
const char *value)
{
struct xpc_object *xo, *xotmp;
xo = xdict;
xotmp = xpc_string_create(value);
xpc_dictionary_set_value(xdict, key, xotmp);
}
bool
xpc_dictionary_get_bool(xpc_object_t xdict, const char *key)
{
xpc_object_t xo;
xo = xpc_dictionary_get_value(xdict, key);
return (xpc_bool_get_value(xo));
}
int64_t
xpc_dictionary_get_int64(xpc_object_t xdict, const char *key)
{
xpc_object_t xo;
xo = xpc_dictionary_get_value(xdict, key);
return (xpc_int64_get_value(xo));
}
uint64_t
xpc_dictionary_get_uint64(xpc_object_t xdict, const char *key)
{
xpc_object_t xo;
xo = xpc_dictionary_get_value(xdict, key);
return (xpc_uint64_get_value(xo));
}
const char *
xpc_dictionary_get_string(xpc_object_t xdict, const char *key)
{
xpc_object_t xo;
xo = xpc_dictionary_get_value(xdict, key);
return (xpc_string_get_string_ptr(xo));
}
bool
xpc_dictionary_apply(xpc_object_t xdict, xpc_dictionary_applier_t applier)
{
struct xpc_object *xo, *xotmp;
struct xpc_dict_head *head;
struct xpc_dict_pair *pair;
xo = xdict;
head = &xo->xo_dict;
TAILQ_FOREACH(pair, head, xo_link) {
if (!applier(pair->key, pair->value))
return (false);
}
return (true);
}