]> git.sur5r.net Git - openldap/blobdiff - libraries/liblber/bprint.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / liblber / bprint.c
index 5c615a129175015084aaf5d9ba5500b5a134f1af..5d55628549586b9eee80623f8a4653c2cac0809e 100644 (file)
@@ -1,7 +1,31 @@
 /* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2013 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
+ * <http://www.OpenLDAP.org/license.html>.
+ */
 /*
- * Copyright 1998-1999 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"
 
 #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
+ * doesn't recognize the symbol as having a constant address. Thus
+ * we set ber_pvt_err_file to stderr later, when it first gets
+ * referenced.
+ */
+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)
 {
@@ -33,13 +70,21 @@ int * ber_errno_addr(void)
 /*
  * Print stuff
  */
-static void
-ber_error_print( char *data )
+void ber_error_print( LDAP_CONST char *data )
 {
        assert( data != NULL );
 
-       fputs( data, stderr );
-       fflush( stderr );
+       if (!ber_pvt_err_file) ber_pvt_err_file = stderr;
+
+       fputs( data, ber_pvt_err_file );
+
+       /* Print to both streams */
+       if (ber_pvt_err_file != stderr) {
+               fputs( data, stderr );
+               fflush( stderr );
+       }
+
+       fflush( ber_pvt_err_file );
 }
 
 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
@@ -48,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 );
@@ -66,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);
 
@@ -82,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 );
@@ -93,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.
@@ -119,49 +181,55 @@ ber_bprint(
        LDAP_CONST char *data,
        ber_len_t len )
 {
-    static const char  hexdig[] = "0123456789abcdef";
-#define BPLEN  48
-    char       out[ BPLEN ];
-    char       buf[ BPLEN + sizeof("\t%s\n") ];
-    int                i = 0;
+       static const char hexdig[] = "0123456789abcdef";
+#define BP_OFFSET 9
+#define BP_GRAPH 60
+#define BP_LEN 80
+       char    line[BP_LEN];
+       ber_len_t i;
 
        assert( data != NULL );
 
-    memset( out, 0, BPLEN );
-    for ( ;; ) {
-       if ( len < 1 ) {
-           sprintf( buf, "\t%s\n", ( i == 0 ) ? "(end)" : out );
-               (*ber_pvt_log_print)( buf );
-           break;
+       /* 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 ) (*ber_pvt_log_print)( 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] = '.';
+               }
        }
 
-#ifndef LDAP_HEX
-       if ( isgraph( (unsigned char)*data )) {
-           out[ i ] = ' ';
-           out[ i+1 ] = *data;
-       } else {
-#endif
-           out[ i ] = hexdig[ ( *data & 0xf0U ) >> 4 ];
-           out[ i+1 ] = hexdig[ *data & 0x0fU ];
-#ifndef LDAP_HEX
-       }
-#endif
-       i += 2;
-       len--;
-       data++;
-
-       if ( i > BPLEN - 2 ) {
-               char data[128 + BPLEN];
-           sprintf( data, "\t%s\n", out );
-               (*ber_pvt_log_print)(data);
-           memset( out, 0, BPLEN );
-           i = 0;
-           continue;
-       }
-       out[ i++ ] = ' ';
-    }
+       (*ber_pvt_log_print)( line );
 }
 
+
 int
 ber_log_dump(
        int errlvl,
@@ -170,7 +238,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;
@@ -186,71 +254,43 @@ ber_dump(
        int inout )
 {
        char buf[132];
+       ber_len_t len;
 
        assert( ber != NULL );
-       assert( BER_VALID( ber ) );
-
-       sprintf( buf, "ber_dump: buf 0x%lx, ptr 0x%lx, end 0x%lx\n",
-           (long) ber->ber_buf,
-               (long) ber->ber_ptr,
-               (long) ber->ber_end );
-
-       (*ber_pvt_log_print)( buf );
+       assert( LBER_VALID( ber ) );
 
        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 );
-
+               len = ber_pvt_ber_remaining(ber);
        } else {
-               sprintf( buf, "          current len %ld, contents:\n",
-                   (long) (ber->ber_ptr - ber->ber_buf) );
-
-               ber_bprint( ber->ber_buf, ber->ber_ptr - ber->ber_buf );
+               len = ber_pvt_ber_write(ber);
        }
+
+       sprintf( buf, "ber_dump: buf=%p ptr=%p end=%p len=%ld\n",
+               ber->ber_buf,
+               ber->ber_ptr,
+               ber->ber_end,
+               (long) len );
+
+       (void) (*ber_pvt_log_print)( buf );
+
+       ber_bprint( ber->ber_ptr, len );
 }
 
+typedef struct seqorset Seqorset;
+
+/* Exists for binary compatibility with OpenLDAP 2.4.17-- */
 int
 ber_log_sos_dump(
        int errlvl,
        int loglvl,
        Seqorset *sos )
 {
-       assert( sos != NULL );
-
-       if ( !ber_log_check( errlvl, loglvl )) {
-               return 0;
-       }
-
-       ber_sos_dump( sos );
-       return 1;
+       return 0;
 }
 
+/* Exists for binary compatibility with OpenLDAP 2.4.17-- */
 void
 ber_sos_dump(
        Seqorset *sos )
 {
-       char buf[132];
-
-       assert( sos != NULL );
-
-       (*ber_pvt_log_print)( "*** sos dump ***\n" );
-
-       while ( sos != NULL ) {
-               sprintf( buf, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
-                   (long) sos->sos_clen,
-                       (long) sos->sos_first,
-                       (long) sos->sos_ptr );
-               (*ber_pvt_log_print)( buf );
-
-               sprintf( buf, "              current len %ld contents:\n",
-                   (long) (sos->sos_ptr - sos->sos_first) );
-               (*ber_pvt_log_print)( buf );
-
-               ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );
-
-               sos = sos->sos_next;
-       }
-
-       (*ber_pvt_log_print)( "*** end dump ***\n" );
 }