]> git.sur5r.net Git - openldap/blob - libraries/liblber/dtest.c
Import some lint removal
[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 #include <ac/errno.h>
28
29 #ifdef HAVE_CONSOLE_H
30 #include <console.h>
31 #endif
32
33 #include <lber.h>
34
35 static void usage( const char *name )
36 {
37         fprintf( stderr, "usage: %s fmt\n", name );
38 }
39
40 int
41 main( int argc, char **argv )
42 {
43         char *s;
44
45         ber_tag_t       tag;
46         ber_len_t       len;
47
48         BerElement      *ber;
49         Sockbuf         *sb;
50         int             fd;
51
52         /* enable debugging */
53         int ival = -1;
54         ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
55
56         if ( argc < 2 ) {
57                 usage( argv[0] );
58                 return( EXIT_FAILURE );
59         }
60
61 #ifdef HAVE_CONSOLE_H
62         ccommand( &argv );
63         cshow( stdout );
64 #endif
65
66         sb = ber_sockbuf_alloc();
67         fd = fileno( stdin );
68         ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
69                 (void *)&fd );
70
71         ber = ber_alloc_t(LBER_USE_DER);
72         if( ber == NULL ) {
73                 perror( "ber_alloc_t" );
74                 return( EXIT_FAILURE );
75         }
76
77         for (;;) {
78                 tag = ber_get_next( sb, &len, ber);
79                 if( tag != LBER_ERROR ) break;
80
81                 if( errno == EWOULDBLOCK ) continue;
82                 if( errno == EAGAIN ) continue;
83
84                 perror( "ber_get_next" );
85                 return( EXIT_FAILURE );
86         }
87
88         printf("decode: message tag 0x%lx and length %ld\n",
89                 (unsigned long) tag, (long) len );
90
91         for( s = argv[1]; *s; s++ ) {
92                 char buf[128];
93                 char fmt[2];
94                 fmt[0] = *s;
95                 fmt[1] = '\0';
96
97                 printf("decode: format %s\n", fmt );
98                 len = sizeof(buf);
99                 tag = ber_scanf( ber, fmt, &buf[0], &len );
100
101                 if( tag == LBER_ERROR ) {
102                         perror( "ber_scanf" );
103                         return( EXIT_FAILURE );
104                 }
105         }
106
107         ber_sockbuf_free( sb );
108         return( EXIT_SUCCESS );
109 }