From 3a5e383dbd02a9641c8cd6d693691e6c8acd9eef Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Fri, 18 Dec 1998 21:08:28 +0000 Subject: [PATCH] Add draft-ietf-ldapext-ldap-c-api-01 routines ber_init() and ber_flatten() to the -llber. --- include/lber.h | 4 +-- libraries/liblber/decode.c | 55 +++++++++++++++++++++++++++--- libraries/liblber/io.c | 70 +++++++++++++++++++++++++++++++++++++- 3 files changed, 122 insertions(+), 7 deletions(-) diff --git a/include/lber.h b/include/lber.h index 1696fe8c9a..559f43b9e9 100644 --- a/include/lber.h +++ b/include/lber.h @@ -40,7 +40,7 @@ LDAP_BEGIN_DECL */ #define LBER_ERROR 0xffffffffL #define LBER_DEFAULT 0xffffffffL -#define LBER_END_SEQORSET 0xfffffffeL +/* #define LBER_END_SEQORSET 0xfffffffeL *//* no part of LDAP C-API */ /* general BER types we know about */ #define LBER_BOOLEAN 0x01L @@ -168,7 +168,7 @@ LDAP_F void ber_init_w_nullc LDAP_P(( BerElement *ber, int options )); LDAP_F void ber_reset LDAP_P(( BerElement *ber, int was_writing )); /* - * LDAPv3 routines (not yet implemented) + * LDAP draft-ietf-ldapext-ldap-c-api-01 routines */ LDAP_F BerElement *ber_init LDAP_P(( struct berval *bv )); LDAP_F int ber_flatten LDAP_P(( BerElement *ber, struct berval **bvPtr )); diff --git a/libraries/liblber/decode.c b/libraries/liblber/decode.c index c36ab991be..4a15d47a38 100644 --- a/libraries/liblber/decode.c +++ b/libraries/liblber/decode.c @@ -344,7 +344,11 @@ ber_first_element( BerElement *ber, unsigned long *len, char **last ) *last = ber->ber_ptr + *len; if ( *last == ber->ber_ptr ) { +#ifdef LBER_END_SEQORSET + return( LBER_END_SEQORSET ); +#else return( LBER_DEFAULT ); +#endif } return( ber_peek_tag( ber, len ) ); @@ -354,7 +358,11 @@ unsigned long ber_next_element( BerElement *ber, unsigned long *len, char *last ) { if ( ber->ber_ptr == last ) { +#ifdef LBER_END_SEQORSET + return( LBER_END_SEQORSET ); +#else return( LBER_DEFAULT ); +#endif } return( ber_peek_tag( ber, len ) ); @@ -462,8 +470,13 @@ va_dcl *sss = NULL; j = 0; for ( tag = ber_first_element( ber, &len, &last ); - tag != LBER_DEFAULT && rc != LBER_DEFAULT; - tag = ber_next_element( ber, &len, last ) ) { + tag != LBER_DEFAULT && +#ifdef LDAP_END_SEQORSET + tag != LBER_END_SEQORSET && +#endif + rc != LBER_DEFAULT; + tag = ber_next_element( ber, &len, last ) ) + { if ( *sss == NULL ) { *sss = (char **) malloc( 2 * sizeof(char *) ); @@ -474,6 +487,13 @@ va_dcl rc = ber_get_stringa( ber, &((*sss)[j]) ); j++; } +#ifdef LDAP_END_SEQORSET + if (rc != LBER_DEFAULT && + tag != LBER_END_SEQORSET ) + { + rc = LBER_DEFAULT; + } +#endif if ( j > 0 ) (*sss)[j] = NULL; break; @@ -483,8 +503,13 @@ va_dcl *bv = NULL; j = 0; for ( tag = ber_first_element( ber, &len, &last ); - tag != LBER_DEFAULT && rc != LBER_DEFAULT; - tag = ber_next_element( ber, &len, last ) ) { + tag != LBER_DEFAULT && +#ifdef LDAP_END_SEQORSET + tag != LBER_END_SEQORSET && +#endif + rc != LBER_DEFAULT; + tag = ber_next_element( ber, &len, last ) ) + { if ( *bv == NULL ) { *bv = (struct berval **) malloc( 2 * sizeof(struct berval *) ); @@ -495,6 +520,13 @@ va_dcl rc = ber_get_stringal( ber, &((*bv)[j]) ); j++; } +#ifdef LDAP_END_SEQORSET + if (rc != LBER_DEFAULT && + tag != LBER_END_SEQORSET ) + { + rc = LBER_DEFAULT; + } +#endif if ( j > 0 ) (*bv)[j] = NULL; break; @@ -532,6 +564,9 @@ va_dcl void ber_bvfree( struct berval *bv ) { +#ifdef LBER_ASSERT + assert(bv != NULL); /* bv damn better point to something */ +#endif if ( bv->bv_val != NULL ) free( bv->bv_val ); free( (char *) bv ); @@ -542,6 +577,9 @@ ber_bvecfree( struct berval **bv ) { int i; +#ifdef LBER_ASSERT + assert(bv != NULL); /* bv damn better point to something */ +#endif for ( i = 0; bv[i] != NULL; i++ ) ber_bvfree( bv[i] ); free( (char *) bv ); @@ -556,9 +594,18 @@ ber_bvdup( struct berval *bv ) == NULL ) { return( NULL ); } + + if ( bv->bv_val == NULL ) { + new->bv_val = NULL; + new->bv_len = 0; + return ( new ); + } + if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) { + free( new ); return( NULL ); } + SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len ); new->bv_val[bv->bv_len] = '\0'; new->bv_len = bv->bv_len; diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c index b9cc3fe033..d54047750d 100644 --- a/libraries/liblber/io.c +++ b/libraries/liblber/io.c @@ -365,6 +365,7 @@ ber_dup( BerElement *ber ) } +/* OLD U-Mich ber_init() */ void ber_init_w_nullc( BerElement *ber, int options ) { @@ -373,12 +374,79 @@ ber_init_w_nullc( BerElement *ber, int options ) ber->ber_options = (char) options; } +/* New C-API ber_init() */ +/* This function constructs a BerElement containing a copy +** of the data in the bv argument. +*/ BerElement * ber_init( struct berval *bv ) { - return ( NULL ); + BerElement *ber; + + if ( bv == NULL ) { + return NULL; + } + + ber = ber_alloc_t( 0 ); + + if( ber == NULLBER ) { + /* allocation failed */ + return ( NULL ); + } + + /* copy the data */ + if ( (ber_write ( ber, bv->bv_val, bv->bv_len, 0 )) != bv->bv_len ) { + /* write failed, so free and return NULL */ + ber_free( ber, 1 ); + return( NULL ); + } + + ber_reset( ber, 1 ); /* reset the pointer to the start of the buffer */ + + return ( ber ); } +/* New C-API ber_flatten routine */ +/* This routine allocates a struct berval whose contents are a BER +** encoding taken from the ber argument. The bvPtr pointer pointers to +** the returned berval. +*/ +int ber_flatten( + BerElement *ber, + struct berval **bvPtr) +{ + struct berval *bv; + + if(bvPtr == NULL) { + return( -1 ); + } + + if ( (bv = malloc( sizeof(struct berval))) == NULL ) { + return( -1 ); + } + + if ( ber == NULL ) { + /* ber is null, create an empty berval */ + bv->bv_val = NULL; + bv->bv_len = 0; + + } else { + /* copy the berval */ + ptrdiff_t len = ber->ber_ptr - ber->ber_buf; + + if ( (bv->bv_val = (char *) malloc( len + 1 )) == NULL ) { + ber_bvfree( bv ); + return( -1 ); + } + + SAFEMEMCPY( bv->bv_val, ber->ber_buf, (size_t)len ); + bv->bv_val[len] = '\0'; + bv->bv_len = len; + } + + *bvPtr = bv; + return( 0 ); +} void ber_reset( BerElement *ber, int was_writing ) -- 2.39.5