]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/SecCFError.h
Security-59754.41.1.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 // requirement error, typically parameters
51 bool SecRequirementError(bool requirement, CFErrorRef *error, CFStringRef format, ...)
52 CF_FORMAT_FUNCTION(3, 4);
53
54 // Allocation failure
55 bool SecAllocationError(const void *allocated, CFErrorRef *error, CFStringRef format, ...)
56 CF_FORMAT_FUNCTION(3, 4);
57
58 //
59 // Create and chain, all return false to make the analyzer happy.
60 //
61 #define SecCFCreateError(errorCode, domain, descriptionString, previousError, newError) \
62 SecCFCreateErrorWithFormat(errorCode, domain, previousError, newError, NULL, descriptionString)
63
64 bool SecCFCreateErrorWithFormat(CFIndex errorCode, CFStringRef domain, CFErrorRef previousError, CFErrorRef *newError,
65 CFDictionaryRef formatoptions, CFStringRef descriptionString, ...)
66 CF_FORMAT_FUNCTION(6,7);
67
68
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);
73
74 //
75 // MARK: Optional value type casting
76 //
77
78 static inline bool asArrayOptional(CFTypeRef cfType, CFArrayRef *array, CFErrorRef *error) {
79 if (!cfType || CFGetTypeID(cfType) == CFArrayGetTypeID()) {
80 if (array) *array = (CFArrayRef)cfType;
81 return true;
82 }
83 if(error) {
84 SecError(-50, error, CFSTR("object %@ is not an array"), cfType);
85 }
86 return false;
87 }
88
89 static inline bool asDataOptional(CFTypeRef cfType, CFDataRef *data, CFErrorRef *error) {
90 if (!cfType || CFGetTypeID(cfType) == CFDataGetTypeID()) {
91 if (data) *data = (CFDataRef)cfType;
92 return true;
93 }
94 if(error) {
95 SecError(-50, error, CFSTR("object %@ is not an data"), cfType);
96 }
97 return false;
98 }
99
100 //
101 // MARK: Required value type casting
102 //
103
104 static inline CFDataRef copyIfData(CFTypeRef cfType, CFErrorRef *error) {
105 if (cfType && CFGetTypeID(cfType) == CFDataGetTypeID()) {
106 return (CFDataRef)CFRetainSafe(cfType);
107 }
108 if(error) {
109 SecError(-50, error, CFSTR("object %@ is not a data"), cfType);
110 }
111 return NULL;
112 }
113
114 static inline CFSetRef copyIfSet(CFTypeRef cfType, CFErrorRef *error) {
115 if (cfType && CFGetTypeID(cfType) == CFSetGetTypeID()) {
116 return (CFSetRef)CFRetainSafe(cfType);
117 }
118 if(error) {
119 SecError(-50, error, CFSTR("object %@ is not a set"), cfType);
120 }
121 return NULL;
122 }
123
124 //
125 // MARK: Analyzer confusing asXxx casting
126 //
127 static inline CFArrayRef asArray(CFTypeRef cfType, CFErrorRef *error) {
128 if (cfType && CFGetTypeID(cfType) == CFArrayGetTypeID()) {
129 return (CFArrayRef)cfType;
130 }
131 if(error) {
132 SecError(-50, error, CFSTR("object %@ is not an array"), cfType);
133 }
134 return NULL;
135 }
136
137 static inline CFBooleanRef asBoolean(CFTypeRef cfType, CFErrorRef *error) {
138 if (cfType && CFGetTypeID(cfType) == CFBooleanGetTypeID()) {
139 return (CFBooleanRef)cfType;
140 }
141 if(error) {
142 SecError(-50, error, CFSTR("object %@ is not an boolean"), cfType);
143 }
144 return NULL;
145 }
146
147 static inline CFDataRef asData(CFTypeRef cfType, CFErrorRef *error) {
148 if (cfType && CFGetTypeID(cfType) == CFDataGetTypeID()) {
149 return (CFDataRef)cfType;
150 }
151 if(error) {
152 SecError(-50, error, CFSTR("object %@ is not a data"), cfType);
153 }
154 return NULL;
155 }
156
157 static inline CFDateRef asDate(CFTypeRef cfType, CFErrorRef *error) {
158 if (cfType && CFGetTypeID(cfType) == CFDateGetTypeID()) {
159 return (CFDateRef)cfType;
160 }
161 if(error) {
162 SecError(-50, error, CFSTR("object %@ is not a date"), cfType);
163 }
164 return NULL;
165 }
166
167 static inline CFDictionaryRef asDictionary(CFTypeRef cfType, CFErrorRef *error) {
168 if (cfType && CFGetTypeID(cfType) == CFDictionaryGetTypeID()) {
169 return (CFDictionaryRef)cfType;
170 }
171 if(error) {
172 SecError(-50, error, CFSTR("object %@ is not a dictionary"), cfType);
173 }
174 return NULL;
175 }
176
177 static inline CFSetRef asSet(CFTypeRef cfType, CFErrorRef *error) {
178 if (cfType && CFGetTypeID(cfType) == CFSetGetTypeID()) {
179 return (CFSetRef)cfType;
180 }
181 if(error) {
182 SecError(-50, error, CFSTR("object %@ is not a set"), cfType);
183 }
184 return NULL;
185 }
186
187 static inline CFStringRef asString(CFTypeRef cfType, CFErrorRef *error) {
188 if (cfType && CFGetTypeID(cfType) == CFStringGetTypeID()) {
189 return (CFStringRef)cfType;
190 }
191 if(error) {
192 SecError(-50, error, CFSTR("object %@ is not a string"), cfType);
193 }
194 return NULL;
195 }
196
197 #endif /* _SECCFERROR_H_ */