1 /* test.c - lber encoding test program */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Copyright (c) 1990 Regents of the University of Michigan.
16 #include <ac/stdlib.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/unistd.h>
24 #endif /* HAVE_CONSOLE_H */
28 static void usage( char *name )
30 fprintf( stderr, "usage: %s fmtstring\n", name );
33 static char* getbuf() {
37 if ( fgets( buf, sizeof(buf), stdin ) == NULL )
40 if ( (p = strchr( buf, '\n' )) != NULL )
47 main( int argc, char **argv )
55 /* enable debugging */
57 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
61 return( EXIT_FAILURE );
68 if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
71 return( EXIT_FAILURE );
78 sb = ber_sockbuf_alloc_fd( fd );
81 perror( "ber_sockbuf_alloc_fd" );
82 return( EXIT_FAILURE );
85 if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
86 perror( "ber_alloc" );
87 return( EXIT_FAILURE );
90 fprintf(stderr, "encode: start\n" );
91 if( ber_printf( ber, "{" /*}*/ ) ) {
92 perror( "ber_printf {" /*}*/ );
93 return( EXIT_FAILURE );
96 for ( s = argv[1]; *s; s++ ) {
103 fprintf(stderr, "encode: %s\n", fmt );
106 case 'b': /* boolean */
107 case 'e': /* enumeration */
109 rc = ber_printf( ber, fmt, atoi(buf) );
113 case '{': /* begin sequence */
114 case '}': /* end sequence */
115 case '[': /* begin set */
116 case ']': /* end set */
117 rc = ber_printf( ber, fmt );
120 case 'o': /* octet string (non-null terminated) */
121 case 'B': /* bit string */
123 rc = ber_printf( ber, fmt, buf, strlen(buf) );
126 case 's': /* string */
127 case 't': /* tag for the next element */
129 rc = ber_printf( ber, fmt, buf );
133 fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
139 perror( "ber_printf" );
140 return( EXIT_FAILURE );
144 fprintf(stderr, "encode: end\n" );
145 if( ber_printf( ber, /*{*/ "}" ) == -1 ) {
146 perror( /*{*/ "ber_printf }" );
147 return( EXIT_FAILURE );
150 if ( ber_flush( sb, ber, 1 ) == -1 ) {
151 perror( "ber_flush" );
152 return( EXIT_FAILURE );
155 ber_sockbuf_free( sb );
156 return( EXIT_SUCCESS );