xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblber/etest.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: etest.c,v 1.3 2021/08/14 16:14:55 christos Exp $	*/
2 
3 /* etest.c - lber encoding 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: etest.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 
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/unistd.h>
45 
46 #ifdef HAVE_CONSOLE_H
47 #include <console.h>
48 #endif /* HAVE_CONSOLE_H */
49 
50 #include "lber.h"
51 
usage(const char * name)52 static void usage( const char *name )
53 {
54 	fprintf( stderr, "usage: %s fmtstring\n", name );
55 }
56 
getbuf(void)57 static char* getbuf( void ) {
58 	char *p;
59 	static char buf[1024];
60 
61 	if ( fgets( buf, sizeof(buf), stdin ) == NULL ) return NULL;
62 
63 	if ( (p = strchr( buf, '\n' )) != NULL ) *p = '\0';
64 
65 	return buf;
66 }
67 
68 int
main(int argc,char ** argv)69 main( int argc, char **argv )
70 {
71 	char	*s;
72 	int tag;
73 
74 	int			fd, rc;
75 	BerElement	*ber;
76 	Sockbuf		*sb;
77 
78 	/* enable debugging */
79 	int ival = -1;
80 	ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
81 
82 	if ( argc < 2 ) {
83 		usage( argv[0] );
84 		return( EXIT_FAILURE );
85 	}
86 
87 #ifdef HAVE_CONSOLE_H
88 	ccommand( &argv );
89 	cshow( stdout );
90 
91 	if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
92 		< 0 ) {
93 	    perror( "open" );
94 	    return( EXIT_FAILURE );
95 	}
96 
97 #else
98 	fd = fileno(stdout);
99 #endif
100 
101 	sb = ber_sockbuf_alloc();
102 	ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
103 		(void *)&fd );
104 
105 	if( sb == NULL ) {
106 		perror( "ber_sockbuf_alloc_fd" );
107 		return( EXIT_FAILURE );
108 	}
109 
110 	if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
111 		perror( "ber_alloc" );
112 		return( EXIT_FAILURE );
113 	}
114 
115 	fprintf(stderr, "encode: start\n" );
116 	if( ber_printf( ber, "{" /*}*/ ) ) {
117 		perror( "ber_printf {" /*}*/ );
118 		return( EXIT_FAILURE );
119 	}
120 
121 	for ( s = argv[1]; *s; s++ ) {
122 		char *buf;
123 		char fmt[2];
124 
125 		fmt[0] = *s;
126 		fmt[1] = '\0';
127 
128 		fprintf(stderr, "encode: %s\n", fmt );
129 		switch ( *s ) {
130 		case 'i':	/* int */
131 		case 'b':	/* boolean */
132 		case 'e':	/* enumeration */
133 			buf = getbuf();
134 			rc = ber_printf( ber, fmt, atoi(buf) );
135 			break;
136 
137 		case 'n':	/* null */
138 		case '{':	/* begin sequence */
139 		case '}':	/* end sequence */
140 		case '[':	/* begin set */
141 		case ']':	/* end set */
142 			rc = ber_printf( ber, fmt );
143 			break;
144 
145 		case 'o':	/* octet string (non-null terminated) */
146 		case 'B':	/* bit string */
147 			buf = getbuf();
148 			rc = ber_printf( ber, fmt, buf, strlen(buf) );
149 			break;
150 
151 		case 's':	/* string */
152 			buf = getbuf();
153 			rc = ber_printf( ber, fmt, buf );
154 			break;
155 		case 't':	/* tag for the next element */
156 			buf = getbuf();
157 			tag = atoi(buf);
158 			rc = ber_printf( ber, fmt, tag );
159 			break;
160 
161 		default:
162 			fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
163 			rc = -1;
164 			break;
165 		}
166 
167 		if( rc == -1 ) {
168 			perror( "ber_printf" );
169 			return( EXIT_FAILURE );
170 		}
171 	}
172 
173 	fprintf(stderr, "encode: end\n" );
174 	if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
175 		perror( /*{*/ "ber_printf }" );
176 		return( EXIT_FAILURE );
177 	}
178 
179 	if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
180 		perror( "ber_flush2" );
181 		return( EXIT_FAILURE );
182 	}
183 
184 	ber_sockbuf_free( sb );
185 	return( EXIT_SUCCESS );
186 }
187