]> git.sur5r.net Git - openldap/blob - libraries/liblber/etest.c
acknowledgements and notices
[openldap] / libraries / liblber / etest.c
1 /* etest.c - lber encoding test program */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26 /* ACKNOWLEDGEMENTS:
27  * This work was originally developed by the University of Michigan
28  * (as part of U-MICH LDAP).
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/stdlib.h>
36
37 #include <ac/socket.h>
38 #include <ac/string.h>
39 #include <ac/unistd.h>
40
41 #ifdef HAVE_CONSOLE_H
42 #include <console.h>
43 #endif /* HAVE_CONSOLE_H */
44
45 #include "lber.h"
46
47 static void usage( const char *name )
48 {
49         fprintf( stderr, "usage: %s fmtstring\n", name );
50 }
51
52 static char* getbuf( void ) {
53         char *p;
54         static char buf[1024];
55
56         if ( fgets( buf, sizeof(buf), stdin ) == NULL ) return NULL;
57
58         if ( (p = strchr( buf, '\n' )) != NULL ) *p = '\0';
59
60         return buf;
61 }
62
63 int
64 main( int argc, char **argv )
65 {
66         char    *s;
67
68         int                     fd, rc;
69         BerElement      *ber;
70         Sockbuf         *sb;
71
72         /* enable debugging */
73         int ival = -1;
74         ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
75
76         if ( argc < 2 ) {
77                 usage( argv[0] );
78                 return( EXIT_FAILURE );
79         }
80
81 #ifdef HAVE_CONSOLE_H
82         ccommand( &argv );
83         cshow( stdout );
84
85         if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
86                 < 0 ) {
87             perror( "open" );
88             return( EXIT_FAILURE );
89         }
90
91 #else
92         fd = fileno(stdout);
93 #endif
94
95         sb = ber_sockbuf_alloc();
96         ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
97                 (void *)&fd );
98
99         if( sb == NULL ) {
100                 perror( "ber_sockbuf_alloc_fd" );
101                 return( EXIT_FAILURE );
102         }
103
104         if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
105                 perror( "ber_alloc" );
106                 return( EXIT_FAILURE );
107         }
108
109         fprintf(stderr, "encode: start\n" );
110         if( ber_printf( ber, "{" /*}*/ ) ) {
111                 perror( "ber_printf {" /*}*/ );
112                 return( EXIT_FAILURE );
113         }
114
115         for ( s = argv[1]; *s; s++ ) {
116                 char *buf;
117                 char fmt[2];
118
119                 fmt[0] = *s;
120                 fmt[1] = '\0';
121
122                 fprintf(stderr, "encode: %s\n", fmt );
123                 switch ( *s ) {
124                 case 'i':       /* int */
125                 case 'b':       /* boolean */
126                 case 'e':       /* enumeration */
127                         buf = getbuf();
128                         rc = ber_printf( ber, fmt, atoi(buf) );
129                         break;
130
131                 case 'n':       /* null */
132                 case '{':       /* begin sequence */
133                 case '}':       /* end sequence */
134                 case '[':       /* begin set */
135                 case ']':       /* end set */
136                         rc = ber_printf( ber, fmt );
137                         break;
138
139                 case 'o':       /* octet string (non-null terminated) */
140                 case 'B':       /* bit string */
141                         buf = getbuf();
142                         rc = ber_printf( ber, fmt, buf, strlen(buf) );
143                         break;
144
145                 case 's':       /* string */
146                 case 't':       /* tag for the next element */
147                         buf = getbuf();
148                         rc = ber_printf( ber, fmt, buf );
149                         break;
150
151                 default:
152                         fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
153                         rc = -1;
154                         break;
155                 }
156
157                 if( rc == -1 ) {
158                         perror( "ber_printf" );
159                         return( EXIT_FAILURE );
160                 }
161         }
162
163         fprintf(stderr, "encode: end\n" );
164         if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
165                 perror( /*{*/ "ber_printf }" );
166                 return( EXIT_FAILURE );
167         }
168
169         if ( ber_flush( sb, ber, 1 ) == -1 ) {
170                 perror( "ber_flush" );
171                 return( EXIT_FAILURE );
172         }
173
174         ber_sockbuf_free( sb );
175         return( EXIT_SUCCESS );
176 }