]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/src/SecCFError.h
Security-57740.31.2.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
30 //
31 // Leaf error creation from other systems
32 //
33
34 // kern_return_t errors
35 #define kSecKernDomain kCFErrorDomainMach
36 bool SecKernError(kern_return_t result, CFErrorRef *error, CFStringRef format, ...);
37
38 // Unix errno errors
39 #define kSecErrnoDomain kCFErrorDomainPOSIX
40 bool SecCheckErrno(int result, CFErrorRef *error, CFStringRef format, ...);
41
42 // OSStatus errors
43 #define kSecErrorDomain kCFErrorDomainOSStatus
44 bool SecError(OSStatus status, CFErrorRef *error, CFStringRef format, ...);
45
46 // Direct checking of POSIX errors.
47 bool SecPOSIXError(int error, CFErrorRef *cferror, CFStringRef format, ...);
48
49 // CoreCrypto error
50 #define kSecCoreCryptoDomain CFSTR("kSecCoreCryptoDomain")
51 bool SecCoreCryptoError(int error, CFErrorRef *cferror, CFStringRef format, ...);
52
53 // Notification
54 #define kSecNotifyDomain CFSTR("kSecNotifyDomain")
55 bool SecNotifyError(uint32_t result, CFErrorRef *error, CFStringRef format, ...);
56
57 // requirement error, typically parameters
58 bool SecRequirementError(bool requirement, CFErrorRef *error, CFStringRef format, ...);
59
60 // Allocation failure
61 bool SecAllocationError(const void *allocated, CFErrorRef *error, CFStringRef format, ...);
62
63
64 //
65 // Create and chain, all return false to make the analyzer happy.
66 //
67 bool SecCFCreateError(CFIndex errorCode, CFStringRef domain, CFStringRef descriptionString,
68 CFErrorRef previousError, CFErrorRef *newError);
69
70 bool SecCFCreateErrorWithFormat(CFIndex errorCode, CFStringRef domain, CFErrorRef previousError, CFErrorRef *newError,
71 CFDictionaryRef formatoptions, CFStringRef descriptionString, ...)
72 CF_FORMAT_FUNCTION(6,7);
73
74
75 bool SecCFCreateErrorWithFormatAndArguments(CFIndex errorCode, CFStringRef domain,
76 CFErrorRef previousError, CFErrorRef *newError,
77 CFDictionaryRef formatoptions, CFStringRef descriptionString, va_list args)
78 CF_FORMAT_FUNCTION(6, 0);
79
80 //
81 // MARK: Optional value type casting
82 //
83
84 static inline bool asArrayOptional(CFTypeRef cfType, CFArrayRef *array, CFErrorRef *error) {
85 if (!cfType || CFGetTypeID(cfType) == CFArrayGetTypeID()) {
86 if (array) *array = (CFArrayRef)cfType;
87 return true;
88 }
89 SecError(-50, error, CFSTR("object %@ is not an array"), cfType);
90 return false;
91 }
92
93 static inline bool asDataOptional(CFTypeRef cfType, CFDataRef *data, CFErrorRef *error) {
94 if (!cfType || CFGetTypeID(cfType) == CFDataGetTypeID()) {
95 if (data) *data = (CFDataRef)cfType;
96 return true;
97 }
98 SecError(-50, error, CFSTR("object %@ is not an data"), cfType);
99 return false;
100 }
101
102 static inline bool asSetOptional(CFTypeRef cfType, CFSetRef *set, CFErrorRef *error) {
103 if (!cfType || CFGetTypeID(cfType) == CFSetGetTypeID()) {
104 if (set) *set = (CFSetRef)cfType;
105 return true;
106 }
107 SecError(-50, error, CFSTR("object %@ is not a set"), cfType);
108 return false;
109 }
110
111 //
112 // MARK: Required value type casting
113 //
114
115 static inline CFArrayRef asArray(CFTypeRef cfType, CFErrorRef *error) {
116 if (cfType && CFGetTypeID(cfType) == CFArrayGetTypeID())
117 return (CFArrayRef)cfType;
118 SecError(-50, error, CFSTR("object %@ is not an array"), cfType);
119 return NULL;
120 }
121
122 static inline CFBooleanRef asBoolean(CFTypeRef cfType, CFErrorRef *error) {
123 if (cfType && CFGetTypeID(cfType) == CFBooleanGetTypeID())
124 return (CFBooleanRef)cfType;
125 SecError(-50, error, CFSTR("object %@ is not an boolean"), cfType);
126 return NULL;
127 }
128
129 static inline CFDataRef asData(CFTypeRef cfType, CFErrorRef *error) {
130 if (cfType && CFGetTypeID(cfType) == CFDataGetTypeID())
131 return (CFDataRef)cfType;
132 SecError(-50, error, CFSTR("object %@ is not a data"), cfType);
133 return NULL;
134 }
135
136 static inline CFDateRef asDate(CFTypeRef cfType, CFErrorRef *error) {
137 if (cfType && CFGetTypeID(cfType) == CFDateGetTypeID())
138 return (CFDateRef)cfType;
139 SecError(-50, error, CFSTR("object %@ is not a date"), cfType);
140 return NULL;
141 }
142
143 static inline CFDictionaryRef asDictionary(CFTypeRef cfType, CFErrorRef *error) {
144 if (cfType && CFGetTypeID(cfType) == CFDictionaryGetTypeID())
145 return (CFDictionaryRef)cfType;
146 SecError(-50, error, CFSTR("object %@ is not a dictionary"), cfType);
147 return NULL;
148 }
149
150 static inline CFSetRef asSet(CFTypeRef cfType, CFErrorRef *error) {
151 if (cfType && CFGetTypeID(cfType) == CFSetGetTypeID())
152 return (CFSetRef)cfType;
153 SecError(-50, error, CFSTR("object %@ is not a set"), cfType);
154 return NULL;
155 }
156
157 static inline CFStringRef asString(CFTypeRef cfType, CFErrorRef *error) {
158 if (cfType && CFGetTypeID(cfType) == CFStringGetTypeID())
159 return (CFStringRef)cfType;
160 SecError(-50, error, CFSTR("object %@ is not a string"), cfType);
161 return NULL;
162 }
163
164 #endif /* _SECCFERROR_H_ */