Back to index

lightning-sunbird  0.9+nobinonly
errormap.c
Go to the documentation of this file.
00001 /*
00002  * errormap.c - map NSPR and OS errors to strings
00003  */
00004  
00005 /* ***** BEGIN LICENSE BLOCK *****
00006  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00007  *
00008  * The contents of this file are subject to the Mozilla Public License Version
00009  * 1.1 (the "License"); you may not use this file except in compliance with
00010  * the License. You may obtain a copy of the License at
00011  * http://www.mozilla.org/MPL/
00012  *
00013  * Software distributed under the License is distributed on an "AS IS" basis,
00014  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00015  * for the specific language governing rights and limitations under the
00016  * License.
00017  *
00018  * The Original Code is errormap.c
00019  *
00020  * The Initial Developer of the Original Code is
00021  * Netscape Communications Corp.
00022  * Portions created by the Initial Developer are Copyright (C) 1998-9
00023  * the Initial Developer. All Rights Reserved.
00024  *
00025  * Contributor(s):
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the LGPL or the GPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK *****
00040  */
00041 
00042 /* XXX ceb
00043  * This code was stolen from Directory server.  
00044  * ns/netsite/ldap/servers/slapd/errormap.c
00045  * OS errors are not handled, so the os error has been removed.
00046  */
00047 
00048 
00049 #if defined( _WINDOWS )
00050 #include <windows.h>
00051 #include "proto-ntutil.h"
00052 #endif
00053 
00054 #include <nspr.h>
00055 #include <ssl.h>
00056 
00057 #include <ldap.h>
00058 #include <ldap_ssl.h>
00059 
00060 
00061 /*
00062  * function protoypes
00063  */
00064 static const char *SECU_Strerror(PRErrorCode errNum);
00065 
00066 
00067 
00068 /*
00069  * return the string equivalent of an NSPR error
00070  */
00071 
00072 const char *
00073 LDAP_CALL
00074 ldapssl_err2string( const int prerrno )
00075 {
00076     const char       *s;
00077 
00078     if (( s = SECU_Strerror( (PRErrorCode)prerrno )) == NULL ) {
00079        s = "unknown";
00080     }
00081 
00082     return( s );
00083 }
00084 
00085 /*
00086  ****************************************************************************
00087  * The code below this point was provided by Nelson Bolyard <nelsonb> of the
00088  *     Netscape Certificate Server team on 27-March-1998.
00089  *     Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
00090  *     Last updated from there: 24-July-1998 by Mark Smith <mcs>
00091  *     Last updated from there: 14-July-1999 by chuck boatwright <cboatwri>
00092  *      
00093  *
00094  * All of the Directory Server specific changes are enclosed inside
00095  *     #ifdef NS_DIRECTORY.
00096  ****************************************************************************
00097  */
00098 #include "nspr.h"
00099 
00100 /*
00101  * XXXceb as a hack, we will locally define NS_DIRECTORY
00102  */
00103 #define NS_DIRECTORY 1
00104 
00105 struct tuple_str {
00106     PRErrorCode       errNum;
00107     const char * errString;
00108 };
00109 
00110 typedef struct tuple_str tuple_str;
00111 
00112 #define ER2(a,b)   {a, b},
00113 #define ER3(a,b,c) {a, c},
00114 
00115 #include "secerr.h"
00116 #include "sslerr.h"
00117 
00118 const tuple_str errStrings[] = {
00119 
00120 /* keep this list in asceding order of error numbers */
00121 #ifdef NS_DIRECTORY
00122 #include "sslerrstrs.h"
00123 #include "secerrstrs.h"
00124 #include "prerrstrs.h"
00125 /* 
00126  * XXXceb -- LDAPSDK won't care about disconnect 
00127 #include "disconnect_error_strings.h"
00128  */
00129 
00130 #else /* NS_DIRECTORY */
00131 #include "SSLerrs.h"
00132 #include "SECerrs.h"
00133 #include "NSPRerrs.h"
00134 #endif /* NS_DIRECTORY */
00135 
00136 };
00137 
00138 const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
00139 
00140 /* Returns a UTF-8 encoded constant error string for "errNum".
00141  * Returns NULL of errNum is unknown.
00142  */
00143 #ifdef NS_DIRECTORY
00144 static
00145 #endif /* NS_DIRECTORY */
00146 const char *
00147 SECU_Strerror(PRErrorCode errNum) {
00148     PRInt32 low  = 0;
00149     PRInt32 high = numStrings - 1;
00150     PRInt32 i;
00151     PRErrorCode num;
00152     static int initDone;
00153 
00154     /* make sure table is in ascending order.
00155      * binary search depends on it.
00156      */
00157     if (!initDone) {
00158        PRErrorCode lastNum = 0x80000000;
00159        for (i = low; i <= high; ++i) {
00160            num = errStrings[i].errNum;
00161            if (num <= lastNum) {
00162 
00163 /*
00164  * XXXceb
00165  * We aren't handling out of sequence errors.
00166  */
00167 
00168 
00169 #if 0
00170 #ifdef NS_DIRECTORY
00171               LDAPDebug( LDAP_DEBUG_ANY,
00172                      "sequence error in error strings at item %d\n"
00173                      "error %d (%s)\n",
00174                      i, lastNum, errStrings[i-1].errString );
00175               LDAPDebug( LDAP_DEBUG_ANY,
00176                      "should come after \n"
00177                      "error %d (%s)\n",
00178                      num, errStrings[i].errString, 0 );
00179 #else /* NS_DIRECTORY */
00180               fprintf(stderr, 
00181 "sequence error in error strings at item %d\n"
00182 "error %d (%s)\n"
00183 "should come after \n"
00184 "error %d (%s)\n",
00185                       i, lastNum, errStrings[i-1].errString, 
00186                      num, errStrings[i].errString);
00187 #endif /* NS_DIRECTORY */
00188 #endif /* 0 */
00189            }
00190            lastNum = num;
00191        }
00192        initDone = 1;
00193     }
00194 
00195     /* Do binary search of table. */
00196     while (low + 1 < high) {
00197        i = (low + high) / 2;
00198        num = errStrings[i].errNum;
00199        if (errNum == num) 
00200            return errStrings[i].errString;
00201         if (errNum < num)
00202            high = i;
00203        else 
00204            low = i;
00205     }
00206     if (errNum == errStrings[low].errNum)
00207        return errStrings[low].errString;
00208     if (errNum == errStrings[high].errNum)
00209        return errStrings[high].errString;
00210     return NULL;
00211 }