X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblber%2Fbprint.c;h=fab02740c763716523392160c02eae2fdc4f9416;hb=7415de8ed142e7855371645a9c31cc02782750d6;hp=69c8683b15c78b445231275934049909488deaa5;hpb=611ccd1ccb7947c35fec4ef9d097aa3dddcb28d4;p=openldap diff --git a/libraries/liblber/bprint.c b/libraries/liblber/bprint.c index 69c8683b15..fab02740c7 100644 --- a/libraries/liblber/bprint.c +++ b/libraries/liblber/bprint.c @@ -1,7 +1,31 @@ /* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . + * + * Copyright 1998-2004 The OpenLDAP Foundation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. + * + * A copy of this license is available in the file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . + */ /* - * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. - * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + * Copyright (c) 1991 Regents of the University of Michigan. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that this notice is preserved and that due credit is given + * to the University of Michigan at Ann Arbor. The name of the University + * may not be used to endorse or promote products derived from this + * software without specific prior written permission. This software + * is provided ``as is'' without express or implied warranty. + */ +/* ACKNOWLEDGEMENTS: + * This work was originally developed by the University of Michigan + * (as part of U-MICH LDAP). */ #include "portable.h" @@ -14,6 +38,10 @@ #include "lber-int.h" +#define ber_log_check(errlvl, loglvl) ((errlvl) & (loglvl)) + +BER_LOG_FN ber_int_log_proc = NULL; + /* * We don't just set ber_pvt_err_file to stderr here, because in NT, * stderr is a symbol imported from a DLL. As such, the compiler @@ -21,12 +49,12 @@ * we set ber_pvt_err_file to stderr later, when it first gets * referenced. */ -FILE *ber_pvt_err_file; +FILE *ber_pvt_err_file = NULL; /* * ber errno */ -BER_ERRNO_FN ber_int_errno_fn; +BER_ERRNO_FN ber_int_errno_fn = NULL; int * ber_errno_addr(void) { @@ -65,14 +93,36 @@ BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print; * lber log */ -static int ber_log_check( int errlvl, int loglvl ) +int ber_pvt_log_output( + const char *subsystem, + int level, + const char *fmt, + ... ) { - return errlvl & loglvl ? 1 : 0; -} + char buf[1024]; + va_list vl; + va_start( vl, fmt ); + + if ( ber_int_log_proc != NULL ) { + ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl ); + + } else { + int level; + ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level ); + buf[sizeof(buf) - 1] = '\0'; + vsnprintf( buf, sizeof(buf)-1, fmt, vl ); + if ( ber_log_check( LDAP_DEBUG_BER, level ) ) { + (*ber_pvt_log_print)( buf ); + } + } + va_end(vl); + return 1; +} + int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... ) { - char buf[ 1024 ]; + char buf[1024]; va_list ap; assert( fmt != NULL ); @@ -83,15 +133,8 @@ int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... ) va_start( ap, fmt ); -#ifdef HAVE_VSNPRINTF buf[sizeof(buf) - 1] = '\0'; vsnprintf( buf, sizeof(buf)-1, fmt, ap ); -#elif HAVE_VSPRINTF - vsprintf( buf, fmt, ap ); /* hope it's not too long */ -#else - /* use doprnt() */ -#error "vsprintf() required." -#endif va_end(ap); @@ -99,6 +142,7 @@ int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... ) return 1; } +#if 0 static int ber_log_puts(int errlvl, int loglvl, char *buf) { assert( buf != NULL ); @@ -110,6 +154,7 @@ static int ber_log_puts(int errlvl, int loglvl, char *buf) (*ber_pvt_log_print)( buf ); return 1; } +#endif /* * Print arbitrary stuff, for debugging. @@ -136,11 +181,11 @@ ber_bprint( LDAP_CONST char *data, ber_len_t len ) { - static const char hexdig[] = "0123456789abcdef"; -#define BP_OFFSET 8 + static const char hexdig[] = "0123456789abcdef"; +#define BP_OFFSET 9 #define BP_GRAPH 60 #define BP_LEN 80 - char line[ BP_LEN ]; + char line[BP_LEN]; ber_len_t i; assert( data != NULL ); @@ -151,7 +196,7 @@ ber_bprint( for ( i = 0 ; i < len ; i++ ) { int n = i % 16; - int off; + unsigned off; if( !n ) { if( i ) (*ber_pvt_log_print)( line ); @@ -161,29 +206,105 @@ ber_bprint( off = i % 0x0ffffU; - line[ 2 ] = hexdig[ ( off & 0xf000U ) >> 12 ]; - line[ 3 ] = hexdig[ ( off & 0x0f00U ) >> 8 ]; - line[ 4 ] = hexdig[ ( off & 0x00f0U ) >> 4 ]; - line[ 5 ] = hexdig[ ( off & 0x000fU ) ]; - line[ 6 ] = ':'; + line[2] = hexdig[0x0f & (off >> 12)]; + line[3] = hexdig[0x0f & (off >> 8)]; + line[4] = hexdig[0x0f & (off >> 4)]; + line[5] = hexdig[0x0f & off]; + line[6] = ':'; } - off = BP_OFFSET + n*3; - line[ off ] = hexdig[ ( data[i] & 0xf0U ) >> 4 ]; - line[ off+1 ] = hexdig[ data[i] & 0x0fU ]; + off = BP_OFFSET + n*3 + ((n >= 8)?1:0); + line[off] = hexdig[0x0f & ( data[i] >> 4 )]; + line[off+1] = hexdig[0x0f & data[i]]; - off = BP_GRAPH + n; + off = BP_GRAPH + n + ((n >= 8)?1:0); - if ( isprint( data[i] )) { - line[ BP_GRAPH + n ] = data[i]; + if ( isprint( (unsigned char) data[i] )) { + line[BP_GRAPH + n] = data[i]; } else { - line[ BP_GRAPH + n ] = '.'; + line[BP_GRAPH + n] = '.'; } } (*ber_pvt_log_print)( line ); } +#ifdef NEW_LOGGING +int ber_output_dump( + const char *subsys, + int level, + BerElement *ber, + int inout ) +{ + static const char hexdig[] = "0123456789abcdef"; + char buf[132]; + ber_len_t len; + char line[ BP_LEN ]; + ber_len_t i; + char *data = ber->ber_ptr; + + if ( inout == 1 ) { + len = ber_pvt_ber_remaining(ber); + } else { + len = ber_pvt_ber_write(ber); + } + + sprintf( buf, "ber_dump: buf=0x%08lx ptr=0x%08lx end=0x%08lx len=%ld\n", + (long) ber->ber_buf, + (long) ber->ber_ptr, + (long) ber->ber_end, + (long) len ); + + (void) ber_pvt_log_output( subsys, level, "%s", buf ); + +#define BP_OFFSET 9 +#define BP_GRAPH 60 +#define BP_LEN 80 + + assert( data != NULL ); + + /* in case len is zero */ + line[0] = '\n'; + line[1] = '\0'; + + for ( i = 0 ; i < len ; i++ ) { + int n = i % 16; + unsigned off; + + if( !n ) { + if( i ) { + (void) ber_pvt_log_output( subsys, level, "%s", line ); + } + memset( line, ' ', sizeof(line)-2 ); + line[sizeof(line)-2] = '\n'; + line[sizeof(line)-1] = '\0'; + + off = i % 0x0ffffU; + + line[2] = hexdig[0x0f & (off >> 12)]; + line[3] = hexdig[0x0f & (off >> 8)]; + line[4] = hexdig[0x0f & (off >> 4)]; + line[5] = hexdig[0x0f & off ]; + line[6] = ':'; + } + + off = BP_OFFSET + n*3 + ((n >= 8)?1:0); + line[off] = hexdig[ 0x0f & ( data[i] >> 4 ) ]; + line[off+1] = hexdig[ 0x0f & data[i] ]; + + off = BP_GRAPH + n + ((n >= 8)?1:0); + + if ( isprint( (unsigned char) data[i] )) { + line[BP_GRAPH + n] = data[i]; + } else { + line[BP_GRAPH + n] = '.'; + } + } + + return ber_pvt_log_output( subsys, level, "%s", line ); +} +#endif + int ber_log_dump( int errlvl, @@ -192,7 +313,7 @@ ber_log_dump( int inout ) { assert( ber != NULL ); - assert( BER_VALID( ber ) ); + assert( LBER_VALID( ber ) ); if ( !ber_log_check( errlvl, loglvl )) { return 0; @@ -208,28 +329,26 @@ ber_dump( int inout ) { char buf[132]; + ber_len_t len; assert( ber != NULL ); - assert( BER_VALID( ber ) ); + assert( LBER_VALID( ber ) ); - sprintf( buf, "ber_dump: buf 0x%lx, ptr 0x%lx, end 0x%lx\n", + if ( inout == 1 ) { + len = ber_pvt_ber_remaining(ber); + } else { + len = ber_pvt_ber_write(ber); + } + + sprintf( buf, "ber_dump: buf=0x%08lx ptr=0x%08lx end=0x%08lx len=%ld\n", (long) ber->ber_buf, (long) ber->ber_ptr, - (long) ber->ber_end ); - - (*ber_pvt_log_print)( buf ); + (long) ber->ber_end, + (long) len ); - if ( inout == 1 ) { - sprintf( buf, " current len %ld, contents:\n", - (long) (ber->ber_end - ber->ber_ptr) ); - ber_bprint( ber->ber_ptr, ber->ber_end - ber->ber_ptr ); - - } else { - sprintf( buf, " current len %ld, contents:\n", - (long) (ber->ber_ptr - ber->ber_buf) ); + (void) (*ber_pvt_log_print)( buf ); - ber_bprint( ber->ber_buf, ber->ber_ptr - ber->ber_buf ); - } + ber_bprint( ber->ber_ptr, len ); } int