bprint.c 4.7 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
5
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8

#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
11

#include <ac/ctype.h>
12
#include <ac/stdarg.h>
13
#include <ac/string.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
14

15
#include "lber-int.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
16

17
18
19
20
21
22
23
24
25
/*
 * 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;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * ber errno
 */
BER_ERRNO_FN ber_int_errno_fn;

int * ber_errno_addr(void)
{
	static int ber_int_errno = LBER_ERROR_NONE;

	if( ber_int_errno_fn ) {
		return (*ber_int_errno_fn)();
	}

	return &ber_int_errno;
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
42
/*
43
 * Print stuff
Kurt Zeilenga's avatar
Kurt Zeilenga committed
44
 */
45
void ber_error_print( LDAP_CONST char *data )
46
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
47
48
	assert( data != NULL );

49
50
51
52
53
54
55
56
	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)
	{
57
58
	fputs( data, stderr );
	fflush( stderr );
59
60
61
	}

	fflush( ber_pvt_err_file );
62
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
63

Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
65

66
67
68
69
/*
 * lber log 
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
70
static int ber_log_check( int errlvl, int loglvl )
71
72
73
74
{
	return errlvl & loglvl ? 1 : 0;
}

75
int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
76
77
78
79
{
	char buf[ 1024 ];
	va_list ap;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
80
81
82
	assert( fmt != NULL );

	if ( !ber_log_check( errlvl, loglvl )) {
83
84
85
		return 0;
	}

86
87
	va_start( ap, fmt );

88
89
90
#ifdef HAVE_VSNPRINTF
	buf[sizeof(buf) - 1] = '\0';
	vsnprintf( buf, sizeof(buf)-1, fmt, ap );
91
#elif HAVE_VSPRINTF
92
93
94
	vsprintf( buf, fmt, ap ); /* hope it's not too long */
#else
	/* use doprnt() */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
95
#error "vsprintf() required."
96
97
98
99
#endif

	va_end(ap);

Kurt Zeilenga's avatar
Kurt Zeilenga committed
100
	(*ber_pvt_log_print)( buf );
101
102
103
	return 1;
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
static int ber_log_puts(int errlvl, int loglvl, char *buf)
105
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
106
107
108
	assert( buf != NULL );

	if ( !ber_log_check( errlvl, loglvl )) {
109
110
111
		return 0;
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
112
	(*ber_pvt_log_print)( buf );
113
114
115
	return 1;
}

116
117
118
/*
 * Print arbitrary stuff, for debugging.
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
119

120
int
Kurt Zeilenga's avatar
Kurt Zeilenga committed
121
122
123
ber_log_bprint(int errlvl,
	int loglvl,
	const char *data,
124
	ber_len_t len )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
125
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
126
127
128
	assert( data != NULL );

	if ( !ber_log_check( errlvl, loglvl )) {
129
130
		return 0;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
131

132
133
134
135
136
	ber_bprint(data, len);
	return 1;
}

void
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
138
ber_bprint(
	LDAP_CONST char *data,
139
	ber_len_t len )
140
{
141
    static const char	hexdig[] = "0123456789abcdef";
142
#define BPLEN	48
Kurt Zeilenga's avatar
Kurt Zeilenga committed
143
    char	out[ BPLEN ];
144
    char	buf[ BPLEN + sizeof("\t%s\n") ];
Kurt Zeilenga's avatar
Kurt Zeilenga committed
145
146
    int		i = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
147
148
	assert( data != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
149
    memset( out, '\0', BPLEN );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
150
151
    for ( ;; ) {
	if ( len < 1 ) {
152
	    sprintf( buf, "\t%s\n", ( i == 0 ) ? "(end)" : out );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
153
		(*ber_pvt_log_print)( buf );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
154
155
156
	    break;
	}

157
#ifndef LDAP_HEX
Kurt Zeilenga's avatar
Kurt Zeilenga committed
158
159
160
161
162
	if ( isgraph( (unsigned char)*data )) {
	    out[ i ] = ' ';
	    out[ i+1 ] = *data;
	} else {
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
163
164
	    out[ i ] = hexdig[ ( *data & 0xf0U ) >> 4 ];
	    out[ i+1 ] = hexdig[ *data & 0x0fU ];
165
#ifndef LDAP_HEX
Kurt Zeilenga's avatar
Kurt Zeilenga committed
166
167
168
169
170
171
172
	}
#endif
	i += 2;
	len--;
	data++;

	if ( i > BPLEN - 2 ) {
173
174
		char data[128 + BPLEN];
	    sprintf( data, "\t%s\n", out );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
175
		(*ber_pvt_log_print)(data);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
176
	    memset( out, '\0', BPLEN );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
177
178
179
180
181
	    i = 0;
	    continue;
	}
	out[ i++ ] = ' ';
    }
182
183
184
}

int
Kurt Zeilenga's avatar
Kurt Zeilenga committed
185
186
187
ber_log_dump(
	int errlvl,
	int loglvl,
188
	BerElement *ber,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
189
	int inout )
190
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
191
	assert( ber != NULL );
192
	assert( BER_VALID( ber ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
193
194

	if ( !ber_log_check( errlvl, loglvl )) {
195
196
197
198
199
200
201
202
		return 0;
	}

	ber_dump(ber, inout);
	return 1;
}

void
Kurt Zeilenga's avatar
Kurt Zeilenga committed
203
ber_dump(
204
	BerElement *ber,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
205
	int inout )
206
207
208
{
	char buf[132];

Kurt Zeilenga's avatar
Kurt Zeilenga committed
209
	assert( ber != NULL );
210
	assert( BER_VALID( ber ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
211

212
213
214
215
216
	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 );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
217
	(*ber_pvt_log_print)( buf );
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

	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 );

	} 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 );
	}
}

int
233
ber_log_sos_dump(
Kurt Zeilenga's avatar
Kurt Zeilenga committed
234
235
	int errlvl,
	int loglvl,
236
	Seqorset *sos )
237
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
239
240
	assert( sos != NULL );

	if ( !ber_log_check( errlvl, loglvl )) {
241
242
243
244
245
246
247
248
		return 0;
	}

	ber_sos_dump( sos );
	return 1;
}

void
Kurt Zeilenga's avatar
Kurt Zeilenga committed
249
ber_sos_dump(
250
	Seqorset *sos )
251
252
253
{
	char buf[132];

Kurt Zeilenga's avatar
Kurt Zeilenga committed
254
255
256
	assert( sos != NULL );

	(*ber_pvt_log_print)( "*** sos dump ***\n" );
257

258
	while ( sos != NULL ) {
259
		sprintf( buf, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
260
261
262
		    (long) sos->sos_clen,
			(long) sos->sos_first,
			(long) sos->sos_ptr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
263
		(*ber_pvt_log_print)( buf );
264
265
266

		sprintf( buf, "              current len %ld contents:\n",
		    (long) (sos->sos_ptr - sos->sos_first) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
267
		(*ber_pvt_log_print)( buf );
268
269
270
271
272

		ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );

		sos = sos->sos_next;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
273

Kurt Zeilenga's avatar
Kurt Zeilenga committed
274
	(*ber_pvt_log_print)( "*** end dump ***\n" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
275
}