]> git.sur5r.net Git - openldap/blob - libraries/liblber/bprint.c
bafbdc3740006d7e87ad84a1c181e0173afd8043
[openldap] / libraries / liblber / bprint.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 void ber_error_print( LDAP_CONST char *data )
46 {
47         assert( data != NULL );
48
49         if (!ber_pvt_err_file)
50             ber_pvt_err_file = stderr;
51
52         fputs( data, ber_pvt_err_file );
53
54         /* Print to both streams */
55         if (ber_pvt_err_file != stderr)
56         {
57         fputs( data, stderr );
58         fflush( stderr );
59         }
60
61         fflush( ber_pvt_err_file );
62 }
63
64 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
65
66 /*
67  * lber log 
68  */
69
70 static int ber_log_check( int errlvl, int loglvl )
71 {
72         return errlvl & loglvl ? 1 : 0;
73 }
74
75 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
76 {
77         char buf[ 1024 ];
78         va_list ap;
79
80         assert( fmt != NULL );
81
82         if ( !ber_log_check( errlvl, loglvl )) {
83                 return 0;
84         }
85
86         va_start( ap, fmt );
87
88 #ifdef HAVE_VSNPRINTF
89         buf[sizeof(buf) - 1] = '\0';
90         vsnprintf( buf, sizeof(buf)-1, fmt, ap );
91 #elif HAVE_VSPRINTF
92         vsprintf( buf, fmt, ap ); /* hope it's not too long */
93 #else
94         /* use doprnt() */
95 #error "vsprintf() required."
96 #endif
97
98         va_end(ap);
99
100         (*ber_pvt_log_print)( buf );
101         return 1;
102 }
103
104 static int ber_log_puts(int errlvl, int loglvl, char *buf)
105 {
106         assert( buf != NULL );
107
108         if ( !ber_log_check( errlvl, loglvl )) {
109                 return 0;
110         }
111
112         (*ber_pvt_log_print)( buf );
113         return 1;
114 }
115
116 /*
117  * Print arbitrary stuff, for debugging.
118  */
119
120 int
121 ber_log_bprint(int errlvl,
122         int loglvl,
123         const char *data,
124         ber_len_t len )
125 {
126         assert( data != NULL );
127
128         if ( !ber_log_check( errlvl, loglvl )) {
129                 return 0;
130         }
131
132         ber_bprint(data, len);
133         return 1;
134 }
135
136 void
137 ber_bprint(
138         LDAP_CONST char *data,
139         ber_len_t len )
140 {
141     static const char   hexdig[] = "0123456789abcdef";
142 #define BPLEN   48
143     char        out[ BPLEN ];
144     char        buf[ BPLEN + sizeof("\t%s\n") ];
145     int         i = 0;
146
147         assert( data != NULL );
148
149     memset( out, '\0', BPLEN );
150     for ( ;; ) {
151         if ( len < 1 ) {
152             sprintf( buf, "\t%s\n", ( i == 0 ) ? "(end)" : out );
153                 (*ber_pvt_log_print)( buf );
154             break;
155         }
156
157 #ifndef LDAP_HEX
158         if ( isgraph( (unsigned char)*data )) {
159             out[ i ] = ' ';
160             out[ i+1 ] = *data;
161         } else {
162 #endif
163             out[ i ] = hexdig[ ( *data & 0xf0U ) >> 4 ];
164             out[ i+1 ] = hexdig[ *data & 0x0fU ];
165 #ifndef LDAP_HEX
166         }
167 #endif
168         i += 2;
169         len--;
170         data++;
171
172         if ( i > BPLEN - 2 ) {
173                 char data[128 + BPLEN];
174             sprintf( data, "\t%s\n", out );
175                 (*ber_pvt_log_print)(data);
176             memset( out, '\0', BPLEN );
177             i = 0;
178             continue;
179         }
180         out[ i++ ] = ' ';
181     }
182 }
183
184 int
185 ber_log_dump(
186         int errlvl,
187         int loglvl,
188         BerElement *ber,
189         int inout )
190 {
191         assert( ber != NULL );
192         assert( BER_VALID( ber ) );
193
194         if ( !ber_log_check( errlvl, loglvl )) {
195                 return 0;
196         }
197
198         ber_dump(ber, inout);
199         return 1;
200 }
201
202 void
203 ber_dump(
204         BerElement *ber,
205         int inout )
206 {
207         char buf[132];
208
209         assert( ber != NULL );
210         assert( BER_VALID( ber ) );
211
212         sprintf( buf, "ber_dump: buf 0x%lx, ptr 0x%lx, end 0x%lx\n",
213             (long) ber->ber_buf,
214                 (long) ber->ber_ptr,
215                 (long) ber->ber_end );
216
217         (*ber_pvt_log_print)( buf );
218
219         if ( inout == 1 ) {
220                 sprintf( buf, "          current len %ld, contents:\n",
221                     (long) (ber->ber_end - ber->ber_ptr) );
222                 ber_bprint( ber->ber_ptr, ber->ber_end - ber->ber_ptr );
223
224         } else {
225                 sprintf( buf, "          current len %ld, contents:\n",
226                     (long) (ber->ber_ptr - ber->ber_buf) );
227
228                 ber_bprint( ber->ber_buf, ber->ber_ptr - ber->ber_buf );
229         }
230 }
231
232 int
233 ber_log_sos_dump(
234         int errlvl,
235         int loglvl,
236         Seqorset *sos )
237 {
238         assert( sos != NULL );
239
240         if ( !ber_log_check( errlvl, loglvl )) {
241                 return 0;
242         }
243
244         ber_sos_dump( sos );
245         return 1;
246 }
247
248 void
249 ber_sos_dump(
250         Seqorset *sos )
251 {
252         char buf[132];
253
254         assert( sos != NULL );
255
256         (*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                     (long) sos->sos_clen,
261                         (long) sos->sos_first,
262                         (long) sos->sos_ptr );
263                 (*ber_pvt_log_print)( buf );
264
265                 sprintf( buf, "              current len %ld contents:\n",
266                     (long) (sos->sos_ptr - sos->sos_first) );
267                 (*ber_pvt_log_print)( buf );
268
269                 ber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first );
270
271                 sos = sos->sos_next;
272         }
273
274         (*ber_pvt_log_print)( "*** end dump ***\n" );
275 }