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