/* 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 "lber-int.h"
-static int ber_put_len LDAP_P(( BerElement *ber,
- unsigned long len, int nosos ));
+static int ber_put_len LDAP_P((
+ BerElement *ber,
+ ber_len_t len,
+ int nosos ));
-static int ber_start_seqorset LDAP_P(( BerElement *ber,
- unsigned long tag ));
+static int ber_start_seqorset LDAP_P((
+ BerElement *ber,
+ ber_tag_t tag ));
static int ber_put_seqorset LDAP_P(( BerElement *ber ));
-static int ber_put_int_or_enum LDAP_P(( BerElement *ber,
- long num, unsigned long tag ));
+static int ber_put_int_or_enum LDAP_P((
+ BerElement *ber,
+ ber_int_t num,
+ ber_tag_t tag ));
-static int
-ber_calc_taglen( unsigned long tag )
+static ber_len_t
+ber_calc_taglen( ber_tag_t tag )
{
int i;
- unsigned long mask;
+ ber_tag_t mask;
/* find the first non-all-zero byte in the tag */
- for ( i = sizeof(long) - 1; i > 0; i-- ) {
- mask = (0xffUL << (i * 8));
+ for ( i = sizeof(ber_tag_t) - 1; i > 0; i-- ) {
+ mask = ((ber_tag_t)0xffU << (i * 8));
/* not all zero */
if ( tag & mask )
break;
}
static int
-ber_put_tag( BerElement *ber, unsigned long tag, int nosos )
+ber_put_tag(
+ BerElement *ber,
+ ber_tag_t tag,
+ int nosos )
{
- int taglen;
- unsigned long ntag;
+ int rc;
+ ber_len_t taglen;
+ ber_len_t i;
+ unsigned char nettag[sizeof(ber_tag_t)];
assert( ber != NULL );
assert( BER_VALID( ber ) );
taglen = ber_calc_taglen( tag );
- ntag = AC_HTONL( tag );
+ for( i=0; i<taglen; i++ ) {
+ nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tag & 0xffU);
+ tag >>= 8;
+ }
+
+ rc = ber_write( ber,
+ &nettag[sizeof(ber_tag_t) - taglen],
+ taglen, nosos );
- return( ber_write( ber, ((char *) &ntag) + sizeof(long) - taglen,
- taglen, nosos ) );
+ return rc;
}
-static int
-ber_calc_lenlen( unsigned long len )
+static ber_len_t
+ber_calc_lenlen( ber_len_t len )
{
/*
* short len if it's less than 128 - one byte giving the len,
* with bit 8 0.
*/
- if ( len <= 0x7FUL )
+ if ( len <= (ber_len_t) 0x7FU )
return( 1 );
/*
* length of the length, followed by the length itself.
*/
- if ( len <= 0xffUL )
+ if ( len <= (ber_len_t) 0xffU )
return( 2 );
- if ( len <= 0xffffUL )
+ if ( len <= (ber_len_t) 0xffffU )
return( 3 );
- if ( len <= 0xffffffUL )
+ if ( len <= (ber_len_t) 0xffffffU )
return( 4 );
return( 5 );
}
static int
-ber_put_len( BerElement *ber, unsigned long len, int nosos )
+ber_put_len( BerElement *ber, ber_len_t len, int nosos )
{
- int i;
+ int rc;
+ int i,j;
char lenlen;
- unsigned long mask;
- unsigned long netlen;
+ ber_len_t mask;
+ unsigned char netlen[sizeof(ber_len_t)];
assert( ber != NULL );
assert( BER_VALID( ber ) );
*/
if ( len <= 127 ) {
- netlen = AC_HTONL( 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(long) - 1; i > 0; i-- ) {
- mask = (0xffUL << (i * 8));
+ for ( i = sizeof(ber_len_t) - 1; i > 0; i-- ) {
+ mask = ((ber_len_t)0xffU << (i * 8));
/* not all zero */
if ( len & mask )
break;
lenlen = (unsigned char) ++i;
if ( lenlen > 4 )
return( -1 );
+
lenlen |= 0x80UL;
/* write the length of the length */
if ( ber_write( ber, &lenlen, 1, nosos ) != 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 = AC_HTONL( len );
- if ( ber_write( ber, (char *) &netlen + (sizeof(long) - 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_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
+ber_put_int_or_enum(
+ BerElement *ber,
+ ber_int_t num,
+ ber_tag_t tag )
{
- int i, sign, taglen;
- int len, lenlen;
- long netnum, mask;
+ int rc;
+ int i, j, sign;
+ ber_len_t len, lenlen, taglen;
+ 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(long) - 1; i > 0; i-- ) {
- mask = (0xffUL << (i * 8));
+ for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
+ mask = ((ber_uint_t)0xffU << (i * 8));
if ( sign ) {
/* not all ones */
- if ( (num & mask) != mask )
+ if ( (unum & mask) != mask )
break;
} else {
/* not all zero */
- if ( num & mask )
+ 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 & (0x80UL << (i * 8)));
+ mask = (unum & ((ber_uint_t)0x80U << (i * 8)));
if ( (mask && !sign) || (sign && !mask) )
i++;
if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
return( -1 );
i++;
- netnum = AC_HTONL( num );
- if ( ber_write( ber, (char *) &netnum + (sizeof(long) - 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
-ber_put_enum( BerElement *ber, long num, unsigned long tag )
+ber_put_enum(
+ BerElement *ber,
+ ber_int_t num,
+ ber_tag_t tag )
{
assert( ber != NULL );
assert( BER_VALID( ber ) );
}
int
-ber_put_int( BerElement *ber, long num, unsigned long tag )
+ber_put_int(
+ BerElement *ber,
+ ber_int_t num,
+ ber_tag_t tag )
{
assert( ber != NULL );
assert( BER_VALID( ber ) );
ber_put_ostring(
BerElement *ber,
LDAP_CONST char *str,
- unsigned long len,
- unsigned long tag )
+ ber_len_t len,
+ ber_tag_t tag )
{
- int taglen, lenlen, rc;
+ ber_len_t taglen, lenlen;
+ int rc;
#ifdef STR_TRANSLATION
int free_str;
#endif /* STR_TRANSLATION */
#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 ) {
+ if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 ) != 0 ) {
return( -1 );
}
free_str = 1;
#endif /* STR_TRANSLATION */
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 */
return( rc );
}
+
int
ber_put_berval(
BerElement *ber,
LDAP_CONST struct berval *bv,
- unsigned long tag )
+ ber_tag_t tag )
{
assert( ber != NULL );
- assert( bv != NULL );
-
assert( BER_VALID( ber ) );
- if( bv == NULL ) {
- return -1;
+ if( bv == NULL || bv->bv_len == 0 ) {
+ return ber_put_ostring( ber, "", (ber_len_t) 0, tag );
}
return ber_put_ostring( ber, bv->bv_val, bv->bv_len, tag );
ber_put_string(
BerElement *ber,
LDAP_CONST char *str,
- unsigned long tag )
+ ber_tag_t tag )
{
assert( ber != NULL );
assert( str != NULL );
ber_put_bitstring(
BerElement *ber,
LDAP_CONST char *str,
- unsigned long blen /* in bits */,
- unsigned long tag )
+ ber_len_t blen /* in bits */,
+ ber_tag_t tag )
{
- int taglen, lenlen, len;
+ ber_len_t taglen, lenlen, len;
unsigned char unusedbits;
assert( ber != NULL );
if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
return( -1 );
- if ( ber_write( ber, str, len, 0 ) != len )
+ if ( (ber_len_t) ber_write( ber, str, len, 0 ) != len )
return( -1 );
/* return length of tag + length + unused bit count + contents */
}
int
-ber_put_null( BerElement *ber, unsigned long tag )
+ber_put_null( BerElement *ber, ber_tag_t tag )
{
- int taglen;
+ ber_len_t taglen;
assert( ber != NULL );
assert( BER_VALID( ber ) );
}
int
-ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
+ber_put_boolean(
+ BerElement *ber,
+ ber_int_t boolval,
+ ber_tag_t tag )
{
- int taglen;
- unsigned char trueval = 0xffU;
- unsigned char falseval = 0x00U;
+ ber_len_t taglen;
+ unsigned char trueval = (unsigned char) -1;
+ unsigned char falseval = 0;
assert( ber != NULL );
assert( BER_VALID( ber ) );
#define FOUR_BYTE_LEN 5
static int
-ber_start_seqorset( BerElement *ber, unsigned long tag )
+ber_start_seqorset(
+ BerElement *ber,
+ ber_tag_t tag )
{
Seqorset *new;
new = (Seqorset *) LBER_CALLOC( 1, sizeof(Seqorset) );
- if ( new == NULLSEQORSET )
+ if ( new == NULL )
return( -1 );
new->sos_ber = ber;
- if ( ber->ber_sos == NULLSEQORSET )
+ if ( ber->ber_sos == NULL )
new->sos_first = ber->ber_ptr;
else
new->sos_first = ber->ber_sos->sos_ptr;
}
int
-ber_start_seq( BerElement *ber, unsigned long tag )
+ber_start_seq( BerElement *ber, ber_tag_t tag )
{
assert( ber != NULL );
assert( BER_VALID( ber ) );
}
int
-ber_start_set( BerElement *ber, unsigned long tag )
+ber_start_set( BerElement *ber, ber_tag_t tag )
{
assert( ber != NULL );
assert( BER_VALID( ber ) );
static int
ber_put_seqorset( BerElement *ber )
{
- unsigned long len, netlen;
- int taglen, lenlen;
+ 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;
Seqorset **sos = &ber->ber_sos;
*/
len = (*sos)->sos_clen;
- netlen = AC_HTONL( len );
- if ( sizeof(long) > 4 && len > 0xffffffffUL )
+
+ 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 ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
+ 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 )
return( -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 )
+ 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 {
- unsigned long 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 = AC_HTONL( (*sos)->sos_tag );
- SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
- sizeof(long) - 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;
+ }
+
+ SAFEMEMCPY( (*sos)->sos_first,
+ &nettag[sizeof(ber_tag_t) - taglen],
+ taglen );
if ( ber->ber_options & LBER_USE_DER ) {
ltag = (lenlen == 1)
? (unsigned char) len
- : (unsigned char) (0x80UL + (lenlen - 1));
+ : (unsigned char) (0x80U + (lenlen - 1));
}
/* one byte of length length */
if (lenlen > 1) {
/* Write the length itself */
SAFEMEMCPY( (*sos)->sos_first + 2,
- (char *)&netlen + sizeof(unsigned long) -
- (lenlen - 1),
- lenlen - 1 );
+ &netlen[sizeof(ber_len_t) - (lenlen - 1)],
+ lenlen - 1 );
}
if (lenlen != FOUR_BYTE_LEN) {
/*
} else {
/* the length itself */
SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
- (char *) &netlen + sizeof(long) -
- (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
+ &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN - 1)],
+ FOUR_BYTE_LEN - 1 );
}
next->sos_clen += (taglen + lenlen + len);
/* 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, i;
- unsigned long len;
-
-#ifdef HAVE_STDARG
- va_start( ap, fmt );
-#else
- va_start( ap );
- ber = va_arg( ap, BerElement * );
- fmt = va_arg( ap, char * );
-#endif
+ int rc;
+ ber_int_t i;
+ ber_len_t len;
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 */
} break;
case 'b': /* boolean */
- i = va_arg( ap, int );
+ i = va_arg( ap, ber_int_t );
rc = ber_put_boolean( ber, i, ber->ber_tag );
break;
case 'i': /* int */
- i = va_arg( ap, int );
+ i = va_arg( ap, ber_int_t );
rc = ber_put_int( ber, i, ber->ber_tag );
break;
case 'e': /* enumeration */
- i = va_arg( ap, int );
+ i = va_arg( ap, ber_int_t );
rc = ber_put_enum( ber, i, ber->ber_tag );
break;
case 'o': /* octet string (non-null terminated) */
s = va_arg( ap, char * );
- len = va_arg( ap, int );
+ len = va_arg( ap, ber_len_t );
rc = ber_put_ostring( ber, s, len, ber->ber_tag );
break;
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 );
break;
case 't': /* tag for the next element */
- ber->ber_tag = va_arg( ap, unsigned long );
+ ber->ber_tag = va_arg( ap, ber_tag_t );
ber->ber_usertag = 1;
break;
if ( (bvp = va_arg( ap, struct berval ** )) == NULL )
break;
for ( i = 0; bvp[i] != NULL; i++ ) {
- if ( (rc = ber_put_ostring( ber, bvp[i]->bv_val,
- bvp[i]->bv_len, ber->ber_tag )) == -1 )
+ if ( (rc = ber_put_berval( ber, bvp[i],
+ ber->ber_tag )) == -1 )
break;
}
break;