1 /* decode.c - ber input decoding routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Copyright (c) 1990 Regents of the University of Michigan.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that this notice is preserved and that due credit is given
13 * to the University of Michigan at Ann Arbor. The name of the University
14 * may not be used to endorse or promote products derived from this
15 * software without specific prior written permission. This software
16 * is provided ``as is'' without express or implied warranty.
23 #include <ac/stdlib.h>
25 #include <ac/stdarg.h>
26 #include <ac/string.h>
27 #include <ac/socket.h>
31 static ber_len_t ber_getnint LDAP_P((
36 /* return the tag - LBER_DEFAULT returned means trouble */
38 ber_get_tag( BerElement *ber )
44 assert( ber != NULL );
45 assert( BER_VALID( ber ) );
47 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
48 return( LBER_DEFAULT );
52 if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
55 for ( i = 1; i < sizeof(ber_tag_t); i++ ) {
56 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
57 return( LBER_DEFAULT );
60 tag |= 0x00ffUL & (ber_tag_t) xbyte;
62 if ( ! (xbyte & LBER_MORE_TAG_MASK) )
67 if ( i == sizeof(ber_tag_t) )
68 return( LBER_DEFAULT );
74 ber_skip_tag( BerElement *ber, ber_len_t *len )
79 unsigned char netlen[sizeof(ber_len_t)];
81 assert( ber != NULL );
82 assert( len != NULL );
83 assert( BER_VALID( ber ) );
86 * Any ber element looks like this: tag length contents.
87 * Assuming everything's ok, we return the tag byte (we
88 * can assume a single byte), and return the length in len.
92 * 2) primitive encodings used whenever possible
98 * First, we read the tag.
101 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
102 return( LBER_DEFAULT );
105 * Next, read the length. The first byte contains the length of
106 * the length. If bit 8 is set, the length is the long form,
107 * otherwise it's the short form. We don't allow a length that's
108 * greater than what we can hold in a ber_len_t.
111 if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
112 return( LBER_DEFAULT );
115 noctets = (lc & 0x7fU);
117 if ( noctets > sizeof(ber_len_t) ) {
118 return( LBER_DEFAULT );
121 if( (unsigned) ber_read( ber, netlen, noctets ) != noctets ) {
122 return( LBER_DEFAULT );
125 for( i = 0; i < noctets; i++ ) {
145 assert( ber_in != NULL );
146 assert( BER_VALID( ber_in ) );
150 ber = ber_dup( ber_in );
156 assert( BER_VALID( ber ) );
158 tag = ber_skip_tag( ber, len );
170 unsigned char buf[sizeof(ber_int_t)];
172 assert( ber != NULL );
173 assert( num != NULL );
175 assert( BER_VALID( ber ) );
178 * The tag and length have already been stripped off. We should
179 * be sitting right before len bytes of 2's complement integer,
180 * ready to be read straight into an int. We may have to sign
181 * extend after we read it in.
184 if ( len > sizeof(ber_int_t) )
187 /* read into the low-order bytes of our buffer */
188 if ( (ber_len_t) ber_read( ber, (char *) buf, len ) != len ) {
193 /* sign extend if necessary */
195 ber_int_t netnum = 0x80 & buf[0] ? -1 : 0;
197 /* shift in the bytes */
198 for( i=0 ; i<len; i++ ) {
199 netnum = (netnum << 8 ) | buf[i];
219 assert( ber != NULL );
220 assert( BER_VALID( ber ) );
222 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
223 return( LBER_DEFAULT );
225 if ( ber_getnint( ber, num, len ) != len )
226 return( LBER_DEFAULT );
240 #ifdef STR_TRANSLATION
242 #endif /* STR_TRANSLATION */
244 assert( ber != NULL );
245 assert( BER_VALID( ber ) );
247 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
248 return( LBER_DEFAULT );
249 if ( datalen > (*len - 1) )
250 return( LBER_DEFAULT );
252 if ( (ber_len_t) ber_read( ber, buf, datalen ) != datalen )
253 return( LBER_DEFAULT );
257 #ifdef STR_TRANSLATION
258 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
259 && ber->ber_decode_translate_proc ) {
262 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
264 return( LBER_DEFAULT );
266 if ( datalen > *len ) {
267 LBER_FREE( transbuf );
268 return( LBER_DEFAULT );
270 SAFEMEMCPY( buf, transbuf, datalen );
271 LBER_FREE( transbuf );
274 #endif /* STR_TRANSLATION */
281 ber_get_stringa( BerElement *ber, char **buf )
286 assert( ber != NULL );
287 assert( buf != NULL );
289 assert( BER_VALID( ber ) );
291 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
293 return( LBER_DEFAULT );
296 if ( (*buf = (char *) LBER_MALLOC( datalen + 1 )) == NULL )
297 return( LBER_DEFAULT );
299 if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
302 return( LBER_DEFAULT );
304 (*buf)[datalen] = '\0';
306 #ifdef STR_TRANSLATION
307 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
308 && ber->ber_decode_translate_proc ) {
310 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
314 return( LBER_DEFAULT );
317 #endif /* STR_TRANSLATION */
323 ber_get_stringal( BerElement *ber, struct berval **bv )
328 assert( ber != NULL );
329 assert( bv != NULL );
331 assert( BER_VALID( ber ) );
333 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
335 return( LBER_DEFAULT );
338 if ( (*bv = (struct berval *) LBER_MALLOC( sizeof(struct berval) )) == NULL )
339 return( LBER_DEFAULT );
341 if ( ((*bv)->bv_val = (char *) LBER_MALLOC( len + 1 )) == NULL ) {
344 return( LBER_DEFAULT );
347 if ( (ber_len_t) ber_read( ber, (*bv)->bv_val, len ) != len ) {
350 return( LBER_DEFAULT );
352 ((*bv)->bv_val)[len] = '\0';
355 #ifdef STR_TRANSLATION
356 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
357 && ber->ber_decode_translate_proc ) {
359 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
363 return( LBER_DEFAULT );
365 (*bv)->bv_len = len - 1;
367 #endif /* STR_TRANSLATION */
380 unsigned char unusedbits;
382 assert( ber != NULL );
383 assert( buf != NULL );
384 assert( blen != NULL );
386 assert( BER_VALID( ber ) );
388 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
390 return( LBER_DEFAULT );
394 if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL )
395 return( LBER_DEFAULT );
397 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
400 return( LBER_DEFAULT );
403 if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
406 return( LBER_DEFAULT );
409 *blen = datalen * 8 - unusedbits;
414 ber_get_null( BerElement *ber )
419 assert( ber != NULL );
420 assert( BER_VALID( ber ) );
422 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
423 return( LBER_DEFAULT );
426 return( LBER_DEFAULT );
439 assert( ber != NULL );
440 assert( boolval != NULL );
442 assert( BER_VALID( ber ) );
444 rc = ber_get_int( ber, &longbool );
456 assert( ber != NULL );
457 assert( len != NULL );
458 assert( last != NULL );
460 /* skip the sequence header, use the len to mark where to stop */
461 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
463 return( LBER_DEFAULT );
466 *last = ber->ber_ptr + *len;
468 if ( *last == ber->ber_ptr ) {
469 return( LBER_DEFAULT );
472 return( ber_peek_tag( ber, len ) );
479 LDAP_CONST char *last )
481 assert( ber != NULL );
482 assert( len != NULL );
483 assert( last != NULL );
485 assert( BER_VALID( ber ) );
487 if ( ber->ber_ptr == last ) {
488 return( LBER_DEFAULT );
491 return( ber_peek_tag( ber, len ) );
496 ber_scanf ( BerElement *ber,
497 LDAP_CONST char *fmt,
501 LDAP_CONST char *fmt_reset;
503 char *s, **ss, ***sss;
504 struct berval ***bv, **bvp, *bval;
514 assert( ber != NULL );
515 assert( fmt != NULL );
517 assert( BER_VALID( ber ) );
521 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
522 "ber_scanf fmt (%s) ber:\n", fmt );
523 ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
525 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
526 /* When this is modified, remember to update
527 * the error-cleanup code below accordingly. */
529 case '!': { /* Hook */
530 BERDecodeCallback *f;
533 f = va_arg( ap, BERDecodeCallback * );
534 p = va_arg( ap, void * );
536 rc = (*f)( ber, p, 0 );
539 case 'a': /* octet string - allocate storage as needed */
540 ss = va_arg( ap, char ** );
541 rc = ber_get_stringa( ber, ss );
544 case 'b': /* boolean */
545 i = va_arg( ap, ber_int_t * );
546 rc = ber_get_boolean( ber, i );
549 case 'e': /* enumerated */
551 i = va_arg( ap, ber_int_t * );
552 rc = ber_get_int( ber, i );
555 case 'l': /* length of next item */
556 l = va_arg( ap, ber_len_t * );
557 rc = ber_peek_tag( ber, l );
561 rc = ber_get_null( ber );
564 case 's': /* octet string - in a buffer */
565 s = va_arg( ap, char * );
566 l = va_arg( ap, ber_len_t * );
567 rc = ber_get_stringb( ber, s, l );
570 case 'o': /* octet string in a supplied berval */
571 bval = va_arg( ap, struct berval * );
572 ber_peek_tag( ber, &bval->bv_len );
573 rc = ber_get_stringa( ber, &bval->bv_val );
576 case 'O': /* octet string - allocate & include length */
577 bvp = va_arg( ap, struct berval ** );
578 rc = ber_get_stringal( ber, bvp );
581 case 'B': /* bit string - allocate storage as needed */
582 ss = va_arg( ap, char ** );
583 l = va_arg( ap, ber_len_t * ); /* for length, in bits */
584 rc = ber_get_bitstringa( ber, ss, l );
587 case 't': /* tag of next item */
588 t = va_arg( ap, ber_tag_t * );
589 *t = rc = ber_peek_tag( ber, &len );
592 case 'T': /* skip tag of next item */
593 t = va_arg( ap, ber_tag_t * );
594 *t = rc = ber_skip_tag( ber, &len );
597 case 'v': /* sequence of strings */
598 sss = va_arg( ap, char *** );
601 for ( tag = ber_first_element( ber, &len, &last );
602 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
603 tag = ber_next_element( ber, &len, last ) )
605 *sss = (char **) LBER_REALLOC( *sss,
606 (j + 2) * sizeof(char *) );
608 rc = ber_get_stringa( ber, &((*sss)[j]) );
615 case 'V': /* sequence of strings + lengths */
616 bv = va_arg( ap, struct berval *** );
619 for ( tag = ber_first_element( ber, &len, &last );
620 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
621 tag = ber_next_element( ber, &len, last ) )
623 *bv = (struct berval **) LBER_REALLOC( *bv,
624 (j + 2) * sizeof(struct berval *) );
626 rc = ber_get_stringal( ber, &((*bv)[j]) );
633 case 'x': /* skip the next element - whatever it is */
634 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
639 case '{': /* begin sequence */
640 case '[': /* begin set */
641 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
642 rc = ber_skip_tag( ber, &len );
645 case '}': /* end sequence */
646 case ']': /* end set */
650 if( ber->ber_debug ) {
651 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
652 "ber_scanf: unknown fmt %c\n", *fmt );
661 if ( rc == LBER_DEFAULT ) {
663 * Error. Reclaim malloced memory that was given to the caller.
664 * Set allocated pointers to NULL, "data length" outvalues to 0.
668 for ( ; fmt_reset < fmt; fmt_reset++ ) {
669 switch ( *fmt_reset ) {
670 case '!': { /* Hook */
671 BERDecodeCallback *f;
674 f = va_arg( ap, BERDecodeCallback * );
675 p = va_arg( ap, void * );
677 (void) (*f)( ber, p, 1 );
680 case 'a': /* octet string - allocate storage as needed */
681 ss = va_arg( ap, char ** );
688 case 'b': /* boolean */
689 case 'e': /* enumerated */
691 (void) va_arg( ap, int * );
694 case 's': /* octet string - in a buffer */
695 (void) va_arg( ap, char * );
696 (void) va_arg( ap, ber_len_t * );
699 case 'l': /* length of next item */
700 (void) va_arg( ap, ber_len_t * );
703 case 't': /* tag of next item */
704 case 'T': /* skip tag of next item */
705 (void) va_arg( ap, ber_tag_t * );
708 case 'o': /* octet string in a supplied berval */
709 bval = va_arg( ap, struct berval * );
710 if ( bval->bv_val != NULL ) {
711 LBER_FREE( bval->bv_val );
717 case 'O': /* octet string - allocate & include length */
718 bvp = va_arg( ap, struct berval ** );
725 case 'B': /* bit string - allocate storage as needed */
726 ss = va_arg( ap, char ** );
731 *(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
734 case 'v': /* sequence of strings */
735 sss = va_arg( ap, char *** );
737 for (j = 0; (*sss)[j]; j++) {
738 LBER_FREE( (*sss)[j] );
746 case 'V': /* sequence of strings + lengths */
747 bv = va_arg( ap, struct berval *** );
755 case 'x': /* skip the next element - whatever it is */
756 case '{': /* begin sequence */
757 case '[': /* begin set */
758 case '}': /* end sequence */
759 case ']': /* end set */
763 /* format should be good */
775 #ifdef STR_TRANSLATION
777 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
778 BERTranslateProc decode_proc )
780 assert( ber != NULL );
781 assert( BER_VALID( ber ) );
783 ber->ber_encode_translate_proc = encode_proc;
784 ber->ber_decode_translate_proc = decode_proc;
786 #endif /* STR_TRANSLATION */