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 );
236 ber_get_int( ber, num );
248 #ifdef STR_TRANSLATION
250 #endif /* STR_TRANSLATION */
252 assert( ber != NULL );
253 assert( BER_VALID( ber ) );
255 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
256 return( LBER_DEFAULT );
257 if ( datalen > (*len - 1) )
258 return( LBER_DEFAULT );
260 if ( (ber_len_t) ber_read( ber, buf, datalen ) != datalen )
261 return( LBER_DEFAULT );
265 #ifdef STR_TRANSLATION
266 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
267 && ber->ber_decode_translate_proc ) {
270 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
272 return( LBER_DEFAULT );
274 if ( datalen > *len ) {
275 LBER_FREE( transbuf );
276 return( LBER_DEFAULT );
278 SAFEMEMCPY( buf, transbuf, datalen );
279 LBER_FREE( transbuf );
282 #endif /* STR_TRANSLATION */
289 ber_get_stringa( BerElement *ber, char **buf )
294 assert( ber != NULL );
295 assert( buf != NULL );
297 assert( BER_VALID( ber ) );
299 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
301 return( LBER_DEFAULT );
304 if ( (*buf = (char *) LBER_MALLOC( datalen + 1 )) == NULL )
305 return( LBER_DEFAULT );
307 if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
310 return( LBER_DEFAULT );
312 (*buf)[datalen] = '\0';
314 #ifdef STR_TRANSLATION
315 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
316 && ber->ber_decode_translate_proc ) {
318 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
322 return( LBER_DEFAULT );
325 #endif /* STR_TRANSLATION */
331 ber_get_stringal( BerElement *ber, struct berval **bv )
336 assert( ber != NULL );
337 assert( bv != NULL );
339 assert( BER_VALID( ber ) );
341 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
343 return( LBER_DEFAULT );
346 if ( (*bv = (struct berval *) LBER_MALLOC( sizeof(struct berval) )) == NULL )
347 return( LBER_DEFAULT );
349 if ( ((*bv)->bv_val = (char *) LBER_MALLOC( len + 1 )) == NULL ) {
352 return( LBER_DEFAULT );
355 if ( (ber_len_t) ber_read( ber, (*bv)->bv_val, len ) != len ) {
358 return( LBER_DEFAULT );
360 ((*bv)->bv_val)[len] = '\0';
363 #ifdef STR_TRANSLATION
364 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
365 && ber->ber_decode_translate_proc ) {
367 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
371 return( LBER_DEFAULT );
373 (*bv)->bv_len = len - 1;
375 #endif /* STR_TRANSLATION */
388 unsigned char unusedbits;
390 assert( ber != NULL );
391 assert( buf != NULL );
392 assert( blen != NULL );
394 assert( BER_VALID( ber ) );
396 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
398 return( LBER_DEFAULT );
402 if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL )
403 return( LBER_DEFAULT );
405 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
408 return( LBER_DEFAULT );
411 if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
414 return( LBER_DEFAULT );
417 *blen = datalen * 8 - unusedbits;
422 ber_get_null( BerElement *ber )
427 assert( ber != NULL );
428 assert( BER_VALID( ber ) );
430 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
431 return( LBER_DEFAULT );
434 return( LBER_DEFAULT );
447 assert( ber != NULL );
448 assert( boolval != NULL );
450 assert( BER_VALID( ber ) );
452 rc = ber_get_int( ber, &longbool );
464 assert( ber != NULL );
465 assert( len != NULL );
466 assert( last != NULL );
468 /* skip the sequence header, use the len to mark where to stop */
469 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
471 return( LBER_DEFAULT );
474 *last = ber->ber_ptr + *len;
476 if ( *last == ber->ber_ptr ) {
477 return( LBER_DEFAULT );
480 return( ber_peek_tag( ber, len ) );
487 LDAP_CONST char *last )
489 assert( ber != NULL );
490 assert( len != NULL );
491 assert( last != NULL );
493 assert( BER_VALID( ber ) );
495 if ( ber->ber_ptr == last ) {
496 return( LBER_DEFAULT );
499 return( ber_peek_tag( ber, len ) );
504 ber_scanf ( BerElement *ber,
505 LDAP_CONST char *fmt,
509 LDAP_CONST char *fmt_reset;
511 char *s, **ss, ***sss;
512 struct berval ***bv, **bvp, *bval;
522 assert( ber != NULL );
523 assert( fmt != NULL );
525 assert( BER_VALID( ber ) );
529 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
530 "ber_scanf fmt (%s) ber:\n", fmt );
531 ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
533 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
534 /* When this is modified, remember to update
535 * the error-cleanup code below accordingly. */
537 case '!': { /* Hook */
538 BERDecodeCallback *f;
541 f = va_arg( ap, BERDecodeCallback * );
542 p = va_arg( ap, void * );
544 rc = (*f)( ber, p, 0 );
547 case 'a': /* octet string - allocate storage as needed */
548 ss = va_arg( ap, char ** );
549 rc = ber_get_stringa( ber, ss );
552 case 'b': /* boolean */
553 i = va_arg( ap, ber_int_t * );
554 rc = ber_get_boolean( ber, i );
557 case 'e': /* enumerated */
559 i = va_arg( ap, ber_int_t * );
560 rc = ber_get_int( ber, i );
563 case 'l': /* length of next item */
564 l = va_arg( ap, ber_len_t * );
565 rc = ber_peek_tag( ber, l );
569 rc = ber_get_null( ber );
572 case 's': /* octet string - in a buffer */
573 s = va_arg( ap, char * );
574 l = va_arg( ap, ber_len_t * );
575 rc = ber_get_stringb( ber, s, l );
578 case 'o': /* octet string in a supplied berval */
579 bval = va_arg( ap, struct berval * );
580 ber_peek_tag( ber, &bval->bv_len );
581 rc = ber_get_stringa( ber, &bval->bv_val );
584 case 'O': /* octet string - allocate & include length */
585 bvp = va_arg( ap, struct berval ** );
586 rc = ber_get_stringal( ber, bvp );
589 case 'B': /* bit string - allocate storage as needed */
590 ss = va_arg( ap, char ** );
591 l = va_arg( ap, ber_len_t * ); /* for length, in bits */
592 rc = ber_get_bitstringa( ber, ss, l );
595 case 't': /* tag of next item */
596 t = va_arg( ap, ber_tag_t * );
597 *t = rc = ber_peek_tag( ber, &len );
600 case 'T': /* skip tag of next item */
601 t = va_arg( ap, ber_tag_t * );
602 *t = rc = ber_skip_tag( ber, &len );
605 case 'v': /* sequence of strings */
606 sss = va_arg( ap, char *** );
609 for ( tag = ber_first_element( ber, &len, &last );
610 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
611 tag = ber_next_element( ber, &len, last ) )
613 *sss = (char **) LBER_REALLOC( *sss,
614 (j + 2) * sizeof(char *) );
616 rc = ber_get_stringa( ber, &((*sss)[j]) );
623 case 'V': /* sequence of strings + lengths */
624 bv = va_arg( ap, struct berval *** );
627 for ( tag = ber_first_element( ber, &len, &last );
628 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
629 tag = ber_next_element( ber, &len, last ) )
631 *bv = (struct berval **) LBER_REALLOC( *bv,
632 (j + 2) * sizeof(struct berval *) );
634 rc = ber_get_stringal( ber, &((*bv)[j]) );
641 case 'x': /* skip the next element - whatever it is */
642 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
647 case '{': /* begin sequence */
648 case '[': /* begin set */
649 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
650 rc = ber_skip_tag( ber, &len );
653 case '}': /* end sequence */
654 case ']': /* end set */
658 if( ber->ber_debug ) {
659 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
660 "ber_scanf: unknown fmt %c\n", *fmt );
669 if ( rc == LBER_DEFAULT ) {
671 * Error. Reclaim malloced memory that was given to the caller.
672 * Set allocated pointers to NULL, "data length" outvalues to 0.
676 for ( ; fmt_reset < fmt; fmt_reset++ ) {
677 switch ( *fmt_reset ) {
678 case '!': { /* Hook */
679 BERDecodeCallback *f;
682 f = va_arg( ap, BERDecodeCallback * );
683 p = va_arg( ap, void * );
685 (void) (*f)( ber, p, 1 );
688 case 'a': /* octet string - allocate storage as needed */
689 ss = va_arg( ap, char ** );
696 case 'b': /* boolean */
697 case 'e': /* enumerated */
699 (void) va_arg( ap, int * );
702 case 's': /* octet string - in a buffer */
703 (void) va_arg( ap, char * );
704 (void) va_arg( ap, ber_len_t * );
707 case 'l': /* length of next item */
708 (void) va_arg( ap, ber_len_t * );
711 case 't': /* tag of next item */
712 case 'T': /* skip tag of next item */
713 (void) va_arg( ap, ber_tag_t * );
716 case 'o': /* octet string in a supplied berval */
717 bval = va_arg( ap, struct berval * );
718 if ( bval->bv_val != NULL ) {
719 LBER_FREE( bval->bv_val );
725 case 'O': /* octet string - allocate & include length */
726 bvp = va_arg( ap, struct berval ** );
733 case 'B': /* bit string - allocate storage as needed */
734 ss = va_arg( ap, char ** );
739 *(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
742 case 'v': /* sequence of strings */
743 sss = va_arg( ap, char *** );
745 for (j = 0; (*sss)[j]; j++) {
746 LBER_FREE( (*sss)[j] );
754 case 'V': /* sequence of strings + lengths */
755 bv = va_arg( ap, struct berval *** );
763 case 'x': /* skip the next element - whatever it is */
764 case '{': /* begin sequence */
765 case '[': /* begin set */
766 case '}': /* end sequence */
767 case ']': /* end set */
771 /* format should be good */
783 #ifdef STR_TRANSLATION
785 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
786 BERTranslateProc decode_proc )
788 assert( ber != NULL );
789 assert( BER_VALID( ber ) );
791 ber->ber_encode_translate_proc = encode_proc;
792 ber->ber_decode_translate_proc = decode_proc;
794 #endif /* STR_TRANSLATION */