xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblber/bprint.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
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/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
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.
10  *
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>.
14  */
15 /*
16  * Copyright (c) 1991 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26 /* ACKNOWLEDGEMENTS:
27  * This work was originally developed by the University of Michigan
28  * (as part of U-MICH LDAP).
29  */
30 
31 #include "portable.h"
32 
33 #include <stdio.h>
34 
35 #include <ac/ctype.h>
36 #include <ac/stdarg.h>
37 #include <ac/string.h>
38 
39 #include "lber-int.h"
40 
41 #define	ber_log_check(errlvl, loglvl)	((errlvl) & (loglvl))
42 
43 BER_LOG_FN ber_int_log_proc = NULL;
44 
45 /*
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.
51  */
52 FILE *ber_pvt_err_file = NULL;
53 
54 /*
55  * ber errno
56  */
57 BER_ERRNO_FN ber_int_errno_fn = NULL;
58 
59 int * ber_errno_addr(void)
60 {
61 	static int ber_int_errno = LBER_ERROR_NONE;
62 
63 	if( ber_int_errno_fn ) {
64 		return (*ber_int_errno_fn)();
65 	}
66 
67 	return &ber_int_errno;
68 }
69 
70 /*
71  * Print stuff
72  */
73 void ber_error_print( LDAP_CONST char *data )
74 {
75 	assert( data != NULL );
76 
77 	if (!ber_pvt_err_file) ber_pvt_err_file = stderr;
78 
79 	fputs( data, ber_pvt_err_file );
80 
81 	/* Print to both streams */
82 	if (ber_pvt_err_file != stderr) {
83 		fputs( data, stderr );
84 		fflush( stderr );
85 	}
86 
87 	fflush( ber_pvt_err_file );
88 }
89 
90 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
91 
92 /*
93  * lber log
94  */
95 
96 int ber_pvt_log_output(
97 	const char *subsystem,
98 	int level,
99 	const char *fmt,
100 	... )
101 {
102 	char buf[1024];
103 	va_list vl;
104 	va_start( vl, fmt );
105 
106 	if ( ber_int_log_proc != NULL ) {
107 		ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
108 
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 );
116 		}
117 	}
118 
119 	va_end(vl);
120 	return 1;
121 }
122 
123 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
124 {
125 	char buf[1024];
126 	va_list ap;
127 
128 	assert( fmt != NULL );
129 
130 	if ( !ber_log_check( errlvl, loglvl )) {
131 		return 0;
132 	}
133 
134 	va_start( ap, fmt );
135 
136 	buf[sizeof(buf) - 1] = '\0';
137 	vsnprintf( buf, sizeof(buf)-1, fmt, ap );
138 
139 	va_end(ap);
140 
141 	(*ber_pvt_log_print)( buf );
142 	return 1;
143 }
144 
145 #if 0
146 static int ber_log_puts(int errlvl, int loglvl, char *buf)
147 {
148 	assert( buf != NULL );
149 
150 	if ( !ber_log_check( errlvl, loglvl )) {
151 		return 0;
152 	}
153 
154 	(*ber_pvt_log_print)( buf );
155 	return 1;
156 }
157 #endif
158 
159 /*
160  * Print arbitrary stuff, for debugging.
161  */
162 
163 int
164 ber_log_bprint(int errlvl,
165 	int loglvl,
166 	const char *data,
167 	ber_len_t len )
168 {
169 	assert( data != NULL );
170 
171 	if ( !ber_log_check( errlvl, loglvl )) {
172 		return 0;
173 	}
174 
175 	ber_bprint(data, len);
176 	return 1;
177 }
178 
179 void
180 ber_bprint(
181 	LDAP_CONST char *data,
182 	ber_len_t len )
183 {
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;
190 
191 	assert( data != NULL );
192 
193 	/* in case len is zero */
194 	line[0] = '\n';
195 	line[1] = '\0';
196 
197 	for ( i = 0 ; i < len ; i++ ) {
198 		int n = i % 16;
199 		unsigned off;
200 
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';
206 
207 			off = i % 0x0ffffU;
208 
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] = ':';
214 		}
215 
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]];
219 
220 		off = BP_GRAPH + n + ((n >= 8)?1:0);
221 
222 		if ( isprint( (unsigned char) data[i] )) {
223 			line[BP_GRAPH + n] = data[i];
224 		} else {
225 			line[BP_GRAPH + n] = '.';
226 		}
227 	}
228 
229 	(*ber_pvt_log_print)( line );
230 }
231 
232 
233 int
234 ber_log_dump(
235 	int errlvl,
236 	int loglvl,
237 	BerElement *ber,
238 	int inout )
239 {
240 	assert( ber != NULL );
241 	assert( LBER_VALID( ber ) );
242 
243 	if ( !ber_log_check( errlvl, loglvl )) {
244 		return 0;
245 	}
246 
247 	ber_dump(ber, inout);
248 	return 1;
249 }
250 
251 void
252 ber_dump(
253 	BerElement *ber,
254 	int inout )
255 {
256 	char buf[132];
257 	ber_len_t len;
258 
259 	assert( ber != NULL );
260 	assert( LBER_VALID( ber ) );
261 
262 	if ( inout == 1 ) {
263 		len = ber_pvt_ber_remaining(ber);
264 	} else {
265 		len = ber_pvt_ber_write(ber);
266 	}
267 
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 );
273 
274 	(void) (*ber_pvt_log_print)( buf );
275 
276 	ber_bprint( ber->ber_ptr, len );
277 }
278 
279 int
280 ber_log_sos_dump(
281 	int errlvl,
282 	int loglvl,
283 	Seqorset *sos )
284 {
285 	assert( sos != NULL );
286 
287 	if ( !ber_log_check( errlvl, loglvl )) {
288 		return 0;
289 	}
290 
291 	ber_sos_dump( sos );
292 	return 1;
293 }
294 
295 void
296 ber_sos_dump(
297 	Seqorset *sos )
298 {
299 	char buf[132];
300 
301 	assert( sos != NULL );
302 
303 	(*ber_pvt_log_print)( "*** sos dump ***\n" );
304 
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 );
311 
312 		sprintf( buf, "              current len %ld contents:\n",
313 		    (long) (sos->sos_ptr - sos->sos_first) );
314 		(*ber_pvt_log_print)( buf );
315 
316 		ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );
317 
318 		sos = sos->sos_next;
319 	}
320 
321 	(*ber_pvt_log_print)( "*** end dump ***\n" );
322 }
323