1 /* decode.c - ber input decoding routines */
3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
20 #if defined(NeXT) || defined(VMS)
22 #else /* next || vms */
24 #endif /* next || vms */
25 #if defined(BC31) || defined(_WIN32)
27 #else /* BC31 || _WIN32 */
29 #endif /* BC31 || _WIN32 */
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
38 #if defined( DOS ) || defined( _WIN32 )
50 static int ber_getnint( BerElement *ber, long *num, int len );
51 #endif /* NEEDPROTOS */
54 /* return the tag - LBER_DEFAULT returned means trouble */
56 ber_get_tag( BerElement *ber )
63 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
64 return( LBER_DEFAULT );
66 if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
67 return( (unsigned long) xbyte );
71 for ( i = 1; i < sizeof(long); i++ ) {
72 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
73 return( LBER_DEFAULT );
77 if ( ! (xbyte & LBER_MORE_TAG_MASK) )
82 if ( i == sizeof(long) )
83 return( LBER_DEFAULT );
85 /* want leading, not trailing 0's */
86 return( tag >> (sizeof(long) - i - 1) );
90 ber_skip_tag( BerElement *ber, unsigned long *len )
98 * Any ber element looks like this: tag length contents.
99 * Assuming everything's ok, we return the tag byte (we
100 * can assume a single byte), and return the length in len.
103 * 1) definite lengths
104 * 2) primitive encodings used whenever possible
108 * First, we read the tag.
111 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
112 return( LBER_DEFAULT );
115 * Next, read the length. The first byte contains the length of
116 * the length. If bit 8 is set, the length is the long form,
117 * otherwise it's the short form. We don't allow a length that's
118 * greater than what we can hold in an unsigned long.
122 if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
123 return( LBER_DEFAULT );
125 noctets = (lc & 0x7f);
126 if ( noctets > sizeof(unsigned long) )
127 return( LBER_DEFAULT );
128 diff = sizeof(unsigned long) - noctets;
129 if ( ber_read( ber, (char *) &netlen + diff, noctets )
131 return( LBER_DEFAULT );
132 *len = LBER_NTOHL( netlen );
141 ber_peek_tag( BerElement *ber, unsigned long *len )
147 tag = ber_skip_tag( ber, len );
154 ber_getnint( BerElement *ber, long *num, int len )
160 * The tag and length have already been stripped off. We should
161 * be sitting right before len bytes of 2's complement integer,
162 * ready to be read straight into an int. We may have to sign
163 * extend after we read it in.
166 if ( len > sizeof(long) )
170 diff = sizeof(long) - len;
171 /* read into the low-order bytes of netnum */
172 if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
175 /* sign extend if necessary */
176 sign = ((0x80 << ((len - 1) * 8)) & netnum);
177 if ( sign && len < sizeof(long) ) {
178 for ( i = sizeof(long) - 1; i > len - 1; i-- ) {
179 netnum |= (0xffL << (i * 8));
182 *num = LBER_NTOHL( netnum );
188 ber_get_int( BerElement *ber, long *num )
190 unsigned long tag, len;
192 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
193 return( LBER_DEFAULT );
195 if ( ber_getnint( ber, num, (int)len ) != len )
196 return( LBER_DEFAULT );
202 ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
204 unsigned long datalen, tag;
205 #ifdef STR_TRANSLATION
207 #endif /* STR_TRANSLATION */
209 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
210 return( LBER_DEFAULT );
211 if ( datalen > (*len - 1) )
212 return( LBER_DEFAULT );
214 if ( ber_read( ber, buf, datalen ) != datalen )
215 return( LBER_DEFAULT );
219 #ifdef STR_TRANSLATION
220 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
221 && ber->ber_decode_translate_proc != NULL ) {
224 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
226 return( LBER_DEFAULT );
228 if ( datalen > *len ) {
230 return( LBER_DEFAULT );
232 SAFEMEMCPY( buf, transbuf, datalen );
236 #endif /* STR_TRANSLATION */
243 ber_get_stringa( BerElement *ber, char **buf )
245 unsigned long datalen, tag;
247 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
248 return( LBER_DEFAULT );
250 if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
251 return( LBER_DEFAULT );
253 if ( ber_read( ber, *buf, datalen ) != datalen )
254 return( LBER_DEFAULT );
255 (*buf)[datalen] = '\0';
257 #ifdef STR_TRANSLATION
258 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
259 && ber->ber_decode_translate_proc != NULL ) {
261 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
264 return( LBER_DEFAULT );
267 #endif /* STR_TRANSLATION */
273 ber_get_stringal( BerElement *ber, struct berval **bv )
275 unsigned long len, tag;
277 if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
278 return( LBER_DEFAULT );
280 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
281 return( LBER_DEFAULT );
283 if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL )
284 return( LBER_DEFAULT );
286 if ( ber_read( ber, (*bv)->bv_val, len ) != len )
287 return( LBER_DEFAULT );
288 ((*bv)->bv_val)[len] = '\0';
291 #ifdef STR_TRANSLATION
292 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
293 && ber->ber_decode_translate_proc != NULL ) {
295 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
297 free( (*bv)->bv_val );
298 return( LBER_DEFAULT );
300 (*bv)->bv_len = len - 1;
302 #endif /* STR_TRANSLATION */
308 ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
310 unsigned long datalen, tag;
311 unsigned char unusedbits;
313 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
314 return( LBER_DEFAULT );
317 if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
318 return( LBER_DEFAULT );
320 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 )
321 return( LBER_DEFAULT );
323 if ( ber_read( ber, *buf, datalen ) != datalen )
324 return( LBER_DEFAULT );
326 *blen = datalen * 8 - unusedbits;
331 ber_get_null( BerElement *ber )
333 unsigned long len, tag;
335 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
336 return( LBER_DEFAULT );
339 return( LBER_DEFAULT );
345 ber_get_boolean( BerElement *ber, int *boolval )
350 rc = ber_get_int( ber, &longbool );
357 ber_first_element( BerElement *ber, unsigned long *len, char **last )
359 /* skip the sequence header, use the len to mark where to stop */
360 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
361 return( LBER_DEFAULT );
364 *last = ber->ber_ptr + *len;
366 if ( *last == ber->ber_ptr ) {
367 return( LBER_DEFAULT );
370 return( ber_peek_tag( ber, len ) );
374 ber_next_element( BerElement *ber, unsigned long *len, char *last )
376 if ( ber->ber_ptr == last ) {
377 return( LBER_DEFAULT );
380 return( ber_peek_tag( ber, len ) );
386 #if defined( MACOS ) || defined( BC31 ) || defined( _WIN32 )
387 BerElement *ber, char *fmt, ... )
394 #if !defined( MACOS ) && !defined( BC31 ) && !defined( _WIN32 )
399 char *s, **ss, ***sss;
400 struct berval ***bv, **bvp, *bval;
405 #if defined( MACOS ) || defined( BC31 ) || defined( _WIN32 )
409 ber = va_arg( ap, BerElement * );
410 fmt = va_arg( ap, char * );
414 if ( lber_debug & 64 ) {
415 fprintf( stderr, "ber_scanf fmt (%s) ber:\n", fmt );
420 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
422 case 'a': /* octet string - allocate storage as needed */
423 ss = va_arg( ap, char ** );
424 rc = ber_get_stringa( ber, ss );
427 case 'b': /* boolean */
428 i = va_arg( ap, int * );
429 rc = ber_get_boolean( ber, i );
432 case 'e': /* enumerated */
434 l = va_arg( ap, long * );
435 rc = ber_get_int( ber, l );
438 case 'l': /* length of next item */
439 l = va_arg( ap, long * );
440 rc = ber_peek_tag( ber, (unsigned long *)l );
444 rc = ber_get_null( ber );
447 case 's': /* octet string - in a buffer */
448 s = va_arg( ap, char * );
449 l = va_arg( ap, long * );
450 rc = ber_get_stringb( ber, s, (unsigned long *)l );
453 case 'o': /* octet string in a supplied berval */
454 bval = va_arg( ap, struct berval * );
455 ber_peek_tag( ber, &bval->bv_len );
456 rc = ber_get_stringa( ber, &bval->bv_val );
459 case 'O': /* octet string - allocate & include length */
460 bvp = va_arg( ap, struct berval ** );
461 rc = ber_get_stringal( ber, bvp );
464 case 'B': /* bit string - allocate storage as needed */
465 ss = va_arg( ap, char ** );
466 l = va_arg( ap, long * ); /* for length, in bits */
467 rc = ber_get_bitstringa( ber, ss, (unsigned long *)l );
470 case 't': /* tag of next item */
471 i = va_arg( ap, int * );
472 *i = rc = ber_peek_tag( ber, &len );
475 case 'T': /* skip tag of next item */
476 i = va_arg( ap, int * );
477 *i = rc = ber_skip_tag( ber, &len );
480 case 'v': /* sequence of strings */
481 sss = va_arg( ap, char *** );
484 for ( tag = ber_first_element( ber, &len, &last );
485 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
486 tag = ber_next_element( ber, &len, last ) ) {
487 if ( *sss == NULL ) {
488 *sss = (char **) malloc(
489 2 * sizeof(char *) );
491 *sss = (char **) realloc( *sss,
492 (j + 2) * sizeof(char *) );
494 rc = ber_get_stringa( ber, &((*sss)[j]) );
501 case 'V': /* sequence of strings + lengths */
502 bv = va_arg( ap, struct berval *** );
505 for ( tag = ber_first_element( ber, &len, &last );
506 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
507 tag = ber_next_element( ber, &len, last ) ) {
509 *bv = (struct berval **) malloc(
510 2 * sizeof(struct berval *) );
512 *bv = (struct berval **) realloc( *bv,
513 (j + 2) * sizeof(struct berval *) );
515 rc = ber_get_stringal( ber, &((*bv)[j]) );
522 case 'x': /* skip the next element - whatever it is */
523 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
528 case '{': /* begin sequence */
529 case '[': /* begin set */
530 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
531 rc = ber_skip_tag( ber, &len );
534 case '}': /* end sequence */
535 case ']': /* end set */
539 #ifndef NO_USERINTERFACE
540 fprintf( stderr, "unknown fmt %c\n", *fmt );
541 #endif /* NO_USERINTERFACE */
553 ber_bvfree( struct berval *bv )
555 if ( bv->bv_val != NULL )
561 ber_bvecfree( struct berval **bv )
565 for ( i = 0; bv[i] != NULL; i++ )
571 ber_bvdup( struct berval *bv )
575 if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
579 if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
582 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
583 new->bv_val[bv->bv_len] = '\0';
584 new->bv_len = bv->bv_len;
590 #ifdef STR_TRANSLATION
592 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
593 BERTranslateProc decode_proc )
595 ber->ber_encode_translate_proc = encode_proc;
596 ber->ber_decode_translate_proc = decode_proc;
598 #endif /* STR_TRANSLATION */