1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-05-29 09:19:14 +08:00
|
|
|
/*
|
2000-05-13 10:36:07 +08:00
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
1999-05-29 09:19:14 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
#include "portable.h"
|
|
|
|
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1999-05-30 02:57:23 +08:00
|
|
|
#include <ac/string.h>
|
1999-05-29 09:19:14 +08:00
|
|
|
|
|
|
|
#include "lber-int.h"
|
|
|
|
|
2000-07-05 02:47:06 +08:00
|
|
|
#ifdef LDAP_MEMORY_TRACE
|
|
|
|
# ifndef LDAP_MEMORY_DEBUG
|
|
|
|
# define LDAP_MEMORY_DEBUG 1
|
|
|
|
# endif
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
1999-06-08 02:48:22 +08:00
|
|
|
#if LDAP_MEMORY_DEBUG
|
2000-02-17 07:51:34 +08:00
|
|
|
/*
|
|
|
|
* LDAP_MEMORY_DEBUG should only be enabled for the purposes of
|
|
|
|
* debugging memory management within OpenLDAP libraries and slapd.
|
|
|
|
* It should only be enabled by an experienced developer as it
|
|
|
|
* causes the inclusion of numerous assert()'s, many of which may
|
|
|
|
* be triggered by a prefectly valid program.
|
|
|
|
*
|
|
|
|
* The code behind this macro is subject to change as needed to
|
|
|
|
* support this testing.
|
|
|
|
*/
|
|
|
|
|
1999-06-03 05:34:34 +08:00
|
|
|
struct ber_mem_hdr {
|
2000-07-05 02:47:06 +08:00
|
|
|
ber_int_t bm_top; /* Pattern to detect buf overrun from prev buffer */
|
|
|
|
ber_int_t bm_length; /* Length of user allocated area */
|
|
|
|
#ifdef LDAP_MEMORY_TRACE
|
|
|
|
ber_int_t bm_sequence; /* Allocation sequence number */
|
|
|
|
#endif
|
|
|
|
union bmu_align_u { /* Force alignment, pattern to detect back clobber */
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t bmu_len_t;
|
|
|
|
ber_tag_t bmu_tag_t;
|
|
|
|
ber_int_t bmu_int_t;
|
|
|
|
|
1999-06-03 05:34:34 +08:00
|
|
|
size_t bmu_size_t;
|
|
|
|
void * bmu_voidp;
|
|
|
|
double bmu_double;
|
|
|
|
long bmu_long;
|
1999-07-13 11:54:42 +08:00
|
|
|
long (*bmu_funcp)( double );
|
2000-07-05 02:47:06 +08:00
|
|
|
unsigned char bmu_char[4];
|
1999-06-03 05:34:34 +08:00
|
|
|
} ber_align;
|
1999-06-19 07:53:05 +08:00
|
|
|
#define bm_junk ber_align.bmu_len_t
|
1999-06-08 02:48:22 +08:00
|
|
|
#define bm_data ber_align.bmu_char[1]
|
2000-07-05 02:47:06 +08:00
|
|
|
#define bm_char ber_align.bmu_char
|
1999-06-03 05:34:34 +08:00
|
|
|
};
|
2000-07-05 02:47:06 +08:00
|
|
|
|
|
|
|
/* Pattern at top of allocated space */
|
2000-01-23 05:09:24 +08:00
|
|
|
#define BER_MEM_JUNK 0xdeaddadaU
|
2000-07-05 02:47:06 +08:00
|
|
|
|
|
|
|
static const struct ber_mem_hdr ber_int_mem_hdr = { BER_MEM_JUNK, 0, 0 };
|
|
|
|
|
|
|
|
/* Note sequence and ber_int_options.lbu_meminuse are counters, but are not
|
|
|
|
* thread safe. If you want to use these values for multithreaded applications,
|
|
|
|
* you must put mutexes around them, otherwise they will have incorrect values.
|
|
|
|
* When debugging, if you sort the debug output, the sequence number will
|
|
|
|
* put allocations/frees together. It is then a simple matter to write a script
|
|
|
|
* to find any allocations that don't have a buffer free function.
|
|
|
|
*/
|
|
|
|
#ifdef LDAP_MEMORY_TRACE
|
|
|
|
static ber_int_t sequence = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Pattern placed just before user data */
|
|
|
|
static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
|
|
|
|
/* Pattern placed just after user data */
|
|
|
|
static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
|
|
|
|
|
|
|
|
#define mbu_len sizeof(ber_int_mem_hdr.ber_align)
|
|
|
|
|
|
|
|
/* Test if pattern placed just before user data is good */
|
|
|
|
#define testdatatop(val) ( \
|
|
|
|
*(val->bm_char+mbu_len-4)==toppattern[0] && \
|
|
|
|
*(val->bm_char+mbu_len-3)==toppattern[1] && \
|
|
|
|
*(val->bm_char+mbu_len-2)==toppattern[2] && \
|
|
|
|
*(val->bm_char+mbu_len-1)==toppattern[3] )
|
|
|
|
|
|
|
|
/* Place pattern just before user data */
|
|
|
|
#define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
|
|
|
|
*(val->bm_char+mbu_len-3)=toppattern[1]; \
|
|
|
|
*(val->bm_char+mbu_len-2)=toppattern[2]; \
|
|
|
|
*(val->bm_char+mbu_len-1)=toppattern[3];
|
|
|
|
|
|
|
|
/* Test if pattern placed just after user data is good */
|
|
|
|
#define testend(val) ( *((unsigned char *)val+0)==endpattern[0] && \
|
|
|
|
*((unsigned char *)val+1)==endpattern[1] && \
|
|
|
|
*((unsigned char *)val+2)==endpattern[2] && \
|
|
|
|
*((unsigned char *)val+3)==endpattern[3] )
|
|
|
|
|
|
|
|
/* Place pattern just after user data */
|
|
|
|
#define setend(val) *((unsigned char *)val+0)=endpattern[0]; \
|
|
|
|
*((unsigned char *)val+1)=endpattern[1]; \
|
|
|
|
*((unsigned char *)val+2)=endpattern[2]; \
|
|
|
|
*((unsigned char *)val+3)=endpattern[3];
|
|
|
|
|
1999-06-08 02:48:22 +08:00
|
|
|
#define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
|
1999-06-08 03:33:08 +08:00
|
|
|
#define BER_MEM_VALID(p) do { \
|
|
|
|
assert( (p) != BER_MEM_BADADDR ); \
|
|
|
|
assert( (p) != (void *) &ber_int_mem_hdr ); \
|
1999-06-08 09:37:47 +08:00
|
|
|
} while(0)
|
2000-02-17 07:51:34 +08:00
|
|
|
|
1999-06-08 03:33:08 +08:00
|
|
|
#else
|
|
|
|
#define BER_MEM_VALID(p) /* no-op */
|
1999-06-03 05:34:34 +08:00
|
|
|
#endif
|
1999-05-31 07:00:52 +08:00
|
|
|
|
1999-06-03 05:34:34 +08:00
|
|
|
BerMemoryFunctions *ber_int_memory_fns = NULL;
|
1999-06-02 03:01:05 +08:00
|
|
|
|
1999-06-08 02:48:22 +08:00
|
|
|
#if 0 && defined( LDAP_MEMORY_DEBUG )
|
|
|
|
void
|
|
|
|
ber_int_memfree( void **p )
|
|
|
|
{
|
|
|
|
assert( p != NULL );
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( *p );
|
1999-06-08 02:48:22 +08:00
|
|
|
|
|
|
|
ber_memfree( p );
|
|
|
|
|
|
|
|
*p = BER_MEM_BADADDR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
void
|
|
|
|
ber_memfree( void *p )
|
|
|
|
{
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
1999-05-31 07:00:52 +08:00
|
|
|
|
1999-06-01 01:30:22 +08:00
|
|
|
if( p == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( p );
|
1999-06-08 03:33:08 +08:00
|
|
|
|
1999-05-31 07:00:52 +08:00
|
|
|
if( ber_int_memory_fns == NULL ) {
|
1999-06-03 05:34:34 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
|
|
|
struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
|
|
|
|
((char *)p - sizeof(struct ber_mem_hdr));
|
2000-07-05 02:47:06 +08:00
|
|
|
assert( mh->bm_top == BER_MEM_JUNK);
|
|
|
|
assert( testdatatop( mh));
|
|
|
|
assert( testend( (char *)&mh[1] + mh->bm_length) );
|
|
|
|
ber_int_options.lbo_meminuse -= mh->bm_length;
|
|
|
|
|
|
|
|
#ifdef LDAP_MEMORY_TRACE
|
|
|
|
fprintf(stderr, "0x%08x 0x%08x -f- %d ber_memfree %d\n",
|
|
|
|
mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
|
|
|
|
#endif
|
|
|
|
/* Fill the free space with poison */
|
2000-10-11 10:43:14 +08:00
|
|
|
memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
|
1999-06-03 05:34:34 +08:00
|
|
|
free( mh );
|
|
|
|
#else
|
1999-05-31 07:00:52 +08:00
|
|
|
free( p );
|
1999-06-03 05:34:34 +08:00
|
|
|
#endif
|
1999-05-31 07:00:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( ber_int_memory_fns->bmf_free );
|
|
|
|
|
|
|
|
(*ber_int_memory_fns->bmf_free)( p );
|
1999-05-29 09:19:14 +08:00
|
|
|
}
|
|
|
|
|
1999-06-02 03:01:05 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
ber_memvfree( void **vec )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1999-06-08 03:33:08 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
1999-06-08 02:48:22 +08:00
|
|
|
|
|
|
|
if( vec == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
1999-06-02 03:01:05 +08:00
|
|
|
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( vec );
|
1999-06-08 03:33:08 +08:00
|
|
|
|
1999-06-02 03:01:05 +08:00
|
|
|
for ( i = 0; vec[i] != NULL; i++ ) {
|
|
|
|
LBER_FREE( vec[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
LBER_FREE( vec );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
void *
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_memalloc( ber_len_t s )
|
1999-05-29 09:19:14 +08:00
|
|
|
{
|
2000-01-09 02:42:11 +08:00
|
|
|
void *new;
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
1999-05-31 07:00:52 +08:00
|
|
|
|
1999-06-08 02:48:22 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
1999-06-08 07:12:34 +08:00
|
|
|
assert( s != 0 );
|
1999-06-08 02:48:22 +08:00
|
|
|
#endif
|
1999-05-31 07:00:52 +08:00
|
|
|
|
1999-06-01 01:30:22 +08:00
|
|
|
if( s == 0 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-05-31 07:00:52 +08:00
|
|
|
if( ber_int_memory_fns == NULL ) {
|
1999-06-03 05:34:34 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
2000-07-05 02:47:06 +08:00
|
|
|
struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
|
1999-06-03 05:34:34 +08:00
|
|
|
if( mh == NULL ) return NULL;
|
|
|
|
|
2000-07-05 02:47:06 +08:00
|
|
|
mh->bm_top = BER_MEM_JUNK;
|
|
|
|
mh->bm_length = s;
|
|
|
|
setdatatop( mh);
|
|
|
|
setend( (char *)&mh[1] + mh->bm_length );
|
|
|
|
|
|
|
|
ber_int_options.lbo_meminuse += mh->bm_length; /* Count mem inuse */
|
|
|
|
|
|
|
|
#ifdef LDAP_MEMORY_TRACE
|
|
|
|
mh->bm_sequence = sequence++;
|
|
|
|
fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memalloc %d\n",
|
|
|
|
mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
|
|
|
|
#endif
|
|
|
|
/* poison new memory */
|
|
|
|
memset( (char *)&mh[1], 0xff, s);
|
1999-06-03 05:34:34 +08:00
|
|
|
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( &mh[1] );
|
2000-01-09 02:42:11 +08:00
|
|
|
new = &mh[1];
|
1999-06-03 05:34:34 +08:00
|
|
|
#else
|
2000-01-09 02:42:11 +08:00
|
|
|
new = malloc( s );
|
1999-06-03 05:34:34 +08:00
|
|
|
#endif
|
2000-01-09 02:42:11 +08:00
|
|
|
} else {
|
|
|
|
new = (*ber_int_memory_fns->bmf_malloc)( s );
|
1999-05-31 07:00:52 +08:00
|
|
|
}
|
|
|
|
|
2000-01-09 02:42:11 +08:00
|
|
|
if( new == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
|
|
|
}
|
1999-05-31 07:00:52 +08:00
|
|
|
|
2000-01-09 02:42:11 +08:00
|
|
|
return new;
|
1999-05-29 09:19:14 +08:00
|
|
|
}
|
|
|
|
|
1999-06-02 03:01:05 +08:00
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
void *
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_memcalloc( ber_len_t n, ber_len_t s )
|
1999-05-29 09:19:14 +08:00
|
|
|
{
|
2000-01-09 02:42:11 +08:00
|
|
|
void *new;
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
1999-05-31 07:00:52 +08:00
|
|
|
|
1999-06-08 02:48:22 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
1999-06-08 07:12:34 +08:00
|
|
|
assert( n != 0 && s != 0);
|
1999-06-08 02:48:22 +08:00
|
|
|
#endif
|
1999-05-31 07:00:52 +08:00
|
|
|
|
1999-06-01 01:30:22 +08:00
|
|
|
if( n == 0 || s == 0 ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-05-31 07:00:52 +08:00
|
|
|
if( ber_int_memory_fns == NULL ) {
|
1999-06-03 05:34:34 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
|
|
|
struct ber_mem_hdr *mh = calloc(1,
|
2000-07-05 02:47:06 +08:00
|
|
|
(n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
|
|
|
|
if( mh == NULL ) return NULL;
|
|
|
|
|
|
|
|
mh->bm_top = BER_MEM_JUNK;
|
|
|
|
mh->bm_length = n*s;
|
|
|
|
setdatatop( mh);
|
|
|
|
setend( (char *)&mh[1] + mh->bm_length );
|
1999-06-03 05:34:34 +08:00
|
|
|
|
2000-07-05 02:47:06 +08:00
|
|
|
ber_int_options.lbo_meminuse += mh->bm_length;
|
1999-06-08 03:33:08 +08:00
|
|
|
|
2000-07-05 02:47:06 +08:00
|
|
|
#ifdef LDAP_MEMORY_TRACE
|
|
|
|
mh->bm_sequence = sequence++;
|
|
|
|
fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memcalloc %d\n",
|
|
|
|
mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
|
|
|
|
#endif
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( &mh[1] );
|
2000-01-09 02:42:11 +08:00
|
|
|
new = &mh[1];
|
1999-06-03 05:34:34 +08:00
|
|
|
#else
|
2000-01-09 02:42:11 +08:00
|
|
|
new = calloc( n, s );
|
1999-06-03 05:34:34 +08:00
|
|
|
#endif
|
2000-01-09 02:42:11 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
new = (*ber_int_memory_fns->bmf_calloc)( n, s );
|
1999-05-31 07:00:52 +08:00
|
|
|
}
|
|
|
|
|
2000-01-09 02:42:11 +08:00
|
|
|
if( new == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
|
|
|
}
|
1999-05-31 07:00:52 +08:00
|
|
|
|
2000-01-09 02:42:11 +08:00
|
|
|
return new;
|
1999-05-29 09:19:14 +08:00
|
|
|
}
|
|
|
|
|
1999-06-02 03:01:05 +08:00
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
void *
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_memrealloc( void* p, ber_len_t s )
|
1999-05-29 09:19:14 +08:00
|
|
|
{
|
2000-06-10 08:17:55 +08:00
|
|
|
void *new = NULL;
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
1999-06-01 01:30:22 +08:00
|
|
|
/* realloc(NULL,s) -> malloc(s) */
|
1999-05-31 07:00:52 +08:00
|
|
|
if( p == NULL ) {
|
|
|
|
return ber_memalloc( s );
|
|
|
|
}
|
|
|
|
|
1999-06-01 01:30:22 +08:00
|
|
|
/* realloc(p,0) -> free(p) */
|
1999-05-31 07:00:52 +08:00
|
|
|
if( s == 0 ) {
|
|
|
|
ber_memfree( p );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-05-29 13:16:31 +08:00
|
|
|
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( p );
|
1999-06-08 03:33:08 +08:00
|
|
|
|
1999-05-31 07:00:52 +08:00
|
|
|
if( ber_int_memory_fns == NULL ) {
|
1999-06-03 05:34:34 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
2000-07-05 02:47:06 +08:00
|
|
|
ber_int_t oldlen;
|
1999-06-03 05:34:34 +08:00
|
|
|
struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
|
|
|
|
((char *)p - sizeof(struct ber_mem_hdr));
|
2000-07-05 02:47:06 +08:00
|
|
|
assert( mh->bm_top == BER_MEM_JUNK);
|
|
|
|
assert( testdatatop( mh));
|
|
|
|
assert( testend( (char *)&mh[1] + mh->bm_length) );
|
|
|
|
oldlen = mh->bm_length;
|
|
|
|
|
|
|
|
p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
|
|
|
|
if( p == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-06-03 05:34:34 +08:00
|
|
|
|
2000-01-09 02:42:11 +08:00
|
|
|
mh = p;
|
2000-07-05 02:47:06 +08:00
|
|
|
mh->bm_length = s;
|
|
|
|
setend( (char *)&mh[1] + mh->bm_length );
|
|
|
|
if( (s - oldlen) > 0 ) {
|
|
|
|
/* poison any new memory */
|
|
|
|
memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
|
|
|
|
}
|
1999-06-03 05:34:34 +08:00
|
|
|
|
2000-07-05 02:47:06 +08:00
|
|
|
assert( mh->bm_top == BER_MEM_JUNK);
|
|
|
|
assert( testdatatop( mh));
|
1999-06-03 05:34:34 +08:00
|
|
|
|
2000-07-05 02:47:06 +08:00
|
|
|
ber_int_options.lbo_meminuse += s - oldlen;
|
|
|
|
#ifdef LDAP_MEMORY_TRACE
|
|
|
|
fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memrealloc %d\n",
|
|
|
|
mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
|
|
|
|
#endif
|
2000-01-09 02:42:11 +08:00
|
|
|
BER_MEM_VALID( &mh[1] );
|
2000-07-05 02:47:06 +08:00
|
|
|
return &mh[1];
|
1999-06-03 05:34:34 +08:00
|
|
|
#else
|
2000-01-09 02:42:11 +08:00
|
|
|
new = realloc( p, s );
|
1999-06-03 05:34:34 +08:00
|
|
|
#endif
|
2000-01-09 02:42:11 +08:00
|
|
|
} else {
|
|
|
|
new = (*ber_int_memory_fns->bmf_realloc)( p, s );
|
1999-05-29 13:16:31 +08:00
|
|
|
}
|
1999-05-31 07:00:52 +08:00
|
|
|
|
2000-01-09 02:42:11 +08:00
|
|
|
if( new == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
|
|
|
}
|
1999-05-31 07:00:52 +08:00
|
|
|
|
2000-01-09 02:42:11 +08:00
|
|
|
return new;
|
1999-05-29 13:16:31 +08:00
|
|
|
}
|
|
|
|
|
1999-06-02 03:01:05 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
ber_bvfree( struct berval *bv )
|
|
|
|
{
|
1999-06-08 03:33:08 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
1999-06-08 02:48:22 +08:00
|
|
|
|
|
|
|
if( bv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
1999-06-02 03:01:05 +08:00
|
|
|
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( bv );
|
1999-06-02 03:01:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( bv->bv_val != NULL ) {
|
1999-06-02 03:01:05 +08:00
|
|
|
LBER_FREE( bv->bv_val );
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1999-06-02 03:01:05 +08:00
|
|
|
|
|
|
|
LBER_FREE( (char *) bv );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ber_bvecfree( struct berval **bv )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1999-06-08 03:33:08 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
1999-06-08 02:48:22 +08:00
|
|
|
|
|
|
|
if( bv == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
1999-06-02 03:01:05 +08:00
|
|
|
|
1999-06-08 09:41:38 +08:00
|
|
|
BER_MEM_VALID( bv );
|
1999-06-02 03:01:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
for ( i = 0; bv[i] != NULL; i++ ) {
|
1999-06-02 03:01:05 +08:00
|
|
|
ber_bvfree( bv[i] );
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1999-06-02 03:01:05 +08:00
|
|
|
|
|
|
|
LBER_FREE( (char *) bv );
|
|
|
|
}
|
|
|
|
|
2000-02-15 04:57:34 +08:00
|
|
|
int
|
|
|
|
ber_bvecadd( struct berval ***bvec, struct berval *bv )
|
|
|
|
{
|
|
|
|
ber_len_t i;
|
|
|
|
struct berval **new;
|
|
|
|
|
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
|
|
|
if( bvec == NULL ) {
|
|
|
|
if( bv == NULL ) {
|
|
|
|
/* nothing to add */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bvec = ber_memalloc( 2 * sizeof(struct berval *) );
|
|
|
|
|
|
|
|
if( *bvec == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*bvec)[0] = bv;
|
|
|
|
(*bvec)[1] = NULL;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BER_MEM_VALID( bvec );
|
|
|
|
|
|
|
|
/* count entries */
|
|
|
|
for ( i = 0; bvec[i] != NULL; i++ ) {
|
|
|
|
/* EMPTY */;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( bv == NULL ) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
new = ber_memrealloc( *bvec, (i+2) * sizeof(struct berval *));
|
|
|
|
|
|
|
|
if( new == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bvec = new;
|
|
|
|
|
|
|
|
(*bvec)[i++] = bv;
|
|
|
|
(*bvec)[i] = NULL;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
1999-06-02 03:01:05 +08:00
|
|
|
|
|
|
|
struct berval *
|
|
|
|
ber_bvdup(
|
|
|
|
LDAP_CONST struct berval *bv )
|
|
|
|
{
|
|
|
|
struct berval *new;
|
|
|
|
|
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
|
|
|
if( bv == NULL ) {
|
1999-12-06 14:33:26 +08:00
|
|
|
ber_errno = LBER_ERROR_PARAM;
|
1999-06-02 03:01:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
|
1999-12-06 14:33:26 +08:00
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
1999-06-02 03:01:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bv->bv_val == NULL ) {
|
|
|
|
new->bv_val = NULL;
|
|
|
|
new->bv_len = 0;
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(( new->bv_val = LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
|
1999-12-06 14:33:26 +08:00
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
1999-06-02 03:01:05 +08:00
|
|
|
LBER_FREE( new );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( new->bv_val, bv->bv_val, bv->bv_len );
|
1999-06-02 03:01:05 +08:00
|
|
|
new->bv_val[bv->bv_len] = '\0';
|
|
|
|
new->bv_len = bv->bv_len;
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return new;
|
1999-06-02 03:01:05 +08:00
|
|
|
}
|
1999-06-03 06:28:22 +08:00
|
|
|
|
1999-12-17 13:37:33 +08:00
|
|
|
struct berval *
|
|
|
|
ber_bvstr(
|
|
|
|
LDAP_CONST char *s )
|
|
|
|
{
|
|
|
|
struct berval *new;
|
|
|
|
|
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
|
|
|
if( s == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_PARAM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new->bv_val = (char *) s;
|
|
|
|
new->bv_len = strlen( s );
|
|
|
|
|
|
|
|
return( new );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct berval *
|
|
|
|
ber_bvstrdup(
|
|
|
|
LDAP_CONST char *s )
|
|
|
|
{
|
|
|
|
struct berval *new;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
|
|
|
if( s == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_PARAM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = LBER_STRDUP( s );
|
|
|
|
|
|
|
|
if( p == NULL ) {
|
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
new = ber_bvstr( p );
|
|
|
|
|
|
|
|
if( new == NULL || *p == '\0' ) {
|
|
|
|
LBER_FREE( p );
|
|
|
|
}
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return new;
|
1999-12-17 13:37:33 +08:00
|
|
|
}
|
|
|
|
|
1999-06-03 06:28:22 +08:00
|
|
|
char *
|
1999-06-08 02:48:22 +08:00
|
|
|
ber_strdup( LDAP_CONST char *s )
|
1999-06-03 06:28:22 +08:00
|
|
|
{
|
|
|
|
char *p;
|
1999-06-08 02:48:22 +08:00
|
|
|
size_t len;
|
|
|
|
|
1999-06-08 03:33:08 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
1999-06-08 02:48:22 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
|
|
|
assert(s != NULL); /* bv damn better point to something */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( s == NULL ) {
|
2000-01-09 02:42:11 +08:00
|
|
|
ber_errno = LBER_ERROR_PARAM;
|
2000-10-11 10:43:14 +08:00
|
|
|
return NULL;
|
1999-06-08 02:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen( s ) + 1;
|
1999-06-03 06:28:22 +08:00
|
|
|
|
1999-06-08 07:12:34 +08:00
|
|
|
if ( (p = LBER_MALLOC( len )) == NULL ) {
|
1999-12-06 14:33:26 +08:00
|
|
|
ber_errno = LBER_ERROR_MEMORY;
|
2000-10-11 10:43:14 +08:00
|
|
|
return NULL;
|
1999-06-03 06:28:22 +08:00
|
|
|
}
|
|
|
|
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( p, s, len );
|
2000-10-11 10:43:14 +08:00
|
|
|
return p;
|
1999-06-08 03:33:08 +08:00
|
|
|
}
|