xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblber/dtest.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: dtest.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $	*/
2 
3 /* dtest.c - lber decoding test program */
4 /* OpenLDAP: pkg/ldap/libraries/liblber/dtest.c,v 1.37.2.4 2009/01/22 00:00:53 kurt Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2009 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 "portable.h"
34 
35 #include <stdio.h>
36 
37 #include <ac/stdlib.h>
38 #include <ac/string.h>
39 #include <ac/socket.h>
40 #include <ac/unistd.h>
41 #include <ac/errno.h>
42 
43 #ifdef HAVE_CONSOLE_H
44 #include <console.h>
45 #endif
46 
47 #include <lber.h>
48 
49 static void usage( const char *name )
50 {
51 	fprintf( stderr, "usage: %s fmt\n", name );
52 }
53 
54 int
55 main( int argc, char **argv )
56 {
57 	char *s;
58 
59 	ber_tag_t	tag;
60 	ber_len_t	len;
61 
62 	BerElement	*ber;
63 	Sockbuf		*sb;
64 	int		fd;
65 
66 	/* enable debugging */
67 	int ival = -1;
68 	ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
69 
70 	if ( argc < 2 ) {
71 		usage( argv[0] );
72 		return( EXIT_FAILURE );
73 	}
74 
75 #ifdef HAVE_CONSOLE_H
76 	ccommand( &argv );
77 	cshow( stdout );
78 #endif
79 
80 	sb = ber_sockbuf_alloc();
81 	fd = fileno( stdin );
82 	ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
83 		(void *)&fd );
84 
85 	ber = ber_alloc_t(LBER_USE_DER);
86 	if( ber == NULL ) {
87 		perror( "ber_alloc_t" );
88 		return( EXIT_FAILURE );
89 	}
90 
91 	for (;;) {
92 		tag = ber_get_next( sb, &len, ber);
93 		if( tag != LBER_ERROR ) break;
94 
95 		if( errno == EWOULDBLOCK ) continue;
96 		if( errno == EAGAIN ) continue;
97 
98 		perror( "ber_get_next" );
99 		return( EXIT_FAILURE );
100 	}
101 
102 	printf("decode: message tag 0x%lx and length %ld\n",
103 		(unsigned long) tag, (long) len );
104 
105 	for( s = argv[1]; *s; s++ ) {
106 		char buf[128];
107 		char fmt[2];
108 		fmt[0] = *s;
109 		fmt[1] = '\0';
110 
111 		printf("decode: format %s\n", fmt );
112 		len = sizeof(buf);
113 		tag = ber_scanf( ber, fmt, &buf[0], &len );
114 
115 		if( tag == LBER_ERROR ) {
116 			perror( "ber_scanf" );
117 			return( EXIT_FAILURE );
118 		}
119 	}
120 
121 	ber_sockbuf_free( sb );
122 	return( EXIT_SUCCESS );
123 }
124