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.
14 #define DISABLE_BRIDGE /* disable LDAP_BRIDGE code */
25 #if defined(NeXT) || defined(VMS) || defined(__FreeBSD__)
27 #else /* next || vms || freebsd */
29 #endif /* next || vms || freebsd */
30 #if defined(BC31) || defined(_WIN32)
32 #else /* BC31 || _WIN32 */
34 #endif /* BC31 || _WIN32 */
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
43 #if defined( DOS ) || defined( _WIN32 )
55 static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len ));
57 /* return the tag - LBER_DEFAULT returned means trouble */
59 ber_get_tag( BerElement *ber )
66 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
67 return( LBER_DEFAULT );
69 if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
70 return( (unsigned long) xbyte );
74 for ( i = 1; i < sizeof(long); i++ ) {
75 if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
76 return( LBER_DEFAULT );
80 if ( ! (xbyte & LBER_MORE_TAG_MASK) )
85 if ( i == sizeof(long) )
86 return( LBER_DEFAULT );
88 /* want leading, not trailing 0's */
89 return( tag >> (sizeof(long) - i - 1) );
93 ber_skip_tag( BerElement *ber, unsigned long *len )
101 * Any ber element looks like this: tag length contents.
102 * Assuming everything's ok, we return the tag byte (we
103 * can assume a single byte), and return the length in len.
106 * 1) definite lengths
107 * 2) primitive encodings used whenever possible
111 * First, we read the tag.
114 if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
115 return( LBER_DEFAULT );
118 * Next, read the length. The first byte contains the length of
119 * the length. If bit 8 is set, the length is the long form,
120 * otherwise it's the short form. We don't allow a length that's
121 * greater than what we can hold in an unsigned long.
125 if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
126 return( LBER_DEFAULT );
128 noctets = (lc & 0x7f);
129 if ( noctets > sizeof(unsigned long) )
130 return( LBER_DEFAULT );
131 diff = sizeof(unsigned long) - noctets;
132 if ( ber_read( ber, (char *) &netlen + diff, noctets )
134 return( LBER_DEFAULT );
135 *len = LBER_NTOHL( netlen );
144 ber_peek_tag( BerElement *ber, unsigned long *len )
150 tag = ber_skip_tag( ber, len );
157 ber_getnint( BerElement *ber, long *num, int len )
164 * The tag and length have already been stripped off. We should
165 * be sitting right before len bytes of 2's complement integer,
166 * ready to be read straight into an int. We may have to sign
167 * extend after we read it in.
170 if ( len > sizeof(long) )
174 diff = sizeof(long) - len;
175 /* read into the low-order bytes of netnum */
176 if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
179 /* sign extend if necessary */
180 p = (char *) &netnum;
181 sign = (0x80 & *(p+diff) );
182 if ( sign && len < sizeof(long) ) {
183 for ( i = 0; i < diff; i++ ) {
187 *num = LBER_NTOHL( netnum );
193 ber_get_int( BerElement *ber, long *num )
195 unsigned long tag, len;
197 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
198 return( LBER_DEFAULT );
200 if ( ber_getnint( ber, num, (int)len ) != len )
201 return( LBER_DEFAULT );
207 ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
209 unsigned long datalen, tag;
210 #ifdef STR_TRANSLATION
212 #endif /* STR_TRANSLATION */
214 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
215 return( LBER_DEFAULT );
216 if ( datalen > (*len - 1) )
217 return( LBER_DEFAULT );
219 if ( ber_read( ber, buf, datalen ) != datalen )
220 return( LBER_DEFAULT );
224 #ifdef STR_TRANSLATION
225 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
226 && ber->ber_decode_translate_proc != NULL ) {
229 if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
231 return( LBER_DEFAULT );
233 if ( datalen > *len ) {
235 return( LBER_DEFAULT );
237 SAFEMEMCPY( buf, transbuf, datalen );
241 #endif /* STR_TRANSLATION */
248 ber_get_stringa( BerElement *ber, char **buf )
250 unsigned long datalen, tag;
252 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
253 return( LBER_DEFAULT );
255 if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
256 return( LBER_DEFAULT );
258 if ( ber_read( ber, *buf, datalen ) != datalen )
259 return( LBER_DEFAULT );
260 (*buf)[datalen] = '\0';
262 #ifdef STR_TRANSLATION
263 if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
264 && ber->ber_decode_translate_proc != NULL ) {
266 if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
269 return( LBER_DEFAULT );
272 #endif /* STR_TRANSLATION */
278 ber_get_stringal( BerElement *ber, struct berval **bv )
280 unsigned long len, tag;
282 if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
283 return( LBER_DEFAULT );
285 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
286 return( LBER_DEFAULT );
288 if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL )
289 return( LBER_DEFAULT );
291 if ( ber_read( ber, (*bv)->bv_val, len ) != len )
292 return( LBER_DEFAULT );
293 ((*bv)->bv_val)[len] = '\0';
296 #ifdef STR_TRANSLATION
297 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
298 && ber->ber_decode_translate_proc != NULL ) {
300 if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
302 free( (*bv)->bv_val );
303 return( LBER_DEFAULT );
305 (*bv)->bv_len = len - 1;
307 #endif /* STR_TRANSLATION */
313 ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
315 unsigned long datalen, tag;
316 unsigned char unusedbits;
318 if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
319 return( LBER_DEFAULT );
322 if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
323 return( LBER_DEFAULT );
325 if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 )
326 return( LBER_DEFAULT );
328 if ( ber_read( ber, *buf, datalen ) != datalen )
329 return( LBER_DEFAULT );
331 *blen = datalen * 8 - unusedbits;
336 ber_get_null( BerElement *ber )
338 unsigned long len, tag;
340 if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
341 return( LBER_DEFAULT );
344 return( LBER_DEFAULT );
350 ber_get_boolean( BerElement *ber, int *boolval )
355 rc = ber_get_int( ber, &longbool );
362 ber_first_element( BerElement *ber, unsigned long *len, char **last )
364 /* skip the sequence header, use the len to mark where to stop */
365 if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
366 return( LBER_DEFAULT );
369 *last = ber->ber_ptr + *len;
371 if ( *last == ber->ber_ptr ) {
372 return( LBER_DEFAULT );
375 return( ber_peek_tag( ber, len ) );
379 ber_next_element( BerElement *ber, unsigned long *len, char *last )
381 if ( ber->ber_ptr == last ) {
382 return( LBER_DEFAULT );
385 return( ber_peek_tag( ber, len ) );
391 #if defined( MACOS ) || defined( BC31 ) || defined( _WIN32 )
392 BerElement *ber, char *fmt, ... )
399 #if !defined( MACOS ) && !defined( BC31 ) && !defined( _WIN32 )
404 char *s, **ss, ***sss;
405 struct berval ***bv, **bvp, *bval;
410 #if defined( MACOS ) || defined( BC31 ) || defined( _WIN32 )
414 ber = va_arg( ap, BerElement * );
415 fmt = va_arg( ap, char * );
419 if ( lber_debug & 64 ) {
420 fprintf( stderr, "ber_scanf fmt (%s) ber:\n", fmt );
425 for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
427 case 'a': /* octet string - allocate storage as needed */
428 ss = va_arg( ap, char ** );
429 rc = ber_get_stringa( ber, ss );
432 case 'b': /* boolean */
433 i = va_arg( ap, int * );
434 rc = ber_get_boolean( ber, i );
437 case 'e': /* enumerated */
439 l = va_arg( ap, long * );
440 rc = ber_get_int( ber, l );
443 case 'l': /* length of next item */
444 l = va_arg( ap, long * );
445 rc = ber_peek_tag( ber, (unsigned long *)l );
449 rc = ber_get_null( ber );
452 case 's': /* octet string - in a buffer */
453 s = va_arg( ap, char * );
454 l = va_arg( ap, long * );
455 rc = ber_get_stringb( ber, s, (unsigned long *)l );
458 case 'o': /* octet string in a supplied berval */
459 bval = va_arg( ap, struct berval * );
460 ber_peek_tag( ber, &bval->bv_len );
461 rc = ber_get_stringa( ber, &bval->bv_val );
464 case 'O': /* octet string - allocate & include length */
465 bvp = va_arg( ap, struct berval ** );
466 rc = ber_get_stringal( ber, bvp );
469 case 'B': /* bit string - allocate storage as needed */
470 ss = va_arg( ap, char ** );
471 l = va_arg( ap, long * ); /* for length, in bits */
472 rc = ber_get_bitstringa( ber, ss, (unsigned long *)l );
475 case 't': /* tag of next item */
476 i = va_arg( ap, int * );
477 *i = rc = ber_peek_tag( ber, &len );
480 case 'T': /* skip tag of next item */
481 i = va_arg( ap, int * );
482 *i = rc = ber_skip_tag( ber, &len );
485 case 'v': /* sequence of strings */
486 sss = va_arg( ap, char *** );
489 for ( tag = ber_first_element( ber, &len, &last );
490 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
491 tag = ber_next_element( ber, &len, last ) ) {
492 if ( *sss == NULL ) {
493 *sss = (char **) malloc(
494 2 * sizeof(char *) );
496 *sss = (char **) realloc( *sss,
497 (j + 2) * sizeof(char *) );
499 rc = ber_get_stringa( ber, &((*sss)[j]) );
506 case 'V': /* sequence of strings + lengths */
507 bv = va_arg( ap, struct berval *** );
510 for ( tag = ber_first_element( ber, &len, &last );
511 tag != LBER_DEFAULT && rc != LBER_DEFAULT;
512 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]) );
527 case 'x': /* skip the next element - whatever it is */
528 if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
533 case '{': /* begin sequence */
534 case '[': /* begin set */
535 if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
536 rc = ber_skip_tag( ber, &len );
539 case '}': /* end sequence */
540 case ']': /* end set */
544 #ifndef NO_USERINTERFACE
545 fprintf( stderr, "unknown fmt %c\n", *fmt );
546 #endif /* NO_USERINTERFACE */
558 ber_bvfree( struct berval *bv )
560 if ( bv->bv_val != NULL )
566 ber_bvecfree( struct berval **bv )
570 for ( i = 0; bv[i] != NULL; i++ )
576 ber_bvdup( struct berval *bv )
580 if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
584 if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
587 SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
588 new->bv_val[bv->bv_len] = '\0';
589 new->bv_len = bv->bv_len;
595 #ifdef STR_TRANSLATION
597 ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
598 BERTranslateProc decode_proc )
600 ber->ber_encode_translate_proc = encode_proc;
601 ber->ber_decode_translate_proc = decode_proc;
603 #endif /* STR_TRANSLATION */