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