1 /* $OpenLDAP: pkg/ldap/libraries/liblber/bprint.c,v 1.57.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 * Copyright (c) 1991 Regents of the University of Michigan.
17 * All rights reserved.
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.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
36 #include <ac/stdarg.h>
37 #include <ac/string.h>
41 #define ber_log_check(errlvl, loglvl) ((errlvl) & (loglvl))
43 BER_LOG_FN ber_int_log_proc
= NULL
;
46 * We don't just set ber_pvt_err_file to stderr here, because in NT,
47 * stderr is a symbol imported from a DLL. As such, the compiler
48 * doesn't recognize the symbol as having a constant address. Thus
49 * we set ber_pvt_err_file to stderr later, when it first gets
52 FILE *ber_pvt_err_file
= NULL
;
57 BER_ERRNO_FN ber_int_errno_fn
= NULL
;
59 int * ber_errno_addr(void)
61 static int ber_int_errno
= LBER_ERROR_NONE
;
63 if( ber_int_errno_fn
) {
64 return (*ber_int_errno_fn
)();
67 return &ber_int_errno
;
73 void ber_error_print( LDAP_CONST
char *data
)
75 assert( data
!= NULL
);
77 if (!ber_pvt_err_file
) ber_pvt_err_file
= stderr
;
79 fputs( data
, ber_pvt_err_file
);
81 /* Print to both streams */
82 if (ber_pvt_err_file
!= stderr
) {
83 fputs( data
, stderr
);
87 fflush( ber_pvt_err_file
);
90 BER_LOG_PRINT_FN ber_pvt_log_print
= ber_error_print
;
96 int ber_pvt_log_output(
97 const char *subsystem
,
106 if ( ber_int_log_proc
!= NULL
) {
107 ber_int_log_proc( ber_pvt_err_file
, subsystem
, level
, fmt
, vl
);
111 ber_get_option( NULL
, LBER_OPT_BER_DEBUG
, &level
);
112 buf
[sizeof(buf
) - 1] = '\0';
113 vsnprintf( buf
, sizeof(buf
)-1, fmt
, vl
);
114 if ( ber_log_check( LDAP_DEBUG_BER
, level
) ) {
115 (*ber_pvt_log_print
)( buf
);
123 int ber_pvt_log_printf( int errlvl
, int loglvl
, const char *fmt
, ... )
128 assert( fmt
!= NULL
);
130 if ( !ber_log_check( errlvl
, loglvl
)) {
136 buf
[sizeof(buf
) - 1] = '\0';
137 vsnprintf( buf
, sizeof(buf
)-1, fmt
, ap
);
141 (*ber_pvt_log_print
)( buf
);
146 static int ber_log_puts(int errlvl
, int loglvl
, char *buf
)
148 assert( buf
!= NULL
);
150 if ( !ber_log_check( errlvl
, loglvl
)) {
154 (*ber_pvt_log_print
)( buf
);
160 * Print arbitrary stuff, for debugging.
164 ber_log_bprint(int errlvl
,
169 assert( data
!= NULL
);
171 if ( !ber_log_check( errlvl
, loglvl
)) {
175 ber_bprint(data
, len
);
181 LDAP_CONST
char *data
,
184 static const char hexdig
[] = "0123456789abcdef";
191 assert( data
!= NULL
);
193 /* in case len is zero */
197 for ( i
= 0 ; i
< len
; i
++ ) {
202 if( i
) (*ber_pvt_log_print
)( line
);
203 memset( line
, ' ', sizeof(line
)-2 );
204 line
[sizeof(line
)-2] = '\n';
205 line
[sizeof(line
)-1] = '\0';
209 line
[2] = hexdig
[0x0f & (off
>> 12)];
210 line
[3] = hexdig
[0x0f & (off
>> 8)];
211 line
[4] = hexdig
[0x0f & (off
>> 4)];
212 line
[5] = hexdig
[0x0f & off
];
216 off
= BP_OFFSET
+ n
*3 + ((n
>= 8)?1:0);
217 line
[off
] = hexdig
[0x0f & ( data
[i
] >> 4 )];
218 line
[off
+1] = hexdig
[0x0f & data
[i
]];
220 off
= BP_GRAPH
+ n
+ ((n
>= 8)?1:0);
222 if ( isprint( (unsigned char) data
[i
] )) {
223 line
[BP_GRAPH
+ n
] = data
[i
];
225 line
[BP_GRAPH
+ n
] = '.';
229 (*ber_pvt_log_print
)( line
);
240 assert( ber
!= NULL
);
241 assert( LBER_VALID( ber
) );
243 if ( !ber_log_check( errlvl
, loglvl
)) {
247 ber_dump(ber
, inout
);
259 assert( ber
!= NULL
);
260 assert( LBER_VALID( ber
) );
263 len
= ber_pvt_ber_remaining(ber
);
265 len
= ber_pvt_ber_write(ber
);
268 sprintf( buf
, "ber_dump: buf=%p ptr=%p end=%p len=%ld\n",
274 (void) (*ber_pvt_log_print
)( buf
);
276 ber_bprint( ber
->ber_ptr
, len
);
285 assert( sos
!= NULL
);
287 if ( !ber_log_check( errlvl
, loglvl
)) {
301 assert( sos
!= NULL
);
303 (*ber_pvt_log_print
)( "*** sos dump ***\n" );
305 while ( sos
!= NULL
) {
306 sprintf( buf
, "ber_sos_dump: clen %ld first %p ptr %p\n",
307 (long) sos
->sos_clen
,
310 (*ber_pvt_log_print
)( buf
);
312 sprintf( buf
, " current len %ld contents:\n",
313 (long) (sos
->sos_ptr
- sos
->sos_first
) );
314 (*ber_pvt_log_print
)( buf
);
316 ber_bprint( sos
->sos_first
, sos
->sos_ptr
- sos
->sos_first
);
321 (*ber_pvt_log_print
)( "*** end dump ***\n" );