errormap.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * errormap.c - map NSPR and OS errors to strings
  43. *
  44. */
  45. #include "slap.h"
  46. #ifndef SYSERRLIST_IN_STDIO
  47. extern int sys_nerr;
  48. extern char *sys_errlist[];
  49. #endif
  50. /*
  51. * function protoypes
  52. */
  53. static const char *SECU_Strerror(PRErrorCode errNum);
  54. /*
  55. * return the string equivalent of an NSPR error
  56. */
  57. char *
  58. slapd_pr_strerror( const int prerrno )
  59. {
  60. char *s;
  61. if (( s = (char *)SECU_Strerror( (PRErrorCode)prerrno )) == NULL ) {
  62. s = "unknown";
  63. }
  64. return( s );
  65. }
  66. /*
  67. * return the string equivalent of a system error
  68. */
  69. const char *
  70. slapd_system_strerror( const int syserrno )
  71. {
  72. const char *s;
  73. /* replaced
  74. if ( syserrno > -1 && syserrno < sys_nerr ) {
  75. s = sys_errlist[ syserrno ];
  76. } else {
  77. s = "unknown";
  78. }
  79. with s= strerror(syserrno)*/
  80. s=strerror(syserrno);
  81. return( s );
  82. }
  83. /*
  84. * return the string equivalent of an NSPR error. If "prerrno" is not
  85. * an NSPR error, assume it is a system error. Please use slapd_pr_strerror()
  86. * or slapd_system_strerror() if you can since the concept behind this
  87. * function is a bit of a kludge -- one should *really* know what kind of
  88. * error code they have.
  89. */
  90. const char *
  91. slapd_versatile_strerror( const PRErrorCode prerrno )
  92. {
  93. const char *s;
  94. if (( s = (const char *)SECU_Strerror( prerrno )) == NULL ) {
  95. s = slapd_system_strerror( prerrno );
  96. }
  97. return( s );
  98. }
  99. /*
  100. ****************************************************************************
  101. * The code below this point was provided by Nelson Bolyard <nelsonb> of the
  102. * Netscape Certificate Server team on 27-March-1998.
  103. * Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
  104. * Last updated from there: 24-July-1998 by Mark Smith <mcs>
  105. ****************************************************************************
  106. */
  107. #include "nspr.h"
  108. struct tuple_str {
  109. PRErrorCode errNum;
  110. const char * errString;
  111. };
  112. typedef struct tuple_str tuple_str;
  113. #define ER2(a,b) {a, b},
  114. #define ER3(a,b,c) {a, c},
  115. #include "secerr.h"
  116. #include "sslerr.h"
  117. static const tuple_str errStrings[] = {
  118. /* keep this list in ascending order of error numbers */
  119. #include "dberrstrs.h"
  120. #include "sslerrstrs.h"
  121. #include "secerrstrs.h"
  122. #include "prerrstrs.h"
  123. #include "disconnect_error_strings.h"
  124. };
  125. static const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
  126. /* Returns a UTF-8 encoded constant error string for "errNum".
  127. * Returns NULL of errNum is unknown.
  128. */
  129. static const char *
  130. SECU_Strerror(PRErrorCode errNum) {
  131. PRInt32 low = 0;
  132. PRInt32 high = numStrings - 1;
  133. PRInt32 i;
  134. PRErrorCode num;
  135. static int initDone;
  136. /* make sure table is in ascending order.
  137. * binary search depends on it.
  138. */
  139. if (!initDone) {
  140. PRErrorCode lastNum = errStrings[low].errNum;
  141. for (i = low + 1; i <= high; ++i) {
  142. num = errStrings[i].errNum;
  143. if (num <= lastNum) {
  144. LDAPDebug( LDAP_DEBUG_ANY,
  145. "sequence error in error strings at item %d\n"
  146. "error %d (%s)\n",
  147. i, lastNum, errStrings[i-1].errString );
  148. LDAPDebug( LDAP_DEBUG_ANY,
  149. "should come after \n"
  150. "error %d (%s)\n",
  151. num, errStrings[i].errString, 0 );
  152. }
  153. lastNum = num;
  154. }
  155. initDone = 1;
  156. }
  157. /* Do binary search of table. */
  158. while (low + 1 < high) {
  159. i = (low + high) / 2;
  160. num = errStrings[i].errNum;
  161. if (errNum == num)
  162. return errStrings[i].errString;
  163. if (errNum < num)
  164. high = i;
  165. else
  166. low = i;
  167. }
  168. if (errNum == errStrings[low].errNum)
  169. return errStrings[low].errString;
  170. if (errNum == errStrings[high].errNum)
  171. return errStrings[high].errString;
  172. return NULL;
  173. }