/* decode.c - ber input decoding routines */
+/* $OpenLDAP$ */
/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
/* Portions
#include <ac/string.h>
#include <ac/socket.h>
-#undef LDAP_F_PRE
-#define LDAP_F_PRE LDAP_F_EXPORT
-
#include "lber-int.h"
-static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len ));
+static ber_len_t ber_getnint LDAP_P((
+ BerElement *ber,
+ ber_int_t *num,
+ ber_len_t len ));
/* return the tag - LBER_DEFAULT returned means trouble */
-unsigned long
+ber_tag_t
ber_get_tag( BerElement *ber )
{
unsigned char xbyte;
- unsigned long tag;
- char *tagp;
+ ber_tag_t tag;
unsigned int i;
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
- return( LBER_DEFAULT );
+ if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) {
+ return LBER_DEFAULT;
+ }
+
+ tag = xbyte;
- if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
- return( (unsigned long) xbyte );
+ if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
+ return tag;
+ }
- tagp = (char *) &tag;
- tagp[0] = xbyte;
- for ( i = 1; i < sizeof(long); i++ ) {
- if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
- return( LBER_DEFAULT );
+ for ( i = 1; i < sizeof(ber_tag_t); i++ ) {
+ if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) {
+ return LBER_DEFAULT;
+ }
- tagp[i] = xbyte;
+ tag <<= 8;
+ tag |= 0x00ffUL & (ber_tag_t) xbyte;
- if ( ! (xbyte & LBER_MORE_TAG_MASK) )
+ if ( ! (xbyte & LBER_MORE_TAG_MASK) ) {
break;
+ }
}
/* tag too big! */
- if ( i == sizeof(long) )
- return( LBER_DEFAULT );
+ if ( i == sizeof(ber_tag_t) ) {
+ return LBER_DEFAULT;
+ }
- /* want leading, not trailing 0's */
- return( tag >> (sizeof(long) - i - 1) );
+ return tag;
}
-unsigned long
-ber_skip_tag( BerElement *ber, unsigned long *len )
+ber_tag_t
+ber_skip_tag( BerElement *ber, ber_len_t *len )
{
- unsigned long tag;
+ ber_tag_t tag;
unsigned char lc;
- unsigned int noctets;
- int diff;
- unsigned long netlen;
+ ber_len_t i, noctets;
+ unsigned char netlen[sizeof(ber_len_t)];
assert( ber != NULL );
assert( len != NULL );
* 2) primitive encodings used whenever possible
*/
+ *len = 0;
+
/*
* First, we read the tag.
*/
- if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
- return( LBER_DEFAULT );
+ if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT ) {
+ return LBER_DEFAULT;
+ }
/*
* Next, read the length. The first byte contains the length of
* the length. If bit 8 is set, the length is the long form,
* otherwise it's the short form. We don't allow a length that's
- * greater than what we can hold in an unsigned long.
+ * greater than what we can hold in a ber_len_t.
*/
- *len = netlen = 0;
if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
+
if ( lc & 0x80U ) {
noctets = (lc & 0x7fU);
- if ( (unsigned) noctets > sizeof(unsigned long) )
- return( LBER_DEFAULT );
- diff = sizeof(unsigned long) - noctets;
- if ( (unsigned) ber_read( ber, (char *) &netlen + diff, noctets )
- != noctets )
- return( LBER_DEFAULT );
- *len = AC_NTOHL( netlen );
+
+ if ( noctets > sizeof(ber_len_t) ) {
+ return LBER_DEFAULT;
+ }
+
+ if( (unsigned) ber_read( ber, netlen, noctets ) != noctets ) {
+ return LBER_DEFAULT;
+ }
+
+ for( i = 0; i < noctets; i++ ) {
+ *len <<= 8;
+ *len |= netlen[i];
+ }
+
} else {
*len = lc;
}
- return( tag );
+ return tag;
}
-unsigned long
+ber_tag_t
ber_peek_tag(
- LDAP_CONST BerElement *ber_in, /* not const per c-api-02 */
- unsigned long *len )
+ BerElement *ber,
+ ber_len_t *len )
{
- unsigned long tag;
- BerElement *ber;
-
- assert( ber_in != NULL );
- assert( BER_VALID( ber_in ) );
-
- ber = ber_dup( ber_in );
-
- if( ber == NULL ) {
- return LBER_ERROR;
- }
+ /*
+ * This implementation assumes ber_skip_tag() only
+ * modifies ber_ptr field of the BerElement.
+ */
- assert( BER_VALID( ber ) );
+ char *save;
+ ber_tag_t tag;
+ save = ber->ber_ptr;
tag = ber_skip_tag( ber, len );
+ ber->ber_ptr = save;
- ber_free( ber, 0 );
- return( tag );
+ return tag;
}
-static int
-ber_getnint( BerElement *ber, long *num, int len )
+static ber_len_t
+ber_getnint(
+ BerElement *ber,
+ ber_int_t *num,
+ ber_len_t len )
{
- int diff, sign, i;
- long netnum;
- char *p;
+ unsigned char buf[sizeof(ber_int_t)];
assert( ber != NULL );
assert( num != NULL );
-
assert( BER_VALID( ber ) );
/*
* extend after we read it in.
*/
- if ( (unsigned) len > sizeof(long) )
- return( -1 );
-
- netnum = 0;
- diff = sizeof(long) - len;
- /* read into the low-order bytes of netnum */
- if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
- return( -1 );
-
- /* sign extend if necessary */
- p = (char *) &netnum;
- sign = (0x80 & *(p+diff) );
- if ( sign && ((unsigned) len < sizeof(long)) ) {
- for ( i = 0; i < diff; i++ ) {
- *(p+i) = (unsigned char) 0xff;
+ if ( len > sizeof(ber_int_t) ) {
+ return -1;
+ }
+
+ /* read into the low-order bytes of our buffer */
+ if ( (ber_len_t) ber_read( ber, (char *) buf, len ) != len ) {
+ return -1;
+ }
+
+ if( len ) {
+ /* sign extend if necessary */
+ ber_len_t i;
+ ber_int_t netnum = 0x80 & buf[0] ? -1 : 0;
+
+ /* shift in the bytes */
+ for( i=0 ; i<len; i++ ) {
+ netnum = (netnum << 8 ) | buf[i];
}
+
+ *num = netnum;
+
+ } else {
+ *num = 0;
}
- *num = AC_NTOHL( netnum );
- return( len );
+ return len;
}
-unsigned long
-ber_get_int( BerElement *ber, long *num )
+ber_tag_t
+ber_get_int(
+ BerElement *ber,
+ ber_int_t *num )
{
- unsigned long tag, len;
+ ber_tag_t tag;
+ ber_len_t len;
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
- return( LBER_DEFAULT );
+ if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
+ return LBER_DEFAULT;
+ }
+
+ if ( ber_getnint( ber, num, len ) != len ) {
+ return LBER_DEFAULT;
+ }
+
+ return tag;
+}
- if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len )
- return( LBER_DEFAULT );
- else
- return( tag );
+ber_tag_t
+ber_get_enum(
+ BerElement *ber,
+ ber_int_t *num )
+{
+ return ber_get_int( ber, num );
}
-unsigned long
-ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
+ber_tag_t
+ber_get_stringb(
+ BerElement *ber,
+ char *buf,
+ ber_len_t *len )
{
- unsigned long datalen, tag;
-#ifdef STR_TRANSLATION
- char *transbuf;
-#endif /* STR_TRANSLATION */
+ ber_len_t datalen;
+ ber_tag_t tag;
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
- return( LBER_DEFAULT );
- if ( datalen > (*len - 1) )
- return( LBER_DEFAULT );
+ if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
+ return LBER_DEFAULT;
+ }
+ if ( datalen > (*len - 1) ) {
+ return LBER_DEFAULT;
+ }
- if ( (unsigned long) ber_read( ber, buf, datalen ) != datalen )
- return( LBER_DEFAULT );
+ if ( (ber_len_t) ber_read( ber, buf, datalen ) != datalen ) {
+ return LBER_DEFAULT;
+ }
buf[datalen] = '\0';
-#ifdef STR_TRANSLATION
- if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
- && ber->ber_decode_translate_proc ) {
- transbuf = buf;
- ++datalen;
- if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
- 0 ) != 0 ) {
- return( LBER_DEFAULT );
- }
- if ( datalen > *len ) {
- LBER_FREE( transbuf );
- return( LBER_DEFAULT );
- }
- SAFEMEMCPY( buf, transbuf, datalen );
- LBER_FREE( transbuf );
- --datalen;
- }
-#endif /* STR_TRANSLATION */
-
*len = datalen;
- return( tag );
+ return tag;
}
-unsigned long
+ber_tag_t
ber_get_stringa( BerElement *ber, char **buf )
{
- unsigned long datalen, tag;
+ ber_len_t datalen;
+ ber_tag_t tag;
assert( ber != NULL );
assert( buf != NULL );
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
*buf = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
- if ( (*buf = (char *) LBER_MALLOC( (size_t)datalen + 1 )) == NULL )
- return( LBER_DEFAULT );
+ if ( (*buf = (char *) LBER_MALLOC( datalen + 1 )) == NULL ) {
+ return LBER_DEFAULT;
+ }
- if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
+ if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
LBER_FREE( *buf );
*buf = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
(*buf)[datalen] = '\0';
-#ifdef STR_TRANSLATION
- if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
- && ber->ber_decode_translate_proc ) {
- ++datalen;
- if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
- != 0 ) {
- LBER_FREE( *buf );
- *buf = NULL;
- return( LBER_DEFAULT );
- }
- }
-#endif /* STR_TRANSLATION */
-
- return( tag );
+ return tag;
}
-unsigned long
+ber_tag_t
ber_get_stringal( BerElement *ber, struct berval **bv )
{
- unsigned long len, tag;
+ ber_len_t len;
+ ber_tag_t tag;
assert( ber != NULL );
assert( bv != NULL );
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
*bv = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
- if ( (*bv = (struct berval *) LBER_MALLOC( sizeof(struct berval) )) == NULL )
- return( LBER_DEFAULT );
+ *bv = (struct berval *) LBER_MALLOC( sizeof(struct berval) );
+ if ( *bv == NULL ) {
+ return LBER_DEFAULT;
+ }
+
+ if( len == 0 ) {
+ (*bv)->bv_val = NULL;
+ (*bv)->bv_len = 0;
+ return tag;
+ }
- if ( ((*bv)->bv_val = (char *) LBER_MALLOC( (size_t)len + 1 )) == NULL ) {
+ (*bv)->bv_val = (char *) LBER_MALLOC( len + 1 );
+ if ( (*bv)->bv_val == NULL ) {
LBER_FREE( *bv );
*bv = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
- if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len ) {
+ if ( (ber_len_t) 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_len = len;
-#ifdef STR_TRANSLATION
- if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
- && ber->ber_decode_translate_proc ) {
- ++len;
- if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
- &len, 1 ) != 0 ) {
- ber_bvfree( *bv );
- *bv = NULL;
- return( LBER_DEFAULT );
- }
- (*bv)->bv_len = len - 1;
- }
-#endif /* STR_TRANSLATION */
-
- return( tag );
+ return tag;
}
-unsigned long
-ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
+ber_tag_t
+ber_get_bitstringa(
+ BerElement *ber,
+ char **buf,
+ ber_len_t *blen )
{
- unsigned long datalen, tag;
+ ber_len_t datalen;
+ ber_tag_t tag;
unsigned char unusedbits;
assert( ber != NULL );
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
*buf = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
--datalen;
- if ( (*buf = (char *) LBER_MALLOC( (size_t)datalen )) == NULL )
- return( LBER_DEFAULT );
+ if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL ) {
+ return LBER_DEFAULT;
+ }
if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
LBER_FREE( buf );
*buf = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
- if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
+ if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
LBER_FREE( buf );
*buf = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
*blen = datalen * 8 - unusedbits;
- return( tag );
+ return tag;
}
-unsigned long
+ber_tag_t
ber_get_null( BerElement *ber )
{
- unsigned long len, tag;
+ ber_len_t len;
+ ber_tag_t tag;
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
- return( LBER_DEFAULT );
+ if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
+ return LBER_DEFAULT;
+ }
- if ( len != 0 )
- return( LBER_DEFAULT );
+ if ( len != 0 ) {
+ return LBER_DEFAULT;
+ }
return( tag );
}
-unsigned long
-ber_get_boolean( BerElement *ber, int *boolval )
+ber_tag_t
+ber_get_boolean(
+ BerElement *ber,
+ ber_int_t *boolval )
{
- long longbool;
- int rc;
+ ber_int_t longbool;
+ ber_tag_t rc;
assert( ber != NULL );
assert( boolval != NULL );
rc = ber_get_int( ber, &longbool );
*boolval = longbool;
- return( rc );
+ return rc;
}
-unsigned long
-ber_first_element( BerElement *ber, unsigned long *len, char **last )
+ber_tag_t
+ber_first_element(
+ BerElement *ber,
+ ber_len_t *len,
+ char **last )
{
assert( ber != NULL );
assert( len != NULL );
/* skip the sequence header, use the len to mark where to stop */
if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
*last = NULL;
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
*last = ber->ber_ptr + *len;
if ( *last == ber->ber_ptr ) {
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
- return( ber_peek_tag( ber, len ) );
+ return ber_peek_tag( ber, len );
}
-unsigned long
-ber_next_element( BerElement *ber, unsigned long *len, char *last )
+ber_tag_t
+ber_next_element(
+ BerElement *ber,
+ ber_len_t *len,
+ LDAP_CONST char *last )
{
assert( ber != NULL );
assert( len != NULL );
assert( BER_VALID( ber ) );
if ( ber->ber_ptr == last ) {
- return( LBER_DEFAULT );
+ return LBER_DEFAULT;
}
- return( ber_peek_tag( ber, len ) );
+ return ber_peek_tag( ber, len );
}
/* VARARGS */
-unsigned long
-ber_scanf
-#if HAVE_STDARG
- ( BerElement *ber,
+ber_tag_t
+ber_scanf ( BerElement *ber,
LDAP_CONST char *fmt,
... )
-#else
- ( va_alist )
-va_dcl
-#endif
{
va_list ap;
-#ifndef HAVE_STDARG
- BerElement *ber;
- char *fmt;
-#endif
LDAP_CONST char *fmt_reset;
char *last;
char *s, **ss, ***sss;
struct berval ***bv, **bvp, *bval;
- int *i, j;
- long *l;
- unsigned long rc, tag, len;
+ ber_int_t *i;
+ int j;
+ ber_len_t *l;
+ ber_tag_t *t;
+ ber_tag_t rc, tag;
+ ber_len_t len;
-#ifdef HAVE_STDARG
va_start( ap, fmt );
-#else
- va_start( ap );
- ber = va_arg( ap, BerElement * );
- fmt = va_arg( ap, char * );
-#endif
assert( ber != NULL );
assert( fmt != NULL );
fmt_reset = fmt;
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "LIBLBER", LDAP_LEVEL_ENTRY, "ber_scanf fmt (%s) ber:\n", fmt ));
+ BER_DUMP(( "LIBLBER", LDAP_LEVEL_DETAIL2, ber, 1 ));
+#else
ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
"ber_scanf fmt (%s) ber:\n", fmt );
ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
+#endif
for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
/* When this is modified, remember to update
break;
case 'b': /* boolean */
- i = va_arg( ap, int * );
+ i = va_arg( ap, ber_int_t * );
rc = ber_get_boolean( ber, i );
break;
case 'e': /* enumerated */
case 'i': /* int */
- l = va_arg( ap, long * );
- rc = ber_get_int( ber, l );
+ i = va_arg( ap, ber_int_t * );
+ rc = ber_get_int( ber, i );
break;
case 'l': /* length of next item */
- l = va_arg( ap, long * );
+ l = va_arg( ap, ber_len_t * );
rc = ber_peek_tag( ber, l );
break;
case 's': /* octet string - in a buffer */
s = va_arg( ap, char * );
- l = va_arg( ap, long * );
+ l = va_arg( ap, ber_len_t * );
rc = ber_get_stringb( ber, s, l );
break;
case 'B': /* bit string - allocate storage as needed */
ss = va_arg( ap, char ** );
- l = va_arg( ap, long * ); /* for length, in bits */
+ l = va_arg( ap, ber_len_t * ); /* for length, in bits */
rc = ber_get_bitstringa( ber, ss, l );
break;
case 't': /* tag of next item */
- l = va_arg( ap, long * );
- *l = rc = ber_peek_tag( ber, &len );
+ t = va_arg( ap, ber_tag_t * );
+ *t = rc = ber_peek_tag( ber, &len );
break;
case 'T': /* skip tag of next item */
- l = va_arg( ap, long * );
- *l = rc = ber_skip_tag( ber, &len );
+ t = va_arg( ap, ber_tag_t * );
+ *t = rc = ber_skip_tag( ber, &len );
break;
case 'v': /* sequence of strings */
* 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 ) {
break;
case 'b': /* boolean */
+ case 'e': /* enumerated */
+ case 'i': /* int */
(void) va_arg( ap, int * );
break;
case 's': /* octet string - in a buffer */
(void) va_arg( ap, char * );
- (void) va_arg( ap, long * );
+ (void) va_arg( ap, ber_len_t * );
break;
- case 'e': /* enumerated */
- case 'i': /* int */
case 'l': /* length of next item */
+ (void) va_arg( ap, ber_len_t * );
+ break;
+
case 't': /* tag of next item */
case 'T': /* skip tag of next item */
- (void) va_arg( ap, long * );
+ (void) va_arg( ap, ber_tag_t * );
break;
case 'o': /* octet string in a supplied berval */
LBER_FREE( *ss );
*ss = NULL;
}
- *(va_arg( ap, long * )) = 0; /* for length, in bits */
+ *(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
break;
case 'v': /* sequence of strings */
va_end( ap );
}
- return( rc );
-}
-
-
-#ifdef STR_TRANSLATION
-void
-ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
- BERTranslateProc decode_proc )
-{
- assert( ber != NULL );
- assert( BER_VALID( ber ) );
-
- ber->ber_encode_translate_proc = encode_proc;
- ber->ber_decode_translate_proc = decode_proc;
+ return rc;
}
-#endif /* STR_TRANSLATION */