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#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//
49CommonError::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//
59CommonError::CommonError(const CommonError &source)
60{
61	SECURITY_EXCEPTION_COPY(this, &source);
62}
63
64CommonError::~CommonError() throw ()
65{
66	SECURITY_EXCEPTION_HANDLED(this);
67}
68
69
70//
71// UnixError exceptions
72//
73UnixError::UnixError() : error(errno)
74{
75	SECURITY_EXCEPTION_THROW_UNIX(this, errno);
76}
77
78UnixError::UnixError(int err) : error(err)
79{
80	SECURITY_EXCEPTION_THROW_UNIX(this, err);
81}
82
83const char *UnixError::what() const throw ()
84{ return "UNIX error exception"; }
85
86
87OSStatus UnixError::osStatus() const
88{
89	return error + errSecErrnoBase;
90}
91
92int UnixError::unixError() const
93{ return error; }
94
95void UnixError::throwMe(int err) { throw UnixError(err); }
96
97// @@@ This is a hack for the Network protocol state machine
98UnixError UnixError::make(int err) { return UnixError(err); }
99
100
101//
102// MacOSError exceptions
103//
104MacOSError::MacOSError(int err) : error(err)
105{
106	SECURITY_EXCEPTION_THROW_OSSTATUS(this, err);
107}
108
109const char *MacOSError::what() const throw ()
110{ return "MacOS error"; }
111
112OSStatus MacOSError::osStatus() const
113{ return error; }
114
115int 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
128void MacOSError::throwMe(int error)
129{ throw MacOSError(error); }
130
131
132//
133// CFError exceptions
134//
135CFError::CFError()
136{
137	SECURITY_EXCEPTION_THROW_CF(this);
138}
139
140const char *CFError::what() const throw ()
141{ return "CoreFoundation error"; }
142
143OSStatus CFError::osStatus() const
144{ return errSecCoreFoundationUnknown; }
145
146int CFError::unixError() const
147{
148	return EFAULT;		// nothing really matches
149}
150
151void CFError::throwMe()
152{ throw CFError(); }
153
154
155
156
157void ModuleNexusError::throwMe()
158{
159    throw ModuleNexusError();
160}
161
162
163
164OSStatus ModuleNexusError::osStatus() const
165{
166    return errSecParam;
167}
168
169
170
171int ModuleNexusError::unixError() const
172{
173    return EINVAL;
174}
175
176