Back to index

lightning-sunbird  0.9+nobinonly
etest.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Copyright (c) 1990 Regents of the University of Michigan.
00040  * All rights reserved.
00041  * Redistribution and use in source and binary forms are permitted
00042  * provided that this notice is preserved and that due credit is given
00043  * to the University of Michigan at Ann Arbor. The name of the University
00044  * may not be used to endorse or promote products derived from this
00045  * software without specific prior written permission. This software
00046  * is provided ``as is'' without express or implied warranty.
00047  */
00048 
00049 /* test.c - lber encoding test program */
00050 
00051 #include <stdio.h>
00052 #include <string.h>
00053 #ifdef MACOS
00054 #include <stdlib.h>
00055 #include <unix.h>
00056 #include <fcntl.h>
00057 #include <console.h>
00058 #else /* MACOS */
00059 #include <sys/types.h>
00060 #ifdef _WIN32
00061 #include <windows.h>
00062 #else
00063 #include <sys/socket.h>
00064 #endif /* _WIN32 */
00065 #endif /* MACOS */
00066 #include "lber.h"
00067 
00068 int
00069 SSL_Recv( int s, char *b, unsigned l, int dummy )
00070 {
00071        return( read( s, b, l ) );
00072 }
00073 
00074 SSL_Send( int s, char *b, unsigned l, int dummy )
00075 {
00076        return( write( s, b, l ) );
00077 }
00078 
00079 int
00080 getline( char *prompt, char c, char *buf, int bsize )
00081 {
00082        char   *p;
00083 
00084        if ( prompt != NULL ) {
00085               fprintf( stderr, "%s: ", prompt );
00086        } else {
00087               fprintf( stderr, "enter value for '%c': ", c );
00088        }
00089        if ( fgets( buf, bsize, stdin ) == NULL ) {
00090               return( -1 );
00091        }
00092        if ( (p = strchr( buf, '\n' )) != NULL ) {
00093               *p = '\0';
00094        }
00095 
00096        return( 0 );
00097 }
00098 
00099 
00100 static void usage( char *name )
00101 {
00102        fprintf( stderr, "usage: %s fmtstring\n", name );
00103 }
00104 
00105 main( int argc, char **argv )
00106 {
00107        int           rc, fd;
00108        char          *s, *p;
00109        void          *arg1, *arg2;
00110        Sockbuf              *sb;
00111        BerElement    *ber;
00112        char          fmt[2];
00113        char          buf[BUFSIZ];
00114        extern int    lber_debug;
00115 
00116        lber_debug = 255;
00117        if ( argc < 2 ) {
00118               usage( argv[0] );
00119               exit( 1 );
00120        }
00121 
00122        sb = ber_sockbuf_alloc();
00123        fd = 1;
00124        ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_DESC, &fd );
00125 
00126        if ( (ber = der_alloc()) == NULL ) {
00127               perror( "ber_alloc" );
00128               exit( 1 );
00129        }
00130 
00131        rc = 0;
00132        fmt[1] = '\0';
00133        for ( s = argv[1]; *s; s++ ) {
00134               switch ( *s ) {
00135               case 'i':     /* int */
00136               case 'b':     /* boolean */
00137               case 'e':     /* enumeration */
00138                      getline( NULL, *s, buf, sizeof(buf) );
00139                      arg1 = (void *) atoi( buf );
00140                      break;
00141 
00142               case 'n':     /* null */
00143                      arg1 = NULL;
00144                      break;
00145 
00146               case 'o':     /* octet string (non-null terminated) */
00147                      getline( NULL, *s, buf, sizeof(buf) );
00148                      arg1 = (void *) buf;
00149                      arg2 = (void *) strlen( buf );
00150                      break;
00151 
00152               case 's':     /* string */
00153                      getline( NULL, *s, buf, sizeof(buf) );
00154                      arg1 = (void *) buf;
00155                      break;
00156 
00157               case 'B':     /* bit string */
00158                      getline( NULL, *s, buf, sizeof(buf) );
00159                      arg1 = (void *) buf;
00160                      arg2 = (void *) strlen( buf );
00161                      break;
00162 
00163               case 't':     /* tag for the next element */
00164                      getline( NULL, *s, buf, sizeof(buf) );
00165                      arg1 = (void *) buf;
00166                      break;
00167 
00168               case '{':     /* begin sequence */
00169               case '}':     /* end sequence */
00170               case '[':     /* begin set */
00171               case ']':     /* end set */
00172                      break;
00173 
00174               default:
00175                      fprintf( stderr, "unknown fmt %c\n", *s );
00176                      rc = -1;
00177                      break;
00178               }
00179 
00180               fmt[0] = *s;
00181               if ( ber_printf( ber, fmt, arg1, arg2 ) == -1 ) {
00182                      fprintf( stderr, "ber_printf\n" );
00183                      exit( 1 );
00184               }
00185        }
00186 
00187        if ( ber_flush( sb, ber, 1 ) != 0 ) {
00188               perror( "ber_flush" );
00189               rc = -1;
00190        }
00191 
00192        return( rc );
00193 }