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