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