errormap.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. char *
  67. slapi_pr_strerror( const int prerrno )
  68. {
  69. return slapd_pr_strerror(prerrno);
  70. }
  71. /*
  72. * return the string equivalent of a system error
  73. */
  74. const char *
  75. slapd_system_strerror( const int syserrno )
  76. {
  77. const char *s;
  78. /* replaced
  79. if ( syserrno > -1 && syserrno < sys_nerr ) {
  80. s = sys_errlist[ syserrno ];
  81. } else {
  82. s = "unknown";
  83. }
  84. with s= strerror(syserrno)*/
  85. s=strerror(syserrno);
  86. return( s );
  87. }
  88. const char *
  89. slapi_system_strerror( const int syserrno )
  90. {
  91. return slapd_system_strerror(syserrno);
  92. }
  93. /*
  94. * return the string equivalent of an NSPR error. If "prerrno" is not
  95. * an NSPR error, assume it is a system error. Please use slapd_pr_strerror()
  96. * or slapd_system_strerror() if you can since the concept behind this
  97. * function is a bit of a kludge -- one should *really* know what kind of
  98. * error code they have.
  99. */
  100. const char *
  101. slapd_versatile_strerror( const PRErrorCode prerrno )
  102. {
  103. const char *s;
  104. if (( s = (const char *)SECU_Strerror( prerrno )) == NULL ) {
  105. s = slapd_system_strerror( prerrno );
  106. }
  107. return( s );
  108. }
  109. /*
  110. ****************************************************************************
  111. * The code below this point was provided by Nelson Bolyard <nelsonb> of the
  112. * Netscape Certificate Server team on 27-March-1998.
  113. * Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
  114. * Last updated from there: 24-July-1998 by Mark Smith <mcs>
  115. ****************************************************************************
  116. */
  117. #include "nspr.h"
  118. struct tuple_str {
  119. PRErrorCode errNum;
  120. const char * errString;
  121. };
  122. typedef struct tuple_str tuple_str;
  123. #define ER2(a,b) {a, b},
  124. #define ER3(a,b,c) {a, c},
  125. #include "secerr.h"
  126. #include "sslerr.h"
  127. static const tuple_str errStrings[] = {
  128. /* keep this list in ascending order of error numbers */
  129. #include "dberrstrs.h"
  130. #include "sslerrstrs.h"
  131. #include "secerrstrs.h"
  132. #include "prerrstrs.h"
  133. #include "disconnect_error_strings.h"
  134. };
  135. static const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
  136. /* Returns a UTF-8 encoded constant error string for "errNum".
  137. * Returns NULL of errNum is unknown.
  138. */
  139. static const char *
  140. SECU_Strerror(PRErrorCode errNum) {
  141. PRInt32 low = 0;
  142. PRInt32 high = numStrings - 1;
  143. PRInt32 i;
  144. PRErrorCode num;
  145. static int initDone;
  146. /* make sure table is in ascending order.
  147. * binary search depends on it.
  148. */
  149. if (!initDone) {
  150. PRErrorCode lastNum = errStrings[low].errNum;
  151. for (i = low + 1; i <= high; ++i) {
  152. num = errStrings[i].errNum;
  153. if (num <= lastNum) {
  154. LDAPDebug( LDAP_DEBUG_ANY,
  155. "sequence error in error strings at item %d\n"
  156. "error %d (%s)\n",
  157. i, lastNum, errStrings[i-1].errString );
  158. LDAPDebug( LDAP_DEBUG_ANY,
  159. "should come after \n"
  160. "error %d (%s)\n",
  161. num, errStrings[i].errString, 0 );
  162. }
  163. lastNum = num;
  164. }
  165. initDone = 1;
  166. }
  167. /* Do binary search of table. */
  168. while (low + 1 < high) {
  169. i = (low + high) / 2;
  170. num = errStrings[i].errNum;
  171. if (errNum == num)
  172. return errStrings[i].errString;
  173. if (errNum < num)
  174. high = i;
  175. else
  176. low = i;
  177. }
  178. if (errNum == errStrings[low].errNum)
  179. return errStrings[low].errString;
  180. if (errNum == errStrings[high].errNum)
  181. return errStrings[high].errString;
  182. return NULL;
  183. }