Back to index

openldap  2.4.31
dtest.c
Go to the documentation of this file.
00001 /* dtest.c - lber decoding test program */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 /* ACKNOWLEDGEMENTS:
00027  * This work was originally developed by the University of Michigan
00028  * (as part of U-MICH LDAP).
00029  */
00030 
00031 #include "portable.h"
00032 
00033 #include <stdio.h>
00034 
00035 #include <ac/stdlib.h>
00036 #include <ac/string.h>
00037 #include <ac/socket.h>
00038 #include <ac/unistd.h>
00039 #include <ac/errno.h>
00040 
00041 #ifdef HAVE_CONSOLE_H
00042 #include <console.h>
00043 #endif
00044 
00045 #include <lber.h>
00046 
00047 static void usage( const char *name )
00048 {
00049        fprintf( stderr, "usage: %s fmt\n", name );
00050 }
00051 
00052 int
00053 main( int argc, char **argv )
00054 {
00055        char *s;
00056 
00057        ber_tag_t     tag;
00058        ber_len_t     len;
00059 
00060        BerElement    *ber;
00061        Sockbuf              *sb;
00062        int           fd;
00063 
00064        /* enable debugging */
00065        int ival = -1;
00066        ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
00067 
00068        if ( argc < 2 ) {
00069               usage( argv[0] );
00070               return( EXIT_FAILURE );
00071        }
00072 
00073 #ifdef HAVE_CONSOLE_H
00074        ccommand( &argv );
00075        cshow( stdout );
00076 #endif
00077 
00078        sb = ber_sockbuf_alloc();
00079        fd = fileno( stdin );
00080        ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
00081               (void *)&fd );
00082 
00083        ber = ber_alloc_t(LBER_USE_DER);
00084        if( ber == NULL ) {
00085               perror( "ber_alloc_t" );
00086               return( EXIT_FAILURE );
00087        }
00088 
00089        for (;;) {
00090               tag = ber_get_next( sb, &len, ber);
00091               if( tag != LBER_ERROR ) break;
00092 
00093               if( errno == EWOULDBLOCK ) continue;
00094               if( errno == EAGAIN ) continue;
00095 
00096               perror( "ber_get_next" );
00097               return( EXIT_FAILURE );
00098        }
00099 
00100        printf("decode: message tag 0x%lx and length %ld\n",
00101               (unsigned long) tag, (long) len );
00102 
00103        for( s = argv[1]; *s; s++ ) {
00104               char buf[128];
00105               char fmt[2];
00106               fmt[0] = *s;
00107               fmt[1] = '\0';
00108 
00109               printf("decode: format %s\n", fmt );
00110               len = sizeof(buf);
00111               tag = ber_scanf( ber, fmt, &buf[0], &len );
00112 
00113               if( tag == LBER_ERROR ) {
00114                      perror( "ber_scanf" );
00115                      return( EXIT_FAILURE );
00116               }
00117        }
00118 
00119        ber_sockbuf_free( sb );
00120        return( EXIT_SUCCESS );
00121 }