]> git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/errors.cpp
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_utilities / lib / errors.cpp
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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
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
132 //
133 // CFError exceptions
134 //
135 CFError::CFError()
136 {
137 SECURITY_EXCEPTION_THROW_CF(this);
138 }
139
140 const char *CFError::what() const throw ()
141 { return "CoreFoundation error"; }
142
143 // can't get this from CarbonCore/MacErrors, but it's too good to pass up
144 enum {
145 coreFoundationUnknownErr = -4960
146 };
147
148 OSStatus CFError::osStatus() const
149 { return coreFoundationUnknownErr; }
150
151 int CFError::unixError() const
152 {
153 return EFAULT; // nothing really matches
154 }
155
156 void CFError::throwMe()
157 { throw CFError(); }