From: Howard Chu Date: Wed, 18 Dec 2002 19:15:27 +0000 (+0000) Subject: Added ber_flatten2 - uses passed in struct berval, allocates copy or X-Git-Tag: NO_SLAP_OP_BLOCKS~659 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=6a37f1ae8087e609c1f321d3c151251ecce487a1;p=openldap Added ber_flatten2 - uses passed in struct berval, allocates copy or directly re-uses existing ber buf. --- diff --git a/include/lber.h b/include/lber.h index aa952bd2c3..af9a4bff85 100644 --- a/include/lber.h +++ b/include/lber.h @@ -469,6 +469,12 @@ ber_flatten LDAP_P(( BerElement *ber, struct berval **bvPtr )); +LBER_F( int ) +ber_flatten2 LDAP_P(( + BerElement *ber, + struct berval *bv, + int alloc )); + /* * LBER ber accessor functions */ diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c index 74abb24fd4..a63d237f22 100644 --- a/libraries/liblber/io.c +++ b/libraries/liblber/io.c @@ -362,24 +362,21 @@ ber_init( struct berval *bv ) /* 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 +** encoding taken from the ber argument. The bvPtr pointer points to ** the returned berval. +** +** ber_flatten2 is the same, but uses a struct berval passed by +** the caller. If alloc is 0 the returned bv uses the ber buf directly. */ -int ber_flatten( +int ber_flatten2( BerElement *ber, - struct berval **bvPtr) + struct berval *bv, + int alloc ) { - struct berval *bv; - - assert( bvPtr != NULL ); - - ber_int_options.lbo_valid = LBER_INITIALIZED; + assert( bv != NULL ); - if(bvPtr == NULL) { - return -1; - } + ber_int_options.lbo_valid = LBER_INITIALIZED; - bv = LBER_MALLOC( sizeof(struct berval) ); if ( bv == NULL ) { return -1; } @@ -393,21 +390,49 @@ int ber_flatten( /* copy the berval */ ber_len_t len = ber_pvt_ber_write( ber ); - bv->bv_val = (char *) LBER_MALLOC( len + 1 ); - if ( bv->bv_val == NULL ) { - LBER_FREE( bv ); - return -1; + if ( alloc ) { + bv->bv_val = (char *) LBER_MALLOC( len + 1 ); + if ( bv->bv_val == NULL ) { + return -1; + } + AC_MEMCPY( bv->bv_val, ber->ber_buf, len ); + } else { + bv->bv_val = ber->ber_buf; } - - AC_MEMCPY( bv->bv_val, ber->ber_buf, len ); bv->bv_val[len] = '\0'; bv->bv_len = len; } - - *bvPtr = bv; return 0; } +int ber_flatten( + BerElement *ber, + struct berval **bvPtr) +{ + struct berval *bv; + int rc; + + assert( bvPtr != NULL ); + + ber_int_options.lbo_valid = LBER_INITIALIZED; + + if(bvPtr == NULL) { + return -1; + } + + bv = LBER_MALLOC( sizeof(struct berval) ); + if ( bv == NULL ) { + return -1; + } + rc = ber_flatten2(ber, bv, 1); + if (rc == -1) { + LBER_FREE(bv); + } else { + *bvPtr = bv; + } + return rc; +} + void ber_reset( BerElement *ber, int was_writing ) {