]>
git.saurik.com Git - apple/security.git/blob - authd/authutilities.c
1 /* Copyright (c) 2012 Apple Inc. All rights reserved. */
3 #include "authutilities.h"
4 #include "authd_private.h"
7 #include <AssertMacros.h>
12 SerializeItemSet(const AuthorizationItemSet
* itemSet
)
14 xpc_object_t set
= NULL
;
15 require_quiet(itemSet
!= NULL
, done
);
16 require_quiet(itemSet
->count
!= 0, done
);
18 set
= xpc_array_create(NULL
, 0);
19 require(set
!= NULL
, done
);
21 for (uint32_t i
= 0; i
< itemSet
->count
; i
++) {
22 xpc_object_t item
= xpc_dictionary_create(NULL
, NULL
, 0);
23 require(item
!= NULL
, done
);
25 xpc_dictionary_set_string(item
, AUTH_XPC_ITEM_NAME
, itemSet
->items
[i
].name
);
26 xpc_dictionary_set_uint64(item
, AUTH_XPC_ITEM_FLAGS
, itemSet
->items
[i
].flags
);
27 xpc_dictionary_set_data(item
, AUTH_XPC_ITEM_VALUE
, itemSet
->items
[i
].value
, itemSet
->items
[i
].valueLength
);
28 xpc_array_set_value(set
, XPC_ARRAY_APPEND
, item
);
36 AuthorizationItemSet
*
37 DeserializeItemSet(const xpc_object_t data
)
39 AuthorizationItemSet
* set
= NULL
;
40 require_quiet(data
!= NULL
, done
);
42 require(xpc_get_type(data
) == XPC_TYPE_ARRAY
, done
);
44 set
= (AuthorizationItemSet
*)calloc(1u, sizeof(AuthorizationItemSet
));
45 require(set
!= NULL
, done
);
47 set
->count
= (uint32_t)xpc_array_get_count(data
);
49 set
->items
= (AuthorizationItem
*)calloc(set
->count
, sizeof(AuthorizationItem
));
50 require_action(set
->items
!= NULL
, done
, set
->count
= 0);
52 xpc_array_apply(data
, ^bool(size_t index
, xpc_object_t value
) {
53 require(xpc_get_type(value
) == XPC_TYPE_DICTIONARY
, done
);
55 const char * name
= xpc_dictionary_get_string(value
, AUTH_XPC_ITEM_NAME
);
57 nameLen
= strlen(name
) + 1;
58 set
->items
[index
].name
= calloc(1u, nameLen
);
59 require(set
->items
[index
].name
!= NULL
, done
);
61 strlcpy((char*)set
->items
[index
].name
, name
, nameLen
);
63 set
->items
[index
].flags
= (uint32_t)xpc_dictionary_get_uint64(value
, AUTH_XPC_ITEM_FLAGS
);
65 const void * valueData
= xpc_dictionary_get_data(value
, AUTH_XPC_ITEM_VALUE
, &len
);
66 set
->items
[index
].valueLength
= len
;
68 set
->items
[index
].value
= calloc(1u, len
);
69 require(set
->items
[index
].value
!= NULL
, done
);
71 memcpy(set
->items
[index
].value
, valueData
, len
);
85 void FreeItemSet(AuthorizationItemSet
* itemSet
)
87 if (!itemSet
) { return; }
89 for(uint32_t i
= 0; i
< itemSet
->count
; i
++ ) {
90 if (itemSet
->items
[i
].name
) {
91 free((void*)itemSet
->items
[i
].name
);
93 if (itemSet
->items
[i
].value
) {
94 free(itemSet
->items
[i
].value
);
105 _copy_cf_string(CFTypeRef str
, const char * defaultValue
)
107 char * result
= NULL
;
108 require(str
!= NULL
, done
);
109 require(CFGetTypeID(str
) == CFStringGetTypeID(), done
);
111 CFIndex length
= CFStringGetLength(str
);
112 CFIndex size
= CFStringGetMaximumSizeForEncoding(length
, kCFStringEncodingUTF8
) + 1;
114 result
= (char*)calloc(1u, (size_t)size
);
115 check(result
!= NULL
);
117 if (!CFStringGetCString(str
, result
, size
, kCFStringEncodingUTF8
)) {
122 if (result
== NULL
&& defaultValue
) {
123 size_t len
= strlen(defaultValue
);
124 result
= (char*)calloc(1u, len
);
125 check(result
!= NULL
);
127 strlcpy(result
, defaultValue
, len
);
134 _get_cf_int(CFTypeRef num
, int64_t defaultValue
)
136 int64_t result
= defaultValue
;
137 require(num
!= NULL
, done
);
138 require(CFGetTypeID(num
) == CFNumberGetTypeID(), done
);
140 if (!CFNumberGetValue(num
, kCFNumberSInt64Type
, &result
)) {
141 result
= defaultValue
;
149 _get_cf_bool(CFTypeRef value
, bool defaultValue
)
151 bool result
= defaultValue
;
152 require(value
!= NULL
, done
);
153 require(CFGetTypeID(value
) == CFBooleanGetTypeID(), done
);
155 result
= CFBooleanGetValue(value
);
162 _compare_string(const char * str1
, const char * str2
)
164 if (!(str1
== str2
)) { // compare null or same pointer
165 if (str1
&& str2
) { // check both are non null
166 if (strcasecmp(str1
, str2
) != 0) { // compare strings
167 return false; // return false if not equal
170 return false; // return false if one null
178 _copy_string(const char * str
)
180 char * result
= NULL
;
181 require(str
!= NULL
, done
);
183 size_t len
= strlen(str
) + 1;
184 result
= calloc(1u, len
);
185 require(result
!= NULL
, done
);
187 strlcpy(result
, str
, len
);
194 _copy_data(const void * data
, size_t dataLen
)
196 void * result
= NULL
;
197 require(data
!= NULL
, done
);
199 result
= calloc(1u, dataLen
);
200 require(result
!= NULL
, done
);
202 memcpy(result
, data
, dataLen
);
208 bool _cf_set_iterate(CFSetRef set
, bool(^iterator
)(CFTypeRef value
))
211 CFTypeRef
* values
= NULL
;
213 require(set
!= NULL
, done
);
215 CFIndex count
= CFSetGetCount(set
);
216 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
217 require(values
!= NULL
, done
);
219 CFSetGetValues(set
, values
);
220 for (CFIndex i
= 0; i
< count
; i
++) {
221 result
= iterator(values
[i
]);
232 bool _cf_bag_iterate(CFBagRef bag
, bool(^iterator
)(CFTypeRef value
))
235 CFTypeRef
* values
= NULL
;
237 require(bag
!= NULL
, done
);
239 CFIndex count
= CFBagGetCount(bag
);
240 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
241 require(values
!= NULL
, done
);
243 CFBagGetValues(bag
, values
);
244 for (CFIndex i
= 0; i
< count
; i
++) {
245 result
= iterator(values
[i
]);
256 bool _cf_dictionary_iterate(CFDictionaryRef dict
, bool(^iterator
)(CFTypeRef key
, CFTypeRef value
))
259 CFTypeRef
* keys
= NULL
;
260 CFTypeRef
* values
= NULL
;
262 require(dict
!= NULL
, done
);
264 CFIndex count
= CFDictionaryGetCount(dict
);
265 keys
= calloc((size_t)count
, sizeof(CFTypeRef
));
266 require(keys
!= NULL
, done
);
268 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
269 require(values
!= NULL
, done
);
271 CFDictionaryGetKeysAndValues(dict
, keys
, values
);
272 for (CFIndex i
= 0; i
< count
; i
++) {
273 result
= iterator(keys
[i
], values
[i
]);