]> git.sur5r.net Git - openldap/blob - libraries/liblber/dtest.c
0415ccd98218e4f5eb5091f47d278a9c1a38b061
[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 #include <stdlib.h>
22
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 /* MACOS */
30
31 #include "lber-int.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         unsigned long tag, len;
45
46         BerElement      *ber;
47         Sockbuf         *sb;
48
49         /* enable debugging */
50         ber_int_debug = -1;
51
52         if ( argc < 2 ) {
53                 usage( argv[0] );
54                 return( EXIT_FAILURE );
55         }
56
57 #ifdef HAVE_CONSOLE_H
58         ccommand( &argv );
59         cshow( stdout );
60 #endif /* MACOS */
61
62         sb = ber_sockbuf_alloc_fd( fileno(stdin) );
63
64         if( (ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
65                 perror( "ber_alloc_t" );
66                 return( EXIT_FAILURE );
67         }
68
69         if(( tag = ber_get_next( sb, &len, ber) ) == LBER_ERROR ) {
70                 perror( "ber_get_next" );
71                 return( EXIT_FAILURE );
72         }
73
74         printf("decode: message tag 0x%lx and length %ld\n",
75                 tag, len );
76
77         for( s = argv[1]; *s; s++ ) {
78                 char buf[128];
79                 char fmt[2];
80                 fmt[0] = *s;
81                 fmt[1] = '\0';
82
83                 printf("decode: format %s\n", fmt );
84                 len = sizeof(buf);
85                 rc = ber_scanf( ber, fmt, &buf[0], &len );
86
87                 if( rc == LBER_ERROR ) {
88                         perror( "ber_scanf" );
89                         return( EXIT_FAILURE );
90                 }
91         }
92
93         if(( tag = ber_get_next( sb, &len, ber) ) == LBER_ERROR ) {
94                 perror( "ber_get_next" );
95                 return( EXIT_FAILURE );
96         }
97
98         printf("decode: message tag 0x%lx and length %ld\n",
99                 tag, len );
100
101         ber_sockbuf_free( sb );
102         return( EXIT_SUCCESS );
103 }