1 /* decode.c - ber input decoding routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
35 #include <ac/stdlib.h>
37 #include <ac/stdarg.h>
38 #include <ac/string.h>
39 #include <ac/socket.h>
43 static ber_len_t ber_getnint LDAP_P((
48 /* return the tag - LBER_DEFAULT returned means trouble */
50 ber_get_tag( BerElement *ber )
56 assert( ber != NULL );
57 assert( LBER_VALID( ber ) );
59 if ( ber_pvt_ber_remaining( ber ) < 1 ) {
63 if ( ber->ber_ptr == ber->ber_buf ) {
64 tag = *(unsigned char *)ber->ber_ptr;
70 if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
74 for ( i = 1; i < sizeof(ber_tag_t); i++ ) {
75 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) {
80 tag |= 0x00ffUL & (ber_tag_t) xbyte;
82 if ( ! (xbyte & LBER_MORE_TAG_MASK) ) {
88 if ( i == sizeof(ber_tag_t) ) {
96 ber_skip_tag( BerElement *ber, ber_len_t *len )
100 ber_len_t i, noctets;
101 unsigned char netlen[sizeof(ber_len_t)];
103 assert( ber != NULL );
104 assert( len != NULL );
105 assert( LBER_VALID( ber ) );
108 * Any ber element looks like this: tag length contents.
109 * Assuming everything's ok, we return the tag byte (we
110 * can assume a single byte), and return the length in len.
113 * 1) definite lengths
114 * 2) primitive encodings used whenever possible
120 * First, we read the tag.
123 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT ) {
128 * Next, read the length. The first byte contains the length of
129 * the length. If bit 8 is set, the length is the long form,
130 * otherwise it's the short form. We don't allow a length that's
131 * greater than what we can hold in a ber_len_t.
134 if ( ber_read( ber, (char *) &lc, 1 ) != 1 ) {
139 noctets = (lc & 0x7fU);
141 if ( noctets > sizeof(ber_len_t) ) {
145 if( (unsigned) ber_read( ber, (char *) netlen, noctets ) != noctets ) {
149 for( i = 0; i < noctets; i++ ) {
158 /* BER element should have enough data left */
159 if( *len > (ber_len_t) ber_pvt_ber_remaining( ber ) ) {
162 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
173 * This implementation assumes ber_skip_tag() only
174 * modifies ber_ptr field of the BerElement.
182 tag = ber_skip_tag( ber, len );
195 unsigned char buf[sizeof(ber_int_t)];
197 assert( ber != NULL );
198 assert( num != NULL );
199 assert( LBER_VALID( ber ) );
202 * The tag and length have already been stripped off. We should
203 * be sitting right before len bytes of 2's complement integer,
204 * ready to be read straight into an int. We may have to sign
205 * extend after we read it in.
208 if ( len > sizeof(ber_int_t) ) {
212 /* read into the low-order bytes of our buffer */
213 if ( (ber_len_t) ber_read( ber, (char *) buf, len ) != len ) {
218 /* sign extend if necessary */
220 ber_int_t netnum = 0x80 & buf[0] ? -1 : 0;
222 /* shift in the bytes */
223 for( i=0 ; i<len; i++ ) {
224 netnum = (netnum << 8 ) | buf[i];
232 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
245 assert( ber != NULL );
246 assert( LBER_VALID( ber ) );
248 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
252 if ( ber_getnint( ber, num, len ) != len ) {
264 return ber_get_int( ber, num );
276 assert( ber != NULL );
277 assert( LBER_VALID( ber ) );
279 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
283 /* must fit within allocated space with termination */
284 if ( datalen >= *len ) {
288 if ( (ber_len_t) ber_read( ber, buf, datalen ) != datalen ) {
291 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
299 /* Definitions for get_string vector
301 * ChArray, BvArray, and BvVec are self-explanatory.
302 * BvOff is a struct berval embedded in an array of larger structures
303 * of siz bytes at off bytes from the beginning of the struct.
305 enum bgbvc { ChArray, BvArray, BvVec, BvOff };
307 /* Use this single cookie for state, to keep actual
308 * stack use to the absolute minimum.
310 typedef struct bgbvr {
324 ber_get_stringbvl( bgbvr *b, ber_len_t *rlen )
330 struct berval bv, *bvp = NULL;
332 /* For rewinding, just like ber_peek_tag() */
333 orig = b->ber->ber_ptr;
334 tag = b->ber->ber_tag;
336 if ( ber_first_element( b->ber, &len, &last ) != LBER_DEFAULT ) {
337 for ( ; b->ber->ber_ptr < last; i++ ) {
338 if (ber_skip_tag( b->ber, &len ) == LBER_DEFAULT) break;
339 b->ber->ber_ptr += len;
340 b->ber->ber_tag = *(unsigned char *)b->ber->ber_ptr;
344 if ( rlen ) *rlen = i;
353 /* Allocate the result vector */
356 *b->res.c = ber_memalloc_x( (n+1)*sizeof( char * ),
358 if ( *b->res.c == NULL ) return LBER_DEFAULT;
359 (*b->res.c)[n] = NULL;
362 *b->res.ba = ber_memalloc_x( (n+1)*sizeof( struct berval ),
364 if ( *b->res.ba == NULL ) return LBER_DEFAULT;
365 (*b->res.ba)[n].bv_val = NULL;
368 *b->res.bv = ber_memalloc_x( (n+1)*sizeof( struct berval *),
370 if ( *b->res.bv == NULL ) return LBER_DEFAULT;
371 (*b->res.bv)[n] = NULL;
374 *b->res.ba = ber_memalloc_x( (n+1) * b->siz, b->ber->ber_memctx );
375 if ( *b->res.ba == NULL ) return LBER_DEFAULT;
376 ((struct berval *)((long)(*b->res.ba) + n*b->siz +
377 b->off))->bv_val = NULL;
380 b->ber->ber_ptr = orig;
381 b->ber->ber_tag = tag;
382 ber_skip_tag( b->ber, &len );
386 tag = ber_next_element( b->ber, &len, last );
387 if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) {
391 /* store my result */
394 (*b->res.c)[n] = bv.bv_val;
397 (*b->res.ba)[n] = bv;
400 bvp = ber_memalloc_x( sizeof( struct berval ), b->ber->ber_memctx);
402 LBER_FREE(bv.bv_val);
405 (*b->res.bv)[n] = bvp;
409 *(BerVarray)((long)(*b->res.ba)+n*b->siz+b->off) = bv;
416 if (b->alloc || b->choice == BvVec) {
417 for (--n; n>=0; n--) {
420 LBER_FREE((*b->res.c)[n]);
423 LBER_FREE((*b->res.ba)[n].bv_val);
426 LBER_FREE((*b->res.bv)[n]->bv_val);
427 LBER_FREE((*b->res.bv)[n]);
434 LBER_FREE(*b->res.c);
440 ber_get_stringbv( BerElement *ber, struct berval *bv, int alloc )
444 assert( ber != NULL );
445 assert( bv != NULL );
447 assert( LBER_VALID( ber ) );
449 if ( (tag = ber_skip_tag( ber, &bv->bv_len )) == LBER_DEFAULT ) {
454 if ( (ber_len_t) ber_pvt_ber_remaining( ber ) < bv->bv_len ) {
459 bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
461 if ( bv->bv_val == NULL ) {
465 if ( bv->bv_len > 0 && (ber_len_t) ber_read( ber, bv->bv_val,
466 bv->bv_len ) != bv->bv_len )
468 LBER_FREE( bv->bv_val );
473 bv->bv_val = ber->ber_ptr;
474 ber->ber_ptr += bv->bv_len;
476 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
477 bv->bv_val[bv->bv_len] = '\0';
483 ber_get_stringa( BerElement *ber, char **buf )
488 assert( buf != NULL );
490 tag = ber_get_stringbv( ber, &bv, 1 );
497 ber_get_stringal( BerElement *ber, struct berval **bv )
501 assert( ber != NULL );
502 assert( bv != NULL );
504 *bv = (struct berval *) ber_memalloc_x( sizeof(struct berval),
510 tag = ber_get_stringbv( ber, *bv, 1 );
511 if ( tag == LBER_DEFAULT ) {
526 unsigned char unusedbits;
528 assert( ber != NULL );
529 assert( buf != NULL );
530 assert( blen != NULL );
532 assert( LBER_VALID( ber ) );
534 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
540 *buf = (char *) ber_memalloc_x( datalen, ber->ber_memctx );
541 if ( *buf == NULL ) {
545 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
551 if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
556 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
558 *blen = datalen * 8 - unusedbits;
563 ber_get_null( BerElement *ber )
568 assert( ber != NULL );
569 assert( LBER_VALID( ber ) );
571 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
578 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
591 assert( ber != NULL );
592 assert( boolval != NULL );
594 assert( LBER_VALID( ber ) );
596 rc = ber_get_int( ber, &longbool );
608 assert( ber != NULL );
609 assert( len != NULL );
610 assert( last != NULL );
612 /* skip the sequence header, use the len to mark where to stop */
613 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
617 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
619 *last = ber->ber_ptr + *len;
621 if ( *last == ber->ber_ptr ) {
625 return ber_peek_tag( ber, len );
632 LDAP_CONST char *last )
634 assert( ber != NULL );
635 assert( len != NULL );
636 assert( last != NULL );
638 assert( LBER_VALID( ber ) );
640 if ( ber->ber_ptr >= last ) {
644 return ber_peek_tag( ber, len );
649 ber_scanf ( BerElement *ber,
650 LDAP_CONST char *fmt,
654 LDAP_CONST char *fmt_reset;
656 struct berval **bvp, *bval;
665 assert( ber != NULL );
666 assert( fmt != NULL );
668 assert( LBER_VALID( ber ) );
672 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
673 "ber_scanf fmt (%s) ber:\n", fmt );
674 ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
676 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
677 /* When this is modified, remember to update
678 * the error-cleanup code below accordingly. */
680 case '!': { /* Hook */
681 BERDecodeCallback *f;
684 f = va_arg( ap, BERDecodeCallback * );
685 p = va_arg( ap, void * );
687 rc = (*f)( ber, p, 0 );
690 case 'a': /* octet string - allocate storage as needed */
691 ss = va_arg( ap, char ** );
692 rc = ber_get_stringa( ber, ss );
695 case 'b': /* boolean */
696 i = va_arg( ap, ber_int_t * );
697 rc = ber_get_boolean( ber, i );
700 case 'e': /* enumerated */
702 i = va_arg( ap, ber_int_t * );
703 rc = ber_get_int( ber, i );
706 case 'l': /* length of next item */
707 l = va_arg( ap, ber_len_t * );
708 rc = ber_peek_tag( ber, l );
712 rc = ber_get_null( ber );
715 case 's': /* octet string - in a buffer */
716 s = va_arg( ap, char * );
717 l = va_arg( ap, ber_len_t * );
718 rc = ber_get_stringb( ber, s, l );
721 case 'm': /* octet string in berval, in-place */
722 bval = va_arg( ap, struct berval * );
723 rc = ber_get_stringbv( ber, bval, 0 );
726 case 'o': /* octet string in a supplied berval */
727 bval = va_arg( ap, struct berval * );
728 rc = ber_get_stringbv( ber, bval, 1 );
731 case 'O': /* octet string - allocate & include length */
732 bvp = va_arg( ap, struct berval ** );
733 rc = ber_get_stringal( ber, bvp );
736 case 'B': /* bit string - allocate storage as needed */
737 ss = va_arg( ap, char ** );
738 l = va_arg( ap, ber_len_t * ); /* for length, in bits */
739 rc = ber_get_bitstringa( ber, ss, l );
742 case 't': /* tag of next item */
743 t = va_arg( ap, ber_tag_t * );
744 *t = rc = ber_peek_tag( ber, &len );
747 case 'T': /* skip tag of next item */
748 t = va_arg( ap, ber_tag_t * );
749 *t = rc = ber_skip_tag( ber, &len );
752 case 'v': /* sequence of strings */
754 bgbvr cookie = { ChArray };
756 cookie.res.c = va_arg( ap, char *** );
758 rc = ber_get_stringbvl( &cookie, NULL );
762 case 'V': /* sequence of strings + lengths */
764 bgbvr cookie = { BvVec };
766 cookie.res.bv = va_arg( ap, struct berval *** );
768 rc = ber_get_stringbvl( &cookie, NULL );
772 case 'W': /* bvarray */
774 bgbvr cookie = { BvArray };
776 cookie.res.ba = va_arg( ap, struct berval ** );
778 rc = ber_get_stringbvl( &cookie, NULL );
782 case 'M': /* bvoffarray - must include address of
783 * a record len, and record offset.
784 * number of records will be returned thru
785 * len ptr on finish. parsed in-place.
788 bgbvr cookie = { BvOff };
790 cookie.res.ba = va_arg( ap, struct berval ** );
792 l = va_arg( ap, ber_len_t * );
794 cookie.off = va_arg( ap, ber_len_t );
795 rc = ber_get_stringbvl( &cookie, l );
799 case 'x': /* skip the next element - whatever it is */
800 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
803 ber->ber_tag = *(unsigned char *)ber->ber_ptr;
806 case '{': /* begin sequence */
807 case '[': /* begin set */
808 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V'
809 && *(fmt + 1) != 'W' && *(fmt + 1) != 'M' )
810 rc = ber_skip_tag( ber, &len );
813 case '}': /* end sequence */
814 case ']': /* end set */
818 if( ber->ber_debug ) {
819 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
820 "ber_scanf: unknown fmt %c\n", *fmt );
828 if ( rc == LBER_DEFAULT ) {
830 * Error. Reclaim malloced memory that was given to the caller.
831 * Set allocated pointers to NULL, "data length" outvalues to 0.
835 for ( ; fmt_reset < fmt; fmt_reset++ ) {
836 switch ( *fmt_reset ) {
837 case '!': { /* Hook */
838 BERDecodeCallback *f;
841 f = va_arg( ap, BERDecodeCallback * );
842 p = va_arg( ap, void * );
844 (void) (*f)( ber, p, 1 );
847 case 'a': /* octet string - allocate storage as needed */
848 ss = va_arg( ap, char ** );
855 case 'b': /* boolean */
856 case 'e': /* enumerated */
858 (void) va_arg( ap, int * );
861 case 's': /* octet string - in a buffer */
862 (void) va_arg( ap, char * );
863 (void) va_arg( ap, ber_len_t * );
866 case 'l': /* length of next item */
867 (void) va_arg( ap, ber_len_t * );
870 case 't': /* tag of next item */
871 case 'T': /* skip tag of next item */
872 (void) va_arg( ap, ber_tag_t * );
875 case 'o': /* octet string in a supplied berval */
876 bval = va_arg( ap, struct berval * );
877 if ( bval->bv_val != NULL ) {
878 LBER_FREE( bval->bv_val );
884 case 'O': /* octet string - allocate & include length */
885 bvp = va_arg( ap, struct berval ** );
892 case 'B': /* bit string - allocate storage as needed */
893 ss = va_arg( ap, char ** );
898 *(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
901 case 'v': /* sequence of strings */
902 case 'V': /* sequence of strings + lengths */
903 case 'W': /* BerVarray */
904 case 'm': /* berval in-place */
905 case 'M': /* BVoff array in-place */
907 case 'x': /* skip the next element - whatever it is */
908 case '{': /* begin sequence */
909 case '[': /* begin set */
910 case '}': /* end sequence */
911 case ']': /* end set */
915 /* format should be good */