2 * Copyright (c) 2012-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 _SECCFERROR_H_
26 #define _SECCFERROR_H_
28 #include <CoreFoundation/CoreFoundation.h>
29 #include "utilities/SecCFRelease.h"
32 // Leaf error creation from other systems
35 // kern_return_t errors
36 #define kSecKernDomain kCFErrorDomainMach
37 bool SecKernError(kern_return_t result
, CFErrorRef
*error
, CFStringRef format
, ...)
38 CF_FORMAT_FUNCTION(3, 4);
41 #define kSecErrnoDomain kCFErrorDomainPOSIX
42 bool SecCheckErrno(int result
, CFErrorRef
*error
, CFStringRef format
, ...)
43 CF_FORMAT_FUNCTION(3, 4);
46 #define kSecErrorDomain kCFErrorDomainOSStatus
47 bool SecError(OSStatus status
, CFErrorRef
*error
, CFStringRef format
, ...)
48 CF_FORMAT_FUNCTION(3, 4);
50 // requirement error, typically parameters
51 bool SecRequirementError(bool requirement
, CFErrorRef
*error
, CFStringRef format
, ...)
52 CF_FORMAT_FUNCTION(3, 4);
55 bool SecAllocationError(const void *allocated
, CFErrorRef
*error
, CFStringRef format
, ...)
56 CF_FORMAT_FUNCTION(3, 4);
59 // Create and chain, all return false to make the analyzer happy.
61 #define SecCFCreateError(errorCode, domain, descriptionString, previousError, newError) \
62 SecCFCreateErrorWithFormat(errorCode, domain, previousError, newError, NULL, descriptionString)
64 bool SecCFCreateErrorWithFormat(CFIndex errorCode
, CFStringRef domain
, CFErrorRef previousError
, CFErrorRef
*newError
,
65 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, ...)
66 CF_FORMAT_FUNCTION(6,7);
69 bool SecCFCreateErrorWithFormatAndArguments(CFIndex errorCode
, CFStringRef domain
,
70 CFErrorRef previousError
, CFErrorRef
*newError
,
71 CFDictionaryRef formatoptions
, CFStringRef descriptionString
, va_list args
)
72 CF_FORMAT_FUNCTION(6, 0);
75 // MARK: Optional value type casting
78 static inline bool asArrayOptional(CFTypeRef cfType
, CFArrayRef
*array
, CFErrorRef
*error
) {
79 if (!cfType
|| CFGetTypeID(cfType
) == CFArrayGetTypeID()) {
80 if (array
) *array
= (CFArrayRef
)cfType
;
84 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
89 static inline bool asDataOptional(CFTypeRef cfType
, CFDataRef
*data
, CFErrorRef
*error
) {
90 if (!cfType
|| CFGetTypeID(cfType
) == CFDataGetTypeID()) {
91 if (data
) *data
= (CFDataRef
)cfType
;
95 SecError(-50, error
, CFSTR("object %@ is not an data"), cfType
);
101 // MARK: Required value type casting
104 static inline CFDataRef
copyIfData(CFTypeRef cfType
, CFErrorRef
*error
) {
105 if (cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID()) {
106 return (CFDataRef
)CFRetainSafe(cfType
);
109 SecError(-50, error
, CFSTR("object %@ is not a data"), cfType
);
114 static inline CFSetRef
copyIfSet(CFTypeRef cfType
, CFErrorRef
*error
) {
115 if (cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID()) {
116 return (CFSetRef
)CFRetainSafe(cfType
);
119 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
125 // MARK: Analyzer confusing asXxx casting
127 static inline CFArrayRef
asArray(CFTypeRef cfType
, CFErrorRef
*error
) {
128 if (cfType
&& CFGetTypeID(cfType
) == CFArrayGetTypeID()) {
129 return (CFArrayRef
)cfType
;
132 SecError(-50, error
, CFSTR("object %@ is not an array"), cfType
);
137 static inline CFBooleanRef
asBoolean(CFTypeRef cfType
, CFErrorRef
*error
) {
138 if (cfType
&& CFGetTypeID(cfType
) == CFBooleanGetTypeID()) {
139 return (CFBooleanRef
)cfType
;
142 SecError(-50, error
, CFSTR("object %@ is not an boolean"), cfType
);
147 static inline CFDataRef
asData(CFTypeRef cfType
, CFErrorRef
*error
) {
148 if (cfType
&& CFGetTypeID(cfType
) == CFDataGetTypeID()) {
149 return (CFDataRef
)cfType
;
152 SecError(-50, error
, CFSTR("object %@ is not a data"), cfType
);
157 static inline CFDateRef
asDate(CFTypeRef cfType
, CFErrorRef
*error
) {
158 if (cfType
&& CFGetTypeID(cfType
) == CFDateGetTypeID()) {
159 return (CFDateRef
)cfType
;
162 SecError(-50, error
, CFSTR("object %@ is not a date"), cfType
);
167 static inline CFDictionaryRef
asDictionary(CFTypeRef cfType
, CFErrorRef
*error
) {
168 if (cfType
&& CFGetTypeID(cfType
) == CFDictionaryGetTypeID()) {
169 return (CFDictionaryRef
)cfType
;
172 SecError(-50, error
, CFSTR("object %@ is not a dictionary"), cfType
);
177 static inline CFSetRef
asSet(CFTypeRef cfType
, CFErrorRef
*error
) {
178 if (cfType
&& CFGetTypeID(cfType
) == CFSetGetTypeID()) {
179 return (CFSetRef
)cfType
;
182 SecError(-50, error
, CFSTR("object %@ is not a set"), cfType
);
187 static inline CFStringRef
asString(CFTypeRef cfType
, CFErrorRef
*error
) {
188 if (cfType
&& CFGetTypeID(cfType
) == CFStringGetTypeID()) {
189 return (CFStringRef
)cfType
;
192 SecError(-50, error
, CFSTR("object %@ is not a string"), cfType
);
197 #endif /* _SECCFERROR_H_ */