No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / liblber / bprint.c
blobbb4067d04bc237c526c6d94e7e6cdb9d1cc173b4
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.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
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.
26 /* ACKNOWLEDGEMENTS:
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
31 #include "portable.h"
33 #include <stdio.h>
35 #include <ac/ctype.h>
36 #include <ac/stdarg.h>
37 #include <ac/string.h>
39 #include "lber-int.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
50 * referenced.
52 FILE *ber_pvt_err_file = NULL;
55 * ber errno
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;
71 * Print stuff
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 );
84 fflush( stderr );
87 fflush( ber_pvt_err_file );
90 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
93 * lber log
96 int ber_pvt_log_output(
97 const char *subsystem,
98 int level,
99 const char *fmt,
100 ... )
102 char buf[1024];
103 va_list vl;
104 va_start( vl, fmt );
106 if ( ber_int_log_proc != NULL ) {
107 ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
109 } else {
110 int level;
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 );
119 va_end(vl);
120 return 1;
123 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
125 char buf[1024];
126 va_list ap;
128 assert( fmt != NULL );
130 if ( !ber_log_check( errlvl, loglvl )) {
131 return 0;
134 va_start( ap, fmt );
136 buf[sizeof(buf) - 1] = '\0';
137 vsnprintf( buf, sizeof(buf)-1, fmt, ap );
139 va_end(ap);
141 (*ber_pvt_log_print)( buf );
142 return 1;
145 #if 0
146 static int ber_log_puts(int errlvl, int loglvl, char *buf)
148 assert( buf != NULL );
150 if ( !ber_log_check( errlvl, loglvl )) {
151 return 0;
154 (*ber_pvt_log_print)( buf );
155 return 1;
157 #endif
160 * Print arbitrary stuff, for debugging.
164 ber_log_bprint(int errlvl,
165 int loglvl,
166 const char *data,
167 ber_len_t len )
169 assert( data != NULL );
171 if ( !ber_log_check( errlvl, loglvl )) {
172 return 0;
175 ber_bprint(data, len);
176 return 1;
179 void
180 ber_bprint(
181 LDAP_CONST char *data,
182 ber_len_t len )
184 static const char hexdig[] = "0123456789abcdef";
185 #define BP_OFFSET 9
186 #define BP_GRAPH 60
187 #define BP_LEN 80
188 char line[BP_LEN];
189 ber_len_t i;
191 assert( data != NULL );
193 /* in case len is zero */
194 line[0] = '\n';
195 line[1] = '\0';
197 for ( i = 0 ; i < len ; i++ ) {
198 int n = i % 16;
199 unsigned off;
201 if( !n ) {
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';
207 off = i % 0x0ffffU;
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];
213 line[6] = ':';
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];
224 } else {
225 line[BP_GRAPH + n] = '.';
229 (*ber_pvt_log_print)( line );
234 ber_log_dump(
235 int errlvl,
236 int loglvl,
237 BerElement *ber,
238 int inout )
240 assert( ber != NULL );
241 assert( LBER_VALID( ber ) );
243 if ( !ber_log_check( errlvl, loglvl )) {
244 return 0;
247 ber_dump(ber, inout);
248 return 1;
251 void
252 ber_dump(
253 BerElement *ber,
254 int inout )
256 char buf[132];
257 ber_len_t len;
259 assert( ber != NULL );
260 assert( LBER_VALID( ber ) );
262 if ( inout == 1 ) {
263 len = ber_pvt_ber_remaining(ber);
264 } else {
265 len = ber_pvt_ber_write(ber);
268 sprintf( buf, "ber_dump: buf=%p ptr=%p end=%p len=%ld\n",
269 ber->ber_buf,
270 ber->ber_ptr,
271 ber->ber_end,
272 (long) len );
274 (void) (*ber_pvt_log_print)( buf );
276 ber_bprint( ber->ber_ptr, len );
280 ber_log_sos_dump(
281 int errlvl,
282 int loglvl,
283 Seqorset *sos )
285 assert( sos != NULL );
287 if ( !ber_log_check( errlvl, loglvl )) {
288 return 0;
291 ber_sos_dump( sos );
292 return 1;
295 void
296 ber_sos_dump(
297 Seqorset *sos )
299 char buf[132];
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,
308 sos->sos_first,
309 sos->sos_ptr );
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 );
318 sos = sos->sos_next;
321 (*ber_pvt_log_print)( "*** end dump ***\n" );