1 /*        $NetBSD: dtest.c,v 1.3 2021/08/14 16:14:55 christos Exp $   */
2 
3 /* dtest.c - lber decoding test program */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2021 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* Portions Copyright (c) 1990 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: dtest.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/stdlib.h>
41 #include <ac/string.h>
42 #include <ac/socket.h>
43 #include <ac/unistd.h>
44 #include <ac/errno.h>
45 
46 #ifdef HAVE_CONSOLE_H
47 #include <console.h>
48 #endif
49 
50 #include <lber.h>
51 
usage(const char * name)52 static void usage( const char *name )
53 {
54           fprintf( stderr, "usage: %s fmt\n", name );
55 }
56 
57 int
main(int argc,char ** argv)58 main( int argc, char **argv )
59 {
60           char *s;
61 
62           ber_tag_t tag;
63           ber_len_t len;
64 
65           BerElement          *ber;
66           Sockbuf             *sb;
67           int                 fd;
68 
69           /* enable debugging */
70           int ival = -1;
71           ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
72 
73           if ( argc < 2 ) {
74                     usage( argv[0] );
75                     return( EXIT_FAILURE );
76           }
77 
78 #ifdef HAVE_CONSOLE_H
79           ccommand( &argv );
80           cshow( stdout );
81 #endif
82 
83           sb = ber_sockbuf_alloc();
84           fd = fileno( stdin );
85           ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
86                     (void *)&fd );
87 
88           ber = ber_alloc_t(LBER_USE_DER);
89           if( ber == NULL ) {
90                     perror( "ber_alloc_t" );
91                     return( EXIT_FAILURE );
92           }
93 
94           for (;;) {
95                     tag = ber_get_next( sb, &len, ber);
96                     if( tag != LBER_ERROR ) break;
97 
98                     if( errno == EWOULDBLOCK ) continue;
99                     if( errno == EAGAIN ) continue;
100 
101                     perror( "ber_get_next" );
102                     return( EXIT_FAILURE );
103           }
104 
105           printf("decode: message tag 0x%lx and length %ld\n",
106                     (unsigned long) tag, (long) len );
107 
108           for( s = argv[1]; *s; s++ ) {
109                     char buf[128];
110                     char fmt[2];
111                     fmt[0] = *s;
112                     fmt[1] = '\0';
113 
114                     printf("decode: format %s\n", fmt );
115                     len = sizeof(buf);
116                     tag = ber_scanf( ber, fmt, &buf[0], &len );
117 
118                     if( tag == LBER_ERROR ) {
119                               perror( "ber_scanf" );
120                               return( EXIT_FAILURE );
121                     }
122           }
123 
124           ber_sockbuf_free( sb );
125           return( EXIT_SUCCESS );
126 }
127