bprint.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2024 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /*
  16. * Copyright (c) 1991 Regents of the University of Michigan.
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms are permitted
  20. * provided that this notice is preserved and that due credit is given
  21. * to the University of Michigan at Ann Arbor. The name of the University
  22. * may not be used to endorse or promote products derived from this
  23. * software without specific prior written permission. This software
  24. * is provided ``as is'' without express or implied warranty.
  25. */
  26. /* ACKNOWLEDGEMENTS:
  27. * This work was originally developed by the University of Michigan
  28. * (as part of U-MICH LDAP).
  29. */
  30. #include "portable.h"
  31. #include <stdio.h>
  32. #include <ac/ctype.h>
  33. #include <ac/stdarg.h>
  34. #include <ac/string.h>
  35. #include "lber-int.h"
  36. #define ber_log_check(errlvl, loglvl) ((errlvl) & (loglvl))
  37. BER_LOG_FN ber_int_log_proc = NULL;
  38. /*
  39. * We don't just set ber_pvt_err_file to stderr here, because in NT,
  40. * stderr is a symbol imported from a DLL. As such, the compiler
  41. * doesn't recognize the symbol as having a constant address. Thus
  42. * we set ber_pvt_err_file to stderr later, when it first gets
  43. * referenced.
  44. */
  45. FILE *ber_pvt_err_file = NULL;
  46. /*
  47. * ber errno
  48. */
  49. BER_ERRNO_FN ber_int_errno_fn = NULL;
  50. int * ber_errno_addr(void)
  51. {
  52. static int ber_int_errno = LBER_ERROR_NONE;
  53. if( ber_int_errno_fn ) {
  54. return (*ber_int_errno_fn)();
  55. }
  56. return &ber_int_errno;
  57. }
  58. /*
  59. * Print stuff
  60. */
  61. void ber_error_print( LDAP_CONST char *data )
  62. {
  63. assert( data != NULL );
  64. if (!ber_pvt_err_file) ber_pvt_err_file = stderr;
  65. fputs( data, ber_pvt_err_file );
  66. /* Print to both streams */
  67. if (ber_pvt_err_file != stderr) {
  68. fputs( data, stderr );
  69. fflush( stderr );
  70. }
  71. fflush( ber_pvt_err_file );
  72. }
  73. BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
  74. /*
  75. * lber log
  76. */
  77. int ber_pvt_log_output(
  78. const char *subsystem,
  79. int level,
  80. const char *fmt,
  81. ... )
  82. {
  83. char buf[1024];
  84. va_list vl;
  85. va_start( vl, fmt );
  86. if ( ber_int_log_proc != NULL ) {
  87. ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
  88. } else {
  89. int level;
  90. ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
  91. buf[sizeof(buf) - 1] = '\0';
  92. vsnprintf( buf, sizeof(buf)-1, fmt, vl );
  93. if ( ber_log_check( LDAP_DEBUG_BER, level ) ) {
  94. (*ber_pvt_log_print)( buf );
  95. }
  96. }
  97. va_end(vl);
  98. return 1;
  99. }
  100. int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
  101. {
  102. char buf[1024];
  103. va_list ap;
  104. assert( fmt != NULL );
  105. if ( !ber_log_check( errlvl, loglvl )) {
  106. return 0;
  107. }
  108. va_start( ap, fmt );
  109. buf[sizeof(buf) - 1] = '\0';
  110. vsnprintf( buf, sizeof(buf)-1, fmt, ap );
  111. va_end(ap);
  112. (*ber_pvt_log_print)( buf );
  113. return 1;
  114. }
  115. #if 0
  116. static int ber_log_puts(int errlvl, int loglvl, char *buf)
  117. {
  118. assert( buf != NULL );
  119. if ( !ber_log_check( errlvl, loglvl )) {
  120. return 0;
  121. }
  122. (*ber_pvt_log_print)( buf );
  123. return 1;
  124. }
  125. #endif
  126. /*
  127. * Print arbitrary stuff, for debugging.
  128. */
  129. int
  130. ber_log_bprint(int errlvl,
  131. int loglvl,
  132. const char *data,
  133. ber_len_t len )
  134. {
  135. assert( data != NULL );
  136. if ( !ber_log_check( errlvl, loglvl )) {
  137. return 0;
  138. }
  139. ber_bprint(data, len);
  140. return 1;
  141. }
  142. void
  143. ber_bprint(
  144. LDAP_CONST char *data,
  145. ber_len_t len )
  146. {
  147. static const char hexdig[] = "0123456789abcdef";
  148. #define BP_OFFSET 9
  149. #define BP_GRAPH 60
  150. #define BP_LEN 80
  151. char line[BP_LEN];
  152. ber_len_t i;
  153. assert( data != NULL );
  154. /* in case len is zero */
  155. line[0] = '\n';
  156. line[1] = '\0';
  157. for ( i = 0 ; i < len ; i++ ) {
  158. int n = i % 16;
  159. unsigned off;
  160. if( !n ) {
  161. if( i ) (*ber_pvt_log_print)( line );
  162. memset( line, ' ', sizeof(line)-2 );
  163. line[sizeof(line)-2] = '\n';
  164. line[sizeof(line)-1] = '\0';
  165. off = i % 0x0ffffU;
  166. line[2] = hexdig[0x0f & (off >> 12)];
  167. line[3] = hexdig[0x0f & (off >> 8)];
  168. line[4] = hexdig[0x0f & (off >> 4)];
  169. line[5] = hexdig[0x0f & off];
  170. line[6] = ':';
  171. }
  172. off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
  173. line[off] = hexdig[0x0f & ( data[i] >> 4 )];
  174. line[off+1] = hexdig[0x0f & data[i]];
  175. off = BP_GRAPH + n + ((n >= 8)?1:0);
  176. if ( isprint( (unsigned char) data[i] )) {
  177. line[BP_GRAPH + n] = data[i];
  178. } else {
  179. line[BP_GRAPH + n] = '.';
  180. }
  181. }
  182. (*ber_pvt_log_print)( line );
  183. }
  184. int
  185. ber_log_dump(
  186. int errlvl,
  187. int loglvl,
  188. BerElement *ber,
  189. int inout )
  190. {
  191. assert( ber != NULL );
  192. assert( LBER_VALID( ber ) );
  193. if ( !ber_log_check( errlvl, loglvl )) {
  194. return 0;
  195. }
  196. ber_dump(ber, inout);
  197. return 1;
  198. }
  199. void
  200. ber_dump(
  201. BerElement *ber,
  202. int inout )
  203. {
  204. char buf[132];
  205. ber_len_t len;
  206. assert( ber != NULL );
  207. assert( LBER_VALID( ber ) );
  208. if ( inout == 1 ) {
  209. len = ber_pvt_ber_remaining(ber);
  210. } else {
  211. len = ber_pvt_ber_write(ber);
  212. }
  213. sprintf( buf, "ber_dump: buf=%p ptr=%p end=%p len=%ld\n",
  214. (void *) ber->ber_buf,
  215. (void *) ber->ber_ptr,
  216. (void *) ber->ber_end,
  217. (long) len );
  218. (void) (*ber_pvt_log_print)( buf );
  219. ber_bprint( ber->ber_ptr, len );
  220. }
  221. typedef struct seqorset Seqorset;
  222. /* Exists for binary compatibility with OpenLDAP 2.4.17-- */
  223. int
  224. ber_log_sos_dump(
  225. int errlvl,
  226. int loglvl,
  227. Seqorset *sos )
  228. {
  229. return 0;
  230. }
  231. /* Exists for binary compatibility with OpenLDAP 2.4.17-- */
  232. void
  233. ber_sos_dump(
  234. Seqorset *sos )
  235. {
  236. }