errormap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * errormap.c - map NSPR and OS errors to strings
  14. *
  15. */
  16. #include "slap.h"
  17. #ifndef SYSERRLIST_IN_STDIO
  18. extern int sys_nerr;
  19. extern char *sys_errlist[];
  20. #endif
  21. /*
  22. * function protoypes
  23. */
  24. static const char *SECU_Strerror(PRErrorCode errNum);
  25. /*
  26. * return the string equivalent of an NSPR error
  27. */
  28. char *
  29. slapd_pr_strerror( const int prerrno )
  30. {
  31. char *s;
  32. if(prerrno == 0){
  33. s = "no error";
  34. } else {
  35. if (( s = (char *)SECU_Strerror( (PRErrorCode)prerrno )) == NULL ) {
  36. s = "unknown error";
  37. }
  38. }
  39. return( s );
  40. }
  41. char *
  42. slapi_pr_strerror( const int prerrno )
  43. {
  44. return slapd_pr_strerror(prerrno);
  45. }
  46. /*
  47. * return the string equivalent of a system error
  48. */
  49. const char *
  50. slapd_system_strerror( const int syserrno )
  51. {
  52. const char *s;
  53. /* replaced
  54. if ( syserrno > -1 && syserrno < sys_nerr ) {
  55. s = sys_errlist[ syserrno ];
  56. } else {
  57. s = "unknown";
  58. }
  59. with s= strerror(syserrno)*/
  60. s=strerror(syserrno);
  61. return( s );
  62. }
  63. const char *
  64. slapi_system_strerror( const int syserrno )
  65. {
  66. return slapd_system_strerror(syserrno);
  67. }
  68. /*
  69. * return the string equivalent of an NSPR error. If "prerrno" is not
  70. * an NSPR error, assume it is a system error. Please use slapd_pr_strerror()
  71. * or slapd_system_strerror() if you can since the concept behind this
  72. * function is a bit of a kludge -- one should *really* know what kind of
  73. * error code they have.
  74. */
  75. const char *
  76. slapd_versatile_strerror( const PRErrorCode prerrno )
  77. {
  78. const char *s;
  79. if (( s = (const char *)SECU_Strerror( prerrno )) == NULL ) {
  80. s = slapd_system_strerror( prerrno );
  81. }
  82. return( s );
  83. }
  84. /*
  85. ****************************************************************************
  86. * The code below this point was provided by Nelson Bolyard <nelsonb> of the
  87. * Netscape Certificate Server team on 27-March-1998.
  88. * Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
  89. * Last updated from there: 24-July-1998 by Mark Smith <mcs>
  90. ****************************************************************************
  91. */
  92. #include "nspr.h"
  93. struct tuple_str {
  94. PRErrorCode errNum;
  95. const char * errString;
  96. };
  97. typedef struct tuple_str tuple_str;
  98. #define ER2(a,b) {a, b},
  99. #define ER3(a,b,c) {a, c},
  100. #include "secerr.h"
  101. #include "sslerr.h"
  102. static const tuple_str errStrings[] = {
  103. /* keep this list in ascending order of error numbers */
  104. #include "dberrstrs.h"
  105. #include "sslerrstrs.h"
  106. #include "secerrstrs.h"
  107. #include "prerrstrs.h"
  108. #include "disconnect_error_strings.h"
  109. };
  110. static const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
  111. /* Returns a UTF-8 encoded constant error string for "errNum".
  112. * Returns NULL of errNum is unknown.
  113. */
  114. static const char *
  115. SECU_Strerror(PRErrorCode errNum) {
  116. PRInt32 low = 0;
  117. PRInt32 high = numStrings - 1;
  118. PRInt32 i;
  119. PRErrorCode num;
  120. static int initDone;
  121. /* make sure table is in ascending order.
  122. * binary search depends on it.
  123. */
  124. if (!initDone) {
  125. PRErrorCode lastNum = errStrings[low].errNum;
  126. for (i = low + 1; i <= high; ++i) {
  127. num = errStrings[i].errNum;
  128. if (num <= lastNum) {
  129. LDAPDebug( LDAP_DEBUG_ANY,
  130. "sequence error in error strings at item %d\n"
  131. "error %d (%s)\n",
  132. i, lastNum, errStrings[i-1].errString );
  133. LDAPDebug( LDAP_DEBUG_ANY,
  134. "should come after \n"
  135. "error %d (%s)\n",
  136. num, errStrings[i].errString, 0 );
  137. }
  138. lastNum = num;
  139. }
  140. initDone = 1;
  141. }
  142. /* Do binary search of table. */
  143. while (low + 1 < high) {
  144. i = (low + high) / 2;
  145. num = errStrings[i].errNum;
  146. if (errNum == num)
  147. return errStrings[i].errString;
  148. if (errNum < num)
  149. high = i;
  150. else
  151. low = i;
  152. }
  153. if (errNum == errStrings[low].errNum)
  154. return errStrings[low].errString;
  155. if (errNum == errStrings[high].errNum)
  156. return errStrings[high].errString;
  157. return NULL;
  158. }