]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/src/SecCFError.h
Security-57740.60.18.tar.gz
[apple/security.git] / OSX / utilities / src / SecCFError.h
1 /*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #ifndef _SECCFERROR_H_
26 #define _SECCFERROR_H_
27
28 #include <CoreFoundation/CoreFoundation.h>
29 #include <utilities/SecCFRelease.h>
30
31 //
32 // Leaf error creation from other systems
33 //
34
35 // kern_return_t errors
36 #define kSecKernDomain kCFErrorDomainMach
37 bool SecKernError(kern_return_t result, CFErrorRef *error, CFStringRef format, ...);
38
39 // Unix errno errors
40 #define kSecErrnoDomain kCFErrorDomainPOSIX
41 bool SecCheckErrno(int result, CFErrorRef *error, CFStringRef format, ...);
42
43 // OSStatus errors
44 #define kSecErrorDomain kCFErrorDomainOSStatus
45 bool SecError(OSStatus status, CFErrorRef *error, CFStringRef format, ...);
46
47 // Direct checking of POSIX errors.
48 bool SecPOSIXError(int error, CFErrorRef *cferror, CFStringRef format, ...);
49
50 // CoreCrypto error
51 #define kSecCoreCryptoDomain CFSTR("kSecCoreCryptoDomain")
52 bool SecCoreCryptoError(int error, CFErrorRef *cferror, CFStringRef format, ...);
53
54 // Notification
55 #define kSecNotifyDomain CFSTR("kSecNotifyDomain")
56 bool SecNotifyError(uint32_t result, CFErrorRef *error, CFStringRef format, ...);
57
58 // requirement error, typically parameters
59 bool SecRequirementError(bool requirement, CFErrorRef *error, CFStringRef format, ...);
60
61 // Allocation failure
62 bool SecAllocationError(const void *allocated, CFErrorRef *error, CFStringRef format, ...);
63
64
65 //
66 // Create and chain, all return false to make the analyzer happy.
67 //
68 bool SecCFCreateError(CFIndex errorCode, CFStringRef domain, CFStringRef descriptionString,
69 CFErrorRef previousError, CFErrorRef *newError);
70
71 bool SecCFCreateErrorWithFormat(CFIndex errorCode, CFStringRef domain, CFErrorRef previousError, CFErrorRef *newError,
72 CFDictionaryRef formatoptions, CFStringRef descriptionString, ...)
73 CF_FORMAT_FUNCTION(6,7);
74
75
76 bool SecCFCreateErrorWithFormatAndArguments(CFIndex errorCode, CFStringRef domain,
77 CFErrorRef previousError, CFErrorRef *newError,
78 CFDictionaryRef formatoptions, CFStringRef descriptionString, va_list args)
79 CF_FORMAT_FUNCTION(6, 0);
80
81 //
82 // MARK: Optional value type casting
83 //
84
85 static inline bool asArrayOptional(CFTypeRef cfType, CFArrayRef *array, CFErrorRef *error) {
86 if (!cfType || CFGetTypeID(cfType) == CFArrayGetTypeID()) {
87 if (array) *array = (CFArrayRef)cfType;
88 return true;
89 }
90 SecError(-50, error, CFSTR("object %@ is not an array"), cfType);
91 return false;
92 }
93
94 static inline bool asDataOptional(CFTypeRef cfType, CFDataRef *data, CFErrorRef *error) {
95 if (!cfType || CFGetTypeID(cfType) == CFDataGetTypeID()) {
96 if (data) *data = (CFDataRef)cfType;
97 return true;
98 }
99 SecError(-50, error, CFSTR("object %@ is not an data"), cfType);
100 return false;
101 }
102
103 static inline bool asSetOptional(CFTypeRef cfType, CFSetRef *set, CFErrorRef *error) {
104 if (!cfType || CFGetTypeID(cfType) == CFSetGetTypeID()) {
105 if (set) *set = (CFSetRef)cfType;
106 return true;
107 }
108 SecError(-50, error, CFSTR("object %@ is not a set"), cfType);
109 return false;
110 }
111
112 //
113 // MARK: Required value type casting
114 //
115
116 //
117 // MARK: Required value type casting
118 //
119
120 static inline CFArrayRef copyIfArray(CFTypeRef cfType, CFErrorRef *error) {
121 if (cfType && CFGetTypeID(cfType) == CFArrayGetTypeID())
122 return (CFArrayRef)CFRetainSafe(cfType);
123 SecError(-50, error, CFSTR("object %@ is not an array"), cfType);
124 return NULL;
125 }
126
127 static inline CFBooleanRef copyIfBoolean(CFTypeRef cfType, CFErrorRef *error) {
128 if (cfType && CFGetTypeID(cfType) == CFBooleanGetTypeID())
129 return (CFBooleanRef)CFRetainSafe(cfType);
130 SecError(-50, error, CFSTR("object %@ is not an boolean"), cfType);
131 return NULL;
132 }
133
134 static inline CFDataRef copyIfData(CFTypeRef cfType, CFErrorRef *error) {
135 if (cfType && CFGetTypeID(cfType) == CFDataGetTypeID())
136 return (CFDataRef)CFRetainSafe(cfType);
137 SecError(-50, error, CFSTR("object %@ is not a data"), cfType);
138 return NULL;
139 }
140
141 static inline CFDateRef copyIfDate(CFTypeRef cfType, CFErrorRef *error) {
142 if (cfType && CFGetTypeID(cfType) == CFDateGetTypeID())
143 return (CFDateRef)CFRetainSafe(cfType);
144 SecError(-50, error, CFSTR("object %@ is not a date"), cfType);
145 return NULL;
146 }
147
148 static inline CFDictionaryRef copyIfDictionary(CFTypeRef cfType, CFErrorRef *error) {
149 if (cfType && CFGetTypeID(cfType) == CFDictionaryGetTypeID())
150 return (CFDictionaryRef)CFRetainSafe(cfType);
151 SecError(-50, error, CFSTR("object %@ is not a dictionary"), cfType);
152 return NULL;
153 }
154
155 static inline CFSetRef copyIfSet(CFTypeRef cfType, CFErrorRef *error) {
156 if (cfType && CFGetTypeID(cfType) == CFSetGetTypeID())
157 return (CFSetRef)CFRetainSafe(cfType);
158 SecError(-50, error, CFSTR("object %@ is not a set"), cfType);
159 return NULL;
160 }
161
162 static inline CFStringRef copyIfString(CFTypeRef cfType, CFErrorRef *error) {
163 if (cfType && CFGetTypeID(cfType) == CFStringGetTypeID())
164 return (CFStringRef)CFRetainSafe(cfType);
165 SecError(-50, error, CFSTR("object %@ is not a string"), cfType);
166 return NULL;
167 }
168
169 static inline CFUUIDRef copyIfUUID(CFTypeRef cfType, CFErrorRef *error) {
170 if (cfType && CFGetTypeID(cfType) == CFUUIDGetTypeID())
171 return (CFUUIDRef)CFRetainSafe(cfType);
172 SecError(-50, error, CFSTR("object %@ is not a UUID"), cfType);
173 return NULL;
174 }
175
176 //
177 // MARK: Analyzer confusing asXxx casting
178 //
179 static inline CFArrayRef asArray(CFTypeRef cfType, CFErrorRef *error) {
180 if (cfType && CFGetTypeID(cfType) == CFArrayGetTypeID())
181 return (CFArrayRef)cfType;
182 SecError(-50, error, CFSTR("object %@ is not an array"), cfType);
183 return NULL;
184 }
185
186 static inline CFBooleanRef asBoolean(CFTypeRef cfType, CFErrorRef *error) {
187 if (cfType && CFGetTypeID(cfType) == CFBooleanGetTypeID())
188 return (CFBooleanRef)cfType;
189 SecError(-50, error, CFSTR("object %@ is not an boolean"), cfType);
190 return NULL;
191 }
192
193 static inline CFDataRef asData(CFTypeRef cfType, CFErrorRef *error) {
194 if (cfType && CFGetTypeID(cfType) == CFDataGetTypeID())
195 return (CFDataRef)cfType;
196 SecError(-50, error, CFSTR("object %@ is not a data"), cfType);
197 return NULL;
198 }
199
200 static inline CFDateRef asDate(CFTypeRef cfType, CFErrorRef *error) {
201 if (cfType && CFGetTypeID(cfType) == CFDateGetTypeID())
202 return (CFDateRef)cfType;
203 SecError(-50, error, CFSTR("object %@ is not a date"), cfType);
204 return NULL;
205 }
206
207 static inline CFDictionaryRef asDictionary(CFTypeRef cfType, CFErrorRef *error) {
208 if (cfType && CFGetTypeID(cfType) == CFDictionaryGetTypeID())
209 return (CFDictionaryRef)cfType;
210 SecError(-50, error, CFSTR("object %@ is not a dictionary"), cfType);
211 return NULL;
212 }
213
214 static inline CFSetRef asSet(CFTypeRef cfType, CFErrorRef *error) {
215 if (cfType && CFGetTypeID(cfType) == CFSetGetTypeID())
216 return (CFSetRef)cfType;
217 SecError(-50, error, CFSTR("object %@ is not a set"), cfType);
218 return NULL;
219 }
220
221 static inline CFStringRef asString(CFTypeRef cfType, CFErrorRef *error) {
222 if (cfType && CFGetTypeID(cfType) == CFStringGetTypeID())
223 return (CFStringRef)cfType;
224 SecError(-50, error, CFSTR("object %@ is not a string"), cfType);
225 return NULL;
226 }
227
228 static inline CFUUIDRef asUUID(CFTypeRef cfType, CFErrorRef *error) {
229 if (cfType && CFGetTypeID(cfType) == CFUUIDGetTypeID())
230 return (CFUUIDRef)cfType;
231 SecError(-50, error, CFSTR("object %@ is not a UUID"), cfType);
232 return NULL;
233 }
234
235 #endif /* _SECCFERROR_H_ */