No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / liblber / etest.c
blob16900b473cde50b5fd46b2f751a6e822cb0afff6
1 /* etest.c - lber encoding test program */
2 /* $OpenLDAP: pkg/ldap/libraries/liblber/etest.c,v 1.35.2.4 2008/02/11 23:26:41 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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.
26 /* ACKNOWLEDGEMENTS:
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
31 #include "portable.h"
33 #include <stdio.h>
35 #include <ac/stdlib.h>
37 #include <ac/socket.h>
38 #include <ac/string.h>
39 #include <ac/unistd.h>
41 #ifdef HAVE_CONSOLE_H
42 #include <console.h>
43 #endif /* HAVE_CONSOLE_H */
45 #include "lber.h"
47 static void usage( const char *name )
49 fprintf( stderr, "usage: %s fmtstring\n", name );
52 static char* getbuf( void ) {
53 char *p;
54 static char buf[1024];
56 if ( fgets( buf, sizeof(buf), stdin ) == NULL ) return NULL;
58 if ( (p = strchr( buf, '\n' )) != NULL ) *p = '\0';
60 return buf;
63 int
64 main( int argc, char **argv )
66 char *s;
67 int tag;
69 int fd, rc;
70 BerElement *ber;
71 Sockbuf *sb;
73 /* enable debugging */
74 int ival = -1;
75 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
77 if ( argc < 2 ) {
78 usage( argv[0] );
79 return( EXIT_FAILURE );
82 #ifdef HAVE_CONSOLE_H
83 ccommand( &argv );
84 cshow( stdout );
86 if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
87 < 0 ) {
88 perror( "open" );
89 return( EXIT_FAILURE );
92 #else
93 fd = fileno(stdout);
94 #endif
96 sb = ber_sockbuf_alloc();
97 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
98 (void *)&fd );
100 if( sb == NULL ) {
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++ ) {
117 char *buf;
118 char fmt[2];
120 fmt[0] = *s;
121 fmt[1] = '\0';
123 fprintf(stderr, "encode: %s\n", fmt );
124 switch ( *s ) {
125 case 'i': /* int */
126 case 'b': /* boolean */
127 case 'e': /* enumeration */
128 buf = getbuf();
129 rc = ber_printf( ber, fmt, atoi(buf) );
130 break;
132 case 'n': /* null */
133 case '{': /* begin sequence */
134 case '}': /* end sequence */
135 case '[': /* begin set */
136 case ']': /* end set */
137 rc = ber_printf( ber, fmt );
138 break;
140 case 'o': /* octet string (non-null terminated) */
141 case 'B': /* bit string */
142 buf = getbuf();
143 rc = ber_printf( ber, fmt, buf, strlen(buf) );
144 break;
146 case 's': /* string */
147 buf = getbuf();
148 rc = ber_printf( ber, fmt, buf );
149 break;
150 case 't': /* tag for the next element */
151 buf = getbuf();
152 tag = atoi(buf);
153 rc = ber_printf( ber, fmt, tag );
154 break;
156 default:
157 fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
158 rc = -1;
159 break;
162 if( rc == -1 ) {
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 );