dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libldap5 / sources / ldap / ssldap / errormap.c
blobb3c276f446801e673b2486bd3bc7c1b0fa5a978a
1 /*
2 * Copyright (c) 2001 by Sun Microsystems, Inc.
3 * All rights reserved.
4 */
6 /*
7 * The contents of this file are subject to the Netscape Public
8 * License Version 1.1 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of
10 * the License at http://www.mozilla.org/NPL/
12 * Software distributed under the License is distributed on an "AS
13 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * rights and limitations under the License.
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
20 * The Initial Developer of the Original Code is Netscape
21 * Communications Corporation. Portions created by Netscape are
22 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
23 * Rights Reserved.
25 * Contributor(s):
28 #pragma ident "%Z%%M% %I% %E% SMI"
31 * errormap.c - map NSPR and OS errors to strings
33 * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF NETSCAPE COMMUNICATIONS
34 * CORPORATION
36 * Copyright (C) 1998-9 Netscape Communications Corporation. All Rights Reserved.
38 * Use of this Source Code is subject to the terms of the applicable license
39 * agreement from Netscape Communications Corporation.
41 * The copyright notice(s) in this Source Code does not indicate actual or
42 * intended publication of this Source Code.
45 /* XXX ceb
46 * This code was stolen from Directory server.
47 * ns/netsite/ldap/servers/slapd/errormap.c
48 * OS errors are not handled, so the os error has been removed.
52 #if defined( _WINDOWS )
53 #include <windows.h>
54 #include "proto-ntutil.h"
55 #endif
57 #include <nspr.h>
58 #include <ssl.h>
60 #include <ldap.h>
62 #ifdef _SOLARIS_SDK
63 #include <synch.h>
64 #include <libintl.h>
65 #endif /* _SOLARIS_SDK */
69 * function protoypes
71 static const char *SECU_Strerror(PRErrorCode errNum);
76 * return the string equivalent of an NSPR error
79 const char *
80 LDAP_CALL
81 ldapssl_err2string( const int prerrno )
83 const char *s;
85 if (( s = SECU_Strerror( (PRErrorCode)prerrno )) == NULL ) {
86 s = dgettext(TEXT_DOMAIN, "unknown");
89 return( s );
93 ****************************************************************************
94 * The code below this point was provided by Nelson Bolyard <nelsonb> of the
95 * Netscape Certificate Server team on 27-March-1998.
96 * Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
97 * Last updated from there: 24-July-1998 by Mark Smith <mcs>
98 * Last updated from there: 14-July-1999 by chuck boatwright <cboatwri>
101 * All of the Directory Server specific changes are enclosed inside
102 * #ifdef NS_DIRECTORY.
103 ****************************************************************************
105 #include "nspr.h"
108 * XXXceb as a hack, we will locally define NS_DIRECTORY
110 #define NS_DIRECTORY 1
112 struct tuple_str {
113 PRErrorCode errNum;
114 const char * errString;
117 typedef struct tuple_str tuple_str;
119 #ifndef _SOLARIS_SDK
120 #define ER2(a,b) {a, b},
121 #define ER3(a,b,c) {a, c},
122 #else
123 #define ER2(a,b) {a, NULL},
124 #define ER3(a,b,c) {a, NULL},
125 #endif
127 #include "secerr.h"
128 #include "sslerr.h"
130 #ifndef _SOLARIS_SDK
131 const tuple_str errStrings[] = {
132 #else
133 tuple_str errStrings[] = {
134 #endif
136 /* keep this list in asceding order of error numbers */
137 #ifdef NS_DIRECTORY
138 #include "sslerrstrs.h"
139 #include "secerrstrs.h"
140 #include "prerrstrs.h"
142 * XXXceb -- LDAPSDK won't care about disconnect
143 #include "disconnect_error_strings.h"
146 #else /* NS_DIRECTORY */
147 #include "SSLerrs.h"
148 #include "SECerrs.h"
149 #include "NSPRerrs.h"
150 #endif /* NS_DIRECTORY */
154 const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
156 /* Returns a UTF-8 encoded constant error string for "errNum".
157 * Returns NULL of errNum is unknown.
159 #ifndef _SOLARIS_SDK
160 #ifdef NS_DIRECTORY
161 static
162 #endif /* NS_DIRECTORY */
163 const char *
164 SECU_Strerror(PRErrorCode errNum) {
165 PRInt32 low = 0;
166 PRInt32 high = numStrings - 1;
167 PRInt32 i;
168 PRErrorCode num;
169 static int initDone;
171 /* make sure table is in ascending order.
172 * binary search depends on it.
174 if (!initDone) {
175 PRErrorCode lastNum = 0x80000000;
176 for (i = low; i <= high; ++i) {
177 num = errStrings[i].errNum;
178 if (num <= lastNum) {
181 * XXXceb
182 * We aren't handling out of sequence errors.
186 #if 0
187 #ifdef NS_DIRECTORY
188 LDAPDebug( LDAP_DEBUG_ANY,
189 "sequence error in error strings at item %d\n"
190 "error %d (%s)\n",
191 i, lastNum, errStrings[i-1].errString );
192 LDAPDebug( LDAP_DEBUG_ANY,
193 "should come after \n"
194 "error %d (%s)\n",
195 num, errStrings[i].errString, 0 );
196 #else /* NS_DIRECTORY */
197 fprintf(stderr,
198 "sequence error in error strings at item %d\n"
199 "error %d (%s)\n"
200 "should come after \n"
201 "error %d (%s)\n",
202 i, lastNum, errStrings[i-1].errString,
203 num, errStrings[i].errString);
204 #endif /* NS_DIRECTORY */
205 #endif /* 0 */
207 lastNum = num;
209 initDone = 1;
212 /* Do binary search of table. */
213 while (low + 1 < high) {
214 i = (low + high) / 2;
215 num = errStrings[i].errNum;
216 if (errNum == num)
217 return errStrings[i].errString;
218 if (errNum < num)
219 high = i;
220 else
221 low = i;
223 if (errNum == errStrings[low].errNum)
224 return errStrings[low].errString;
225 if (errNum == errStrings[high].errNum)
226 return errStrings[high].errString;
227 return NULL;
229 #else /* _SOLARIS_SDK */
230 #undef ER3
231 #define ER3(x, y, z) case (x): \
232 s = (z); \
233 break;
234 #undef ER2
235 #define ER2(x, y) case (x): \
236 s = (y); \
237 break;
239 static mutex_t err_mutex = DEFAULTMUTEX;
241 static const char *
242 getErrString(PRInt32 i, PRErrorCode errNum)
244 char *s;
246 mutex_lock(&err_mutex);
248 if (errStrings[i].errString != NULL) {
249 mutex_unlock(&err_mutex);
250 return (errStrings[i].errString);
253 switch (errNum) {
254 #include "sslerrstrs.h"
255 #include "secerrstrs.h"
256 #include "prerrstrs.h"
257 default:
258 s = NULL;
259 break;
261 errStrings[i].errString = s;
262 mutex_unlock(&err_mutex);
263 return (s);
266 static
267 const char *
268 SECU_Strerror(PRErrorCode errNum) {
269 PRInt32 low = 0;
270 PRInt32 high = numStrings - 1;
271 PRInt32 i;
272 PRErrorCode num;
274 /* ASSUME table is in ascending order.
275 * binary search depends on it.
278 /* Do binary search of table. */
279 while (low + 1 < high) {
280 i = (low + high) / 2;
281 num = errStrings[i].errNum;
282 if (errNum == num)
283 return getErrString(i, errNum);
284 if (errNum < num)
285 high = i;
286 else
287 low = i;
289 if (errNum == errStrings[low].errNum)
290 return getErrString(low, errNum);
291 if (errNum == errStrings[high].errNum)
292 return getErrString(high, errNum);
293 return NULL;
295 #endif