2 * Copyright (c) 2011-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #ifndef _SECCFWRAPPERS_H_
26 #define _SECCFWRAPPERS_H_
28 #include <CoreFoundation/CFRuntime.h>
29 #include <CoreFoundation/CoreFoundation.h>
31 #include <utilities/SecCFRelease.h>
32 #include <utilities/debugging.h>
35 #include <dispatch/dispatch.h>
40 #include <corecrypto/ccdigest.h>
43 // Convenience routines.
47 // Macros for the pattern
49 // typedef struct _privateNewClass* NewClassRef;
51 // struct _privateNewClass {
52 // CFRuntimeBase _base;
53 // ... class additions
56 // kClassNameRegisterClass
59 // ClassNameGetTypeID()
61 // CFGiblisFor(NewClass);
63 // .. define NewClassDestroy
64 // .. define NewClassCopyDescription
66 // .. use CFTypeAllocate(NewClass, _privateNewClass, allocator);
70 // Call this to create a function that returns a singleton instance of type stype,
71 // which is initialized once by calling doThisOnce, with result in its context. Upon
72 // completion body should assign to *result.
74 extern CFStringRef kSecDebugFormatOption
;
76 extern CFDictionaryRef
SecGetDebugDescriptionFormatOptions(void);
79 #define CFGiblisGetSingleton(returnType, giblisClassName, result, doThisOnce) \
80 returnType giblisClassName(void); \
81 returnType giblisClassName(void) { \
82 static dispatch_once_t s##giblisClassName##Once; \
83 static returnType s##giblisClassName##Singleton; \
84 returnType *result = &s##giblisClassName##Singleton; \
85 dispatch_once(&s##giblisClassName##Once, doThisOnce); \
86 return s##giblisClassName##Singleton; \
89 #define CFGiblisWithFunctions(gibliClassName, init_func, copy_func, finalize_func, equal_func, hash_func, copyFormattingDesc_func, copyDebugDesc_func, reclaim_func, refcount_func, run_once_block) \
90 CFGiblisGetSingleton(CFTypeID, gibliClassName##GetTypeID, typeID, (^{ \
91 void(^_onceBlock)() = (run_once_block); \
92 static const CFRuntimeClass s##gibliClassName##Class = { \
93 .version = (reclaim_func == NULL ? 0 : _kCFRuntimeResourcefulObject) \
94 | (refcount_func == NULL ? 0 : _kCFRuntimeCustomRefCount), \
95 .className = #gibliClassName, \
98 .finalize = finalize_func, \
99 .equal = equal_func, \
101 .copyFormattingDesc = copyFormattingDesc_func, \
102 .copyDebugDesc = copyDebugDesc_func, \
103 .reclaim = reclaim_func, \
104 .refcount = refcount_func, \
106 *typeID = _CFRuntimeRegisterClass(&s##gibliClassName##Class); \
111 #define CFGiblisWithHashFor(gibliClassName) \
112 static CFStringRef gibliClassName##CopyFormatDescription(CFTypeRef cf, CFDictionaryRef formatOptions); \
113 static void gibliClassName##Destroy(CFTypeRef cf); \
114 static Boolean gibliClassName##Compare(CFTypeRef lhs, CFTypeRef rhs); \
115 static CFHashCode gibliClassName##Hash(CFTypeRef cf); \
116 static CFStringRef gibliClassName##CopyDescription(CFTypeRef cf){\
117 return gibliClassName##CopyFormatDescription(cf, SecGetDebugDescriptionFormatOptions());\
120 CFGiblisWithFunctions(gibliClassName, NULL, NULL, gibliClassName##Destroy, gibliClassName##Compare, gibliClassName##Hash, gibliClassName##CopyFormatDescription, gibliClassName##CopyDescription, NULL, NULL, NULL)
122 #define CFGiblisWithCompareFor(gibliClassName) \
123 static CFStringRef gibliClassName##CopyFormatDescription(CFTypeRef cf, CFDictionaryRef formatOptions); \
124 static void gibliClassName##Destroy(CFTypeRef cf); \
125 static Boolean gibliClassName##Compare(CFTypeRef lhs, CFTypeRef rhs); \
126 static CFStringRef gibliClassName##CopyDescription(CFTypeRef cf){\
127 return gibliClassName##CopyFormatDescription(cf, SecGetDebugDescriptionFormatOptions());\
130 CFGiblisWithFunctions(gibliClassName, NULL, NULL, gibliClassName##Destroy, gibliClassName##Compare, NULL, gibliClassName##CopyFormatDescription, gibliClassName##CopyDescription, NULL, NULL, NULL)
133 #define CFGiblisFor(gibliClassName) \
134 static CFStringRef gibliClassName##CopyFormatDescription(CFTypeRef cf, CFDictionaryRef formatOptions); \
135 static void gibliClassName##Destroy(CFTypeRef cf); \
136 static CFStringRef gibliClassName##CopyDescription(CFTypeRef cf){\
137 return gibliClassName##CopyFormatDescription(cf, SecGetDebugDescriptionFormatOptions());\
140 CFGiblisWithFunctions(gibliClassName, NULL, NULL, gibliClassName##Destroy, NULL, NULL, gibliClassName##CopyFormatDescription, gibliClassName##CopyDescription, NULL, NULL, NULL)
142 #define CFTypeAllocateWithSpace(classType, space, allocator) \
143 (classType##Ref) _CFRuntimeCreateInstance(allocator, classType##GetTypeID(), space, NULL)
145 #define CFTypeAllocate(classType, internalType, allocator) \
146 CFTypeAllocateWithSpace(classType, sizeof(internalType) - sizeof(CFRuntimeBase), allocator)
152 void withStringOfAbsoluteTime(CFAbsoluteTime at
, void (^action
)(CFStringRef decription
));
156 // Call block function
160 static void apply_block_1(const void *value
, void *context
)
162 return ((void (^)(const void *value
))context
)(value
);
165 static void apply_block_2(const void *key
, const void *value
, void *context
)
167 return ((void (^)(const void *key
, const void *value
))context
)(key
, value
);
174 static inline bool CFEqualSafe(CFTypeRef left
, CFTypeRef right
)
176 if (left
== NULL
|| right
== NULL
)
177 return left
== right
;
179 return CFEqual(left
, right
);
187 static void fprint_string(FILE *file
, CFStringRef string
) {
189 CFRange range
= { .location
= 0 };
190 range
.length
= CFStringGetLength(string
);
191 while (range
.length
> 0) {
192 CFIndex bytesUsed
= 0;
193 CFIndex converted
= CFStringGetBytes(string
, range
, kCFStringEncodingUTF8
, 0, false, buf
, sizeof(buf
), &bytesUsed
);
194 fwrite(buf
, 1, bytesUsed
, file
);
195 range
.length
-= converted
;
196 range
.location
+= converted
;
200 static inline void cffprint_v(FILE *file
, CFStringRef fmt
, va_list args
) {
201 CFStringRef line
= CFStringCreateWithFormatAndArguments(NULL
, NULL
, fmt
, args
);
202 fprint_string(file
, line
);
206 static inline void cffprint_c_v(FILE *file
, const char *fmt
, va_list args
) {
207 CFStringRef cffmt
= CFStringCreateWithCString(kCFAllocatorDefault
, fmt
, kCFStringEncodingUTF8
);
208 cffprint_v(file
, cffmt
, args
);
212 static void cffprint(FILE *file
, CFStringRef fmt
, ...) CF_FORMAT_FUNCTION(2,0);
213 static inline void cffprint(FILE *file
, CFStringRef fmt
, ...) {
216 cffprint_v(file
, fmt
, args
);
224 /* Return false if possibleError is set. Propagates possibleError into *error
225 if *error is NULL, otherwise releases possibleError. */
227 bool CFErrorPropagate(CFErrorRef possibleError CF_CONSUMED
, CFErrorRef
*error
) {
229 if (error
&& !*error
) {
230 *error
= possibleError
;
232 CFRelease(possibleError
);
243 static inline CFNumberRef
CFNumberCreateWithCFIndex(CFAllocatorRef allocator
, CFIndex value
)
245 return CFNumberCreate(allocator
, kCFNumberCFIndexType
, &value
);
252 static inline CFMutableDataRef
CFDataCreateMutableWithScratch(CFAllocatorRef allocator
, CFIndex size
) {
253 CFMutableDataRef result
= CFDataCreateMutable(allocator
, 0);
254 CFDataSetLength(result
, size
);
259 static inline void CFDataAppend(CFMutableDataRef appendTo
, CFDataRef dataToAppend
)
261 CFDataAppendBytes(appendTo
, CFDataGetBytePtr(dataToAppend
), CFDataGetLength(dataToAppend
));
264 static inline CFDataRef
CFDataCreateReferenceFromRange(CFAllocatorRef allocator
, CFDataRef sourceData
, CFRange range
)
266 return CFDataCreateWithBytesNoCopy(allocator
,
267 CFDataGetBytePtr(sourceData
) + range
.location
, range
.length
,
271 static inline CFDataRef
CFDataCreateCopyFromRange(CFAllocatorRef allocator
, CFDataRef sourceData
, CFRange range
)
273 return CFDataCreate(allocator
, CFDataGetBytePtr(sourceData
) + range
.location
, range
.length
);
276 CFDataRef
CFDataCreateWithRandomBytes(size_t len
);
278 static inline uint8_t* CFDataIncreaseLengthAndGetMutableBytes(CFMutableDataRef data
, CFIndex extraLength
)
280 CFIndex startOffset
= CFDataGetLength(data
);
282 CFDataIncreaseLength(data
, extraLength
);
284 return CFDataGetMutableBytePtr(data
) + startOffset
;
287 static inline uint8_t* CFDataGetMutablePastEndPtr(CFMutableDataRef theData
)
289 return CFDataGetMutableBytePtr(theData
) + CFDataGetLength(theData
);
292 static inline const uint8_t* CFDataGetPastEndPtr(CFDataRef theData
) {
293 return CFDataGetBytePtr(theData
) + CFDataGetLength(theData
);
296 static inline CFComparisonResult
CFDataCompare(CFDataRef left
, CFDataRef right
)
298 const size_t left_size
= CFDataGetLength(left
);
299 const size_t right_size
= CFDataGetLength(right
);
300 const size_t shortest
= (left_size
<= right_size
) ? left_size
: right_size
;
302 int comparison
= memcmp(CFDataGetBytePtr(left
), CFDataGetBytePtr(right
), shortest
);
304 if (comparison
> 0 || (comparison
== 0 && left_size
> right_size
))
305 return kCFCompareGreaterThan
;
306 else if (comparison
< 0 || (comparison
== 0 && left_size
< right_size
))
307 return kCFCompareLessThan
;
309 return kCFCompareEqualTo
;
312 static inline CFDataRef
CFDataCreateWithHash(CFAllocatorRef allocator
, const struct ccdigest_info
*di
, const uint8_t *buffer
, const uint8_t length
) {
313 CFMutableDataRef result
= CFDataCreateMutableWithScratch(allocator
, di
->output_size
);
315 ccdigest(di
, length
, buffer
, CFDataGetMutableBytePtr(result
));
321 static inline CFDataRef
CFDataCreateCopyFromPositions(CFAllocatorRef allocator
, CFDataRef source
, CFIndex start
, CFIndex end
)
323 return CFDataCreateCopyFromRange(allocator
, source
, CFRangeMake(start
, end
- start
));
332 // Turn a CFString into an allocated UTF8-encoded C string.
334 static inline char *CFStringToCString(CFStringRef inStr
)
337 return (char *)strdup("");
338 CFRetain(inStr
); // compensate for release on exit
340 // need to extract into buffer
341 CFIndex length
= CFStringGetLength(inStr
); // in 16-bit character units
342 size_t len
= CFStringGetMaximumSizeForEncoding(length
, kCFStringEncodingUTF8
);
343 char *buffer
= (char *)malloc(len
); // pessimistic
344 if (!CFStringGetCString(inStr
, buffer
, len
, kCFStringEncodingUTF8
))
351 // runs operation with inStr as a zero terminated C string
352 // in utf8 encoding passed to the operation block.
353 void CFStringPerformWithCString(CFStringRef inStr
, void(^operation
)(const char *utf8Str
));
355 // runs operation with inStr as a zero terminated C string
356 // in utf8 passed to the operation block, the length of
357 // the string is also provided to the block.
358 void CFStringPerformWithCStringAndLength(CFStringRef inStr
, void(^operation
)(const char *utf8Str
, size_t utf8Length
));
360 void CFStringPerformWithUTF8CFData(CFStringRef inStr
, void (^operation
)(CFDataRef stringAsData
));
362 #include <CommonNumerics/CommonCRC.h>
364 static inline void CFStringAppendEncryptedData(CFMutableStringRef s
, CFDataRef edata
)
366 const uint8_t *bytes
= CFDataGetBytePtr(edata
);
367 CFIndex len
= CFDataGetLength(edata
);
368 CFStringAppendFormat(s
, 0, CFSTR("%04lx:"), len
);
370 for (CFIndex ix
= 0; ix
< len
; ++ix
) {
371 CFStringAppendFormat(s
, 0, CFSTR("%02X"), bytes
[ix
]);
375 CNCRC(kCN_CRC_64_ECMA_182
, bytes
+8, len
-8, &crc
);
376 for (CFIndex ix
= 0; ix
< 8; ++ix
) {
377 CFStringAppendFormat(s
, 0, CFSTR("%02X"), bytes
[ix
]);
379 CFStringAppendFormat(s
, 0, CFSTR("...|%08llx"), crc
);
383 static inline void CFStringAppendHexData(CFMutableStringRef s
, CFDataRef data
) {
384 const uint8_t *bytes
= CFDataGetBytePtr(data
);
385 CFIndex len
= CFDataGetLength(data
);
386 for (CFIndex ix
= 0; ix
< len
; ++ix
) {
387 CFStringAppendFormat(s
, 0, CFSTR("%02X"), bytes
[ix
]);
391 static inline CF_RETURNS_RETAINED CFStringRef
CFDataCopyHexString(CFDataRef data
) {
392 CFMutableStringRef hexString
= CFStringCreateMutable(kCFAllocatorDefault
, 2 * CFDataGetLength(data
));
393 CFStringAppendHexData(hexString
, data
);
397 static inline void CFDataPerformWithHexString(CFDataRef data
, void (^operation
)(CFStringRef dataString
)) {
398 CFStringRef hexString
= CFDataCopyHexString(data
);
399 operation(hexString
);
400 CFRelease(hexString
);
403 static inline void BufferPerformWithHexString(const UInt8
*bytes
, CFIndex length
, void (^operation
)(CFStringRef dataString
)) {
404 CFDataRef bufferAsData
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, bytes
, length
, kCFAllocatorNull
);
406 CFDataPerformWithHexString(bufferAsData
, operation
);
408 CFReleaseNull(bufferAsData
);
413 static inline void CFStringWriteToFile(CFStringRef inStr
, FILE* file
)
415 CFStringPerformWithCStringAndLength(inStr
, ^(const char *utf8Str
, size_t utf8Length
) {
416 fwrite(utf8Str
, 1, utf8Length
, file
);
420 static inline void CFStringWriteToFileWithNewline(CFStringRef inStr
, FILE* file
)
422 CFStringWriteToFile(inStr
, file
);
427 // MARK: CFCollectionHelpers
431 const void *SecCFRetainForCollection(CFAllocatorRef allocator
, const void *value
)
433 return CFRetain(value
);
437 void SecCFReleaseForCollection(CFAllocatorRef allocator
, const void *value
)
443 // MARK: CFArray Helpers
446 static inline CFIndex
CFArrayRemoveAllValue(CFMutableArrayRef array
, const void* value
)
448 CFIndex position
= kCFNotFound
;
449 CFIndex numberRemoved
= 0;
451 position
= CFArrayGetFirstIndexOfValue(array
, CFRangeMake(0, CFArrayGetCount(array
)), value
);
452 while (position
!= kCFNotFound
) {
453 CFArrayRemoveValueAtIndex(array
, position
);
455 position
= CFArrayGetFirstIndexOfValue(array
, CFRangeMake(0, CFArrayGetCount(array
)), value
);
458 return numberRemoved
;
461 #define CFArrayForEachC(array, value) for (CFIndex _aCount = CFArrayGetCount(array), _aIX = 0;value = (__typeof__(value))(_aIX < _aCount ? CFArrayGetValueAtIndex(array, _aIX) : 0), _aIX < _aCount; ++_aIX)
463 static inline void CFArrayForEach(CFArrayRef array
, void (^operation
)(const void *value
)) {
464 CFArrayApplyFunction(array
, CFRangeMake(0, CFArrayGetCount(array
)), apply_block_1
, operation
);
467 static inline void CFArrayForEachReverse(CFArrayRef array
, void (^operation
)(const void *value
)) {
468 for(CFIndex count
= CFArrayGetCount(array
); count
> 0; --count
) {
469 operation(CFArrayGetValueAtIndex(array
, count
- 1));
473 static inline const void *CFArrayGetValueMatching(CFArrayRef array
, bool (^match
)(const void *value
)) {
474 CFIndex i
, n
= CFArrayGetCount(array
);
475 for (i
= 0; i
< n
; ++i
) {
476 const void *value
= CFArrayGetValueAtIndex(array
, i
);
484 static inline bool CFArrayHasValueMatching(CFArrayRef array
, bool (^match
)(const void *value
)) {
485 return CFArrayGetValueMatching(array
, match
) != NULL
;
488 static inline void CFMutableArrayModifyValues(CFMutableArrayRef array
, const void * (^process
)(const void *value
)) {
489 CFIndex i
, n
= CFArrayGetCount(array
);
490 for (i
= 0; i
< n
; ++i
) {
491 const void *value
= CFArrayGetValueAtIndex(array
, i
);
492 CFArraySetValueAtIndex(array
, i
, process(value
));
496 static inline void CFArraySubtract(CFMutableArrayRef from
, CFArrayRef remove
) {
497 if (remove
&& from
) {
498 CFArrayForEach(remove
, ^(const void *value
) {
499 CFArrayRemoveAllValue(from
, value
);
504 static inline CFMutableArrayRef
CFArrayCreateDifference(CFAllocatorRef alloc
, CFArrayRef set
, CFArrayRef remove
) {
505 CFMutableArrayRef result
;
507 result
= CFArrayCreateMutable(alloc
, 0, &kCFTypeArrayCallBacks
);
509 result
= CFArrayCreateMutableCopy(alloc
, 0, set
);
511 CFArraySubtract(result
, remove
);
518 // MARK: CFArray creation Var args helper functions.
520 static inline CFArrayRef
CFArrayCreateCountedForVC(CFAllocatorRef allocator
, const CFArrayCallBacks
*cbs
, CFIndex entries
, va_list args
)
522 const void *values
[entries
? entries
: 1];
523 for (CFIndex currentValue
= 0; currentValue
< entries
; ++currentValue
)
525 values
[currentValue
] = va_arg(args
, void*);
527 if (values
[currentValue
] == NULL
)
528 values
[currentValue
] = kCFNull
;
531 return CFArrayCreate(allocator
, values
, entries
, cbs
);
534 static inline CFArrayRef
CFArrayCreateForVC(CFAllocatorRef allocator
, const CFArrayCallBacks
*cbs
, va_list args
)
537 va_copy(count
, args
);
540 while (NULL
!= va_arg(count
, void*)) {
544 return CFArrayCreateCountedForVC(allocator
, cbs
, entries
, args
);
551 // MARK: CFArray of CFTypes support
554 static inline CFMutableArrayRef
CFArrayCreateMutableForCFTypesWithCapacity(CFAllocatorRef allocator
, CFIndex capacity
)
556 return CFArrayCreateMutable(allocator
, capacity
, &kCFTypeArrayCallBacks
);
560 static inline CFMutableArrayRef
CFArrayCreateMutableForCFTypes(CFAllocatorRef allocator
)
562 return CFArrayCreateMutable(allocator
, 0, &kCFTypeArrayCallBacks
);
565 static inline CFArrayRef
CFArrayCreateForCFTypes(CFAllocatorRef allocator
, ...)
568 va_start(args
, allocator
);
570 return CFArrayCreateForVC(allocator
, &kCFTypeArrayCallBacks
, args
);
574 static inline CFArrayRef
CFArrayCreateCountedForCFTypes(CFAllocatorRef allocator
, CFIndex entries
, ...)
577 va_start(args
, entries
);
579 return CFArrayCreateCountedForVC(allocator
, &kCFTypeArrayCallBacks
, entries
, args
);
582 static inline CFArrayRef
CFArrayCreateCountedForCFTypesV(CFAllocatorRef allocator
, CFIndex entries
, va_list args
)
584 return CFArrayCreateCountedForVC(allocator
, &kCFTypeArrayCallBacks
, entries
, args
);
588 // MARK: CFDictionary of CFTypes helpers
591 static inline CFDictionaryRef
CFDictionaryCreateCountedForCFTypesV(CFAllocatorRef allocator
, CFIndex entries
, va_list args
)
593 const void *keys
[entries
];
594 const void *values
[entries
];
596 for(CFIndex currentValue
= 0; currentValue
< entries
; ++currentValue
)
598 keys
[currentValue
] = va_arg(args
, void*);
599 values
[currentValue
] = va_arg(args
, void*);
601 if (values
[currentValue
] == NULL
)
602 values
[currentValue
] = kCFNull
;
605 return CFDictionaryCreate(allocator
, keys
, values
, entries
,
606 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
609 static inline CFDictionaryRef
CFDictionaryCreateForCFTypes(CFAllocatorRef allocator
, ...)
612 va_start(args
, allocator
);
615 while (NULL
!= va_arg(args
, void*)) {
617 (void) va_arg(args
, void*);
622 va_start(args
, allocator
);
624 return CFDictionaryCreateCountedForCFTypesV(allocator
, entries
, args
);
628 static inline CFDictionaryRef
CFDictionaryCreateCountedForCFTypes(CFAllocatorRef allocator
, CFIndex entries
, ...)
631 va_start(args
, entries
);
633 return CFDictionaryCreateCountedForCFTypesV(allocator
, entries
, args
);
636 static inline CFMutableDictionaryRef
CFDictionaryCreateMutableForCFTypes(CFAllocatorRef allocator
)
638 return CFDictionaryCreateMutable(allocator
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
641 static inline CFMutableDictionaryRef
CFDictionaryCreateMutableForCFTypesWith(CFAllocatorRef allocator
, ...)
643 CFMutableDictionaryRef result
= CFDictionaryCreateMutableForCFTypes(allocator
);
646 va_start(args
, allocator
);
648 void* key
= va_arg(args
, void*);
650 while (key
!= NULL
) {
651 CFDictionarySetValue(result
, key
, va_arg(args
, void*));
652 key
= va_arg(args
, void*);
659 // MARK: CFSet Helpers
662 static inline CFMutableSetRef
CFSetCreateMutableForCFTypes(CFAllocatorRef allocator
)
664 return CFSetCreateMutable(allocator
, 0, &kCFTypeSetCallBacks
);
667 static inline void CFSetForEach(CFSetRef set
, void (^operation
)(const void *value
)) {
668 CFSetApplyFunction(set
, apply_block_1
, operation
);
671 static inline void CFSetUnion(CFMutableSetRef set
, CFSetRef unionWith
) {
672 CFSetForEach(unionWith
, ^(const void *value
) {
673 CFSetSetValue(set
, value
);
677 static inline void CFSetSubtract(CFMutableSetRef set
, CFSetRef subtract
) {
678 CFSetForEach(subtract
, ^(const void *value
) {
679 CFSetRemoveValue(set
, value
);
683 static inline void CFSetSetValues(CFMutableSetRef set
, CFArrayRef valuesToSet
) {
684 CFArrayForEach(valuesToSet
, ^(const void *value
) {
685 CFSetSetValue(set
, value
);
689 static inline CFMutableArrayRef
CFSetCopyValues(CFSetRef set
) {
690 CFMutableArrayRef values
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
692 CFSetForEach(set
, ^(const void *value
) {
693 CFArrayAppendValue(values
, value
);
699 static inline CFMutableSetRef
CFSetCreateIntersection(CFAllocatorRef allocator
, CFSetRef a
, CFSetRef b
) {
700 CFMutableSetRef result
= CFSetCreateMutableCopy(allocator
, 0, a
);
702 CFSetRemoveAllValues(result
);
703 CFSetForEach(a
, ^(const void *value
) {
704 if (CFSetContainsValue(b
, value
)) {
705 CFSetAddValue(result
, value
);
712 static inline CFSetRef
CFSetCreateCopyOfArrayForCFTypes(CFArrayRef array
) {
713 CFIndex count
= CFArrayGetCount(array
);
714 const void **values
= (const void **)malloc(sizeof(const void *) * count
);
715 CFArrayGetValues(array
, CFRangeMake(0, count
), values
);
716 CFSetRef set
= CFSetCreate(CFGetAllocator(array
), values
, count
, &kCFTypeSetCallBacks
);
721 static inline void CFSetTransferObject(CFTypeRef object
, CFMutableSetRef from
, CFMutableSetRef to
) {
722 CFSetAddValue(to
, object
);
723 CFSetRemoveValue(from
, object
);
728 // MARK: CFDictionary Helpers
731 static inline void CFDictionaryForEach(CFDictionaryRef dictionary
, void (^operation
)(const void *key
, const void *value
)) {
732 CFDictionaryApplyFunction(dictionary
, apply_block_2
, operation
);
735 CFStringRef
CFDictionaryCopyCompactDescription(CFDictionaryRef dictionary
);
736 CFStringRef
CFDictionaryCopySuperCompactDescription(CFDictionaryRef dictionary
);
739 // MARK: CFCalendar helpers
742 void SecCFCalendarDoWithZuluCalendar(void(^action
)(CFCalendarRef zuluCalendar
));
745 // MARK: CFAbsoluteTime helpers
748 static inline CFAbsoluteTime
CFAbsoluteTimeForCalendarMoment(CFCalendarRef cal
, int year
, int month
, int day
, int hour
, int minute
, int second
) {
750 CFCalendarComposeAbsoluteTime(cal
, &at
, "yMdHms", year
, month
, day
, hour
, minute
, second
);
754 static inline CFAbsoluteTime
CFAbsoluteTimeForCalendarDay(CFCalendarRef cal
, int year
, int month
, int day
) {
756 CFCalendarComposeAbsoluteTime(cal
, &at
, "yMd", year
, month
, day
);
760 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianMoment(CFTimeZoneRef tz
, int year
, int month
, int day
, int hour
, int minute
, int second
)
762 CFCalendarRef cal
= CFCalendarCreateWithIdentifier(NULL
, kCFGregorianCalendar
);
763 CFCalendarSetTimeZone(cal
, tz
);
764 CFAbsoluteTime at
= CFAbsoluteTimeForCalendarMoment(cal
, year
, month
, day
, hour
, minute
, second
);
769 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianDay(CFTimeZoneRef tz
, int year
, int month
, int day
)
771 CFCalendarRef cal
= CFCalendarCreateWithIdentifier(NULL
, kCFGregorianCalendar
);
772 CFCalendarSetTimeZone(cal
, tz
);
773 CFAbsoluteTime at
= CFAbsoluteTimeForCalendarDay(cal
, year
, month
, day
);
778 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianZuluMoment(int year
, int month
, int day
, int hour
, int minute
, int second
)
780 __block CFAbsoluteTime result
= 0.0;
781 SecCFCalendarDoWithZuluCalendar(^(CFCalendarRef zuluCalendar
) {
782 result
= CFAbsoluteTimeForCalendarMoment(zuluCalendar
, year
, month
, day
, hour
, minute
, second
);
788 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianZuluDay(int year
, int month
, int day
)
790 __block CFAbsoluteTime result
= 0.0;
791 SecCFCalendarDoWithZuluCalendar(^(CFCalendarRef zuluCalendar
) {
792 result
= CFAbsoluteTimeForCalendarDay(zuluCalendar
, year
, month
, day
);
800 // MARK: CFDate Helpers
803 static inline CFDateRef
CFDateCreateForGregorianMoment(CFAllocatorRef allocator
, CFTimeZoneRef tz
, int year
, int month
, int day
, int hour
, int minute
, int second
)
805 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianMoment(tz
, year
, month
, day
, hour
, minute
, second
));
808 static inline CFDateRef
CFDateCreateForGregorianDay(CFAllocatorRef allocator
, CFTimeZoneRef tz
, int year
, int month
, int day
, int hour
, int minute
, int second
)
810 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianDay(tz
, year
, month
, day
));
813 static inline CFDateRef
CFDateCreateForGregorianZuluMoment(CFAllocatorRef allocator
, int year
, int month
, int day
, int hour
, int minute
, int second
)
815 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianZuluMoment(year
, month
, day
, hour
, minute
, second
));
818 static inline CFDateRef
CFDateCreateForGregorianZuluDay(CFAllocatorRef allocator
, int year
, int month
, int day
)
820 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianZuluDay(year
, month
, day
));
825 // MARK: Type checking
828 static inline bool isArray(CFTypeRef cfType
) {
829 return cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID();
832 static inline bool isSet(CFTypeRef cfType
) {
833 return cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID();
836 static inline bool isData(CFTypeRef cfType
) {
837 return cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID();
840 static inline bool isDate(CFTypeRef cfType
) {
841 return cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID();
844 static inline bool isDictionary(CFTypeRef cfType
) {
845 return cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID();
848 static inline bool isNumber(CFTypeRef cfType
) {
849 return cfType
&& CFGetTypeID(cfType
) == CFNumberGetTypeID();
852 static inline bool isNumberOfType(CFTypeRef cfType
, CFNumberType number
) {
853 return isNumber(cfType
) && CFNumberGetType((CFNumberRef
)cfType
) == number
;
856 static inline bool isString(CFTypeRef cfType
) {
857 return cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID();
860 static inline bool isBoolean(CFTypeRef cfType
) {
861 return cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID();
864 static inline bool isNull(CFTypeRef cfType
) {
865 return cfType
&& CFGetTypeID(cfType
) == CFNullGetTypeID();
869 // MARK: PropertyList Helpers
873 // Crazy reading and writing stuff
876 static inline void CFPropertyListWriteToFile(CFPropertyListRef plist
, CFURLRef file
)
878 CFWriteStreamRef writeStream
= CFWriteStreamCreateWithFile(kCFAllocatorDefault
, file
);
879 CFErrorRef error
= NULL
;
881 CFWriteStreamOpen(writeStream
);
882 CFPropertyListWrite(plist
, writeStream
, kCFPropertyListBinaryFormat_v1_0
, 0, &error
);
884 secerror("Can't write plist: %@", error
);
886 CFReleaseNull(error
);
887 CFReleaseNull(writeStream
);
890 static inline CF_RETURNS_RETAINED CFPropertyListRef
CFPropertyListReadFromFile(CFURLRef file
)
892 CFPropertyListRef result
= NULL
;
893 CFErrorRef error
= NULL
;
894 CFBooleanRef isRegularFile
;
895 if (!CFURLCopyResourcePropertyForKey(file
, kCFURLIsRegularFileKey
, &isRegularFile
, &error
)) {
896 secdebug("plist", "file %@: %@", file
, error
);
897 } else if (CFBooleanGetValue(isRegularFile
)) {
898 CFReadStreamRef readStream
= CFReadStreamCreateWithFile(kCFAllocatorDefault
, file
);
900 if (CFReadStreamOpen(readStream
)) {
901 CFPropertyListFormat format
;
902 result
= CFPropertyListCreateWithStream(kCFAllocatorDefault
, readStream
, 0, kCFPropertyListMutableContainers
, &format
, &error
);
904 secerror("read plist from %@: %@", file
, error
);
907 CFRelease(readStream
);
910 CFReleaseNull(error
);
918 #endif /* _SECCFWRAPPERS_H_ */