1 /*        $NetBSD: bprint.c,v 1.3 2021/08/14 16:14:55 christos Exp $  */
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2021 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /*
18  * Copyright (c) 1991 Regents of the University of Michigan.
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms are permitted
22  * provided that this notice is preserved and that due credit is given
23  * to the University of Michigan at Ann Arbor. The name of the University
24  * may not be used to endorse or promote products derived from this
25  * software without specific prior written permission. This software
26  * is provided ``as is'' without express or implied warranty.
27  */
28 /* ACKNOWLEDGEMENTS:
29  * This work was originally developed by the University of Michigan
30  * (as part of U-MICH LDAP).
31  */
32 
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: bprint.c,v 1.3 2021/08/14 16:14:55 christos Exp $");
35 
36 #include "portable.h"
37 
38 #include <stdio.h>
39 
40 #include <ac/ctype.h>
41 #include <ac/stdarg.h>
42 #include <ac/string.h>
43 
44 #include "lber-int.h"
45 
46 #define   ber_log_check(errlvl, loglvl) ((errlvl) & (loglvl))
47 
48 BER_LOG_FN ber_int_log_proc = NULL;
49 
50 /*
51  * We don't just set ber_pvt_err_file to stderr here, because in NT,
52  * stderr is a symbol imported from a DLL. As such, the compiler
53  * doesn't recognize the symbol as having a constant address. Thus
54  * we set ber_pvt_err_file to stderr later, when it first gets
55  * referenced.
56  */
57 FILE *ber_pvt_err_file = NULL;
58 
59 /*
60  * ber errno
61  */
62 BER_ERRNO_FN ber_int_errno_fn = NULL;
63 
ber_errno_addr(void)64 int * ber_errno_addr(void)
65 {
66           static int ber_int_errno = LBER_ERROR_NONE;
67 
68           if( ber_int_errno_fn ) {
69                     return (*ber_int_errno_fn)();
70           }
71 
72           return &ber_int_errno;
73 }
74 
75 /*
76  * Print stuff
77  */
ber_error_print(LDAP_CONST char * data)78 void ber_error_print( LDAP_CONST char *data )
79 {
80           assert( data != NULL );
81 
82           if (!ber_pvt_err_file) ber_pvt_err_file = stderr;
83 
84           fputs( data, ber_pvt_err_file );
85 
86           /* Print to both streams */
87           if (ber_pvt_err_file != stderr) {
88                     fputs( data, stderr );
89                     fflush( stderr );
90           }
91 
92           fflush( ber_pvt_err_file );
93 }
94 
95 BER_LOG_PRINT_FN ber_pvt_log_print = ber_error_print;
96 
97 /*
98  * lber log
99  */
100 
ber_pvt_log_output(const char * subsystem,int level,const char * fmt,...)101 int ber_pvt_log_output(
102           const char *subsystem,
103           int level,
104           const char *fmt,
105           ... )
106 {
107           char buf[1024];
108           va_list vl;
109           va_start( vl, fmt );
110 
111           if ( ber_int_log_proc != NULL ) {
112                     ber_int_log_proc( ber_pvt_err_file, subsystem, level, fmt, vl );
113 
114           } else {
115                     int level;
116                     ber_get_option( NULL, LBER_OPT_BER_DEBUG, &level );
117                     buf[sizeof(buf) - 1] = '\0';
118                     vsnprintf( buf, sizeof(buf)-1, fmt, vl );
119                     if ( ber_log_check( LDAP_DEBUG_BER, level ) ) {
120                               (*ber_pvt_log_print)( buf );
121                     }
122           }
123 
124           va_end(vl);
125           return 1;
126 }
127 
ber_pvt_log_printf(int errlvl,int loglvl,const char * fmt,...)128 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... )
129 {
130           char buf[1024];
131           va_list ap;
132 
133           assert( fmt != NULL );
134 
135           if ( !ber_log_check( errlvl, loglvl )) {
136                     return 0;
137           }
138 
139           va_start( ap, fmt );
140 
141           buf[sizeof(buf) - 1] = '\0';
142           vsnprintf( buf, sizeof(buf)-1, fmt, ap );
143 
144           va_end(ap);
145 
146           (*ber_pvt_log_print)( buf );
147           return 1;
148 }
149 
150 #if 0
151 static int ber_log_puts(int errlvl, int loglvl, char *buf)
152 {
153           assert( buf != NULL );
154 
155           if ( !ber_log_check( errlvl, loglvl )) {
156                     return 0;
157           }
158 
159           (*ber_pvt_log_print)( buf );
160           return 1;
161 }
162 #endif
163 
164 /*
165  * Print arbitrary stuff, for debugging.
166  */
167 
168 int
ber_log_bprint(int errlvl,int loglvl,const char * data,ber_len_t len)169 ber_log_bprint(int errlvl,
170           int loglvl,
171           const char *data,
172           ber_len_t len )
173 {
174           assert( data != NULL );
175 
176           if ( !ber_log_check( errlvl, loglvl )) {
177                     return 0;
178           }
179 
180           ber_bprint(data, len);
181           return 1;
182 }
183 
184 void
ber_bprint(LDAP_CONST char * data,ber_len_t len)185 ber_bprint(
186           LDAP_CONST char *data,
187           ber_len_t len )
188 {
189           static const char hexdig[] = "0123456789abcdef";
190 #define BP_OFFSET 9
191 #define BP_GRAPH 60
192 #define BP_LEN      80
193           char      line[BP_LEN];
194           ber_len_t i;
195 
196           assert( data != NULL );
197 
198           /* in case len is zero */
199           line[0] = '\n';
200           line[1] = '\0';
201 
202           for ( i = 0 ; i < len ; i++ ) {
203                     int n = i % 16;
204                     unsigned off;
205 
206                     if( !n ) {
207                               if( i ) (*ber_pvt_log_print)( line );
208                               memset( line, ' ', sizeof(line)-2 );
209                               line[sizeof(line)-2] = '\n';
210                               line[sizeof(line)-1] = '\0';
211 
212                               off = i % 0x0ffffU;
213 
214                               line[2] = hexdig[0x0f & (off >> 12)];
215                               line[3] = hexdig[0x0f & (off >>  8)];
216                               line[4] = hexdig[0x0f & (off >>  4)];
217                               line[5] = hexdig[0x0f & off];
218                               line[6] = ':';
219                     }
220 
221                     off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
222                     line[off] = hexdig[0x0f & ( data[i] >> 4 )];
223                     line[off+1] = hexdig[0x0f & data[i]];
224 
225                     off = BP_GRAPH + n + ((n >= 8)?1:0);
226 
227                     if ( isprint( (unsigned char) data[i] )) {
228                               line[BP_GRAPH + n] = data[i];
229                     } else {
230                               line[BP_GRAPH + n] = '.';
231                     }
232           }
233 
234           (*ber_pvt_log_print)( line );
235 }
236 
237 
238 int
ber_log_dump(int errlvl,int loglvl,BerElement * ber,int inout)239 ber_log_dump(
240           int errlvl,
241           int loglvl,
242           BerElement *ber,
243           int inout )
244 {
245           assert( ber != NULL );
246           assert( LBER_VALID( ber ) );
247 
248           if ( !ber_log_check( errlvl, loglvl )) {
249                     return 0;
250           }
251 
252           ber_dump(ber, inout);
253           return 1;
254 }
255 
256 void
ber_dump(BerElement * ber,int inout)257 ber_dump(
258           BerElement *ber,
259           int inout )
260 {
261           char buf[132];
262           ber_len_t len;
263 
264           assert( ber != NULL );
265           assert( LBER_VALID( ber ) );
266 
267           if ( inout == 1 ) {
268                     len = ber_pvt_ber_remaining(ber);
269           } else {
270                     len = ber_pvt_ber_write(ber);
271           }
272 
273           sprintf( buf, "ber_dump: buf=%p ptr=%p end=%p len=%ld\n",
274                     ber->ber_buf,
275                     ber->ber_ptr,
276                     ber->ber_end,
277                     (long) len );
278 
279           (void) (*ber_pvt_log_print)( buf );
280 
281           ber_bprint( ber->ber_ptr, len );
282 }
283 
284 typedef struct seqorset Seqorset;
285 
286 /* Exists for binary compatibility with OpenLDAP 2.4.17-- */
287 int
ber_log_sos_dump(int errlvl,int loglvl,Seqorset * sos)288 ber_log_sos_dump(
289           int errlvl,
290           int loglvl,
291           Seqorset *sos )
292 {
293           return 0;
294 }
295 
296 /* Exists for binary compatibility with OpenLDAP 2.4.17-- */
297 void
ber_sos_dump(Seqorset * sos)298 ber_sos_dump(
299           Seqorset *sos )
300 {
301 }
302