]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/errors.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / errors.cpp
1 /*
2 * Copyright (c) 2000-2004,2011-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 //
26 // Error hierarchy
27 //
28 #include <security_utilities/errors.h>
29 #include <security_utilities/debugging.h>
30 #include <typeinfo>
31 #include <stdio.h>
32 #include <Security/SecBase.h>
33
34 //@@@
35 // From cssmapple.h - layering break
36 // Where should this go?
37 //@@@
38 #define errSecErrnoBase 100000
39 #define errSecErrnoLimit 100255
40
41 //
42 // The base of the exception hierarchy.
43 // Note that the debug output here depends on a particular
44 // implementation feature of gcc; to wit, that the exception object
45 // is created and then copied (at least once) via its copy constructor.
46 // If your compiler does not invoke the copy constructor, you won't get
47 // debug output, but nothing worse should happen.
48 //
49 CommonError::CommonError()
50 {
51 }
52
53
54 //
55 // We strongly encourage catching all exceptions by const reference, so the copy
56 // constructor of our exceptions should never be called.
57 // We trace a copy to help catch violations of this rule.
58 //
59 CommonError::CommonError(const CommonError &source)
60 {
61 SECURITY_EXCEPTION_COPY(this, &source);
62 }
63
64 CommonError::~CommonError() throw ()
65 {
66 SECURITY_EXCEPTION_HANDLED(this);
67 }
68
69
70 //
71 // UnixError exceptions
72 //
73 UnixError::UnixError() : error(errno)
74 {
75 SECURITY_EXCEPTION_THROW_UNIX(this, errno);
76 }
77
78 UnixError::UnixError(int err) : error(err)
79 {
80 SECURITY_EXCEPTION_THROW_UNIX(this, err);
81 }
82
83 const char *UnixError::what() const throw ()
84 { return "UNIX error exception"; }
85
86
87 OSStatus UnixError::osStatus() const
88 {
89 return error + errSecErrnoBase;
90 }
91
92 int UnixError::unixError() const
93 { return error; }
94
95 void UnixError::throwMe(int err) { throw UnixError(err); }
96
97 // @@@ This is a hack for the Network protocol state machine
98 UnixError UnixError::make(int err) { return UnixError(err); }
99
100
101 //
102 // MacOSError exceptions
103 //
104 MacOSError::MacOSError(int err) : error(err)
105 {
106 SECURITY_EXCEPTION_THROW_OSSTATUS(this, err);
107 }
108
109 const char *MacOSError::what() const throw ()
110 { return "MacOS error"; }
111
112 OSStatus MacOSError::osStatus() const
113 { return error; }
114
115 int MacOSError::unixError() const
116 {
117 // embedded UNIX errno values are returned verbatim
118 if (error >= errSecErrnoBase && error <= errSecErrnoLimit)
119 return error - errSecErrnoBase;
120
121 switch (error) {
122 default:
123 // cannot map this to errno space
124 return -1;
125 }
126 }
127
128 void MacOSError::throwMe(int error)
129 { throw MacOSError(error); }
130
131 MacOSError MacOSError::make(int error)
132 { return MacOSError(error); }
133
134
135 //
136 // CFError exceptions
137 //
138 CFError::CFError()
139 {
140 SECURITY_EXCEPTION_THROW_CF(this);
141 }
142
143 const char *CFError::what() const throw ()
144 { return "CoreFoundation error"; }
145
146 OSStatus CFError::osStatus() const
147 { return errSecCoreFoundationUnknown; }
148
149 int CFError::unixError() const
150 {
151 return EFAULT; // nothing really matches
152 }
153
154 void CFError::throwMe()
155 { throw CFError(); }
156
157
158
159
160 void ModuleNexusError::throwMe()
161 {
162 throw ModuleNexusError();
163 }
164
165
166
167 OSStatus ModuleNexusError::osStatus() const
168 {
169 return errSecParam;
170 }
171
172
173
174 int ModuleNexusError::unixError() const
175 {
176 return EINVAL;
177 }
178