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"
33 #include "utilities/SecCFError.h"
35 #include <IOKit/IOReturn.h>
38 #include <dispatch/dispatch.h>
43 #include <corecrypto/ccdigest.h>
47 #if __has_feature(objc_arc)
48 #define __SECBRIDGE __bridge
54 // Convenience routines.
58 // Macros for the pattern
60 // typedef struct _privateNewClass* NewClassRef;
62 // struct _privateNewClass {
63 // CFRuntimeBase _base;
64 // ... class additions
67 // kClassNameRegisterClass
70 // ClassNameGetTypeID()
72 // CFGiblisFor(NewClass);
74 // .. define NewClassDestroy
75 // .. define NewClassCopyDescription
77 // .. use CFTypeAllocate(NewClass, _privateNewClass, allocator);
81 // Call this to create a function that returns a singleton instance of type stype,
82 // which is initialized once by calling doThisOnce, with result in its context. Upon
83 // completion body should assign to *result.
85 extern CFStringRef kSecDebugFormatOption
;
87 extern CFDictionaryRef
SecGetDebugDescriptionFormatOptions(void);
89 typedef void (^SecBoolCFErrorCallback
) (bool, CFErrorRef
);
91 #define CFGiblisGetSingleton(returnType, giblisClassName, result, doThisOnce) \
92 returnType giblisClassName(void); \
93 returnType giblisClassName(void) { \
94 static dispatch_once_t s##giblisClassName##Once; \
95 static returnType s##giblisClassName##Singleton; \
96 returnType *result = &s##giblisClassName##Singleton; \
97 dispatch_once(&s##giblisClassName##Once, doThisOnce); \
98 return s##giblisClassName##Singleton; \
101 #define CFGiblisWithFunctions(gibliClassName, init_func, copy_func, finalize_func, equal_func, hash_func, copyFormattingDesc_func, copyDebugDesc_func, reclaim_func, refcount_func, run_once_block) \
102 CFGiblisGetSingleton(CFTypeID, gibliClassName##GetTypeID, typeID, (^{ \
103 void (^ const _onceBlock)(void) = (run_once_block); \
104 static const CFRuntimeClass s##gibliClassName##Class = { \
105 .version = (reclaim_func == NULL ? 0 : _kCFRuntimeResourcefulObject) \
106 | (refcount_func == NULL ? 0 : _kCFRuntimeCustomRefCount), \
107 .className = #gibliClassName, \
110 .finalize = finalize_func, \
111 .equal = equal_func, \
113 .copyFormattingDesc = copyFormattingDesc_func, \
114 .copyDebugDesc = copyDebugDesc_func, \
115 .reclaim = reclaim_func, \
116 .refcount = refcount_func, \
118 *typeID = _CFRuntimeRegisterClass(&s##gibliClassName##Class); \
123 #define CFGiblisWithHashFor(gibliClassName) \
124 static CFStringRef gibliClassName##CopyFormatDescription(CFTypeRef cf, CFDictionaryRef formatOptions); \
125 static void gibliClassName##Destroy(CFTypeRef cf); \
126 static Boolean gibliClassName##Compare(CFTypeRef lhs, CFTypeRef rhs); \
127 static CFHashCode gibliClassName##Hash(CFTypeRef cf); \
128 static CFStringRef gibliClassName##CopyDescription(CFTypeRef cf){\
129 return gibliClassName##CopyFormatDescription(cf, SecGetDebugDescriptionFormatOptions());\
132 CFGiblisWithFunctions(gibliClassName, NULL, NULL, gibliClassName##Destroy, gibliClassName##Compare, gibliClassName##Hash, gibliClassName##CopyFormatDescription, gibliClassName##CopyDescription, NULL, NULL, NULL)
134 #define CFGiblisWithCompareFor(gibliClassName) \
135 static CFStringRef gibliClassName##CopyFormatDescription(CFTypeRef cf, CFDictionaryRef formatOptions); \
136 static void gibliClassName##Destroy(CFTypeRef cf); \
137 static Boolean gibliClassName##Compare(CFTypeRef lhs, CFTypeRef rhs); \
138 static CFStringRef gibliClassName##CopyDescription(CFTypeRef cf){\
139 return gibliClassName##CopyFormatDescription(cf, SecGetDebugDescriptionFormatOptions());\
142 CFGiblisWithFunctions(gibliClassName, NULL, NULL, gibliClassName##Destroy, gibliClassName##Compare, NULL, gibliClassName##CopyFormatDescription, gibliClassName##CopyDescription, NULL, NULL, NULL)
145 #define CFGiblisFor(gibliClassName) \
146 static CFStringRef gibliClassName##CopyFormatDescription(CFTypeRef cf, CFDictionaryRef formatOptions); \
147 static void gibliClassName##Destroy(CFTypeRef cf); \
148 static CFStringRef gibliClassName##CopyDescription(CFTypeRef cf){\
149 return gibliClassName##CopyFormatDescription(cf, SecGetDebugDescriptionFormatOptions());\
152 CFGiblisWithFunctions(gibliClassName, NULL, NULL, gibliClassName##Destroy, NULL, NULL, gibliClassName##CopyFormatDescription, gibliClassName##CopyDescription, NULL, NULL, NULL)
154 #define CFTypeAllocateWithSpace(classType, space, allocator) \
155 (classType##Ref) _CFRuntimeCreateInstance(allocator, classType##GetTypeID(), space, NULL)
157 #define CFTypeAllocate(classType, internalType, allocator) \
158 CFTypeAllocateWithSpace(classType, sizeof(internalType) - sizeof(CFRuntimeBase), allocator)
160 #define SECWRAPPER_SENTINEL __attribute__((__sentinel__))
164 void withStringOfAbsoluteTime(CFAbsoluteTime at
, void (^action
)(CFStringRef decription
));
168 // MARK: Call block function
172 static void apply_block_1(const void *value
, void *context
)
174 ((__SECBRIDGE
void (^)(const void *value
))context
)(value
);
177 static void apply_block_2(const void *key
, const void *value
, void *context
)
179 ((__SECBRIDGE
void (^)(const void *key
, const void *value
))context
)(key
, value
);
183 // MARK: Type checking
186 static inline bool isArray(CFTypeRef cfType
) {
187 return cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID();
190 static inline bool isSet(CFTypeRef cfType
) {
191 return cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID();
194 static inline bool isData(CFTypeRef cfType
) {
195 return cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID();
198 static inline bool isDate(CFTypeRef cfType
) {
199 return cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID();
202 static inline bool isDictionary(CFTypeRef cfType
) {
203 return cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID();
206 static inline bool isNumber(CFTypeRef cfType
) {
207 return cfType
&& CFGetTypeID(cfType
) == CFNumberGetTypeID();
210 static inline bool isNumberOfType(CFTypeRef cfType
, CFNumberType number
) {
211 return isNumber(cfType
) && CFNumberGetType((CFNumberRef
)cfType
) == number
;
214 static inline bool isString(CFTypeRef cfType
) {
215 return cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID();
218 static inline bool isBoolean(CFTypeRef cfType
) {
219 return cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID();
222 static inline bool isNull(CFTypeRef cfType
) {
223 return cfType
&& CFGetTypeID(cfType
) == CFNullGetTypeID();
227 // MARK CFEqual Helpers
230 static inline bool CFEqualSafe(CFTypeRef left
, CFTypeRef right
)
232 if (left
== NULL
|| right
== NULL
)
233 return left
== right
;
235 return CFEqual(left
, right
);
243 static void fprint_string(FILE *file
, CFStringRef string
) {
245 CFRange range
= { .location
= 0 };
246 range
.length
= CFStringGetLength(string
);
247 while (range
.length
> 0) {
248 CFIndex bytesUsed
= 0;
249 CFIndex converted
= CFStringGetBytes(string
, range
, kCFStringEncodingUTF8
, 0, false, buf
, sizeof(buf
), &bytesUsed
);
250 fwrite(buf
, 1, bytesUsed
, file
);
251 range
.length
-= converted
;
252 range
.location
+= converted
;
256 static inline void cffprint_v(FILE *file
, CFStringRef fmt
, va_list args
) CF_FORMAT_FUNCTION(2, 0);
257 static void cffprint(FILE *file
, CFStringRef fmt
, ...) CF_FORMAT_FUNCTION(2,0);
259 static inline void cffprint_v(FILE *file
, CFStringRef fmt
, va_list args
) {
260 CFStringRef line
= CFStringCreateWithFormatAndArguments(NULL
, NULL
, fmt
, args
);
261 fprint_string(file
, line
);
265 static inline void cffprint(FILE *file
, CFStringRef fmt
, ...) {
268 cffprint_v(file
, fmt
, args
);
273 // MARK: CFError Helpers
276 /* Return false if possibleError is set. Propagates possibleError into *error
277 if *error is NULL, otherwise releases possibleError. */
279 bool CFErrorPropagate(CFErrorRef possibleError CF_CONSUMED
, CFErrorRef
*error
) {
281 if (error
&& !*error
) {
282 *error
= possibleError
;
284 CFRelease(possibleError
);
291 static inline bool CFErrorIsMalfunctioningKeybagError(CFErrorRef error
){
292 switch(CFErrorGetCode(error
))
294 case(kAKSReturnError
):
295 case(kAKSReturnBusy
):
296 case(kAKSReturnNoPermission
):
301 return CFEqualSafe(CFErrorGetDomain(error
), kSecKernDomain
);
305 // MARK: CFNumber Helpers
308 static inline CFNumberRef
CFNumberCreateWithCFIndex(CFAllocatorRef allocator
, CFIndex value
)
310 return CFNumberCreate(allocator
, kCFNumberCFIndexType
, &value
);
314 // MARK: CFData Helpers
317 static inline CFMutableDataRef
CFDataCreateMutableWithScratch(CFAllocatorRef allocator
, CFIndex size
) {
318 CFMutableDataRef result
= CFDataCreateMutable(allocator
, 0);
319 CFDataSetLength(result
, size
);
324 static inline void CFDataAppend(CFMutableDataRef appendTo
, CFDataRef dataToAppend
)
326 CFDataAppendBytes(appendTo
, CFDataGetBytePtr(dataToAppend
), CFDataGetLength(dataToAppend
));
329 static inline CFDataRef
CFDataCreateReferenceFromRange(CFAllocatorRef allocator
, CFDataRef sourceData
, CFRange range
)
331 return CFDataCreateWithBytesNoCopy(allocator
,
332 CFDataGetBytePtr(sourceData
) + range
.location
, range
.length
,
336 static inline CFDataRef
CFDataCreateCopyFromRange(CFAllocatorRef allocator
, CFDataRef sourceData
, CFRange range
)
338 return CFDataCreate(allocator
, CFDataGetBytePtr(sourceData
) + range
.location
, range
.length
);
341 CFDataRef
CFDataCreateWithRandomBytes(size_t len
);
343 CFDataRef
CFDataCreateWithInitializer(CFAllocatorRef allocator
, CFIndex size
, bool (^operation
)(size_t size
, uint8_t *buffer
));
345 static inline uint8_t* CFDataIncreaseLengthAndGetMutableBytes(CFMutableDataRef data
, CFIndex extraLength
)
347 CFIndex startOffset
= CFDataGetLength(data
);
349 CFDataIncreaseLength(data
, extraLength
);
351 return CFDataGetMutableBytePtr(data
) + startOffset
;
354 static inline uint8_t* CFDataGetMutablePastEndPtr(CFMutableDataRef theData
)
356 return CFDataGetMutableBytePtr(theData
) + CFDataGetLength(theData
);
359 static inline const uint8_t* CFDataGetPastEndPtr(CFDataRef theData
) {
360 return CFDataGetBytePtr(theData
) + CFDataGetLength(theData
);
363 static inline CFComparisonResult
CFDataCompare(CFDataRef left
, CFDataRef right
)
365 const size_t left_size
= CFDataGetLength(left
);
366 const size_t right_size
= CFDataGetLength(right
);
367 const size_t shortest
= (left_size
<= right_size
) ? left_size
: right_size
;
369 int comparison
= memcmp(CFDataGetBytePtr(left
), CFDataGetBytePtr(right
), shortest
);
371 if (comparison
> 0 || (comparison
== 0 && left_size
> right_size
))
372 return kCFCompareGreaterThan
;
373 else if (comparison
< 0 || (comparison
== 0 && left_size
< right_size
))
374 return kCFCompareLessThan
;
376 return kCFCompareEqualTo
;
379 static inline CFDataRef
CFDataCreateWithHash(CFAllocatorRef allocator
, const struct ccdigest_info
*di
, const uint8_t *buffer
, const uint8_t length
) {
380 CFMutableDataRef result
= CFDataCreateMutableWithScratch(allocator
, di
->output_size
);
382 ccdigest(di
, length
, buffer
, CFDataGetMutableBytePtr(result
));
388 static inline CFDataRef
CFDataCreateCopyFromPositions(CFAllocatorRef allocator
, CFDataRef source
, CFIndex start
, CFIndex end
)
390 return CFDataCreateCopyFromRange(allocator
, source
, CFRangeMake(start
, end
- start
));
393 static inline int nibletToByte(char niblet
) {
394 if(niblet
>= '0' && niblet
<= '9') return niblet
- '0';
395 if(niblet
>= 'a' && niblet
<= 'f') return niblet
- 'a' + 10;
396 if(niblet
>= 'A' && niblet
<= 'F') return niblet
- 'A' + 10;
400 static inline CFDataRef
CFDataCreateFromHexString(CFAllocatorRef allocator
, CFStringRef sourceHex
) {
401 CFIndex sourceLen
= CFStringGetLength(sourceHex
);
402 if((sourceLen
% 2) != 0) return NULL
;
403 const char *src
= CFStringGetCStringPtr(sourceHex
, kCFStringEncodingUTF8
);
404 UInt8 bytes
[sourceLen
/2];
405 for(int i
= 0; i
< sourceLen
; i
+=2) {
406 bytes
[i
/2] = (UInt8
) (nibletToByte(src
[i
]) * 16 + nibletToByte(src
[i
+1]));
408 return CFDataCreate(allocator
, bytes
, sourceLen
/2);
413 // MARK: CFString Helpers
416 CFComparisonResult
CFStringCompareSafe(const void *val1
, const void *val2
, void *context
);
419 // Turn a CFString into an allocated UTF8-encoded C string.
421 static inline char *CFStringToCString(CFStringRef inStr
)
424 return (char *)strdup("");
425 CFRetain(inStr
); // compensate for release on exit
427 // need to extract into buffer
428 CFIndex length
= CFStringGetLength(inStr
); // in 16-bit character units
429 size_t len
= CFStringGetMaximumSizeForEncoding(length
, kCFStringEncodingUTF8
) + 1;
430 char *buffer
= (char *)malloc(len
); // pessimistic
431 if (!CFStringGetCString(inStr
, buffer
, len
, kCFStringEncodingUTF8
))
438 // runs operation with inStr as a zero terminated C string
439 // in utf8 encoding passed to the operation block.
440 void CFStringPerformWithCString(CFStringRef inStr
, void(^operation
)(const char *utf8Str
));
442 // runs operation with inStr as a zero terminated C string
443 // in utf8 passed to the operation block, the length of
444 // the string is also provided to the block.
445 void CFStringPerformWithCStringAndLength(CFStringRef inStr
, void(^operation
)(const char *utf8Str
, size_t utf8Length
));
447 void CFStringPerformWithUTF8CFData(CFStringRef inStr
, void (^operation
)(CFDataRef stringAsData
));
449 #include <CommonNumerics/CommonCRC.h>
451 static inline void CFStringAppendEncryptedData(CFMutableStringRef s
, CFDataRef edata
)
453 const uint8_t *bytes
= CFDataGetBytePtr(edata
);
454 CFIndex len
= CFDataGetLength(edata
);
455 CFStringAppendFormat(s
, 0, CFSTR("%04lx:"), len
);
457 for (CFIndex ix
= 0; ix
< len
; ++ix
) {
458 CFStringAppendFormat(s
, 0, CFSTR("%02X"), bytes
[ix
]);
462 CNCRC(kCN_CRC_64_ECMA_182
, bytes
+8, len
-8, &crc
);
463 for (CFIndex ix
= 0; ix
< 8; ++ix
) {
464 CFStringAppendFormat(s
, 0, CFSTR("%02X"), bytes
[ix
]);
466 CFStringAppendFormat(s
, 0, CFSTR("...|%08llx"), crc
);
470 static inline void CFStringAppendHexData(CFMutableStringRef s
, CFDataRef data
) {
471 const uint8_t *bytes
= CFDataGetBytePtr(data
);
472 CFIndex len
= CFDataGetLength(data
);
473 for (CFIndex ix
= 0; ix
< len
; ++ix
) {
474 CFStringAppendFormat(s
, 0, CFSTR("%02X"), bytes
[ix
]);
478 static inline CF_RETURNS_RETAINED CFStringRef
CFDataCopyHexString(CFDataRef data
) {
479 CFMutableStringRef hexString
= CFStringCreateMutable(kCFAllocatorDefault
, 2 * CFDataGetLength(data
));
480 CFStringAppendHexData(hexString
, data
);
484 static inline void CFDataPerformWithHexString(CFDataRef data
, void (^operation
)(CFStringRef dataString
)) {
485 CFStringRef hexString
= data
? CFDataCopyHexString(data
) : CFSTR("(null)");
486 operation(hexString
);
487 CFRelease(hexString
);
490 static inline void BufferPerformWithHexString(const UInt8
*bytes
, CFIndex length
, void (^operation
)(CFStringRef dataString
)) {
491 CFDataRef bufferAsData
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, bytes
, length
, kCFAllocatorNull
);
493 CFDataPerformWithHexString(bufferAsData
, operation
);
495 CFReleaseNull(bufferAsData
);
500 static inline void CFStringWriteToFile(CFStringRef inStr
, FILE* file
)
502 CFStringPerformWithCStringAndLength(inStr
, ^(const char *utf8Str
, size_t utf8Length
) {
503 fwrite(utf8Str
, 1, utf8Length
, file
);
507 static inline void CFStringWriteToFileWithNewline(CFStringRef inStr
, FILE* file
)
509 CFStringWriteToFile(inStr
, file
);
513 static inline CFStringRef
CFStringCreateTruncatedCopy(CFStringRef s
, CFIndex len
) {
515 if(len
>= CFStringGetLength(s
)) return CFStringCreateCopy(kCFAllocatorDefault
, s
);
516 return CFStringCreateWithSubstring(kCFAllocatorDefault
, s
, CFRangeMake(0, len
));
520 // MARK: CFCollectionHelpers
524 const void *SecCFRetainForCollection(CFAllocatorRef allocator
, const void *value
)
526 return CFRetain(value
);
530 void SecCFReleaseForCollection(CFAllocatorRef allocator
, const void *value
)
536 // MARK: CFArray Helpers
539 static inline CFIndex
CFArrayRemoveAllValue(CFMutableArrayRef array
, const void* value
)
541 CFIndex position
= kCFNotFound
;
542 CFIndex numberRemoved
= 0;
544 position
= CFArrayGetFirstIndexOfValue(array
, CFRangeMake(0, CFArrayGetCount(array
)), value
);
545 while (position
!= kCFNotFound
) {
546 CFArrayRemoveValueAtIndex(array
, position
);
548 position
= CFArrayGetFirstIndexOfValue(array
, CFRangeMake(0, CFArrayGetCount(array
)), value
);
551 return numberRemoved
;
554 static inline void CFArrayAppendAll(CFMutableArrayRef array
, CFArrayRef arrayToAppend
) {
555 CFArrayAppendArray(array
, arrayToAppend
, CFRangeMake(0, CFArrayGetCount(arrayToAppend
)));
558 #define CFArrayForEachC(array, value) for (CFIndex _aCount = CFArrayGetCount(array), _aIX = 0;value = (__typeof__(value))(_aIX < _aCount ? CFArrayGetValueAtIndex(array, _aIX) : 0), _aIX < _aCount; ++_aIX)
560 static inline void CFArrayForEach(CFArrayRef array
, void (^operation
)(const void *value
)) {
561 CFArrayApplyFunction(array
, CFRangeMake(0, CFArrayGetCount(array
)), apply_block_1
, (__SECBRIDGE
void *)operation
);
564 static inline void CFArrayForEachReverse(CFArrayRef array
, void (^operation
)(const void *value
)) {
565 for(CFIndex count
= CFArrayGetCount(array
); count
> 0; --count
) {
566 operation(CFArrayGetValueAtIndex(array
, count
- 1));
570 static inline const void *CFArrayGetValueMatching(CFArrayRef array
, bool (^match
)(const void *value
)) {
571 CFIndex i
, n
= CFArrayGetCount(array
);
572 for (i
= 0; i
< n
; ++i
) {
573 const void *value
= CFArrayGetValueAtIndex(array
, i
);
581 static inline bool CFArrayHasValueMatching(CFArrayRef array
, bool (^match
)(const void *value
)) {
582 return CFArrayGetValueMatching(array
, match
) != NULL
;
585 static inline void CFMutableArrayModifyValues(CFMutableArrayRef array
, const void * (^process
)(const void *value
)) {
586 CFIndex i
, n
= CFArrayGetCount(array
);
587 for (i
= 0; i
< n
; ++i
) {
588 const void *value
= CFArrayGetValueAtIndex(array
, i
);
589 CFArraySetValueAtIndex(array
, i
, process(value
));
593 static inline void CFArraySubtract(CFMutableArrayRef from
, CFArrayRef remove
) {
594 if (remove
&& from
) {
595 CFArrayForEach(remove
, ^(const void *value
) {
596 CFArrayRemoveAllValue(from
, value
);
601 static inline CFMutableArrayRef
CFArrayCreateDifference(CFAllocatorRef alloc
, CFArrayRef set
, CFArrayRef remove
) {
602 CFMutableArrayRef result
;
604 result
= CFArrayCreateMutable(alloc
, 0, &kCFTypeArrayCallBacks
);
606 result
= CFArrayCreateMutableCopy(alloc
, 0, set
);
608 CFArraySubtract(result
, remove
);
615 // MARK: CFArray creation Var args helper functions.
617 static inline CFArrayRef
CFArrayCreateCountedForVC(CFAllocatorRef allocator
, const CFArrayCallBacks
*cbs
, CFIndex entries
, va_list args
)
619 CFMutableArrayRef array
= CFArrayCreateMutable(allocator
, entries
, cbs
);
623 for (CFIndex currentValue
= 0; currentValue
< entries
; ++currentValue
) {
624 const void * value
= va_arg(args
, const void *);
628 CFArrayAppendValue(array
, value
);
631 CFArrayRef constArray
= CFArrayCreateCopy(allocator
, array
);
636 static inline CFArrayRef
CFArrayCreateForVC(CFAllocatorRef allocator
, const CFArrayCallBacks
*cbs
, va_list args
)
639 va_copy(count
, args
);
642 while (NULL
!= va_arg(count
, void*)) {
646 return CFArrayCreateCountedForVC(allocator
, cbs
, entries
, args
);
652 // MARK: CFArray of CFTypes support
655 static inline CFMutableArrayRef
CFArrayCreateMutableForCFTypesWithCapacity(CFAllocatorRef allocator
, CFIndex capacity
)
657 return CFArrayCreateMutable(allocator
, capacity
, &kCFTypeArrayCallBacks
);
660 static inline CFMutableArrayRef SECWRAPPER_SENTINEL
CFArrayCreateMutableForCFTypesWith(CFAllocatorRef allocator
, ...)
664 va_start(args
, allocator
);
665 CFIndex capacity
= 0;
666 void* object
= va_arg(args
, void*);
668 while (object
!= NULL
) {
669 object
= va_arg(args
, void*);
675 CFMutableArrayRef result
= CFArrayCreateMutableForCFTypesWithCapacity(allocator
, capacity
);
677 va_start(args
, allocator
);
678 object
= va_arg(args
, void*);
680 while (object
!= NULL
) {
681 CFArrayAppendValue(result
, object
);
682 object
= va_arg(args
, void*);
690 static inline CFMutableArrayRef
CFArrayCreateMutableForCFTypes(CFAllocatorRef allocator
)
692 return CFArrayCreateMutable(allocator
, 0, &kCFTypeArrayCallBacks
);
695 static inline CFArrayRef SECWRAPPER_SENTINEL
CFArrayCreateForCFTypes(CFAllocatorRef allocator
, ...)
698 va_start(args
, allocator
);
699 CFArrayRef allocatedArray
= CFArrayCreateForVC(allocator
, &kCFTypeArrayCallBacks
, args
);
701 return allocatedArray
;
705 static inline CFArrayRef
CFArrayCreateCountedForCFTypes(CFAllocatorRef allocator
, CFIndex entries
, ...)
708 va_start(args
, entries
);
709 CFArrayRef allocatedArray
= CFArrayCreateCountedForVC(allocator
, &kCFTypeArrayCallBacks
, entries
, args
);
711 return allocatedArray
;
714 static inline CFArrayRef
CFArrayCreateCountedForCFTypesV(CFAllocatorRef allocator
, CFIndex entries
, va_list args
)
716 return CFArrayCreateCountedForVC(allocator
, &kCFTypeArrayCallBacks
, entries
, args
);
720 // MARK: CFDictionary of CFTypes helpers
723 static void CFDictionarySetIfNonNull(CFMutableDictionaryRef dictionary
, const void *key
, const void *value
) {
725 CFDictionarySetValue(dictionary
, key
, value
);
729 static inline CFDictionaryRef
CFDictionaryCreateCountedForCFTypesV(CFAllocatorRef allocator
, CFIndex entries
, va_list args
)
731 CFMutableDictionaryRef dictionary
= CFDictionaryCreateMutable(allocator
, entries
,
732 &kCFTypeDictionaryKeyCallBacks
,
733 &kCFTypeDictionaryValueCallBacks
);
734 if (dictionary
== NULL
) {
738 for(CFIndex currentValue
= 0; currentValue
< entries
; ++currentValue
) {
739 CFTypeRef key
= va_arg(args
, CFTypeRef
);
740 CFTypeRef value
= va_arg(args
, CFTypeRef
);
744 CFDictionarySetValue(dictionary
, key
, value
);
747 CFDictionaryRef constDictionary
= CFDictionaryCreateCopy(allocator
, dictionary
);
748 CFRelease(dictionary
);
749 return constDictionary
;
752 static inline CFDictionaryRef SECWRAPPER_SENTINEL
CFDictionaryCreateForCFTypes(CFAllocatorRef allocator
, ...)
755 va_start(args
, allocator
);
758 while (NULL
!= va_arg(args
, void*)) {
760 (void) va_arg(args
, void*);
765 va_start(args
, allocator
);
766 CFDictionaryRef allocatedDictionary
= CFDictionaryCreateCountedForCFTypesV(allocator
, entries
, args
);
768 return allocatedDictionary
;
771 static inline CFDictionaryRef
CFDictionaryCreateCountedForCFTypes(CFAllocatorRef allocator
, CFIndex entries
, ...)
774 va_start(args
, entries
);
775 CFDictionaryRef allocatedDictionary
= CFDictionaryCreateCountedForCFTypesV(allocator
, entries
, args
);
778 return allocatedDictionary
;
781 static inline CFMutableDictionaryRef
CFDictionaryCreateMutableForCFTypes(CFAllocatorRef allocator
)
783 return CFDictionaryCreateMutable(allocator
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
786 static inline CFMutableDictionaryRef SECWRAPPER_SENTINEL
CFDictionaryCreateMutableForCFTypesWith(CFAllocatorRef allocator
, ...)
788 CFMutableDictionaryRef result
= CFDictionaryCreateMutableForCFTypes(allocator
);
791 va_start(args
, allocator
);
793 void* key
= va_arg(args
, void*);
795 while (key
!= NULL
) {
796 CFDictionarySetValue(result
, key
, va_arg(args
, void*));
797 key
= va_arg(args
, void*);
803 static inline CFMutableDictionaryRef SECWRAPPER_SENTINEL
CFDictionaryCreateMutableForCFTypesWithSafe(CFAllocatorRef allocator
, ...)
805 CFMutableDictionaryRef result
= CFDictionaryCreateMutableForCFTypes(allocator
);
808 va_start(args
, allocator
);
810 void* key
= va_arg(args
, void*);
812 while (key
!= NULL
) {
813 CFDictionarySetIfNonNull(result
, key
, va_arg(args
, void*));
814 key
= va_arg(args
, void*);
821 // MARK: CFSet Helpers
824 static inline CFMutableSetRef
CFSetCreateMutableForCFTypes(CFAllocatorRef allocator
)
826 return CFSetCreateMutable(allocator
, 0, &kCFTypeSetCallBacks
);
829 static inline bool CFSetIsEmpty(CFSetRef set
) {
830 return CFSetGetCount(set
) == 0;
833 static inline void CFSetForEach(CFSetRef set
, void (^operation
)(const void *value
)) {
834 CFSetApplyFunction(set
, apply_block_1
, (__SECBRIDGE
void *)operation
);
837 static inline void CFSetUnion(CFMutableSetRef set
, CFSetRef unionWith
) {
838 CFSetForEach(unionWith
, ^(const void *value
) {
839 CFSetSetValue(set
, value
);
843 static inline void CFSetSubtract(CFMutableSetRef set
, CFSetRef subtract
) {
844 CFSetForEach(subtract
, ^(const void *value
) {
845 CFSetRemoveValue(set
, value
);
849 static inline bool CFSetIsSubset(CFSetRef smaller
, CFSetRef bigger
) {
850 __block
bool isSubset
= true;
851 CFSetForEach(smaller
, ^(const void *value
) {
852 if (!CFSetContainsValue(bigger
, value
)) {
860 static inline void CFSetSetValues(CFMutableSetRef set
, CFArrayRef valuesToSet
) {
861 CFArrayForEach(valuesToSet
, ^(const void *value
) {
862 CFSetSetValue(set
, value
);
866 static inline CFMutableArrayRef
CFSetCopyValues(CFSetRef set
) {
867 CFMutableArrayRef values
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
869 CFSetForEach(set
, ^(const void *value
) {
870 CFArrayAppendValue(values
, value
);
876 static inline bool CFSetIntersectionIsEmpty(CFSetRef set1
, CFSetRef set2
) {
877 __block
bool intersectionIsEmpty
= true;
878 if(set1
!= NULL
&& set2
!= NULL
) {
879 CFSetForEach(set1
, ^(const void *value
) {
880 intersectionIsEmpty
&= !CFSetContainsValue(set2
, value
);
883 return intersectionIsEmpty
;
886 static inline bool CFSetIntersects(CFSetRef set1
, CFSetRef set2
) {
887 return !CFSetIntersectionIsEmpty(set1
, set2
);
890 static inline CFMutableSetRef
CFSetCreateIntersection(CFAllocatorRef allocator
, CFSetRef a
, CFSetRef b
) {
891 CFMutableSetRef result
= CFSetCreateMutableCopy(allocator
, 0, a
);
893 CFSetRemoveAllValues(result
);
894 CFSetForEach(a
, ^(const void *value
) {
895 if (CFSetContainsValue(b
, value
)) {
896 CFSetAddValue(result
, value
);
903 static inline CFSetRef
CFSetCreateCopyOfArrayForCFTypes(CFArrayRef array
) {
904 CFIndex count
= CFArrayGetCount(array
);
905 if (SIZE_MAX
/sizeof(const void *) < (size_t)count
) {
908 const void **values
= (const void **)malloc(sizeof(const void *) * count
);
909 CFArrayGetValues(array
, CFRangeMake(0, count
), values
);
910 CFSetRef set
= CFSetCreate(CFGetAllocator(array
), values
, count
, &kCFTypeSetCallBacks
);
915 static inline void CFSetTransferObject(CFTypeRef object
, CFMutableSetRef from
, CFMutableSetRef to
) {
916 CFSetAddValue(to
, object
);
917 CFSetRemoveValue(from
, object
);
921 // MARK: CFStringXxx Helpers
924 void CFStringArrayPerformWithDelimiterWithDescription(CFArrayRef strings
, CFStringRef start
, CFStringRef end
, void (^action
)(CFStringRef description
));
925 void CFStringArrayPerformWithDescription(CFArrayRef strings
, void (^action
)(CFStringRef description
));
926 void CFStringSetPerformWithDescription(CFSetRef set
, void (^action
)(CFStringRef description
));
929 // MARK: CFDictionary Helpers
932 static inline void CFDictionaryForEach(CFDictionaryRef dictionary
, void (^operation
)(const void *key
, const void *value
)) {
933 CFDictionaryApplyFunction(dictionary
, apply_block_2
, (__SECBRIDGE
void *)operation
);
936 CFStringRef
CFDictionaryCopyCompactDescription(CFDictionaryRef dictionary
);
937 CFStringRef
CFDictionaryCopySuperCompactDescription(CFDictionaryRef dictionary
);
940 // MARK: CFCalendar helpers
943 void SecCFCalendarDoWithZuluCalendar(void(^action
)(CFCalendarRef zuluCalendar
));
946 // MARK: CFAbsoluteTime helpers
949 static inline CFAbsoluteTime
CFAbsoluteTimeForCalendarMoment(CFCalendarRef cal
, int year
, int month
, int day
, int hour
, int minute
, int second
) {
951 CFCalendarComposeAbsoluteTime(cal
, &at
, "yMdHms", year
, month
, day
, hour
, minute
, second
);
955 static inline CFAbsoluteTime
CFAbsoluteTimeForCalendarDay(CFCalendarRef cal
, int year
, int month
, int day
) {
957 CFCalendarComposeAbsoluteTime(cal
, &at
, "yMd", year
, month
, day
);
961 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianMoment(CFTimeZoneRef tz
, int year
, int month
, int day
, int hour
, int minute
, int second
)
963 CFCalendarRef cal
= CFCalendarCreateWithIdentifier(NULL
, kCFGregorianCalendar
);
964 CFCalendarSetTimeZone(cal
, tz
);
965 CFAbsoluteTime at
= CFAbsoluteTimeForCalendarMoment(cal
, year
, month
, day
, hour
, minute
, second
);
970 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianDay(CFTimeZoneRef tz
, int year
, int month
, int day
)
972 CFCalendarRef cal
= CFCalendarCreateWithIdentifier(NULL
, kCFGregorianCalendar
);
973 CFCalendarSetTimeZone(cal
, tz
);
974 CFAbsoluteTime at
= CFAbsoluteTimeForCalendarDay(cal
, year
, month
, day
);
979 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianZuluMoment(int year
, int month
, int day
, int hour
, int minute
, int second
)
981 __block CFAbsoluteTime result
= 0.0;
982 SecCFCalendarDoWithZuluCalendar(^(CFCalendarRef zuluCalendar
) {
983 result
= CFAbsoluteTimeForCalendarMoment(zuluCalendar
, year
, month
, day
, hour
, minute
, second
);
989 static inline CFAbsoluteTime
CFAbsoluteTimeForGregorianZuluDay(int year
, int month
, int day
)
991 __block CFAbsoluteTime result
= 0.0;
992 SecCFCalendarDoWithZuluCalendar(^(CFCalendarRef zuluCalendar
) {
993 result
= CFAbsoluteTimeForCalendarDay(zuluCalendar
, year
, month
, day
);
1001 // MARK: CFDate Helpers
1004 static inline CFDateRef
CFDateCreateForGregorianMoment(CFAllocatorRef allocator
, CFTimeZoneRef tz
, int year
, int month
, int day
, int hour
, int minute
, int second
)
1006 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianMoment(tz
, year
, month
, day
, hour
, minute
, second
));
1009 static inline CFDateRef
CFDateCreateForGregorianDay(CFAllocatorRef allocator
, CFTimeZoneRef tz
, int year
, int month
, int day
, int hour
, int minute
, int second
)
1011 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianDay(tz
, year
, month
, day
));
1014 static inline CFDateRef
CFDateCreateForGregorianZuluMoment(CFAllocatorRef allocator
, int year
, int month
, int day
, int hour
, int minute
, int second
)
1016 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianZuluMoment(year
, month
, day
, hour
, minute
, second
));
1019 static inline CFDateRef
CFDateCreateForGregorianZuluDay(CFAllocatorRef allocator
, int year
, int month
, int day
)
1021 return CFDateCreate(allocator
, CFAbsoluteTimeForGregorianZuluDay(year
, month
, day
));
1025 // MARK: PropertyList Helpers
1029 // Crazy reading and writing stuff
1032 static inline void CFPropertyListWriteToFile(CFPropertyListRef plist
, CFURLRef file
)
1034 CFWriteStreamRef writeStream
= CFWriteStreamCreateWithFile(kCFAllocatorDefault
, file
);
1035 CFErrorRef error
= NULL
;
1037 CFWriteStreamOpen(writeStream
);
1038 CFPropertyListWrite(plist
, writeStream
, kCFPropertyListBinaryFormat_v1_0
, 0, &error
);
1040 secerror("Can't write plist: %@", error
);
1042 CFReleaseNull(error
);
1043 CFReleaseNull(writeStream
);
1046 static inline CF_RETURNS_RETAINED CFPropertyListRef
CFPropertyListReadFromFile(CFURLRef file
)
1048 CFPropertyListRef result
= NULL
;
1049 CFErrorRef error
= NULL
;
1050 CFBooleanRef isRegularFile
;
1051 if (!CFURLCopyResourcePropertyForKey(file
, kCFURLIsRegularFileKey
, &isRegularFile
, &error
)) {
1052 secinfo("plist", "file %@: %@", file
, error
);
1053 } else if (CFBooleanGetValue(isRegularFile
)) {
1054 CFReadStreamRef readStream
= CFReadStreamCreateWithFile(kCFAllocatorDefault
, file
);
1056 if (CFReadStreamOpen(readStream
)) {
1057 CFPropertyListFormat format
;
1058 result
= CFPropertyListCreateWithStream(kCFAllocatorDefault
, readStream
, 0, kCFPropertyListMutableContainers
, &format
, &error
);
1060 secerror("read plist from %@: %@", file
, error
);
1063 CFRelease(readStream
);
1066 CFReleaseNull(error
);
1073 #endif /* _SECCFWRAPPERS_H_ */