1 /* etest.c - lber encoding test program */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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 )
73 /* enable debugging */
75 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
79 return( EXIT_FAILURE );
86 if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
89 return( EXIT_FAILURE );
96 sb = ber_sockbuf_alloc();
97 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
101 perror( "ber_sockbuf_alloc_fd" );
102 return( EXIT_FAILURE );
105 if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
106 perror( "ber_alloc" );
107 return( EXIT_FAILURE );
110 fprintf(stderr, "encode: start\n" );
111 if( ber_printf( ber, "{" /*}*/ ) ) {
112 perror( "ber_printf {" /*}*/ );
113 return( EXIT_FAILURE );
116 for ( s = argv[1]; *s; s++ ) {
123 fprintf(stderr, "encode: %s\n", fmt );
126 case 'b': /* boolean */
127 case 'e': /* enumeration */
129 rc = ber_printf( ber, fmt, atoi(buf) );
133 case '{': /* begin sequence */
134 case '}': /* end sequence */
135 case '[': /* begin set */
136 case ']': /* end set */
137 rc = ber_printf( ber, fmt );
140 case 'o': /* octet string (non-null terminated) */
141 case 'B': /* bit string */
143 rc = ber_printf( ber, fmt, buf, strlen(buf) );
146 case 's': /* string */
148 rc = ber_printf( ber, fmt, buf );
150 case 't': /* tag for the next element */
153 rc = ber_printf( ber, fmt, tag );
157 fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
163 perror( "ber_printf" );
164 return( EXIT_FAILURE );
168 fprintf(stderr, "encode: end\n" );
169 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
170 perror( /*{*/ "ber_printf }" );
171 return( EXIT_FAILURE );
174 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
175 perror( "ber_flush2" );
176 return( EXIT_FAILURE );
179 ber_sockbuf_free( sb );
180 return( EXIT_SUCCESS );