7 #include "SOSAccountPriv.h"
9 #include <utilities/SecCFWrappers.h>
12 // MARK: Generic Value manipulation
15 static inline bool SOSAccountEnsureExpansion(SOSAccountRef account
, CFErrorRef
*error
) {
16 if (!account
->expansion
) {
17 account
->expansion
= CFDictionaryCreateMutableForCFTypes(NULL
);
20 return SecAllocationError(account
->expansion
, error
, CFSTR("Can't Alloc Account Expansion dictionary"));
23 bool SOSAccountClearValue(SOSAccountRef account
, CFStringRef key
, CFErrorRef
*error
) {
24 bool success
= SOSAccountEnsureExpansion(account
, error
);
25 require_quiet(success
, errOut
);
27 CFDictionaryRemoveValue(account
->expansion
, key
);
32 bool SOSAccountSetValue(SOSAccountRef account
, CFStringRef key
, CFTypeRef value
, CFErrorRef
*error
) {
33 if (value
== NULL
) return SOSAccountClearValue(account
, key
, error
);
35 bool success
= SOSAccountEnsureExpansion(account
, error
);
36 require_quiet(success
, errOut
);
38 CFDictionarySetValue(account
->expansion
, key
, value
);
43 CFTypeRef
SOSAccountGetValue(SOSAccountRef account
, CFStringRef key
, CFErrorRef
*error
) {
44 if (!account
->expansion
) {
47 return CFDictionaryGetValue(account
->expansion
, key
);
52 // MARK: Value as Set manipulation
54 bool SOSAccountValueSetContainsValue(SOSAccountRef account
, CFStringRef key
, CFTypeRef value
);
55 void SOSAccountValueUnionWith(SOSAccountRef account
, CFStringRef key
, CFSetRef valuesToUnion
);
56 void SOSAccountValueSubtractFrom(SOSAccountRef account
, CFStringRef key
, CFSetRef valuesToSubtract
);
58 bool SOSAccountValueSetContainsValue(SOSAccountRef account
, CFStringRef key
, CFTypeRef value
) {
59 CFSetRef foundSet
= asSet(SOSAccountGetValue(account
, key
, NULL
), NULL
);
60 return foundSet
&& CFSetContainsValue(foundSet
, value
);
63 void SOSAccountValueUnionWith(SOSAccountRef account
, CFStringRef key
, CFSetRef valuesToUnion
) {
64 CFMutableSetRef unionedSet
= CFSetCreateMutableCopy(kCFAllocatorDefault
, 0, valuesToUnion
);
65 CFSetRef foundSet
= asSet(SOSAccountGetValue(account
, key
, NULL
), NULL
);
67 CFSetUnion(unionedSet
, foundSet
);
69 SOSAccountSetValue(account
, key
, unionedSet
, NULL
);
70 CFReleaseNull(unionedSet
);
73 void SOSAccountValueSubtractFrom(SOSAccountRef account
, CFStringRef key
, CFSetRef valuesToSubtract
) {
74 CFSetRef foundSet
= asSet(SOSAccountGetValue(account
, key
, NULL
), NULL
);
76 CFMutableSetRef subtractedSet
= CFSetCreateMutableCopy(kCFAllocatorDefault
, 0, foundSet
);
77 CFSetSubtract(subtractedSet
, valuesToSubtract
);
78 SOSAccountSetValue(account
, key
, subtractedSet
, NULL
);
79 CFReleaseNull(subtractedSet
);
85 CFStringRef
SOSAccountCopyUUID(SOSAccountRef account
) {
86 CFStringRef uuid
= CFRetainSafe(asString(SOSAccountGetValue(account
, kSOSAccountUUID
, NULL
), NULL
));
88 CFUUIDRef newID
= CFUUIDCreate(kCFAllocatorDefault
);
89 uuid
= CFUUIDCreateString(kCFAllocatorDefault
, newID
);
91 CFErrorRef setError
= NULL
;
92 if (!SOSAccountSetValue(account
, kSOSAccountUUID
, uuid
, &setError
)) {
93 secerror("Failed to set UUID: %@ (%@)", uuid
, setError
);
95 CFReleaseNull(setError
);
101 void SOSAccountEnsureUUID(SOSAccountRef account
) {
102 CFStringRef uuid
= SOSAccountCopyUUID(account
);