1 /* etest.c - lber encoding test program */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
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.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
35 #include <ac/stdlib.h>
37 #include <ac/socket.h>
38 #include <ac/string.h>
39 #include <ac/unistd.h>
43 #endif /* HAVE_CONSOLE_H */
47 static void usage( const char *name )
49 fprintf( stderr, "usage: %s fmtstring\n", name );
52 static char* getbuf( void ) {
54 static char buf[1024];
56 if ( fgets( buf, sizeof(buf), stdin ) == NULL ) return NULL;
58 if ( (p = strchr( buf, '\n' )) != NULL ) *p = '\0';
64 main( int argc, char **argv )
72 /* enable debugging */
74 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
78 return( EXIT_FAILURE );
85 if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
88 return( EXIT_FAILURE );
95 sb = ber_sockbuf_alloc();
96 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
100 perror( "ber_sockbuf_alloc_fd" );
101 return( EXIT_FAILURE );
104 if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
105 perror( "ber_alloc" );
106 return( EXIT_FAILURE );
109 fprintf(stderr, "encode: start\n" );
110 if( ber_printf( ber, "{" /*}*/ ) ) {
111 perror( "ber_printf {" /*}*/ );
112 return( EXIT_FAILURE );
115 for ( s = argv[1]; *s; s++ ) {
122 fprintf(stderr, "encode: %s\n", fmt );
125 case 'b': /* boolean */
126 case 'e': /* enumeration */
128 rc = ber_printf( ber, fmt, atoi(buf) );
132 case '{': /* begin sequence */
133 case '}': /* end sequence */
134 case '[': /* begin set */
135 case ']': /* end set */
136 rc = ber_printf( ber, fmt );
139 case 'o': /* octet string (non-null terminated) */
140 case 'B': /* bit string */
142 rc = ber_printf( ber, fmt, buf, strlen(buf) );
145 case 's': /* string */
146 case 't': /* tag for the next element */
148 rc = ber_printf( ber, fmt, buf );
152 fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
158 perror( "ber_printf" );
159 return( EXIT_FAILURE );
163 fprintf(stderr, "encode: end\n" );
164 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
165 perror( /*{*/ "ber_printf }" );
166 return( EXIT_FAILURE );
169 if ( ber_flush( sb, ber, 1 ) == -1 ) {
170 perror( "ber_flush" );
171 return( EXIT_FAILURE );
174 ber_sockbuf_free( sb );
175 return( EXIT_SUCCESS );