]> git.sur5r.net Git - openldap/blob - libraries/liblber/bprint.c
nt_err.c has moved from lutil to lber
[openldap] / libraries / liblber / bprint.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/ctype.h>
12 #include <ac/stdarg.h>
13 #include <ac/string.h>
14
15 #include "lber-int.h"
16
17 /*
18  * We don't just set ber_pvt_err_file to stderr here, because in NT,
19  * stderr is a symbol imported from a DLL. As such, the compiler
20  * doesn't recognize the symbol as having a constant address. Thus
21  * we set ber_pvt_err_file to stderr later, when it first gets
22  * referenced.
23  */
24 FILE *ber_pvt_err_file;
25
26 /*
27  * ber errno
28  */
29 BER_ERRNO_FN ber_int_errno_fn;
30
31 int * ber_errno_addr(void)
32 {
33         static int ber_int_errno = LBER_ERROR_NONE;
34
35         if( ber_int_errno_fn ) {
36                 return (*ber_int_errno_fn)();
37         }
38
39         return &ber_int_errno;
40 }
41
42 /*
43  * Print stuff
44  */
45 static void
46 ber_error_print( char *data )
47 {
48         assert( data != NULL );
49
50         if (!ber_pvt_err_file)
51             ber_pvt_err_file = stderr;
52
53         fputs( data, ber_pvt_err_file );
54
55         /* Print to both streams */
56         if (ber_pvt_err_file != stderr)
57         {
58         fputs( data, stderr );
59         fflush( stderr );
60         }
61
62         fflush( ber_pvt_err_file );
63 }
64
65 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
66
67 /*
68  * lber log 
69  */
70
71 static int ber_log_check( int errlvl, int loglvl )
72 {
73         return errlvl & loglvl ? 1 : 0;
74 }
75
76 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
77 {
78         char buf[ 1024 ];
79         va_list ap;
80
81         assert( fmt != NULL );
82
83         if ( !ber_log_check( errlvl, loglvl )) {
84                 return 0;
85         }
86
87         va_start( ap, fmt );
88
89 #ifdef HAVE_VSNPRINTF
90         buf[sizeof(buf) - 1] = '\0';
91         vsnprintf( buf, sizeof(buf)-1, fmt, ap );
92 #elif HAVE_VSPRINTF
93         vsprintf( buf, fmt, ap ); /* hope it's not too long */
94 #else
95         /* use doprnt() */
96 #error "vsprintf() required."
97 #endif
98
99         va_end(ap);
100
101         (*ber_pvt_log_print)( buf );
102         return 1;
103 }
104
105 static int ber_log_puts(int errlvl, int loglvl, char *buf)
106 {
107         assert( buf != NULL );
108
109         if ( !ber_log_check( errlvl, loglvl )) {
110                 return 0;
111         }
112
113         (*ber_pvt_log_print)( buf );
114         return 1;
115 }
116
117 /*
118  * Print arbitrary stuff, for debugging.
119  */
120
121 int
122 ber_log_bprint(int errlvl,
123         int loglvl,
124         const char *data,
125         ber_len_t len )
126 {
127         assert( data != NULL );
128
129         if ( !ber_log_check( errlvl, loglvl )) {
130                 return 0;
131         }
132
133         ber_bprint(data, len);
134         return 1;
135 }
136
137 void
138 ber_bprint(
139         LDAP_CONST char *data,
140         ber_len_t len )
141 {
142     static const char   hexdig[] = "0123456789abcdef";
143 #define BPLEN   48
144     char        out[ BPLEN ];
145     char        buf[ BPLEN + sizeof("\t%s\n") ];
146     int         i = 0;
147
148         assert( data != NULL );
149
150     memset( out, 0, BPLEN );
151     for ( ;; ) {
152         if ( len < 1 ) {
153             sprintf( buf, "\t%s\n", ( i == 0 ) ? "(end)" : out );
154                 (*ber_pvt_log_print)( buf );
155             break;
156         }
157
158 #ifndef LDAP_HEX
159         if ( isgraph( (unsigned char)*data )) {
160             out[ i ] = ' ';
161             out[ i+1 ] = *data;
162         } else {
163 #endif
164             out[ i ] = hexdig[ ( *data & 0xf0U ) >> 4 ];
165             out[ i+1 ] = hexdig[ *data & 0x0fU ];
166 #ifndef LDAP_HEX
167         }
168 #endif
169         i += 2;
170         len--;
171         data++;
172
173         if ( i > BPLEN - 2 ) {
174                 char data[128 + BPLEN];
175             sprintf( data, "\t%s\n", out );
176                 (*ber_pvt_log_print)(data);
177             memset( out, 0, BPLEN );
178             i = 0;
179             continue;
180         }
181         out[ i++ ] = ' ';
182     }
183 }
184
185 int
186 ber_log_dump(
187         int errlvl,
188         int loglvl,
189         BerElement *ber,
190         int inout )
191 {
192         assert( ber != NULL );
193         assert( BER_VALID( ber ) );
194
195         if ( !ber_log_check( errlvl, loglvl )) {
196                 return 0;
197         }
198
199         ber_dump(ber, inout);
200         return 1;
201 }
202
203 void
204 ber_dump(
205         BerElement *ber,
206         int inout )
207 {
208         char buf[132];
209
210         assert( ber != NULL );
211         assert( BER_VALID( ber ) );
212
213         sprintf( buf, "ber_dump: buf 0x%lx, ptr 0x%lx, end 0x%lx\n",
214             (long) ber->ber_buf,
215                 (long) ber->ber_ptr,
216                 (long) ber->ber_end );
217
218         (*ber_pvt_log_print)( buf );
219
220         if ( inout == 1 ) {
221                 sprintf( buf, "          current len %ld, contents:\n",
222                     (long) (ber->ber_end - ber->ber_ptr) );
223                 ber_bprint( ber->ber_ptr, ber->ber_end - ber->ber_ptr );
224
225         } else {
226                 sprintf( buf, "          current len %ld, contents:\n",
227                     (long) (ber->ber_ptr - ber->ber_buf) );
228
229                 ber_bprint( ber->ber_buf, ber->ber_ptr - ber->ber_buf );
230         }
231 }
232
233 int
234 ber_log_sos_dump(
235         int errlvl,
236         int loglvl,
237         Seqorset *sos )
238 {
239         assert( sos != NULL );
240
241         if ( !ber_log_check( errlvl, loglvl )) {
242                 return 0;
243         }
244
245         ber_sos_dump( sos );
246         return 1;
247 }
248
249 void
250 ber_sos_dump(
251         Seqorset *sos )
252 {
253         char buf[132];
254
255         assert( sos != NULL );
256
257         (*ber_pvt_log_print)( "*** sos dump ***\n" );
258
259         while ( sos != NULL ) {
260                 sprintf( buf, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n",
261                     (long) sos->sos_clen,
262                         (long) sos->sos_first,
263                         (long) sos->sos_ptr );
264                 (*ber_pvt_log_print)( buf );
265
266                 sprintf( buf, "              current len %ld contents:\n",
267                     (long) (sos->sos_ptr - sos->sos_first) );
268                 (*ber_pvt_log_print)( buf );
269
270                 ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );
271
272                 sos = sos->sos_next;
273         }
274
275         (*ber_pvt_log_print)( "*** end dump ***\n" );
276 }