Back to index

openldap  2.4.31
etest.c
Go to the documentation of this file.
00001 /* etest.c - lber encoding 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 
00037 #include <ac/socket.h>
00038 #include <ac/string.h>
00039 #include <ac/unistd.h>
00040 
00041 #ifdef HAVE_CONSOLE_H
00042 #include <console.h>
00043 #endif /* HAVE_CONSOLE_H */
00044 
00045 #include "lber.h"
00046 
00047 static void usage( const char *name )
00048 {
00049        fprintf( stderr, "usage: %s fmtstring\n", name );
00050 }
00051 
00052 static char* getbuf( void ) {
00053        char *p;
00054        static char buf[1024];
00055 
00056        if ( fgets( buf, sizeof(buf), stdin ) == NULL ) return NULL;
00057 
00058        if ( (p = strchr( buf, '\n' )) != NULL ) *p = '\0';
00059 
00060        return buf;
00061 }
00062 
00063 int
00064 main( int argc, char **argv )
00065 {
00066        char   *s;
00067        int tag;
00068 
00069        int                  fd, rc;
00070        BerElement    *ber;
00071        Sockbuf              *sb;
00072 
00073        /* enable debugging */
00074        int ival = -1;
00075        ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ival );
00076 
00077        if ( argc < 2 ) {
00078               usage( argv[0] );
00079               return( EXIT_FAILURE );
00080        }
00081 
00082 #ifdef HAVE_CONSOLE_H
00083        ccommand( &argv );
00084        cshow( stdout );
00085 
00086        if (( fd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY ))
00087               < 0 ) {
00088            perror( "open" );
00089            return( EXIT_FAILURE );
00090        }
00091 
00092 #else
00093        fd = fileno(stdout);
00094 #endif
00095 
00096        sb = ber_sockbuf_alloc();
00097        ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd, LBER_SBIOD_LEVEL_PROVIDER,
00098               (void *)&fd );
00099 
00100        if( sb == NULL ) {
00101               perror( "ber_sockbuf_alloc_fd" );
00102               return( EXIT_FAILURE );
00103        }
00104 
00105        if ( (ber = ber_alloc_t( LBER_USE_DER )) == NULL ) {
00106               perror( "ber_alloc" );
00107               return( EXIT_FAILURE );
00108        }
00109 
00110        fprintf(stderr, "encode: start\n" );
00111        if( ber_printf( ber, "{" /*}*/ ) ) {
00112               perror( "ber_printf {" /*}*/ );
00113               return( EXIT_FAILURE );
00114        }
00115 
00116        for ( s = argv[1]; *s; s++ ) {
00117               char *buf;
00118               char fmt[2];
00119 
00120               fmt[0] = *s;
00121               fmt[1] = '\0';
00122 
00123               fprintf(stderr, "encode: %s\n", fmt );
00124               switch ( *s ) {
00125               case 'i':     /* int */
00126               case 'b':     /* boolean */
00127               case 'e':     /* enumeration */
00128                      buf = getbuf();
00129                      rc = ber_printf( ber, fmt, atoi(buf) );
00130                      break;
00131 
00132               case 'n':     /* null */
00133               case '{':     /* begin sequence */
00134               case '}':     /* end sequence */
00135               case '[':     /* begin set */
00136               case ']':     /* end set */
00137                      rc = ber_printf( ber, fmt );
00138                      break;
00139 
00140               case 'o':     /* octet string (non-null terminated) */
00141               case 'B':     /* bit string */
00142                      buf = getbuf();
00143                      rc = ber_printf( ber, fmt, buf, strlen(buf) );
00144                      break;
00145 
00146               case 's':     /* string */
00147                      buf = getbuf();
00148                      rc = ber_printf( ber, fmt, buf );
00149                      break;
00150               case 't':     /* tag for the next element */
00151                      buf = getbuf();
00152                      tag = atoi(buf);
00153                      rc = ber_printf( ber, fmt, tag );
00154                      break;
00155 
00156               default:
00157                      fprintf( stderr, "encode: unknown fmt %c\n", *fmt );
00158                      rc = -1;
00159                      break;
00160               }
00161 
00162               if( rc == -1 ) {
00163                      perror( "ber_printf" );
00164                      return( EXIT_FAILURE );
00165               }
00166        }
00167 
00168        fprintf(stderr, "encode: end\n" );
00169        if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
00170               perror( /*{*/ "ber_printf }" );
00171               return( EXIT_FAILURE );
00172        }
00173 
00174        if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) == -1 ) {
00175               perror( "ber_flush2" );
00176               return( EXIT_FAILURE );
00177        }
00178 
00179        ber_sockbuf_free( sb );
00180        return( EXIT_SUCCESS );
00181 }