]> git.sur5r.net Git - openldap/blob - libraries/liblber/dtest.c
Fix typo
[openldap] / libraries / liblber / dtest.c
1 /* dtest.c - lber decoding test program */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* Portions
8  * Copyright (c) 1990 Regents of the University of Michigan.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms are permitted
12  * provided that this notice is preserved and that due credit is given
13  * to the University of Michigan at Ann Arbor. The name of the University
14  * may not be used to endorse or promote products derived from this
15  * software without specific prior written permission. This software
16  * is provided ``as is'' without express or implied warranty.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
25 #include <ac/socket.h>
26 #include <ac/unistd.h>
27
28 #ifdef HAVE_CONSOLE_H
29 #include <console.h>
30 #endif
31
32 #include <lber.h>
33
34 static void usage( const char *name )
35 {
36         fprintf( stderr, "usage: %s fmt\n", name );
37 }
38
39 int
40 main( int argc, char **argv )
41 {
42         char *s;
43
44         ber_tag_t       tag;
45         ber_len_t       len;
46
47         BerElement      *ber;
48         Sockbuf         *sb;
49         int             fd;
50
51         /* enable debugging */
52         int ival = -1;
53         ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
54
55         if ( argc < 2 ) {
56                 usage( argv[0] );
57                 return( EXIT_FAILURE );
58         }
59
60 #ifdef HAVE_CONSOLE_H
61         ccommand( &argv );
62         cshow( stdout );
63 #endif
64
65         sb = ber_sockbuf_alloc();
66         fd = fileno( stdin );
67         ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
68                 (void *)&fd );
69
70         ber = ber_alloc_t(LBER_USE_DER);
71         if( ber == NULL ) {
72                 perror( "ber_alloc_t" );
73                 return( EXIT_FAILURE );
74         }
75
76         tag = ber_get_next( sb, &len, ber);
77         if( tag == LBER_ERROR ) {
78                 perror( "ber_get_next" );
79                 return( EXIT_FAILURE );
80         }
81
82         printf("decode: message tag 0x%lx and length %ld\n",
83                 (unsigned long) tag, (long) len );
84
85         for( s = argv[1]; *s; s++ ) {
86                 char buf[128];
87                 char fmt[2];
88                 fmt[0] = *s;
89                 fmt[1] = '\0';
90
91                 printf("decode: format %s\n", fmt );
92                 len = sizeof(buf);
93                 tag = ber_scanf( ber, fmt, &buf[0], &len );
94
95                 if( tag == LBER_ERROR ) {
96                         perror( "ber_scanf" );
97                         return( EXIT_FAILURE );
98                 }
99         }
100
101         ber_sockbuf_free( sb );
102         return( EXIT_SUCCESS );
103 }