]> git.sur5r.net Git - openldap/blobdiff - libraries/liblber/bprint.c
return to releng
[openldap] / libraries / liblber / bprint.c
index c2721064f4107b2c3cd5a54af598be66b77d1f0f..f497d72700ecef8e836fb1bb4eee3c63fac175e7 100644 (file)
@@ -1,7 +1,31 @@
 /* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2005 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-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,9 +38,7 @@
 
 #include "lber-int.h"
 
-typedef void (*BER_LOG_FN) LDAP_P((FILE *file, char *subsys, int level, const char *fmt, va_list vl));
-
-static int ber_log_check( int errlvl, int loglvl );
+#define        ber_log_check(errlvl, loglvl)   ((errlvl) & (loglvl))
 
 BER_LOG_FN ber_int_log_proc = NULL;
 
@@ -71,45 +93,36 @@ BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
  * lber log 
  */
 
-int ber_pvt_log_output( char *subsystem, int level, const char *fmt, ... )
+int ber_pvt_log_output(
+       const char *subsystem,
+       int level,
+       const char *fmt,
+       ... )
 {
-       char buf[ 1024 ];
+       char buf[1024];
        va_list vl;
        va_start( vl, fmt );
 
-       if ( ber_int_log_proc != NULL )
-       {
+       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 );
+               }
        }
-       else
-       {
-            int level;
-            ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
-#ifdef HAVE_VSNPRINTF
-            buf[sizeof(buf) - 1] = '\0';
-            vsnprintf( buf, sizeof(buf)-1, fmt, vl );
-#elif HAVE_VSPRINTF
-            vsprintf( buf, fmt, vl ); /* hope it's not too long */
-#else
-                /* use doprnt() */
-#error "vsprintf() required.";
-#endif
-            if ( ber_log_check( LDAP_DEBUG_BER, level ) )
-                (*ber_pvt_log_print)( buf );
-        }
-       va_end(vl);
 
+       va_end(vl);
        return 1;
 }
        
-static int ber_log_check( int errlvl, int loglvl )
-{
-       return errlvl & loglvl ? 1 : 0;
-}
-
 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
 {
-       char buf[ 1024 ];
+       char buf[1024];
        va_list ap;
 
        assert( fmt != NULL );
@@ -120,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);
 
@@ -175,11 +181,11 @@ ber_bprint(
        LDAP_CONST char *data,
        ber_len_t len )
 {
-       static const char       hexdig[] = "0123456789abcdef";
+       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 );
@@ -200,23 +206,23 @@ ber_bprint(
 
                        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 ] = ':';
+                       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] ];
+               line[off] = hexdig[0x0f & ( data[i] >> 4 )];
+               line[off+1] = hexdig[0x0f & data[i]];
                
                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] = '.';
                }
        }
 
@@ -249,7 +255,7 @@ int ber_output_dump(
              (long) ber->ber_end,
              (long) len );
 
-    ber_pvt_log_output( subsys, level, "%s", buf );
+    (void) ber_pvt_log_output( subsys, level, "%s", buf );
 
 #define BP_OFFSET 9
 #define BP_GRAPH 60
@@ -266,34 +272,36 @@ int ber_output_dump(
         unsigned off;
         
         if( !n ) {
-            if( i ) ber_pvt_log_output( subsys, level, "%s", line );
+            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 ] = ':';
+            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] ];
+        line[off] = hexdig[ 0x0f & ( data[i] >> 4 ) ];
+        line[off+1] = hexdig[ 0x0f & data[i] ];
         
         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_output( subsys, level, "%s", line );
+    return ber_pvt_log_output( subsys, level, "%s", line );
 }
 #endif
 
@@ -305,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;
@@ -324,7 +332,7 @@ ber_dump(
        ber_len_t len;
 
        assert( ber != NULL );
-       assert( BER_VALID( ber ) );
+       assert( LBER_VALID( ber ) );
 
        if ( inout == 1 ) {
                len = ber_pvt_ber_remaining(ber);
@@ -338,7 +346,7 @@ ber_dump(
                (long) ber->ber_end,
                (long) len );
 
-       (*ber_pvt_log_print)( buf );
+       (void) (*ber_pvt_log_print)( buf );
 
        ber_bprint( ber->ber_ptr, len );
 }