1 /* decode.c - ber input decoding routines */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
23 #include <ac/stdarg.h>
25 #include <ac/string.h>
26 #include <ac/socket.h>
30 static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len ));
32 /* return the tag - LBER_DEFAULT returned means trouble */
34 ber_get_tag( BerElement *ber )
41 assert( ber != NULL );
43 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
44 return( LBER_DEFAULT );
46 if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
47 return( (unsigned long) xbyte );
51 for ( i = 1; i < sizeof(long); i++ ) {
52 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
53 return( LBER_DEFAULT );
57 if ( ! (xbyte & LBER_MORE_TAG_MASK) )
62 if ( i == sizeof(long) )
63 return( LBER_DEFAULT );
65 /* want leading, not trailing 0's */
66 return( tag >> (sizeof(long) - i - 1) );
70 ber_skip_tag( BerElement *ber, unsigned long *len )
77 assert( ber != NULL );
78 assert( len != NULL );
81 * Any ber element looks like this: tag length contents.
82 * Assuming everything's ok, we return the tag byte (we
83 * can assume a single byte), and return the length in len.
87 * 2) primitive encodings used whenever possible
91 * First, we read the tag.
94 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
95 return( LBER_DEFAULT );
98 * Next, read the length. The first byte contains the length of
99 * the length. If bit 8 is set, the length is the long form,
100 * otherwise it's the short form. We don't allow a length that's
101 * greater than what we can hold in an unsigned long.
105 if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
106 return( LBER_DEFAULT );
108 noctets = (lc & 0x7fU);
109 if ( (unsigned) noctets > sizeof(unsigned long) )
110 return( LBER_DEFAULT );
111 diff = sizeof(unsigned long) - noctets;
112 if ( ber_read( ber, (char *) &netlen + diff, noctets )
114 return( LBER_DEFAULT );
115 *len = AC_NTOHL( netlen );
125 LDAP_CONST BerElement *ber_in, /* not const per c-api-02 */
129 BerElement *ber = ber_dup( ber_in );
135 tag = ber_skip_tag( ber, len );
142 ber_getnint( BerElement *ber, long *num, int len )
148 assert( ber != NULL );
149 assert( num != NULL );
152 * The tag and length have already been stripped off. We should
153 * be sitting right before len bytes of 2's complement integer,
154 * ready to be read straight into an int. We may have to sign
155 * extend after we read it in.
158 if ( (unsigned) len > sizeof(long) )
162 diff = sizeof(long) - len;
163 /* read into the low-order bytes of netnum */
164 if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
167 /* sign extend if necessary */
168 p = (char *) &netnum;
169 sign = (0x80 & *(p+diff) );
170 if ( sign && ((unsigned) len < sizeof(long)) ) {
171 for ( i = 0; i < diff; i++ ) {
172 *(p+i) = (unsigned char) 0xff;
175 *num = AC_NTOHL( netnum );
181 ber_get_int( BerElement *ber, long *num )
183 unsigned long tag, len;
185 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
186 return( LBER_DEFAULT );
188 if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len )
189 return( LBER_DEFAULT );
195 ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
197 unsigned long datalen, tag;
198 #ifdef STR_TRANSLATION
200 #endif /* STR_TRANSLATION */
202 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
203 return( LBER_DEFAULT );
204 if ( datalen > (*len - 1) )
205 return( LBER_DEFAULT );
207 if ( (unsigned long) ber_read( ber, buf, datalen ) != datalen )
208 return( LBER_DEFAULT );
212 #ifdef STR_TRANSLATION
213 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
214 && ber->ber_decode_translate_proc ) {
217 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
219 return( LBER_DEFAULT );
221 if ( datalen > *len ) {
223 return( LBER_DEFAULT );
225 SAFEMEMCPY( buf, transbuf, datalen );
229 #endif /* STR_TRANSLATION */
236 ber_get_stringa( BerElement *ber, char **buf )
238 unsigned long datalen, tag;
240 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
242 return( LBER_DEFAULT );
245 if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
246 return( LBER_DEFAULT );
248 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
251 return( LBER_DEFAULT );
253 (*buf)[datalen] = '\0';
255 #ifdef STR_TRANSLATION
256 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
257 && ber->ber_decode_translate_proc ) {
259 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
263 return( LBER_DEFAULT );
266 #endif /* STR_TRANSLATION */
272 ber_get_stringal( BerElement *ber, struct berval **bv )
274 unsigned long len, tag;
276 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
278 return( LBER_DEFAULT );
281 if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
282 return( LBER_DEFAULT );
284 if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL ) {
287 return( LBER_DEFAULT );
290 if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len ) {
293 return( LBER_DEFAULT );
295 ((*bv)->bv_val)[len] = '\0';
298 #ifdef STR_TRANSLATION
299 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
300 && ber->ber_decode_translate_proc ) {
302 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
306 return( LBER_DEFAULT );
308 (*bv)->bv_len = len - 1;
310 #endif /* STR_TRANSLATION */
316 ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
318 unsigned long datalen, tag;
319 unsigned char unusedbits;
321 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
323 return( LBER_DEFAULT );
327 if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
328 return( LBER_DEFAULT );
330 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
333 return( LBER_DEFAULT );
336 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
339 return( LBER_DEFAULT );
342 *blen = datalen * 8 - unusedbits;
347 ber_get_null( BerElement *ber )
349 unsigned long len, tag;
351 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
352 return( LBER_DEFAULT );
355 return( LBER_DEFAULT );
361 ber_get_boolean( BerElement *ber, int *boolval )
366 rc = ber_get_int( ber, &longbool );
373 ber_first_element( BerElement *ber, unsigned long *len, char **last )
375 /* skip the sequence header, use the len to mark where to stop */
376 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
378 return( LBER_DEFAULT );
381 *last = ber->ber_ptr + *len;
383 if ( *last == ber->ber_ptr ) {
384 #ifdef LBER_END_SEQORSET
385 return( LBER_END_SEQORSET );
387 return( LBER_DEFAULT );
391 return( ber_peek_tag( ber, len ) );
395 ber_next_element( BerElement *ber, unsigned long *len, char *last )
397 if ( ber->ber_ptr == last ) {
398 #ifdef LBER_END_SEQORSET
399 return( LBER_END_SEQORSET );
401 return( LBER_DEFAULT );
405 return( ber_peek_tag( ber, len ) );
413 LDAP_CONST char *fmt,
425 LDAP_CONST char *fmt_reset;
427 char *s, **ss, ***sss;
428 struct berval ***bv, **bvp, *bval;
431 unsigned long rc, tag, len;
433 assert( ber != NULL );
439 ber = va_arg( ap, BerElement * );
440 fmt = va_arg( ap, char * );
443 assert( ber != NULL );
444 assert( fmt != NULL );
448 if ( ber->ber_debug ) {
449 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
450 "ber_scanf fmt (%s) ber:\n", fmt );
451 ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
454 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
455 /* When this is modified, remember to update
456 * the error-cleanup code below accordingly. */
458 case '!': { /* Hook */
459 BERDecodeCallback *f;
462 f = va_arg( ap, BERDecodeCallback * );
463 p = va_arg( ap, void * );
465 rc = (*f)( ber, p, 0 );
468 case 'a': /* octet string - allocate storage as needed */
469 ss = va_arg( ap, char ** );
470 rc = ber_get_stringa( ber, ss );
473 case 'b': /* boolean */
474 i = va_arg( ap, int * );
475 rc = ber_get_boolean( ber, i );
478 case 'e': /* enumerated */
480 l = va_arg( ap, long * );
481 rc = ber_get_int( ber, l );
484 case 'l': /* length of next item */
485 l = va_arg( ap, long * );
486 rc = ber_peek_tag( ber, (unsigned long *)l );
490 rc = ber_get_null( ber );
493 case 's': /* octet string - in a buffer */
494 s = va_arg( ap, char * );
495 l = va_arg( ap, long * );
496 rc = ber_get_stringb( ber, s, (unsigned long *)l );
499 case 'o': /* octet string in a supplied berval */
500 bval = va_arg( ap, struct berval * );
501 ber_peek_tag( ber, &bval->bv_len );
502 rc = ber_get_stringa( ber, &bval->bv_val );
505 case 'O': /* octet string - allocate & include length */
506 bvp = va_arg( ap, struct berval ** );
507 rc = ber_get_stringal( ber, bvp );
510 case 'B': /* bit string - allocate storage as needed */
511 ss = va_arg( ap, char ** );
512 l = va_arg( ap, long * ); /* for length, in bits */
513 rc = ber_get_bitstringa( ber, ss, (unsigned long *)l );
516 case 't': /* tag of next item */
517 i = va_arg( ap, int * );
518 *i = rc = ber_peek_tag( ber, &len );
521 case 'T': /* skip tag of next item */
522 i = va_arg( ap, int * );
523 *i = rc = ber_skip_tag( ber, &len );
526 case 'v': /* sequence of strings */
527 sss = va_arg( ap, char *** );
530 for ( tag = ber_first_element( ber, &len, &last );
531 tag != LBER_DEFAULT &&
532 #ifdef LBER_END_SEQORSET
533 tag != LBER_END_SEQORSET &&
536 tag = ber_next_element( ber, &len, last ) )
538 if ( *sss == NULL ) {
539 *sss = (char **) malloc(
540 2 * sizeof(char *) );
542 *sss = (char **) realloc( *sss,
543 (j + 2) * sizeof(char *) );
545 rc = ber_get_stringa( ber, &((*sss)[j]) );
548 #ifdef LBER_END_SEQORSET
549 if (rc != LBER_DEFAULT &&
550 tag != LBER_END_SEQORSET )
559 case 'V': /* sequence of strings + lengths */
560 bv = va_arg( ap, struct berval *** );
563 for ( tag = ber_first_element( ber, &len, &last );
564 tag != LBER_DEFAULT &&
565 #ifdef LBER_END_SEQORSET
566 tag != LBER_END_SEQORSET &&
569 tag = ber_next_element( ber, &len, last ) )
572 *bv = (struct berval **) malloc(
573 2 * sizeof(struct berval *) );
575 *bv = (struct berval **) realloc( *bv,
576 (j + 2) * sizeof(struct berval *) );
578 rc = ber_get_stringal( ber, &((*bv)[j]) );
581 #ifdef LBER_END_SEQORSET
582 if (rc != LBER_DEFAULT &&
583 tag != LBER_END_SEQORSET )
592 case 'x': /* skip the next element - whatever it is */
593 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
598 case '{': /* begin sequence */
599 case '[': /* begin set */
600 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
601 rc = ber_skip_tag( ber, &len );
604 case '}': /* end sequence */
605 case ']': /* end set */
609 if( ber->ber_debug ) {
610 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
611 "ber_scanf: unknown fmt %c\n", *fmt );
620 if ( rc == LBER_DEFAULT ) {
622 * Error. Reclaim malloced memory that was given to the caller.
623 * Set allocated pointers to NULL, "data length" outvalues to 0.
629 (void) va_arg( ap, BerElement * );
630 (void) va_arg( ap, char * );
633 for ( ; fmt_reset < fmt; fmt_reset++ ) {
634 switch ( *fmt_reset ) {
635 case '!': { /* Hook */
636 BERDecodeCallback *f;
639 f = va_arg( ap, BERDecodeCallback * );
640 p = va_arg( ap, void * );
642 (void) (*f)( ber, p, 1 );
645 case 'a': /* octet string - allocate storage as needed */
646 ss = va_arg( ap, char ** );
653 case 'b': /* boolean */
654 case 't': /* tag of next item */
655 case 'T': /* skip tag of next item */
656 (void) va_arg( ap, int * );
659 case 's': /* octet string - in a buffer */
660 (void) va_arg( ap, char * );
662 case 'e': /* enumerated */
664 case 'l': /* length of next item */
665 (void) va_arg( ap, long * );
668 case 'o': /* octet string in a supplied berval */
669 bval = va_arg( ap, struct berval * );
670 if ( bval->bv_val ) {
671 free( bval->bv_val );
677 case 'O': /* octet string - allocate & include length */
678 bvp = va_arg( ap, struct berval ** );
685 case 'B': /* bit string - allocate storage as needed */
686 ss = va_arg( ap, char ** );
691 *(va_arg( ap, long * )) = 0; /* for length, in bits */
694 case 'v': /* sequence of strings */
695 sss = va_arg( ap, char *** );
697 for (j = 0; (*sss)[j]; j++) {
706 case 'V': /* sequence of strings + lengths */
707 bv = va_arg( ap, struct berval *** );
714 #if 0 /* No action for these format characters */
716 case 'x': /* skip the next element - whatever it is */
717 case '{': /* begin sequence */
718 case '[': /* begin set */
719 case '}': /* end sequence */
720 case ']': /* end set */
733 ber_bvfree( struct berval *bv )
735 assert(bv != NULL); /* bv damn better point to something */
737 if ( bv->bv_val != NULL )
743 ber_bvecfree( struct berval **bv )
747 assert(bv != NULL); /* bv damn better point to something */
749 for ( i = 0; bv[i] != NULL; i++ )
756 LDAP_CONST struct berval *bv )
760 assert( bv != NULL );
766 if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
771 if ( bv->bv_val == NULL ) {
777 if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
782 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
783 new->bv_val[bv->bv_len] = '\0';
784 new->bv_len = bv->bv_len;
790 #ifdef STR_TRANSLATION
792 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
793 BERTranslateProc decode_proc )
795 assert( ber != NULL );
797 ber->ber_encode_translate_proc = encode_proc;
798 ber->ber_decode_translate_proc = decode_proc;
800 #endif /* STR_TRANSLATION */