1 /* dtest.c - lber decoding test program */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 1998-2021 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
15 */
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
25 */
26 /* ACKNOWLEDGEMENTS:
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
29 */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/stdlib.h>
36 #include <ac/string.h>
37 #include <ac/socket.h>
38 #include <ac/unistd.h>
39 #include <ac/errno.h>
40
41 #ifdef HAVE_CONSOLE_H
42 #include <console.h>
43 #endif
44
45 #include <lber.h>
46
usage(const char * name)47 static void usage( const char *name )
48 {
49 fprintf( stderr, "usage: %s fmt\n", name );
50 }
51
52 int
main(int argc,char ** argv)53 main( int argc, char **argv )
54 {
55 char *s;
56
57 ber_tag_t tag;
58 ber_len_t len;
59
60 BerElement *ber;
61 Sockbuf *sb;
62 int fd;
63
64 /* enable debugging */
65 int ival = -1;
66 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
67
68 if ( argc < 2 ) {
69 usage( argv[0] );
70 return( EXIT_FAILURE );
71 }
72
73 #ifdef HAVE_CONSOLE_H
74 ccommand( &argv );
75 cshow( stdout );
76 #endif
77
78 sb = ber_sockbuf_alloc();
79 fd = fileno( stdin );
80 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
81 (void *)&fd );
82
83 ber = ber_alloc_t(LBER_USE_DER);
84 if( ber == NULL ) {
85 perror( "ber_alloc_t" );
86 return( EXIT_FAILURE );
87 }
88
89 for (;;) {
90 tag = ber_get_next( sb, &len, ber);
91 if( tag != LBER_ERROR ) break;
92
93 if( errno == EWOULDBLOCK ) continue;
94 if( errno == EAGAIN ) continue;
95
96 perror( "ber_get_next" );
97 return( EXIT_FAILURE );
98 }
99
100 printf("decode: message tag 0x%lx and length %ld\n",
101 (unsigned long) tag, (long) len );
102
103 for( s = argv[1]; *s; s++ ) {
104 char buf[128];
105 char fmt[2];
106 fmt[0] = *s;
107 fmt[1] = '\0';
108
109 printf("decode: format %s\n", fmt );
110 len = sizeof(buf);
111 tag = ber_scanf( ber, fmt, &buf[0], &len );
112
113 if( tag == LBER_ERROR ) {
114 perror( "ber_scanf" );
115 return( EXIT_FAILURE );
116 }
117 }
118
119 ber_sockbuf_free( sb );
120 return( EXIT_SUCCESS );
121 }
122