3 * Portions Copyright 1998 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
7 #pragma ident "%Z%%M% %I% %E% SMI"
8 /* test.c - lber encoding test program */
10 * Copyright (c) 1990 Regents of the University of Michigan.
11 * All rights reserved.
22 #include <sys/types.h>
23 #include <sys/socket.h>
27 static usage( char *name
)
29 fprintf( stderr
, "usage: %s fmtstring\n", name
);
32 main( int argc
, char **argv
)
36 Seqorset
*sos
= NULLSEQORSET
;
46 bzero( &sb
, sizeof(sb
) );
48 sb
.sb_ber
.ber_buf
= NULL
;
54 if (( sb
.sb_sd
= open( "lber-test", O_WRONLY
|O_CREAT
|O_TRUNC
|O_BINARY
))
61 if ( (ber
= ber_alloc()) == NULLBER
) {
62 perror( "ber_alloc" );
67 if ( ber_printf( ber
, "{ti}", 0x1f44, num
) == -1 ) {
68 fprintf( stderr
, "ber_printf returns -1" );
72 if ( ber_flush( &sb
, ber
, 1 ) == -1 ) {
73 perror( "ber_flush" );
77 for ( s
= argv
[1]; *s
; s
++ ) {
78 if ( fgets( buf
, sizeof(buf
), stdin
) == NULL
)
80 if ( (p
= strchr( buf
, '\n' )) != NULL
)
85 case 'b': /* boolean */
87 if ( ber_printf( ber
, "i", i
) == -1 ) {
88 fprintf( stderr
, "ber_printf i\n" );
93 case 'e': /* enumeration */
94 i
= va_arg( ap
, int );
95 rc
= ber_put_enum( ber
, i
, (char)ber
->ber_tag
);
99 rc
= ber_put_null( ber
, (char)ber
->ber_tag
);
102 case 'o': /* octet string (non-null terminated) */
103 s
= va_arg( ap
, char * );
104 len
= va_arg( ap
, int );
105 rc
= ber_put_ostring( ber
, s
, len
, (char)ber
->ber_tag
);
108 case 's': /* string */
109 s
= va_arg( ap
, char * );
110 rc
= ber_put_string( ber
, s
, (char)ber
->ber_tag
);
113 case 'B': /* bit string */
114 s
= va_arg( ap
, char * );
115 len
= va_arg( ap
, int ); /* in bits */
116 rc
= ber_put_bitstring( ber
, s
, len
, (char)ber
->ber_tag
);
119 case 't': /* tag for the next element */
120 ber
->ber_tag
= va_arg( ap
, int );
121 ber
->ber_usertag
= 1;
124 case 'v': /* vector of strings */
125 if ( (ss
= va_arg( ap
, char ** )) == NULL
)
127 for ( i
= 0; ss
[i
] != NULL
; i
++ ) {
128 if ( (rc
= ber_put_string( ber
, ss
[i
],
129 (char)ber
->ber_tag
)) == -1 )
134 case 'V': /* sequences of strings + lengths */
135 if ( (bv
= va_arg( ap
, struct berval
** )) == NULL
)
137 for ( i
= 0; bv
[i
] != NULL
; i
++ ) {
138 if ( (rc
= ber_put_ostring( ber
, bv
[i
]->bv_val
,
139 bv
[i
]->bv_len
, (char)ber
->ber_tag
)) == -1 )
144 case '{': /* begin sequence */
145 rc
= ber_start_seq( ber
, (char)ber
->ber_tag
);
148 case '}': /* end sequence */
149 rc
= ber_put_seqorset( ber
);
152 case '[': /* begin set */
153 rc
= ber_start_set( ber
, (char)ber
->ber_tag
);
156 case ']': /* end set */
157 rc
= ber_put_seqorset( ber
);
161 #ifndef NO_USERINTERFACE
162 fprintf( stderr
, "unknown fmt %c\n", *fmt
);
163 #endif /* NO_USERINTERFACE */