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>
32 static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len ));
34 /* return the tag - LBER_DEFAULT returned means trouble */
36 ber_get_tag( BerElement *ber )
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 )
78 * Any ber element looks like this: tag length contents.
79 * Assuming everything's ok, we return the tag byte (we
80 * can assume a single byte), and return the length in len.
84 * 2) primitive encodings used whenever possible
88 * First, we read the tag.
91 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
92 return( LBER_DEFAULT );
95 * Next, read the length. The first byte contains the length of
96 * the length. If bit 8 is set, the length is the long form,
97 * otherwise it's the short form. We don't allow a length that's
98 * greater than what we can hold in an unsigned long.
102 if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
103 return( LBER_DEFAULT );
105 noctets = (lc & 0x7f);
106 if ( noctets > sizeof(unsigned long) )
107 return( LBER_DEFAULT );
108 diff = sizeof(unsigned long) - noctets;
109 if ( ber_read( ber, (char *) &netlen + diff, noctets )
111 return( LBER_DEFAULT );
112 *len = AC_NTOHL( netlen );
118 /* BER element should have enough data left */
119 if( *len > ber->ber_end - ber->ber_ptr ) {
127 ber_peek_tag( BerElement *ber, unsigned long *len )
133 tag = ber_skip_tag( ber, len );
140 ber_getnint( BerElement *ber, long *num, int len )
147 * The tag and length have already been stripped off. We should
148 * be sitting right before len bytes of 2's complement integer,
149 * ready to be read straight into an int. We may have to sign
150 * extend after we read it in.
153 if ( len > sizeof(long) )
157 diff = sizeof(long) - len;
158 /* read into the low-order bytes of netnum */
159 if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
162 /* sign extend if necessary */
163 p = (char *) &netnum;
164 sign = (0x80 & *(p+diff) );
165 if ( sign && len < sizeof(long) ) {
166 for ( i = 0; i < diff; i++ ) {
167 *(p+i) = (unsigned char) 0xff;
170 *num = AC_NTOHL( netnum );
176 ber_get_int( BerElement *ber, long *num )
178 unsigned long tag, len;
180 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
181 return( LBER_DEFAULT );
183 if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len )
184 return( LBER_DEFAULT );
190 ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
192 unsigned long datalen, tag;
193 #ifdef STR_TRANSLATION
195 #endif /* STR_TRANSLATION */
197 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
198 return( LBER_DEFAULT );
200 if ( datalen >= *len )
201 return( LBER_DEFAULT );
203 if ( (unsigned long) ber_read( ber, buf, datalen ) != datalen )
204 return( LBER_DEFAULT );
208 #ifdef STR_TRANSLATION
209 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
210 && ber->ber_decode_translate_proc != NULL ) {
213 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
215 return( LBER_DEFAULT );
217 if ( datalen > *len ) {
219 return( LBER_DEFAULT );
221 SAFEMEMCPY( buf, transbuf, datalen );
225 #endif /* STR_TRANSLATION */
232 ber_get_stringa( BerElement *ber, char **buf )
234 unsigned long datalen, tag;
236 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
238 return( LBER_DEFAULT );
241 if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
242 return( LBER_DEFAULT );
244 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
247 return( LBER_DEFAULT );
249 (*buf)[datalen] = '\0';
251 #ifdef STR_TRANSLATION
252 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
253 && ber->ber_decode_translate_proc != NULL ) {
255 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
259 return( LBER_DEFAULT );
262 #endif /* STR_TRANSLATION */
268 ber_get_stringal( BerElement *ber, struct berval **bv )
270 unsigned long len, tag;
272 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
274 return( LBER_DEFAULT );
277 if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
278 return( LBER_DEFAULT );
280 if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL ) {
283 return( LBER_DEFAULT );
286 if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len ) {
289 return( LBER_DEFAULT );
291 ((*bv)->bv_val)[len] = '\0';
294 #ifdef STR_TRANSLATION
295 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
296 && ber->ber_decode_translate_proc != NULL ) {
298 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
302 return( LBER_DEFAULT );
304 (*bv)->bv_len = len - 1;
306 #endif /* STR_TRANSLATION */
312 ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
314 unsigned long datalen, tag;
315 unsigned char unusedbits;
317 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
319 return( LBER_DEFAULT );
323 if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
324 return( LBER_DEFAULT );
326 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
329 return( LBER_DEFAULT );
332 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
335 return( LBER_DEFAULT );
338 *blen = datalen * 8 - unusedbits;
343 ber_get_null( BerElement *ber )
345 unsigned long len, tag;
347 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
348 return( LBER_DEFAULT );
351 return( LBER_DEFAULT );
357 ber_get_boolean( BerElement *ber, int *boolval )
362 rc = ber_get_int( ber, &longbool );
369 ber_first_element( BerElement *ber, unsigned long *len, char **last )
371 /* skip the sequence header, use the len to mark where to stop */
372 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
374 return( LBER_DEFAULT );
377 *last = ber->ber_ptr + *len;
379 if ( *last == ber->ber_ptr ) {
380 return( LBER_DEFAULT );
383 return( ber_peek_tag( ber, len ) );
387 ber_next_element( BerElement *ber, unsigned long *len, char *last )
389 if ( ber->ber_ptr == last ) {
390 return( LBER_DEFAULT );
393 return( ber_peek_tag( ber, len ) );
400 ( BerElement *ber, char *fmt, ... )
413 char *s, **ss, ***sss;
414 struct berval ***bv, **bvp, *bval;
417 unsigned long rc, tag, len;
423 ber = va_arg( ap, BerElement * );
424 fmt = va_arg( ap, char * );
429 if ( lber_debug & 64 ) {
430 fprintf( stderr, "ber_scanf fmt (%s) ber:\n", fmt );
435 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
436 /* When this is modified, remember to update
437 * the error-cleanup code below accordingly. */
439 case 'a': /* octet string - allocate storage as needed */
440 ss = va_arg( ap, char ** );
441 rc = ber_get_stringa( ber, ss );
444 case 'b': /* boolean */
445 i = va_arg( ap, int * );
446 rc = ber_get_boolean( ber, i );
449 case 'e': /* enumerated */
451 l = va_arg( ap, long * );
452 rc = ber_get_int( ber, l );
455 case 'l': /* length of next item */
456 l = va_arg( ap, long * );
457 rc = ber_peek_tag( ber, (unsigned long *)l );
461 rc = ber_get_null( ber );
464 case 's': /* octet string - in a buffer */
465 s = va_arg( ap, char * );
466 l = va_arg( ap, long * );
467 rc = ber_get_stringb( ber, s, (unsigned long *)l );
470 case 'o': /* octet string in a supplied berval */
471 bval = va_arg( ap, struct berval * );
472 ber_peek_tag( ber, &bval->bv_len );
473 rc = ber_get_stringa( ber, &bval->bv_val );
476 case 'O': /* octet string - allocate & include length */
477 bvp = va_arg( ap, struct berval ** );
478 rc = ber_get_stringal( ber, bvp );
481 case 'B': /* bit string - allocate storage as needed */
482 ss = va_arg( ap, char ** );
483 l = va_arg( ap, long * ); /* for length, in bits */
484 rc = ber_get_bitstringa( ber, ss, (unsigned long *)l );
487 case 't': /* tag of next item */
488 i = va_arg( ap, int * );
489 *i = rc = ber_peek_tag( ber, &len );
492 case 'T': /* skip tag of next item */
493 i = va_arg( ap, int * );
494 *i = rc = ber_skip_tag( ber, &len );
497 case 'v': /* sequence of strings */
498 sss = va_arg( ap, char *** );
501 for ( tag = ber_first_element( ber, &len, &last );
502 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
503 tag = ber_next_element( ber, &len, last ) ) {
504 if ( *sss == NULL ) {
505 *sss = (char **) malloc(
506 2 * sizeof(char *) );
508 *sss = (char **) realloc( *sss,
509 (j + 2) * sizeof(char *) );
511 rc = ber_get_stringa( ber, &((*sss)[j]) );
518 case 'V': /* sequence of strings + lengths */
519 bv = va_arg( ap, struct berval *** );
522 for ( tag = ber_first_element( ber, &len, &last );
523 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
524 tag = ber_next_element( ber, &len, last ) ) {
526 *bv = (struct berval **) malloc(
527 2 * sizeof(struct berval *) );
529 *bv = (struct berval **) realloc( *bv,
530 (j + 2) * sizeof(struct berval *) );
532 rc = ber_get_stringal( ber, &((*bv)[j]) );
539 case 'x': /* skip the next element - whatever it is */
540 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
545 case '{': /* begin sequence */
546 case '[': /* begin set */
547 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
548 rc = ber_skip_tag( ber, &len );
551 case '}': /* end sequence */
552 case ']': /* end set */
557 fprintf( stderr, "unknown fmt %c\n", *fmt );
558 #endif /* LDAP_LIBUI */
566 if ( rc == LBER_DEFAULT ) {
568 * Error. Reclaim malloced memory that was given to the caller.
569 * Set allocated pointers to NULL, "data length" outvalues to 0.
575 (void) va_arg( ap, BerElement * );
576 (void) va_arg( ap, char * );
579 for ( ; fmt_reset < fmt; fmt_reset++ ) {
580 switch ( *fmt_reset ) {
581 case 'a': /* octet string - allocate storage as needed */
582 ss = va_arg( ap, char ** );
589 case 'b': /* boolean */
590 case 't': /* tag of next item */
591 case 'T': /* skip tag of next item */
592 (void) va_arg( ap, int * );
595 case 's': /* octet string - in a buffer */
596 (void) va_arg( ap, char * );
598 case 'e': /* enumerated */
600 case 'l': /* length of next item */
601 (void) va_arg( ap, long * );
604 case 'o': /* octet string in a supplied berval */
605 bval = va_arg( ap, struct berval * );
606 if ( bval->bv_val ) {
607 free( bval->bv_val );
613 case 'O': /* octet string - allocate & include length */
614 bvp = va_arg( ap, struct berval ** );
621 case 'B': /* bit string - allocate storage as needed */
622 ss = va_arg( ap, char ** );
627 *(va_arg( ap, long * )) = 0; /* for length, in bits */
630 case 'v': /* sequence of strings */
631 sss = va_arg( ap, char *** );
633 for (j = 0; (*sss)[j]; j++)
640 case 'V': /* sequence of strings + lengths */
641 bv = va_arg( ap, struct berval *** );
648 #if 0 /* No action for these format characters */
650 case 'x': /* skip the next element - whatever it is */
651 case '{': /* begin sequence */
652 case '[': /* begin set */
653 case '}': /* end sequence */
654 case ']': /* end set */
667 ber_bvfree( struct berval *bv )
669 if ( bv->bv_val != NULL )
675 ber_bvecfree( struct berval **bv )
679 for ( i = 0; bv[i] != NULL; i++ )
685 ber_bvdup( struct berval *bv )
689 if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
693 if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
696 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
697 new->bv_val[bv->bv_len] = '\0';
698 new->bv_len = bv->bv_len;
704 #ifdef STR_TRANSLATION
706 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
707 BERTranslateProc decode_proc )
709 ber->ber_encode_translate_proc = encode_proc;
710 ber->ber_decode_translate_proc = decode_proc;
712 #endif /* STR_TRANSLATION */