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.
22 #if defined( HAVE_STDARG_H ) && __STDC__
28 #include <ac/string.h>
29 #include <ac/socket.h>
37 static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len ));
39 /* return the tag - LBER_DEFAULT returned means trouble */
41 ber_get_tag( BerElement *ber )
48 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
49 return( LBER_DEFAULT );
51 if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
52 return( (unsigned long) xbyte );
56 for ( i = 1; i < sizeof(long); i++ ) {
57 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
58 return( LBER_DEFAULT );
62 if ( ! (xbyte & LBER_MORE_TAG_MASK) )
67 if ( i == sizeof(long) )
68 return( LBER_DEFAULT );
70 /* want leading, not trailing 0's */
71 return( tag >> (sizeof(long) - i - 1) );
75 ber_skip_tag( BerElement *ber, unsigned long *len )
83 * Any ber element looks like this: tag length contents.
84 * Assuming everything's ok, we return the tag byte (we
85 * can assume a single byte), and return the length in len.
89 * 2) primitive encodings used whenever possible
93 * First, we read the tag.
96 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
97 return( LBER_DEFAULT );
100 * Next, read the length. The first byte contains the length of
101 * the length. If bit 8 is set, the length is the long form,
102 * otherwise it's the short form. We don't allow a length that's
103 * greater than what we can hold in an unsigned long.
107 if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
108 return( LBER_DEFAULT );
110 noctets = (lc & 0x7f);
111 if ( noctets > sizeof(unsigned long) )
112 return( LBER_DEFAULT );
113 diff = sizeof(unsigned long) - noctets;
114 if ( ber_read( ber, (char *) &netlen + diff, noctets )
116 return( LBER_DEFAULT );
117 *len = AC_NTOHL( netlen );
126 ber_peek_tag( BerElement *ber, unsigned long *len )
132 tag = ber_skip_tag( ber, len );
139 ber_getnint( BerElement *ber, long *num, int len )
146 * The tag and length have already been stripped off. We should
147 * be sitting right before len bytes of 2's complement integer,
148 * ready to be read straight into an int. We may have to sign
149 * extend after we read it in.
152 if ( len > sizeof(long) )
156 diff = sizeof(long) - len;
157 /* read into the low-order bytes of netnum */
158 if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
161 /* sign extend if necessary */
162 p = (char *) &netnum;
163 sign = (0x80 & *(p+diff) );
164 if ( sign && len < sizeof(long) ) {
165 for ( i = 0; i < diff; i++ ) {
166 *(p+i) = (unsigned char) 0xff;
169 *num = AC_NTOHL( netnum );
175 ber_get_int( BerElement *ber, long *num )
177 unsigned long tag, len;
179 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
180 return( LBER_DEFAULT );
182 if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len )
183 return( LBER_DEFAULT );
189 ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
191 unsigned long datalen, tag;
192 #ifdef STR_TRANSLATION
194 #endif /* STR_TRANSLATION */
196 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
197 return( LBER_DEFAULT );
198 if ( datalen > (*len - 1) )
199 return( LBER_DEFAULT );
201 if ( (unsigned long) ber_read( ber, buf, datalen ) != datalen )
202 return( LBER_DEFAULT );
206 #ifdef STR_TRANSLATION
207 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
208 && ber->ber_decode_translate_proc != NULL ) {
211 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
213 return( LBER_DEFAULT );
215 if ( datalen > *len ) {
217 return( LBER_DEFAULT );
219 SAFEMEMCPY( buf, transbuf, datalen );
223 #endif /* STR_TRANSLATION */
230 ber_get_stringa( BerElement *ber, char **buf )
232 unsigned long datalen, tag;
234 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
235 return( LBER_DEFAULT );
237 if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
238 return( LBER_DEFAULT );
240 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen )
241 return( LBER_DEFAULT );
242 (*buf)[datalen] = '\0';
244 #ifdef STR_TRANSLATION
245 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
246 && ber->ber_decode_translate_proc != NULL ) {
248 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
251 return( LBER_DEFAULT );
254 #endif /* STR_TRANSLATION */
260 ber_get_stringal( BerElement *ber, struct berval **bv )
262 unsigned long len, tag;
264 if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
265 return( LBER_DEFAULT );
267 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
268 return( LBER_DEFAULT );
270 if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL )
271 return( LBER_DEFAULT );
273 if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len )
274 return( LBER_DEFAULT );
275 ((*bv)->bv_val)[len] = '\0';
278 #ifdef STR_TRANSLATION
279 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
280 && ber->ber_decode_translate_proc != NULL ) {
282 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
284 free( (*bv)->bv_val );
285 return( LBER_DEFAULT );
287 (*bv)->bv_len = len - 1;
289 #endif /* STR_TRANSLATION */
295 ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
297 unsigned long datalen, tag;
298 unsigned char unusedbits;
300 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
301 return( LBER_DEFAULT );
304 if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
305 return( LBER_DEFAULT );
307 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 )
308 return( LBER_DEFAULT );
310 if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen )
311 return( LBER_DEFAULT );
313 *blen = datalen * 8 - unusedbits;
318 ber_get_null( BerElement *ber )
320 unsigned long len, tag;
322 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
323 return( LBER_DEFAULT );
326 return( LBER_DEFAULT );
332 ber_get_boolean( BerElement *ber, int *boolval )
337 rc = ber_get_int( ber, &longbool );
344 ber_first_element( BerElement *ber, unsigned long *len, char **last )
346 /* skip the sequence header, use the len to mark where to stop */
347 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
348 return( LBER_DEFAULT );
351 *last = ber->ber_ptr + *len;
353 if ( *last == ber->ber_ptr ) {
354 return( LBER_DEFAULT );
357 return( ber_peek_tag( ber, len ) );
361 ber_next_element( BerElement *ber, unsigned long *len, char *last )
363 if ( ber->ber_ptr == last ) {
364 return( LBER_DEFAULT );
367 return( ber_peek_tag( ber, len ) );
373 #if defined( HAVE_STDARG_H ) && __STDC__
374 ( BerElement *ber, char *fmt, ... )
381 #if !(defined( HAVE_STDARG_H ) && __STDC__)
386 char *s, **ss, ***sss;
387 struct berval ***bv, **bvp, *bval;
392 #if defined( HAVE_STDARG_H ) && __STDC__
396 ber = va_arg( ap, BerElement * );
397 fmt = va_arg( ap, char * );
401 if ( lber_debug & 64 ) {
402 fprintf( stderr, "ber_scanf fmt (%s) ber:\n", fmt );
407 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
409 case 'a': /* octet string - allocate storage as needed */
410 ss = va_arg( ap, char ** );
411 rc = ber_get_stringa( ber, ss );
414 case 'b': /* boolean */
415 i = va_arg( ap, int * );
416 rc = ber_get_boolean( ber, i );
419 case 'e': /* enumerated */
421 l = va_arg( ap, long * );
422 rc = ber_get_int( ber, l );
425 case 'l': /* length of next item */
426 l = va_arg( ap, long * );
427 rc = ber_peek_tag( ber, (unsigned long *)l );
431 rc = ber_get_null( ber );
434 case 's': /* octet string - in a buffer */
435 s = va_arg( ap, char * );
436 l = va_arg( ap, long * );
437 rc = ber_get_stringb( ber, s, (unsigned long *)l );
440 case 'o': /* octet string in a supplied berval */
441 bval = va_arg( ap, struct berval * );
442 ber_peek_tag( ber, &bval->bv_len );
443 rc = ber_get_stringa( ber, &bval->bv_val );
446 case 'O': /* octet string - allocate & include length */
447 bvp = va_arg( ap, struct berval ** );
448 rc = ber_get_stringal( ber, bvp );
451 case 'B': /* bit string - allocate storage as needed */
452 ss = va_arg( ap, char ** );
453 l = va_arg( ap, long * ); /* for length, in bits */
454 rc = ber_get_bitstringa( ber, ss, (unsigned long *)l );
457 case 't': /* tag of next item */
458 i = va_arg( ap, int * );
459 *i = rc = ber_peek_tag( ber, &len );
462 case 'T': /* skip tag of next item */
463 i = va_arg( ap, int * );
464 *i = rc = ber_skip_tag( ber, &len );
467 case 'v': /* sequence of strings */
468 sss = va_arg( ap, char *** );
471 for ( tag = ber_first_element( ber, &len, &last );
472 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
473 tag = ber_next_element( ber, &len, last ) ) {
474 if ( *sss == NULL ) {
475 *sss = (char **) malloc(
476 2 * sizeof(char *) );
478 *sss = (char **) realloc( *sss,
479 (j + 2) * sizeof(char *) );
481 rc = ber_get_stringa( ber, &((*sss)[j]) );
488 case 'V': /* sequence of strings + lengths */
489 bv = va_arg( ap, struct berval *** );
492 for ( tag = ber_first_element( ber, &len, &last );
493 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
494 tag = ber_next_element( ber, &len, last ) ) {
496 *bv = (struct berval **) malloc(
497 2 * sizeof(struct berval *) );
499 *bv = (struct berval **) realloc( *bv,
500 (j + 2) * sizeof(struct berval *) );
502 rc = ber_get_stringal( ber, &((*bv)[j]) );
509 case 'x': /* skip the next element - whatever it is */
510 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
515 case '{': /* begin sequence */
516 case '[': /* begin set */
517 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
518 rc = ber_skip_tag( ber, &len );
521 case '}': /* end sequence */
522 case ']': /* end set */
527 fprintf( stderr, "unknown fmt %c\n", *fmt );
528 #endif /* LDAP_LIBUI */
540 ber_bvfree( struct berval *bv )
542 if ( bv->bv_val != NULL )
548 ber_bvecfree( struct berval **bv )
552 for ( i = 0; bv[i] != NULL; i++ )
558 ber_bvdup( struct berval *bv )
562 if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
566 if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
569 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
570 new->bv_val[bv->bv_len] = '\0';
571 new->bv_len = bv->bv_len;
577 #ifdef STR_TRANSLATION
579 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
580 BERTranslateProc decode_proc )
582 ber->ber_encode_translate_proc = encode_proc;
583 ber->ber_decode_translate_proc = decode_proc;
585 #endif /* STR_TRANSLATION */