1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * 3*7c478bd9Sstevel@tonic-gate * Portions Copyright %G% Sun Microsystems, Inc. All Rights Reserved 4*7c478bd9Sstevel@tonic-gate * 5*7c478bd9Sstevel@tonic-gate */ 6*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 7*7c478bd9Sstevel@tonic-gate /* test.c - lber encoding test program */ 8*7c478bd9Sstevel@tonic-gate /* 9*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan. 10*7c478bd9Sstevel@tonic-gate * All rights reserved. 11*7c478bd9Sstevel@tonic-gate */ 12*7c478bd9Sstevel@tonic-gate 13*7c478bd9Sstevel@tonic-gate #include <stdio.h> 14*7c478bd9Sstevel@tonic-gate #include <string.h> 15*7c478bd9Sstevel@tonic-gate #ifdef MACOS 16*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 17*7c478bd9Sstevel@tonic-gate #include <unix.h> 18*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 19*7c478bd9Sstevel@tonic-gate #include <console.h> 20*7c478bd9Sstevel@tonic-gate #else /* MACOS */ 21*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 22*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 23*7c478bd9Sstevel@tonic-gate #endif /* MACOS */ 24*7c478bd9Sstevel@tonic-gate #include "lber.h" 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate static usage( char *name ) 27*7c478bd9Sstevel@tonic-gate { 28*7c478bd9Sstevel@tonic-gate fprintf( stderr, "usage: %s fmtstring\n", name ); 29*7c478bd9Sstevel@tonic-gate } 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate main( int argc, char **argv ) 32*7c478bd9Sstevel@tonic-gate { 33*7c478bd9Sstevel@tonic-gate int i, num, len; 34*7c478bd9Sstevel@tonic-gate char *s, *p; 35*7c478bd9Sstevel@tonic-gate Seqorset *sos = NULLSEQORSET; 36*7c478bd9Sstevel@tonic-gate BerElement *ber; 37*7c478bd9Sstevel@tonic-gate Sockbuf sb; 38*7c478bd9Sstevel@tonic-gate extern char *optarg; 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate if ( argc < 2 ) { 41*7c478bd9Sstevel@tonic-gate usage( argv[0] ); 42*7c478bd9Sstevel@tonic-gate exit( 1 ); 43*7c478bd9Sstevel@tonic-gate } 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate bzero( &sb, sizeof(sb) ); 46*7c478bd9Sstevel@tonic-gate sb.sb_sd = 1; 47*7c478bd9Sstevel@tonic-gate sb.sb_ber.ber_buf = NULL; 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #ifdef MACOS 50*7c478bd9Sstevel@tonic-gate ccommand( &argv ); 51*7c478bd9Sstevel@tonic-gate cshow( stdout ); 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate if (( sb.sb_sd = open( "lber-test", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY )) 54*7c478bd9Sstevel@tonic-gate < 0 ) { 55*7c478bd9Sstevel@tonic-gate perror( "open" ); 56*7c478bd9Sstevel@tonic-gate exit( 1 ); 57*7c478bd9Sstevel@tonic-gate } 58*7c478bd9Sstevel@tonic-gate #endif /* MACOS */ 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate if ( (ber = ber_alloc()) == NULLBER ) { 61*7c478bd9Sstevel@tonic-gate perror( "ber_alloc" ); 62*7c478bd9Sstevel@tonic-gate exit( 1 ); 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate num = 7; 66*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "{ti}", 0x1f44, num ) == -1 ) { 67*7c478bd9Sstevel@tonic-gate fprintf( stderr, "ber_printf returns -1" ); 68*7c478bd9Sstevel@tonic-gate exit( 1 ); 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate if ( ber_flush( &sb, ber, 1 ) == -1 ) { 72*7c478bd9Sstevel@tonic-gate perror( "ber_flush" ); 73*7c478bd9Sstevel@tonic-gate exit( 1 ); 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate #ifdef notdef 76*7c478bd9Sstevel@tonic-gate for ( s = argv[1]; *s; s++ ) { 77*7c478bd9Sstevel@tonic-gate if ( fgets( buf, sizeof(buf), stdin ) == NULL ) 78*7c478bd9Sstevel@tonic-gate break; 79*7c478bd9Sstevel@tonic-gate if ( (p = strchr( buf, '\n' )) != NULL ) 80*7c478bd9Sstevel@tonic-gate *p = '\0'; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate switch ( *s ) { 83*7c478bd9Sstevel@tonic-gate case 'i': /* int */ 84*7c478bd9Sstevel@tonic-gate case 'b': /* boolean */ 85*7c478bd9Sstevel@tonic-gate i = atoi( buf ); 86*7c478bd9Sstevel@tonic-gate if ( ber_printf( ber, "i", i ) == -1 ) { 87*7c478bd9Sstevel@tonic-gate fprintf( stderr, "ber_printf i\n" ); 88*7c478bd9Sstevel@tonic-gate exit( 1 ); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate break; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate case 'e': /* enumeration */ 93*7c478bd9Sstevel@tonic-gate i = va_arg( ap, int ); 94*7c478bd9Sstevel@tonic-gate rc = ber_put_enum( ber, i, (char)ber->ber_tag ); 95*7c478bd9Sstevel@tonic-gate break; 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate case 'n': /* null */ 98*7c478bd9Sstevel@tonic-gate rc = ber_put_null( ber, (char)ber->ber_tag ); 99*7c478bd9Sstevel@tonic-gate break; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate case 'o': /* octet string (non-null terminated) */ 102*7c478bd9Sstevel@tonic-gate s = va_arg( ap, char * ); 103*7c478bd9Sstevel@tonic-gate len = va_arg( ap, int ); 104*7c478bd9Sstevel@tonic-gate rc = ber_put_ostring( ber, s, len, (char)ber->ber_tag ); 105*7c478bd9Sstevel@tonic-gate break; 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate case 's': /* string */ 108*7c478bd9Sstevel@tonic-gate s = va_arg( ap, char * ); 109*7c478bd9Sstevel@tonic-gate rc = ber_put_string( ber, s, (char)ber->ber_tag ); 110*7c478bd9Sstevel@tonic-gate break; 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate case 'B': /* bit string */ 113*7c478bd9Sstevel@tonic-gate s = va_arg( ap, char * ); 114*7c478bd9Sstevel@tonic-gate len = va_arg( ap, int ); /* in bits */ 115*7c478bd9Sstevel@tonic-gate rc = ber_put_bitstring( ber, s, len, (char)ber->ber_tag ); 116*7c478bd9Sstevel@tonic-gate break; 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate case 't': /* tag for the next element */ 119*7c478bd9Sstevel@tonic-gate ber->ber_tag = va_arg( ap, int ); 120*7c478bd9Sstevel@tonic-gate ber->ber_usertag = 1; 121*7c478bd9Sstevel@tonic-gate break; 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate case 'v': /* vector of strings */ 124*7c478bd9Sstevel@tonic-gate if ( (ss = va_arg( ap, char ** )) == NULL ) 125*7c478bd9Sstevel@tonic-gate break; 126*7c478bd9Sstevel@tonic-gate for ( i = 0; ss[i] != NULL; i++ ) { 127*7c478bd9Sstevel@tonic-gate if ( (rc = ber_put_string( ber, ss[i], 128*7c478bd9Sstevel@tonic-gate (char)ber->ber_tag )) == -1 ) 129*7c478bd9Sstevel@tonic-gate break; 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate break; 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate case 'V': /* sequences of strings + lengths */ 134*7c478bd9Sstevel@tonic-gate if ( (bv = va_arg( ap, struct berval ** )) == NULL ) 135*7c478bd9Sstevel@tonic-gate break; 136*7c478bd9Sstevel@tonic-gate for ( i = 0; bv[i] != NULL; i++ ) { 137*7c478bd9Sstevel@tonic-gate if ( (rc = ber_put_ostring( ber, bv[i]->bv_val, 138*7c478bd9Sstevel@tonic-gate bv[i]->bv_len, (char)ber->ber_tag )) == -1 ) 139*7c478bd9Sstevel@tonic-gate break; 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate break; 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate case '{': /* begin sequence */ 144*7c478bd9Sstevel@tonic-gate rc = ber_start_seq( ber, (char)ber->ber_tag ); 145*7c478bd9Sstevel@tonic-gate break; 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate case '}': /* end sequence */ 148*7c478bd9Sstevel@tonic-gate rc = ber_put_seqorset( ber ); 149*7c478bd9Sstevel@tonic-gate break; 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate case '[': /* begin set */ 152*7c478bd9Sstevel@tonic-gate rc = ber_start_set( ber, (char)ber->ber_tag ); 153*7c478bd9Sstevel@tonic-gate break; 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate case ']': /* end set */ 156*7c478bd9Sstevel@tonic-gate rc = ber_put_seqorset( ber ); 157*7c478bd9Sstevel@tonic-gate break; 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate default: 160*7c478bd9Sstevel@tonic-gate #ifndef NO_USERINTERFACE 161*7c478bd9Sstevel@tonic-gate fprintf( stderr, "unknown fmt %c\n", *fmt ); 162*7c478bd9Sstevel@tonic-gate #endif /* NO_USERINTERFACE */ 163*7c478bd9Sstevel@tonic-gate rc = -1; 164*7c478bd9Sstevel@tonic-gate break; 165*7c478bd9Sstevel@tonic-gate } 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate #endif 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate return( 0 ); 172*7c478bd9Sstevel@tonic-gate } 173