xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblber/dtest.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1*549b59edSchristos /*	$NetBSD: dtest.c,v 1.3 2021/08/14 16:14:55 christos Exp $	*/
24e6df137Slukem 
32de962bdSlukem /* dtest.c - lber decoding test program */
4d11b170bStron /* $OpenLDAP$ */
52de962bdSlukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
62de962bdSlukem  *
7*549b59edSchristos  * Copyright 1998-2021 The OpenLDAP Foundation.
82de962bdSlukem  * All rights reserved.
92de962bdSlukem  *
102de962bdSlukem  * Redistribution and use in source and binary forms, with or without
112de962bdSlukem  * modification, are permitted only as authorized by the OpenLDAP
122de962bdSlukem  * Public License.
132de962bdSlukem  *
142de962bdSlukem  * A copy of this license is available in the file LICENSE in the
152de962bdSlukem  * top-level directory of the distribution or, alternatively, at
162de962bdSlukem  * <http://www.OpenLDAP.org/license.html>.
172de962bdSlukem  */
182de962bdSlukem /* Portions Copyright (c) 1990 Regents of the University of Michigan.
192de962bdSlukem  * All rights reserved.
202de962bdSlukem  *
212de962bdSlukem  * Redistribution and use in source and binary forms are permitted
222de962bdSlukem  * provided that this notice is preserved and that due credit is given
232de962bdSlukem  * to the University of Michigan at Ann Arbor. The name of the University
242de962bdSlukem  * may not be used to endorse or promote products derived from this
252de962bdSlukem  * software without specific prior written permission. This software
262de962bdSlukem  * is provided ``as is'' without express or implied warranty.
272de962bdSlukem  */
282de962bdSlukem /* ACKNOWLEDGEMENTS:
292de962bdSlukem  * This work was originally developed by the University of Michigan
302de962bdSlukem  * (as part of U-MICH LDAP).
312de962bdSlukem  */
322de962bdSlukem 
33376af7d7Schristos #include <sys/cdefs.h>
34*549b59edSchristos __RCSID("$NetBSD: dtest.c,v 1.3 2021/08/14 16:14:55 christos Exp $");
35376af7d7Schristos 
362de962bdSlukem #include "portable.h"
372de962bdSlukem 
382de962bdSlukem #include <stdio.h>
392de962bdSlukem 
402de962bdSlukem #include <ac/stdlib.h>
412de962bdSlukem #include <ac/string.h>
422de962bdSlukem #include <ac/socket.h>
432de962bdSlukem #include <ac/unistd.h>
442de962bdSlukem #include <ac/errno.h>
452de962bdSlukem 
462de962bdSlukem #ifdef HAVE_CONSOLE_H
472de962bdSlukem #include <console.h>
482de962bdSlukem #endif
492de962bdSlukem 
502de962bdSlukem #include <lber.h>
512de962bdSlukem 
usage(const char * name)522de962bdSlukem static void usage( const char *name )
532de962bdSlukem {
542de962bdSlukem 	fprintf( stderr, "usage: %s fmt\n", name );
552de962bdSlukem }
562de962bdSlukem 
572de962bdSlukem int
main(int argc,char ** argv)582de962bdSlukem main( int argc, char **argv )
592de962bdSlukem {
602de962bdSlukem 	char *s;
612de962bdSlukem 
622de962bdSlukem 	ber_tag_t	tag;
632de962bdSlukem 	ber_len_t	len;
642de962bdSlukem 
652de962bdSlukem 	BerElement	*ber;
662de962bdSlukem 	Sockbuf		*sb;
672de962bdSlukem 	int		fd;
682de962bdSlukem 
692de962bdSlukem 	/* enable debugging */
702de962bdSlukem 	int ival = -1;
712de962bdSlukem 	ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
722de962bdSlukem 
732de962bdSlukem 	if ( argc < 2 ) {
742de962bdSlukem 		usage( argv[0] );
752de962bdSlukem 		return( EXIT_FAILURE );
762de962bdSlukem 	}
772de962bdSlukem 
782de962bdSlukem #ifdef HAVE_CONSOLE_H
792de962bdSlukem 	ccommand( &argv );
802de962bdSlukem 	cshow( stdout );
812de962bdSlukem #endif
822de962bdSlukem 
832de962bdSlukem 	sb = ber_sockbuf_alloc();
842de962bdSlukem 	fd = fileno( stdin );
852de962bdSlukem 	ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
862de962bdSlukem 		(void *)&fd );
872de962bdSlukem 
882de962bdSlukem 	ber = ber_alloc_t(LBER_USE_DER);
892de962bdSlukem 	if( ber == NULL ) {
902de962bdSlukem 		perror( "ber_alloc_t" );
912de962bdSlukem 		return( EXIT_FAILURE );
922de962bdSlukem 	}
932de962bdSlukem 
942de962bdSlukem 	for (;;) {
952de962bdSlukem 		tag = ber_get_next( sb, &len, ber);
962de962bdSlukem 		if( tag != LBER_ERROR ) break;
972de962bdSlukem 
982de962bdSlukem 		if( errno == EWOULDBLOCK ) continue;
992de962bdSlukem 		if( errno == EAGAIN ) continue;
1002de962bdSlukem 
1012de962bdSlukem 		perror( "ber_get_next" );
1022de962bdSlukem 		return( EXIT_FAILURE );
1032de962bdSlukem 	}
1042de962bdSlukem 
1052de962bdSlukem 	printf("decode: message tag 0x%lx and length %ld\n",
1062de962bdSlukem 		(unsigned long) tag, (long) len );
1072de962bdSlukem 
1082de962bdSlukem 	for( s = argv[1]; *s; s++ ) {
1092de962bdSlukem 		char buf[128];
1102de962bdSlukem 		char fmt[2];
1112de962bdSlukem 		fmt[0] = *s;
1122de962bdSlukem 		fmt[1] = '\0';
1132de962bdSlukem 
1142de962bdSlukem 		printf("decode: format %s\n", fmt );
1152de962bdSlukem 		len = sizeof(buf);
1162de962bdSlukem 		tag = ber_scanf( ber, fmt, &buf[0], &len );
1172de962bdSlukem 
1182de962bdSlukem 		if( tag == LBER_ERROR ) {
1192de962bdSlukem 			perror( "ber_scanf" );
1202de962bdSlukem 			return( EXIT_FAILURE );
1212de962bdSlukem 		}
1222de962bdSlukem 	}
1232de962bdSlukem 
1242de962bdSlukem 	ber_sockbuf_free( sb );
1252de962bdSlukem 	return( EXIT_SUCCESS );
1262de962bdSlukem }
127