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