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