/* encode.c - ber output encoding 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/socket.h>
#include <ac/string.h>
-#undef LDAP_F_PRE
-#define LDAP_F_PRE LDAP_F_EXPORT
-
#include "lber-int.h"
static int ber_put_len LDAP_P((
for ( i = sizeof(ber_tag_t) - 1; i > 0; i-- ) {
mask = ((ber_tag_t)0xffU << (i * 8));
/* not all zero */
- if ( tag & mask )
- break;
+ if ( tag & mask ) break;
}
- return( i + 1 );
+ return i + 1;
}
static int
{
int rc;
ber_len_t taglen;
- ber_tag_t ntag;
+ ber_len_t i;
+ unsigned char nettag[sizeof(ber_tag_t)];
assert( ber != NULL );
assert( BER_VALID( ber ) );
taglen = ber_calc_taglen( tag );
- ntag = LBER_TAG_HTON( tag );
+ for( i=0; i<taglen; i++ ) {
+ nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tag & 0xffU);
+ tag >>= 8;
+ }
rc = ber_write( ber,
- ((char *) &ntag) + sizeof(ber_tag_t) - taglen,
+ &nettag[sizeof(ber_tag_t) - taglen],
taglen, nosos );
return rc;
*/
if ( len <= (ber_len_t) 0x7FU )
- return( 1 );
+ return 1;
/*
* long len otherwise - one byte with bit 8 set, giving the
*/
if ( len <= (ber_len_t) 0xffU )
- return( 2 );
+ return 2;
if ( len <= (ber_len_t) 0xffffU )
- return( 3 );
+ return 3;
if ( len <= (ber_len_t) 0xffffffU )
- return( 4 );
+ return 4;
- return( 5 );
+ return 5;
}
static int
ber_put_len( BerElement *ber, ber_len_t len, int nosos )
{
- int i;
+ int rc;
+ int i,j;
char lenlen;
ber_len_t mask;
- ber_len_t netlen;
+ unsigned char netlen[sizeof(ber_len_t)];
assert( ber != NULL );
assert( BER_VALID( ber ) );
*/
if ( len <= 127 ) {
- netlen = LBER_LEN_HTON( len );
- return( ber_write( ber, (char *) &netlen + sizeof(long) - 1,
- 1, nosos ) );
+ char length_byte = (char) len;
+ return ber_write( ber, &length_byte, 1, nosos );
}
/*
/* find the first non-all-zero byte */
for ( i = sizeof(ber_len_t) - 1; i > 0; i-- ) {
- mask = (0xffU << (i * 8));
+ mask = ((ber_len_t)0xffU << (i * 8));
/* not all zero */
- if ( len & mask )
- break;
+ if ( len & mask ) break;
}
lenlen = (unsigned char) ++i;
if ( lenlen > 4 )
- return( -1 );
+ return -1;
+
lenlen |= 0x80UL;
/* write the length of the length */
if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
- return( -1 );
+ return -1;
+
+ for( j=0; j<i; j++) {
+ netlen[(sizeof(ber_len_t)-1) - j] = (unsigned char)(len & 0xffU);
+ len >>= 8;
+ }
/* write the length itself */
- netlen = LBER_LEN_HTON( len );
- if ( ber_write( ber, (char *) &netlen + (sizeof(ber_len_t) - i), i, nosos )
- != i )
- return( -1 );
+ rc = ber_write( ber,
+ &netlen[sizeof(ber_len_t)-i],
+ i, nosos );
- return( i + 1 );
+ return rc == i ? i+1 : -1;
}
static int
ber_int_t num,
ber_tag_t tag )
{
- int i, sign;
+ int rc;
+ int i, j, sign;
ber_len_t len, lenlen, taglen;
- ber_int_t netnum, mask;
+ ber_uint_t unum, mask;
+ unsigned char netnum[sizeof(ber_uint_t)];
assert( ber != NULL );
assert( BER_VALID( ber ) );
sign = (num < 0);
+ unum = num; /* Bit fiddling should be done with unsigned values */
/*
* high bit is set - look for first non-all-one byte
* high bit is clear - look for first non-all-zero byte
*/
for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
- mask = (0xffU << (i * 8));
+ mask = ((ber_uint_t)0xffU << (i * 8));
if ( sign ) {
/* not all ones */
- if ( (num & mask) != mask )
- break;
+ if ( (unum & mask) != mask ) break;
} else {
/* not all zero */
- if ( num & mask )
- break;
+ if ( unum & mask ) break;
}
}
* we now have the "leading byte". if the high bit on this
* byte matches the sign bit, we need to "back up" a byte.
*/
- mask = (num & (0x80U << (i * 8)));
- if ( (mask && !sign) || (sign && !mask) )
+ mask = (unum & ((ber_uint_t)0x80U << (i * 8)));
+ if ( (mask && !sign) || (sign && !mask) ) {
i++;
+ }
len = i + 1;
- if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
- return( -1 );
+ if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
+ return -1;
+ }
if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
- return( -1 );
+ return -1;
i++;
- netnum = LBER_INT_HTON( num );
- if ( ber_write( ber, (char *) &netnum + (sizeof(ber_int_t) - i), i, 0 )
- != i )
- return( -1 );
+
+ for( j=0; j<i; j++ ) {
+ netnum[(sizeof(ber_int_t)-1) - j] = (unsigned char)(unum & 0xffU);
+ unum >>= 8;
+ }
+
+ rc = ber_write( ber,
+ &netnum[sizeof(ber_int_t) - i],
+ i, 0 );
/* length of tag + length + contents */
- return( taglen + lenlen + i );
+ return rc == i ? taglen + lenlen + i : -1;
}
int
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( tag == LBER_DEFAULT )
+ if ( tag == LBER_DEFAULT ) {
tag = LBER_ENUMERATED;
+ }
- return( ber_put_int_or_enum( ber, num, tag ) );
+ return ber_put_int_or_enum( ber, num, tag );
}
int
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( tag == LBER_DEFAULT )
+ if ( tag == LBER_DEFAULT ) {
tag = LBER_INTEGER;
+ }
- return( ber_put_int_or_enum( ber, num, tag ) );
+ return ber_put_int_or_enum( ber, num, tag );
}
int
{
ber_len_t taglen, lenlen;
int rc;
-#ifdef STR_TRANSLATION
- int free_str;
-#endif /* STR_TRANSLATION */
assert( ber != NULL );
assert( str != NULL );
assert( BER_VALID( ber ) );
- if ( tag == LBER_DEFAULT )
+ if ( tag == LBER_DEFAULT ) {
tag = LBER_OCTETSTRING;
+ }
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
- return( -1 );
-
-#ifdef STR_TRANSLATION
- if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 &&
- ber->ber_encode_translate_proc ) {
- if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
- != 0 ) {
- return( -1 );
- }
- free_str = 1;
- } else {
- free_str = 0;
- }
-#endif /* STR_TRANSLATION */
+ return -1;
if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
- (unsigned long) ber_write( ber, str, len, 0 ) != len ) {
+ (ber_len_t) ber_write( ber, str, len, 0 ) != len ) {
rc = -1;
} else {
/* return length of tag + length + contents */
rc = taglen + lenlen + len;
}
-#ifdef STR_TRANSLATION
- if ( free_str ) {
- LBER_FREE( str );
- }
-#endif /* STR_TRANSLATION */
-
- return( rc );
+ return rc;
}
int
assert( BER_VALID( ber ) );
- return( ber_put_ostring( ber, str, strlen( str ), tag ));
+ return ber_put_ostring( ber, str, strlen( str ), tag );
}
int
assert( BER_VALID( ber ) );
- if ( tag == LBER_DEFAULT )
+ if ( tag == LBER_DEFAULT ) {
tag = LBER_BITSTRING;
+ }
- if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
- return( -1 );
+ if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
+ return -1;
+ }
len = ( blen + 7 ) / 8;
unusedbits = (unsigned char) ((len * 8) - blen);
- if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
- return( -1 );
+ if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 ) {
+ return -1;
+ }
- if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
- return( -1 );
+ if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 ) {
+ return -1;
+ }
- if ( (ber_len_t) ber_write( ber, str, len, 0 ) != len )
- return( -1 );
+ if ( (ber_len_t) ber_write( ber, str, len, 0 ) != len ) {
+ return -1;
+ }
/* return length of tag + length + unused bit count + contents */
- return( taglen + 1 + lenlen + len );
+ return taglen + 1 + lenlen + len;
}
int
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( tag == LBER_DEFAULT )
+ if ( tag == LBER_DEFAULT ) {
tag = LBER_NULL;
+ }
- if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
- return( -1 );
+ if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
+ return -1;
+ }
- if ( ber_put_len( ber, 0, 0 ) != 1 )
- return( -1 );
+ if ( ber_put_len( ber, 0, 0 ) != 1 ) {
+ return -1;
+ }
- return( taglen + 1 );
+ return taglen + 1;
}
int
ber_tag_t tag )
{
ber_len_t taglen;
- unsigned char trueval = 0xFFU;
- unsigned char falseval = 0x00U;
+ unsigned char trueval = (unsigned char) -1;
+ unsigned char falseval = 0;
assert( ber != NULL );
assert( BER_VALID( ber ) );
if ( tag == LBER_DEFAULT )
tag = LBER_BOOLEAN;
- if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
- return( -1 );
+ if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
+ return -1;
+ }
- if ( ber_put_len( ber, 1, 0 ) != 1 )
- return( -1 );
+ if ( ber_put_len( ber, 1, 0 ) != 1 ) {
+ return -1;
+ }
if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
- != 1 )
- return( -1 );
+ != 1 )
+ {
+ return -1;
+ }
- return( taglen + 2 );
+ return taglen + 2;
}
#define FOUR_BYTE_LEN 5
new = (Seqorset *) LBER_CALLOC( 1, sizeof(Seqorset) );
- if ( new == NULL )
- return( -1 );
+ if ( new == NULL ) {
+ return -1;
+ }
new->sos_ber = ber;
- if ( ber->ber_sos == NULL )
+ if ( ber->ber_sos == NULL ) {
new->sos_first = ber->ber_ptr;
- else
+ } else {
new->sos_first = ber->ber_sos->sos_ptr;
+ }
/* Set aside room for a 4 byte length field */
new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
new->sos_next = ber->ber_sos;
ber->ber_sos = new;
- return( 0 );
+ return 0;
}
int
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( tag == LBER_DEFAULT )
+ if ( tag == LBER_DEFAULT ) {
tag = LBER_SEQUENCE;
+ }
- return( ber_start_seqorset( ber, tag ) );
+ return ber_start_seqorset( ber, tag );
}
int
assert( ber != NULL );
assert( BER_VALID( ber ) );
- if ( tag == LBER_DEFAULT )
+ if ( tag == LBER_DEFAULT ) {
tag = LBER_SET;
+ }
- return( ber_start_seqorset( ber, tag ) );
+ return ber_start_seqorset( ber, tag );
}
static int
ber_put_seqorset( BerElement *ber )
{
- ber_len_t len, netlen;
+ int rc;
+ ber_len_t len;
+ unsigned char netlen[sizeof(ber_len_t)];
ber_len_t taglen, lenlen;
unsigned char ltag = 0x80U + FOUR_BYTE_LEN - 1;
Seqorset *next;
*/
len = (*sos)->sos_clen;
- netlen = LBER_LEN_HTON( len );
- if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL )
- return( -1 );
+
+ if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL ) {
+ return -1;
+ }
if ( ber->ber_options & LBER_USE_DER ) {
lenlen = ber_calc_lenlen( len );
+
} else {
lenlen = FOUR_BYTE_LEN;
}
+ if( lenlen > 1 ) {
+ ber_len_t i;
+ for( i=0; i < lenlen-1; i++ ) {
+ netlen[(sizeof(ber_len_t)-1) - i] =
+ (unsigned char)((len >> i*8) & 0xffU);
+ }
+ } else {
+ netlen[sizeof(ber_len_t)-1] = (unsigned char)(len & 0x7fU);
+ }
+
if ( (next = (*sos)->sos_next) == NULL ) {
/* write the tag */
if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
if ( ber->ber_options & LBER_USE_DER ) {
/* Write the length in the minimum # of octets */
- if ( ber_put_len( ber, len, 1 ) == -1 )
- return( -1 );
+ if ( ber_put_len( ber, len, 1 ) == -1 ) {
+ return -1;
+ }
if (lenlen != FOUR_BYTE_LEN) {
/*
* the length field. Move the data if
* we don't actually need that much
*/
- SAFEMEMCPY( (*sos)->sos_first + taglen +
+ AC_MEMCPY( (*sos)->sos_first + taglen +
lenlen, (*sos)->sos_first + taglen +
FOUR_BYTE_LEN, len );
}
} else {
/* Fill FOUR_BYTE_LEN bytes for length field */
/* one byte of length length */
- if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 )
- return( -1 );
+ if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 ) {
+ return -1;
+ }
/* the length itself */
- if ( ber_write( ber, (char *) &netlen + sizeof(long)
- - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
- != FOUR_BYTE_LEN - 1 )
- return( -1 );
+ rc = ber_write( ber,
+ &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN-1)],
+ FOUR_BYTE_LEN-1, 1 );
+
+ if( rc != FOUR_BYTE_LEN - 1 ) {
+ return -1;
+ }
}
/* The ber_ptr is at the set/seq start - move it to the end */
(*sos)->sos_ber->ber_ptr += len;
+
} else {
- ber_tag_t ntag;
+ ber_len_t i;
+ unsigned char nettag[sizeof(ber_tag_t)];
+ ber_tag_t tmptag = (*sos)->sos_tag;
+
+ if( ber->ber_sos->sos_ptr > ber->ber_end ) {
+ /* The sos_ptr exceeds the end of the BerElement
+ * this can happen, for example, when the sos_ptr
+ * is near the end and no data was written for the
+ * 'V'. We must realloc the BerElement to ensure
+ * we don't overwrite the buffer when writing
+ * the tag and length fields.
+ */
+ ber_len_t ext = ber->ber_sos->sos_ptr - ber->ber_end;
+
+ if( ber_realloc( ber, ext ) != 0 ) {
+ return -1;
+ }
+ }
/* the tag */
- taglen = ber_calc_taglen( (*sos)->sos_tag );
- ntag = LBER_TAG_HTON( (*sos)->sos_tag );
- SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
- sizeof(ber_tag_t) - taglen, taglen );
+ taglen = ber_calc_taglen( tmptag );
+
+ for( i = 0; i < taglen; i++ ) {
+ nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tmptag & 0xffU);
+ tmptag >>= 8;
+ }
+
+ AC_FMEMCPY( (*sos)->sos_first,
+ &nettag[sizeof(ber_tag_t) - taglen],
+ taglen );
if ( ber->ber_options & LBER_USE_DER ) {
ltag = (lenlen == 1)
}
/* one byte of length length */
- SAFEMEMCPY( (*sos)->sos_first + 1, <ag, 1 );
+ (*sos)->sos_first[1] = ltag;
if ( ber->ber_options & LBER_USE_DER ) {
if (lenlen > 1) {
/* Write the length itself */
- SAFEMEMCPY( (*sos)->sos_first + 2,
- (char *)&netlen + sizeof(ber_len_t) -
- (lenlen - 1),
- lenlen - 1 );
+ AC_FMEMCPY( (*sos)->sos_first + 2,
+ &netlen[sizeof(ber_len_t) - (lenlen - 1)],
+ lenlen - 1 );
}
if (lenlen != FOUR_BYTE_LEN) {
/*
* the length field. Move the data if
* we don't actually need that much
*/
- SAFEMEMCPY( (*sos)->sos_first + taglen +
+ AC_FMEMCPY( (*sos)->sos_first + taglen +
lenlen, (*sos)->sos_first + taglen +
FOUR_BYTE_LEN, len );
}
} else {
/* the length itself */
- SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
- (char *) &netlen + sizeof(ber_len_t) -
- (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
+ AC_FMEMCPY( (*sos)->sos_first + taglen + 1,
+ &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN - 1)],
+ FOUR_BYTE_LEN - 1 );
}
next->sos_clen += (taglen + lenlen + len);
LBER_FREE( (char *) (*sos) );
*sos = next;
- return( taglen + lenlen + len );
+ return taglen + lenlen + len;
}
int
assert( ber != NULL );
assert( BER_VALID( ber ) );
- return( ber_put_seqorset( ber ) );
+ return ber_put_seqorset( ber );
}
int
assert( ber != NULL );
assert( BER_VALID( ber ) );
- return( ber_put_seqorset( ber ) );
+ return ber_put_seqorset( ber );
}
+/* N tag */
+static ber_tag_t lber_int_null = 0;
+
/* VARARGS */
int
-ber_printf
-#ifdef HAVE_STDARG
- ( BerElement *ber,
- LDAP_CONST char *fmt,
- ... )
-#else
- ( va_alist )
-va_dcl
-#endif
+ber_printf( BerElement *ber, LDAP_CONST char *fmt, ... )
{
va_list ap;
-#ifndef HAVE_STDARG
- BerElement *ber;
- char *fmt;
-#endif
char *s, **ss;
struct berval *bv, **bvp;
int rc;
ber_int_t i;
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 );
assert( BER_VALID( ber ) );
+ va_start( ap, fmt );
+
for ( rc = 0; *fmt && rc != -1; fmt++ ) {
switch ( *fmt ) {
case '!': { /* hook */
rc = ber_put_null( ber, ber->ber_tag );
break;
+ case 'N': /* Debug NULL */
+ if( lber_int_null != 0 ) {
+ /* Insert NULL to ensure peer ignores unknown tags */
+ rc = ber_put_null( ber, lber_int_null );
+ } else {
+ rc = 0;
+ }
+ break;
+
case 'o': /* octet string (non-null terminated) */
s = va_arg( ap, char * );
len = va_arg( ap, ber_len_t );
break;
case 'B': /* bit string */
+ case 'X': /* bit string (deprecated) */
s = va_arg( ap, char * );
len = va_arg( ap, int ); /* in bits */
rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
va_end( ap );
- return( rc );
+ return rc;
}