1 /* decode.c - ber input decoding routines */
3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
19 #include <ac/stdarg.h>
21 #include <ac/string.h>
22 #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 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
42 return( LBER_DEFAULT );
44 if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
45 return( (unsigned long) xbyte );
49 for ( i = 1; i < sizeof(long); i++ ) {
50 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
51 return( LBER_DEFAULT );
55 if ( ! (xbyte & LBER_MORE_TAG_MASK) )
60 if ( i == sizeof(long) )
61 return( LBER_DEFAULT );
63 /* want leading, not trailing 0's */
64 return( tag >> (sizeof(long) - i - 1) );
68 ber_skip_tag( BerElement *ber, unsigned long *len )
76 * Any ber element looks like this: tag length contents.
77 * Assuming everything's ok, we return the tag byte (we
78 * can assume a single byte), and return the length in len.
82 * 2) primitive encodings used whenever possible
86 * First, we read the tag.
89 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
90 return( LBER_DEFAULT );
93 * Next, read the length. The first byte contains the length of
94 * the length. If bit 8 is set, the length is the long form,
95 * otherwise it's the short form. We don't allow a length that's
96 * greater than what we can hold in an unsigned long.
100 if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
101 return( LBER_DEFAULT );
103 noctets = (lc & 0x7f);
104 if ( noctets > sizeof(unsigned long) )
105 return( LBER_DEFAULT );
106 diff = sizeof(unsigned long) - noctets;
107 if ( ber_read( ber, (char *) &netlen + diff, noctets )
109 return( LBER_DEFAULT );
110 *len = AC_NTOHL( netlen );
119 ber_peek_tag( BerElement *ber, unsigned long *len )
125 tag = ber_skip_tag( ber, len );
132 ber_getnint( BerElement *ber, long *num, int len )
139 * The tag and length have already been stripped off. We should
140 * be sitting right before len bytes of 2's complement integer,
141 * ready to be read straight into an int. We may have to sign
142 * extend after we read it in.
145 if ( len > sizeof(long) )
149 diff = sizeof(long) - len;
150 /* read into the low-order bytes of netnum */
151 if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
154 /* sign extend if necessary */
155 p = (char *) &netnum;
156 sign = (0x80 & *(p+diff) );
157 if ( sign && len < sizeof(long) ) {
158 for ( i = 0; i < diff; i++ ) {
159 *(p+i) = (unsigned char) 0xff;
162 *num = AC_NTOHL( netnum );
168 ber_get_int( BerElement *ber, long *num )
170 unsigned long tag, len;
172 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
173 return( LBER_DEFAULT );
175 if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len )
176 return( LBER_DEFAULT );
182 ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
184 unsigned long datalen, tag;
185 #ifdef STR_TRANSLATION
187 #endif /* STR_TRANSLATION */
189 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
190 return( LBER_DEFAULT );
191 if ( datalen > (*len - 1) )
192 return( LBER_DEFAULT );
194 if ( (unsigned long) ber_read( ber, buf, datalen ) != datalen )
195 return( LBER_DEFAULT );
199 #ifdef STR_TRANSLATION
200 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
201 && ber->ber_decode_translate_proc != NULL ) {
204 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
206 return( LBER_DEFAULT );
208 if ( datalen > *len ) {
210 return( LBER_DEFAULT );
212 SAFEMEMCPY( buf, transbuf, datalen );
216 #endif /* STR_TRANSLATION */
223 ber_get_stringa( BerElement *ber, char **buf )
225 unsigned long datalen, tag;
227 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
228 return( LBER_DEFAULT );
230 if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
231 return( LBER_DEFAULT );
233 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen )
234 return( LBER_DEFAULT );
235 (*buf)[datalen] = '\0';
237 #ifdef STR_TRANSLATION
238 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
239 && ber->ber_decode_translate_proc != NULL ) {
241 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
244 return( LBER_DEFAULT );
247 #endif /* STR_TRANSLATION */
253 ber_get_stringal( BerElement *ber, struct berval **bv )
255 unsigned long len, tag;
257 if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
258 return( LBER_DEFAULT );
260 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
261 return( LBER_DEFAULT );
263 if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL )
264 return( LBER_DEFAULT );
266 if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len )
267 return( LBER_DEFAULT );
268 ((*bv)->bv_val)[len] = '\0';
271 #ifdef STR_TRANSLATION
272 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
273 && ber->ber_decode_translate_proc != NULL ) {
275 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
277 free( (*bv)->bv_val );
278 return( LBER_DEFAULT );
280 (*bv)->bv_len = len - 1;
282 #endif /* STR_TRANSLATION */
288 ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
290 unsigned long datalen, tag;
291 unsigned char unusedbits;
293 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
294 return( LBER_DEFAULT );
297 if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
298 return( LBER_DEFAULT );
300 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 )
301 return( LBER_DEFAULT );
303 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen )
304 return( LBER_DEFAULT );
306 *blen = datalen * 8 - unusedbits;
311 ber_get_null( BerElement *ber )
313 unsigned long len, tag;
315 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
316 return( LBER_DEFAULT );
319 return( LBER_DEFAULT );
325 ber_get_boolean( BerElement *ber, int *boolval )
330 rc = ber_get_int( ber, &longbool );
337 ber_first_element( BerElement *ber, unsigned long *len, char **last )
339 /* skip the sequence header, use the len to mark where to stop */
340 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
341 return( LBER_DEFAULT );
344 *last = ber->ber_ptr + *len;
346 if ( *last == ber->ber_ptr ) {
347 #ifdef LBER_END_SEQORSET
348 return( LBER_END_SEQORSET );
350 return( LBER_DEFAULT );
354 return( ber_peek_tag( ber, len ) );
358 ber_next_element( BerElement *ber, unsigned long *len, char *last )
360 if ( ber->ber_ptr == last ) {
361 #ifdef LBER_END_SEQORSET
362 return( LBER_END_SEQORSET );
364 return( LBER_DEFAULT );
368 return( ber_peek_tag( ber, len ) );
375 ( BerElement *ber, char *fmt, ... )
387 char *s, **ss, ***sss;
388 struct berval ***bv, **bvp, *bval;
391 unsigned long rc, tag, len;
397 ber = va_arg( ap, BerElement * );
398 fmt = va_arg( ap, char * );
402 if ( lber_debug & 64 ) {
403 fprintf( stderr, "ber_scanf fmt (%s) ber:\n", fmt );
408 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
410 case 'a': /* octet string - allocate storage as needed */
411 ss = va_arg( ap, char ** );
412 rc = ber_get_stringa( ber, ss );
415 case 'b': /* boolean */
416 i = va_arg( ap, int * );
417 rc = ber_get_boolean( ber, i );
420 case 'e': /* enumerated */
422 l = va_arg( ap, long * );
423 rc = ber_get_int( ber, l );
426 case 'l': /* length of next item */
427 l = va_arg( ap, long * );
428 rc = ber_peek_tag( ber, (unsigned long *)l );
432 rc = ber_get_null( ber );
435 case 's': /* octet string - in a buffer */
436 s = va_arg( ap, char * );
437 l = va_arg( ap, long * );
438 rc = ber_get_stringb( ber, s, (unsigned long *)l );
441 case 'o': /* octet string in a supplied berval */
442 bval = va_arg( ap, struct berval * );
443 ber_peek_tag( ber, &bval->bv_len );
444 rc = ber_get_stringa( ber, &bval->bv_val );
447 case 'O': /* octet string - allocate & include length */
448 bvp = va_arg( ap, struct berval ** );
449 rc = ber_get_stringal( ber, bvp );
452 case 'B': /* bit string - allocate storage as needed */
453 ss = va_arg( ap, char ** );
454 l = va_arg( ap, long * ); /* for length, in bits */
455 rc = ber_get_bitstringa( ber, ss, (unsigned long *)l );
458 case 't': /* tag of next item */
459 i = va_arg( ap, int * );
460 *i = rc = ber_peek_tag( ber, &len );
463 case 'T': /* skip tag of next item */
464 i = va_arg( ap, int * );
465 *i = rc = ber_skip_tag( ber, &len );
468 case 'v': /* sequence of strings */
469 sss = va_arg( ap, char *** );
472 for ( tag = ber_first_element( ber, &len, &last );
473 tag != LBER_DEFAULT &&
474 #ifdef LDAP_END_SEQORSET
475 tag != LBER_END_SEQORSET &&
478 tag = ber_next_element( ber, &len, last ) )
480 if ( *sss == NULL ) {
481 *sss = (char **) malloc(
482 2 * sizeof(char *) );
484 *sss = (char **) realloc( *sss,
485 (j + 2) * sizeof(char *) );
487 rc = ber_get_stringa( ber, &((*sss)[j]) );
490 #ifdef LDAP_END_SEQORSET
491 if (rc != LBER_DEFAULT &&
492 tag != LBER_END_SEQORSET )
501 case 'V': /* sequence of strings + lengths */
502 bv = va_arg( ap, struct berval *** );
505 for ( tag = ber_first_element( ber, &len, &last );
506 tag != LBER_DEFAULT &&
507 #ifdef LDAP_END_SEQORSET
508 tag != LBER_END_SEQORSET &&
511 tag = ber_next_element( ber, &len, last ) )
514 *bv = (struct berval **) malloc(
515 2 * sizeof(struct berval *) );
517 *bv = (struct berval **) realloc( *bv,
518 (j + 2) * sizeof(struct berval *) );
520 rc = ber_get_stringal( ber, &((*bv)[j]) );
523 #ifdef LDAP_END_SEQORSET
524 if (rc != LBER_DEFAULT &&
525 tag != LBER_END_SEQORSET )
534 case 'x': /* skip the next element - whatever it is */
535 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
540 case '{': /* begin sequence */
541 case '[': /* begin set */
542 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
543 rc = ber_skip_tag( ber, &len );
546 case '}': /* end sequence */
547 case ']': /* end set */
552 fprintf( stderr, "unknown fmt %c\n", *fmt );
553 #endif /* LDAP_LIBUI */
565 ber_bvfree( struct berval *bv )
568 assert(bv != NULL); /* bv damn better point to something */
570 if ( bv->bv_val != NULL )
576 ber_bvecfree( struct berval **bv )
581 assert(bv != NULL); /* bv damn better point to something */
583 for ( i = 0; bv[i] != NULL; i++ )
589 ber_bvdup( struct berval *bv )
593 if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
598 if ( bv->bv_val == NULL ) {
604 if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
609 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
610 new->bv_val[bv->bv_len] = '\0';
611 new->bv_len = bv->bv_len;
617 #ifdef STR_TRANSLATION
619 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
620 BERTranslateProc decode_proc )
622 ber->ber_encode_translate_proc = encode_proc;
623 ber->ber_decode_translate_proc = decode_proc;
625 #endif /* STR_TRANSLATION */