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