Back to index

openldap  2.4.31
bprint.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /*
00016  * Copyright (c) 1991 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 /* ACKNOWLEDGEMENTS:
00027  * This work was originally developed by the University of Michigan
00028  * (as part of U-MICH LDAP).
00029  */
00030 
00031 #include "portable.h"
00032 
00033 #include <stdio.h>
00034 
00035 #include <ac/ctype.h>
00036 #include <ac/stdarg.h>
00037 #include <ac/string.h>
00038 
00039 #include "lber-int.h"
00040 
00041 #define       ber_log_check(errlvl, loglvl)      ((errlvl) & (loglvl))
00042 
00043 BER_LOG_FN ber_int_log_proc = NULL;
00044 
00045 /*
00046  * We don't just set ber_pvt_err_file to stderr here, because in NT,
00047  * stderr is a symbol imported from a DLL. As such, the compiler
00048  * doesn't recognize the symbol as having a constant address. Thus
00049  * we set ber_pvt_err_file to stderr later, when it first gets
00050  * referenced.
00051  */
00052 FILE *ber_pvt_err_file = NULL;
00053 
00054 /*
00055  * ber errno
00056  */
00057 BER_ERRNO_FN ber_int_errno_fn = NULL;
00058 
00059 int * ber_errno_addr(void)
00060 {
00061        static int ber_int_errno = LBER_ERROR_NONE;
00062 
00063        if( ber_int_errno_fn ) {
00064               return (*ber_int_errno_fn)();
00065        }
00066 
00067        return &ber_int_errno;
00068 }
00069 
00070 /*
00071  * Print stuff
00072  */
00073 void ber_error_print( LDAP_CONST char *data )
00074 {
00075        assert( data != NULL );
00076 
00077        if (!ber_pvt_err_file) ber_pvt_err_file = stderr;
00078 
00079        fputs( data, ber_pvt_err_file );
00080 
00081        /* Print to both streams */
00082        if (ber_pvt_err_file != stderr) {
00083               fputs( data, stderr );
00084               fflush( stderr );
00085        }
00086 
00087        fflush( ber_pvt_err_file );
00088 }
00089 
00090 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
00091 
00092 /*
00093  * lber log 
00094  */
00095 
00096 int ber_pvt_log_output(
00097        const char *subsystem,
00098        int level,
00099        const char *fmt,
00100        ... )
00101 {
00102        char buf[1024];
00103        va_list vl;
00104        va_start( vl, fmt );
00105 
00106        if ( ber_int_log_proc != NULL ) {
00107               ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
00108 
00109        } else {
00110               int level;
00111               ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
00112               buf[sizeof(buf) - 1] = '\0';
00113               vsnprintf( buf, sizeof(buf)-1, fmt, vl );
00114               if ( ber_log_check( LDAP_DEBUG_BER, level ) ) {
00115                      (*ber_pvt_log_print)( buf );
00116               }
00117        }
00118 
00119        va_end(vl);
00120        return 1;
00121 }
00122        
00123 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
00124 {
00125        char buf[1024];
00126        va_list ap;
00127 
00128        assert( fmt != NULL );
00129 
00130        if ( !ber_log_check( errlvl, loglvl )) {
00131               return 0;
00132        }
00133 
00134        va_start( ap, fmt );
00135 
00136        buf[sizeof(buf) - 1] = '\0';
00137        vsnprintf( buf, sizeof(buf)-1, fmt, ap );
00138 
00139        va_end(ap);
00140 
00141        (*ber_pvt_log_print)( buf );
00142        return 1;
00143 }
00144 
00145 #if 0
00146 static int ber_log_puts(int errlvl, int loglvl, char *buf)
00147 {
00148        assert( buf != NULL );
00149 
00150        if ( !ber_log_check( errlvl, loglvl )) {
00151               return 0;
00152        }
00153 
00154        (*ber_pvt_log_print)( buf );
00155        return 1;
00156 }
00157 #endif
00158 
00159 /*
00160  * Print arbitrary stuff, for debugging.
00161  */
00162 
00163 int
00164 ber_log_bprint(int errlvl,
00165        int loglvl,
00166        const char *data,
00167        ber_len_t len )
00168 {
00169        assert( data != NULL );
00170 
00171        if ( !ber_log_check( errlvl, loglvl )) {
00172               return 0;
00173        }
00174 
00175        ber_bprint(data, len);
00176        return 1;
00177 }
00178 
00179 void
00180 ber_bprint(
00181        LDAP_CONST char *data,
00182        ber_len_t len )
00183 {
00184        static const char hexdig[] = "0123456789abcdef";
00185 #define BP_OFFSET 9
00186 #define BP_GRAPH 60
00187 #define BP_LEN       80
00188        char   line[BP_LEN];
00189        ber_len_t i;
00190 
00191        assert( data != NULL );
00192 
00193        /* in case len is zero */
00194        line[0] = '\n';
00195        line[1] = '\0';
00196        
00197        for ( i = 0 ; i < len ; i++ ) {
00198               int n = i % 16;
00199               unsigned off;
00200 
00201               if( !n ) {
00202                      if( i ) (*ber_pvt_log_print)( line );
00203                      memset( line, ' ', sizeof(line)-2 );
00204                      line[sizeof(line)-2] = '\n';
00205                      line[sizeof(line)-1] = '\0';
00206 
00207                      off = i % 0x0ffffU;
00208 
00209                      line[2] = hexdig[0x0f & (off >> 12)];
00210                      line[3] = hexdig[0x0f & (off >>  8)];
00211                      line[4] = hexdig[0x0f & (off >>  4)];
00212                      line[5] = hexdig[0x0f & off];
00213                      line[6] = ':';
00214               }
00215 
00216               off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
00217               line[off] = hexdig[0x0f & ( data[i] >> 4 )];
00218               line[off+1] = hexdig[0x0f & data[i]];
00219               
00220               off = BP_GRAPH + n + ((n >= 8)?1:0);
00221 
00222               if ( isprint( (unsigned char) data[i] )) {
00223                      line[BP_GRAPH + n] = data[i];
00224               } else {
00225                      line[BP_GRAPH + n] = '.';
00226               }
00227        }
00228 
00229        (*ber_pvt_log_print)( line );
00230 }
00231 
00232 
00233 int
00234 ber_log_dump(
00235        int errlvl,
00236        int loglvl,
00237        BerElement *ber,
00238        int inout )
00239 {
00240        assert( ber != NULL );
00241        assert( LBER_VALID( ber ) );
00242 
00243        if ( !ber_log_check( errlvl, loglvl )) {
00244               return 0;
00245        }
00246 
00247        ber_dump(ber, inout);
00248        return 1;
00249 }
00250 
00251 void
00252 ber_dump(
00253        BerElement *ber,
00254        int inout )
00255 {
00256        char buf[132];
00257        ber_len_t len;
00258 
00259        assert( ber != NULL );
00260        assert( LBER_VALID( ber ) );
00261 
00262        if ( inout == 1 ) {
00263               len = ber_pvt_ber_remaining(ber);
00264        } else {
00265               len = ber_pvt_ber_write(ber);
00266        }
00267 
00268        sprintf( buf, "ber_dump: buf=%p ptr=%p end=%p len=%ld\n",
00269               ber->ber_buf,
00270               ber->ber_ptr,
00271               ber->ber_end,
00272               (long) len );
00273 
00274        (void) (*ber_pvt_log_print)( buf );
00275 
00276        ber_bprint( ber->ber_ptr, len );
00277 }
00278 
00279 typedef struct seqorset Seqorset;
00280 
00281 /* Exists for binary compatibility with OpenLDAP 2.4.17-- */
00282 int
00283 ber_log_sos_dump(
00284        int errlvl,
00285        int loglvl,
00286        Seqorset *sos )
00287 {
00288        return 0;
00289 }
00290 
00291 /* Exists for binary compatibility with OpenLDAP 2.4.17-- */
00292 void
00293 ber_sos_dump(
00294        Seqorset *sos )
00295 {
00296 }