X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblber%2Foptions.c;h=160aac597dcc58c2cf4ebe3b4bfc6ad0401e222f;hb=509fdc1e6d98319e8f85480e7d493a3184504af6;hp=4546861619dc8b2e4551e740be95f269d789271c;hpb=dc07e765f263ef459dcd2afd1ece01cfc85a0edd;p=openldap diff --git a/libraries/liblber/options.c b/libraries/liblber/options.c index 4546861619..160aac597d 100644 --- a/libraries/liblber/options.c +++ b/libraries/liblber/options.c @@ -4,20 +4,27 @@ */ #include "portable.h" -#include +#include +#include + +#undef LDAP_F_PRE +#define LDAP_F_PRE LDAP_F_EXPORT #include "lber-int.h" -int ber_int_debug = 0; +struct lber_options ber_int_options = { + LBER_UNINITIALIZED, 0, 0 }; int ber_get_option( - void *item, + LDAP_CONST void *item, int option, void *outvalue) { - BerElement *ber; - Sockbuf *sb; + LDAP_CONST BerElement *ber; + LDAP_CONST Sockbuf *sb; + + ber_int_options.lbo_valid = LBER_INITIALIZED; if(outvalue == NULL) { /* no place to get to */ @@ -33,15 +40,17 @@ ber_get_option( return LBER_OPT_ERROR; } - ber = (BerElement *) item; - sb = (Sockbuf *) item; + ber = item; + sb = item; switch(option) { case LBER_OPT_BER_OPTIONS: + assert( BER_VALID( ber ) ); * (int *) outvalue = ber->ber_options; return LBER_OPT_SUCCESS; case LBER_OPT_BER_DEBUG: + assert( BER_VALID( ber ) ); * (int *) outvalue = ber->ber_debug; return LBER_OPT_SUCCESS; @@ -57,11 +66,41 @@ int ber_set_option( void *item, int option, - void *invalue) + LDAP_CONST void *invalue) { BerElement *ber; Sockbuf *sb; + if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED) + && ( ber_int_memory_fns == NULL ) + && ( option == LBER_OPT_MEMORY_FNS ) + && ( invalue != NULL )) + { + const BerMemoryFunctions *f = + (const BerMemoryFunctions *) invalue; + + /* make sure all functions are provided */ + if(!( f->bmf_malloc && f->bmf_calloc + && f->bmf_realloc && f->bmf_free )) + { + return LBER_OPT_ERROR; + } + + ber_int_memory_fns = (BerMemoryFunctions *) + (*(f->bmf_malloc))(sizeof(BerMemoryFunctions)); + + if ( ber_int_memory_fns == NULL ) { + return LBER_OPT_ERROR; + } + + memcpy(ber_int_memory_fns, f, sizeof(BerMemoryFunctions)); + + ber_int_options.lbo_valid = LBER_INITIALIZED; + return LBER_OPT_SUCCESS; + } + + ber_int_options.lbo_valid = LBER_INITIALIZED; + if(invalue == NULL) { /* no place to set from */ return LBER_OPT_ERROR; @@ -69,7 +108,7 @@ ber_set_option( if(item == NULL) { if(option == LBER_OPT_BER_DEBUG) { - ber_int_debug = * (int *) invalue; + ber_int_debug = * (const int *) invalue; return LBER_OPT_SUCCESS; } else if(option == LBER_OPT_LOG_PRINT_FN) { @@ -80,16 +119,18 @@ ber_set_option( return LBER_OPT_ERROR; } - ber = (BerElement *) item; - sb = (Sockbuf *) item; + ber = item; + sb = item; switch(option) { case LBER_OPT_BER_OPTIONS: - ber->ber_options = * (int *) invalue; + assert( BER_VALID( ber ) ); + ber->ber_options = * (const int *) invalue; return LBER_OPT_SUCCESS; case LBER_OPT_BER_DEBUG: - ber->ber_debug = * (int *) invalue; + assert( BER_VALID( ber ) ); + ber->ber_debug = * (const int *) invalue; return LBER_OPT_SUCCESS; default: