1 /* test.c - lber encoding test program */
4 * Copyright 1998-2000 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();
79 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
83 perror( "ber_sockbuf_alloc_fd" );
84 return( EXIT_FAILURE );
87 if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
88 perror( "ber_alloc" );
89 return( EXIT_FAILURE );
92 fprintf(stderr, "encode: start\n" );
93 if( ber_printf( ber, "{" /*}*/ ) ) {
94 perror( "ber_printf {" /*}*/ );
95 return( EXIT_FAILURE );
98 for ( s = argv[1]; *s; s++ ) {
105 fprintf(stderr, "encode: %s\n", fmt );
108 case 'b': /* boolean */
109 case 'e': /* enumeration */
111 rc = ber_printf( ber, fmt, atoi(buf) );
115 case '{': /* begin sequence */
116 case '}': /* end sequence */
117 case '[': /* begin set */
118 case ']': /* end set */
119 rc = ber_printf( ber, fmt );
122 case 'o': /* octet string (non-null terminated) */
123 case 'B': /* bit string */
125 rc = ber_printf( ber, fmt, buf, strlen(buf) );
128 case 's': /* string */
129 case 't': /* tag for the next element */
131 rc = ber_printf( ber, fmt, buf );
135 fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
141 perror( "ber_printf" );
142 return( EXIT_FAILURE );
146 fprintf(stderr, "encode: end\n" );
147 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
148 perror( /*{*/ "ber_printf }" );
149 return( EXIT_FAILURE );
152 if ( ber_flush( sb, ber, 1 ) == -1 ) {
153 perror( "ber_flush" );
154 return( EXIT_FAILURE );
157 ber_sockbuf_free( sb );
158 return( EXIT_SUCCESS );