1 /* test.c - lber encoding test program */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
15 #include <ac/stdlib.h>
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/unistd.h>
23 #endif /* HAVE_CONSOLE_H */
27 static void usage( char *name )
29 fprintf( stderr, "usage: %s fmtstring\n", name );
32 static char* getbuf() {
36 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
39 if ( (p = strchr( buf, '\n' )) != NULL )
46 main( int argc, char **argv )
54 /* enable debugging */
56 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
60 return( EXIT_FAILURE );
67 if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
70 return( EXIT_FAILURE );
77 sb = ber_sockbuf_alloc_fd( fd );
80 perror( "ber_sockbuf_alloc_fd" );
81 return( EXIT_FAILURE );
84 if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
85 perror( "ber_alloc" );
86 return( EXIT_FAILURE );
89 fprintf(stderr, "encode: start\n" );
90 if( ber_printf( ber, "{" /*}*/ ) ) {
91 perror( "ber_printf {" /*}*/ );
92 return( EXIT_FAILURE );
95 for ( s = argv[1]; *s; s++ ) {
102 fprintf(stderr, "encode: %s\n", fmt );
105 case 'b': /* boolean */
106 case 'e': /* enumeration */
108 rc = ber_printf( ber, fmt, atoi(buf) );
112 case '{': /* begin sequence */
113 case '}': /* end sequence */
114 case '[': /* begin set */
115 case ']': /* end set */
116 rc = ber_printf( ber, fmt );
119 case 'o': /* octet string (non-null terminated) */
120 case 'B': /* bit string */
122 rc = ber_printf( ber, fmt, buf, strlen(buf) );
125 case 's': /* string */
126 case 't': /* tag for the next element */
128 rc = ber_printf( ber, fmt, buf );
132 fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
138 perror( "ber_printf" );
139 return( EXIT_FAILURE );
143 fprintf(stderr, "encode: end\n" );
144 if( ber_printf( ber, /*{*/ "}" ) == -1 ) {
145 perror( /*{*/ "ber_printf }" );
146 return( EXIT_FAILURE );
149 if ( ber_flush( sb, ber, 1 ) == -1 ) {
150 perror( "ber_flush" );
151 return( EXIT_FAILURE );
154 ber_sockbuf_free( sb );
155 return( EXIT_SUCCESS );