version 1.1.3.1, 1998/08/08 22:43:15
|
version 1.5.2.4.2.2, 1999/01/23 21:05:03
|
Line 1
|
Line 1
|
/* decode.c - ber input decoding routines */ |
/* decode.c - ber input decoding routines */ |
/* |
/* |
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. |
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
|
*/ |
|
/* Portions |
* Copyright (c) 1990 Regents of the University of Michigan. |
* Copyright (c) 1990 Regents of the University of Michigan. |
* All rights reserved. |
* All rights reserved. |
* |
* |
Line 11
|
Line 15
|
* is provided ``as is'' without express or implied warranty. |
* is provided ``as is'' without express or implied warranty. |
*/ |
*/ |
|
|
|
#include "portable.h" |
|
|
#include <stdio.h> |
#include <stdio.h> |
#ifdef MACOS |
|
#include <stdlib.h> |
|
#include <stdarg.h> |
|
#include "macos.h" |
|
#else /* MACOS */ |
|
#if defined(NeXT) || defined(VMS) |
|
#include <stdlib.h> |
#include <stdlib.h> |
#else /* next || vms */ |
|
#include <malloc.h> |
|
#endif /* next || vms */ |
|
#if defined(BC31) || defined(_WIN32) |
|
#include <stdarg.h> |
|
#else /* BC31 || _WIN32 */ |
|
#include <varargs.h> |
|
#endif /* BC31 || _WIN32 */ |
|
#include <sys/types.h> |
|
#include <sys/socket.h> |
|
#include <netinet/in.h> |
|
#ifdef PCNFS |
|
#include <tklib.h> |
|
#endif /* PCNFS */ |
|
#endif /* MACOS */ |
|
|
|
#if defined( DOS ) || defined( _WIN32 ) |
|
#include "msdos.h" |
|
#endif /* DOS */ |
|
|
|
#include <string.h> |
#include <ac/stdarg.h> |
|
|
|
#include <ac/string.h> |
|
#include <ac/socket.h> |
|
|
#include "lber.h" |
#include "lber.h" |
|
|
#ifdef LDAP_DEBUG |
|
int lber_debug; |
int lber_debug; |
#endif |
|
|
|
#ifdef NEEDPROTOS |
|
static int ber_getnint( BerElement *ber, long *num, int len ); |
|
#endif /* NEEDPROTOS */ |
|
|
|
|
static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len )); |
|
|
/* return the tag - LBER_DEFAULT returned means trouble */ |
/* return the tag - LBER_DEFAULT returned means trouble */ |
unsigned long |
unsigned long |
Line 58 ber_get_tag( BerElement *ber )
|
Line 38 ber_get_tag( BerElement *ber )
|
unsigned char xbyte; |
unsigned char xbyte; |
unsigned long tag; |
unsigned long tag; |
char *tagp; |
char *tagp; |
int i; |
unsigned int i; |
|
|
if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) |
if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
Line 129 ber_skip_tag( BerElement *ber, unsigned
|
Line 109 ber_skip_tag( BerElement *ber, unsigned
|
if ( ber_read( ber, (char *) &netlen + diff, noctets ) |
if ( ber_read( ber, (char *) &netlen + diff, noctets ) |
!= noctets ) |
!= noctets ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
*len = LBER_NTOHL( netlen ); |
*len = AC_NTOHL( netlen ); |
} else { |
} else { |
*len = lc; |
*len = lc; |
} |
} |
Line 155 ber_getnint( BerElement *ber, long *num,
|
Line 135 ber_getnint( BerElement *ber, long *num,
|
{ |
{ |
int diff, sign, i; |
int diff, sign, i; |
long netnum; |
long netnum; |
|
char *p; |
|
|
/* |
/* |
* The tag and length have already been stripped off. We should |
* The tag and length have already been stripped off. We should |
Line 172 ber_getnint( BerElement *ber, long *num,
|
Line 153 ber_getnint( BerElement *ber, long *num,
|
if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len ) |
if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len ) |
return( -1 ); |
return( -1 ); |
|
|
/* sign extend if necessary */ |
/* sign extend if necessary */ |
sign = ((0x80 << ((len - 1) * 8)) & netnum); |
p = (char *) &netnum; |
if ( sign && len < sizeof(long) ) { |
sign = (0x80 & *(p+diff) ); |
for ( i = sizeof(long) - 1; i > len - 1; i-- ) { |
if ( sign && len < sizeof(long) ) { |
netnum |= (0xffL << (i * 8)); |
for ( i = 0; i < diff; i++ ) { |
|
*(p+i) = (unsigned char) 0xff; |
} |
} |
} |
} |
*num = LBER_NTOHL( netnum ); |
*num = AC_NTOHL( netnum ); |
|
|
return( len ); |
return( len ); |
} |
} |
Line 192 ber_get_int( BerElement *ber, long *num
|
Line 174 ber_get_int( BerElement *ber, long *num
|
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) |
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
|
if ( ber_getnint( ber, num, (int)len ) != len ) |
if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
else |
else |
return( tag ); |
return( tag ); |
Line 211 ber_get_stringb( BerElement *ber, char *
|
Line 193 ber_get_stringb( BerElement *ber, char *
|
if ( datalen > (*len - 1) ) |
if ( datalen > (*len - 1) ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
|
if ( ber_read( ber, buf, datalen ) != datalen ) |
if ( (unsigned long) ber_read( ber, buf, datalen ) != datalen ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
|
buf[datalen] = '\0'; |
buf[datalen] = '\0'; |
Line 244 ber_get_stringa( BerElement *ber, char *
|
Line 226 ber_get_stringa( BerElement *ber, char *
|
{ |
{ |
unsigned long datalen, tag; |
unsigned long datalen, tag; |
|
|
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) |
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) { |
|
*buf = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
|
|
if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL ) |
if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
|
if ( ber_read( ber, *buf, datalen ) != datalen ) |
if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) { |
|
free( *buf ); |
|
*buf = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
(*buf)[datalen] = '\0'; |
(*buf)[datalen] = '\0'; |
|
|
#ifdef STR_TRANSLATION |
#ifdef STR_TRANSLATION |
Line 261 ber_get_stringa( BerElement *ber, char *
|
Line 248 ber_get_stringa( BerElement *ber, char *
|
if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 ) |
if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 ) |
!= 0 ) { |
!= 0 ) { |
free( *buf ); |
free( *buf ); |
|
*buf = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
} |
} |
} |
} |
Line 274 ber_get_stringal( BerElement *ber, struc
|
Line 262 ber_get_stringal( BerElement *ber, struc
|
{ |
{ |
unsigned long len, tag; |
unsigned long len, tag; |
|
|
if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL ) |
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) { |
|
*bv = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
|
|
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) |
if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
|
if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL ) |
if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL ) { |
|
free( *bv ); |
|
*bv = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
|
|
if ( ber_read( ber, (*bv)->bv_val, len ) != len ) |
if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len ) { |
|
ber_bvfree( *bv ); |
|
*bv = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
((*bv)->bv_val)[len] = '\0'; |
((*bv)->bv_val)[len] = '\0'; |
(*bv)->bv_len = len; |
(*bv)->bv_len = len; |
|
|
Line 294 ber_get_stringal( BerElement *ber, struc
|
Line 290 ber_get_stringal( BerElement *ber, struc
|
++len; |
++len; |
if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val), |
if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val), |
&len, 1 ) != 0 ) { |
&len, 1 ) != 0 ) { |
free( (*bv)->bv_val ); |
ber_bvfree( *bv ); |
|
*bv = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
} |
} |
(*bv)->bv_len = len - 1; |
(*bv)->bv_len = len - 1; |
Line 310 ber_get_bitstringa( BerElement *ber, cha
|
Line 307 ber_get_bitstringa( BerElement *ber, cha
|
unsigned long datalen, tag; |
unsigned long datalen, tag; |
unsigned char unusedbits; |
unsigned char unusedbits; |
|
|
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) |
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) { |
|
*buf = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
--datalen; |
--datalen; |
|
|
if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL ) |
if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL ) |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
|
if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) |
if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) { |
|
free( buf ); |
|
*buf = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
|
|
if ( ber_read( ber, *buf, datalen ) != datalen ) |
if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) { |
|
free( buf ); |
|
*buf = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
|
} |
|
|
*blen = datalen * 8 - unusedbits; |
*blen = datalen * 8 - unusedbits; |
return( tag ); |
return( tag ); |
Line 358 ber_first_element( BerElement *ber, unsi
|
Line 363 ber_first_element( BerElement *ber, unsi
|
{ |
{ |
/* skip the sequence header, use the len to mark where to stop */ |
/* skip the sequence header, use the len to mark where to stop */ |
if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) { |
if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) { |
|
*last = NULL; |
return( LBER_DEFAULT ); |
return( LBER_DEFAULT ); |
} |
} |
|
|
Line 382 ber_next_element( BerElement *ber, unsig
|
Line 388 ber_next_element( BerElement *ber, unsig
|
|
|
/* VARARGS */ |
/* VARARGS */ |
unsigned long |
unsigned long |
ber_scanf( |
ber_scanf |
#if defined( MACOS ) || defined( BC31 ) || defined( _WIN32 ) |
#if HAVE_STDARG |
BerElement *ber, char *fmt, ... ) |
( BerElement *ber, char *fmt, ... ) |
#else |
#else |
va_alist ) |
( va_alist ) |
va_dcl |
va_dcl |
#endif |
#endif |
{ |
{ |
va_list ap; |
va_list ap; |
#if !defined( MACOS ) && !defined( BC31 ) && !defined( _WIN32 ) |
#ifndef HAVE_STDARG |
BerElement *ber; |
BerElement *ber; |
char *fmt; |
char *fmt; |
#endif |
#endif |
|
char *fmt_reset; |
char *last; |
char *last; |
char *s, **ss, ***sss; |
char *s, **ss, ***sss; |
struct berval ***bv, **bvp, *bval; |
struct berval ***bv, **bvp, *bval; |
int *i, j; |
int *i, j; |
long *l, rc, tag; |
long *l; |
unsigned long len; |
unsigned long rc, tag, len; |
|
|
#if defined( MACOS ) || defined( BC31 ) || defined( _WIN32 ) |
#ifdef HAVE_STDARG |
va_start( ap, fmt ); |
va_start( ap, fmt ); |
#else |
#else |
va_start( ap ); |
va_start( ap ); |
ber = va_arg( ap, BerElement * ); |
ber = va_arg( ap, BerElement * ); |
fmt = va_arg( ap, char * ); |
fmt = va_arg( ap, char * ); |
#endif |
#endif |
|
fmt_reset = fmt; |
|
|
#ifdef LDAP_DEBUG |
#ifdef LDAP_DEBUG |
if ( lber_debug & 64 ) { |
if ( lber_debug & 64 ) { |
Line 418 va_dcl
|
Line 426 va_dcl
|
#endif |
#endif |
|
|
for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) { |
for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) { |
|
/* When this is modified, remember to update |
|
* the error-cleanup code below accordingly. */ |
switch ( *fmt ) { |
switch ( *fmt ) { |
case 'a': /* octet string - allocate storage as needed */ |
case 'a': /* octet string - allocate storage as needed */ |
ss = va_arg( ap, char ** ); |
ss = va_arg( ap, char ** ); |
Line 536 va_dcl
|
Line 546 va_dcl
|
break; |
break; |
|
|
default: |
default: |
#ifndef NO_USERINTERFACE |
#ifdef LDAP_LIBUI |
fprintf( stderr, "unknown fmt %c\n", *fmt ); |
fprintf( stderr, "unknown fmt %c\n", *fmt ); |
#endif /* NO_USERINTERFACE */ |
#endif /* LDAP_LIBUI */ |
rc = LBER_DEFAULT; |
rc = LBER_DEFAULT; |
break; |
break; |
} |
} |
Line 546 va_dcl
|
Line 556 va_dcl
|
|
|
va_end( ap ); |
va_end( ap ); |
|
|
|
if ( rc == LBER_DEFAULT ) { |
|
/* |
|
* Error. Reclaim malloced memory that was given to the caller. |
|
* Set allocated pointers to NULL, "data length" outvalues to 0. |
|
*/ |
|
#ifdef HAVE_STDARG |
|
va_start( ap, fmt ); |
|
#else |
|
va_start( ap ); |
|
(void) va_arg( ap, BerElement * ); |
|
(void) va_arg( ap, char * ); |
|
#endif |
|
|
|
for ( ; fmt_reset < fmt; fmt_reset++ ) { |
|
switch ( *fmt_reset ) { |
|
case 'a': /* octet string - allocate storage as needed */ |
|
ss = va_arg( ap, char ** ); |
|
if ( *ss ) { |
|
free( *ss ); |
|
*ss = NULL; |
|
} |
|
break; |
|
|
|
case 'b': /* boolean */ |
|
case 't': /* tag of next item */ |
|
case 'T': /* skip tag of next item */ |
|
(void) va_arg( ap, int * ); |
|
break; |
|
|
|
case 's': /* octet string - in a buffer */ |
|
(void) va_arg( ap, char * ); |
|
/* Fall through */ |
|
case 'e': /* enumerated */ |
|
case 'i': /* int */ |
|
case 'l': /* length of next item */ |
|
(void) va_arg( ap, long * ); |
|
break; |
|
|
|
case 'o': /* octet string in a supplied berval */ |
|
bval = va_arg( ap, struct berval * ); |
|
if ( bval->bv_val ) { |
|
free( bval->bv_val ); |
|
bval->bv_val = NULL; |
|
} |
|
bval->bv_len = 0; |
|
break; |
|
|
|
case 'O': /* octet string - allocate & include length */ |
|
bvp = va_arg( ap, struct berval ** ); |
|
if ( *bvp ) { |
|
ber_bvfree( *bvp ); |
|
*bvp = NULL; |
|
} |
|
break; |
|
|
|
case 'B': /* bit string - allocate storage as needed */ |
|
ss = va_arg( ap, char ** ); |
|
if ( *ss ) { |
|
free( *ss ); |
|
*ss = NULL; |
|
} |
|
*(va_arg( ap, long * )) = 0; /* for length, in bits */ |
|
break; |
|
|
|
case 'v': /* sequence of strings */ |
|
sss = va_arg( ap, char *** ); |
|
if ( *sss ) { |
|
for (j = 0; (*sss)[j]; j++) |
|
free( (*sss)[j] ); |
|
free( *sss ); |
|
*sss = NULL; |
|
} |
|
break; |
|
|
|
case 'V': /* sequence of strings + lengths */ |
|
bv = va_arg( ap, struct berval *** ); |
|
if ( *bv ) { |
|
ber_bvecfree( *bv ); |
|
*bv = NULL; |
|
} |
|
break; |
|
|
|
#if 0 /* No action for these format characters */ |
|
case 'n': /* null */ |
|
case 'x': /* skip the next element - whatever it is */ |
|
case '{': /* begin sequence */ |
|
case '[': /* begin set */ |
|
case '}': /* end sequence */ |
|
case ']': /* end set */ |
|
#endif |
|
|
|
} |
|
} |
|
|
|
va_end( ap ); |
|
} |
|
|
return( rc ); |
return( rc ); |
} |
} |
|
|