3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdarg.h>
14 typedef void (*BER_LOG_FN) LDAP_P((FILE *file, char *subsys, int level, const char *fmt, va_list vl));
15 extern void * ber_pvt_err_file;
16 extern BER_LOG_FN ber_int_log_proc;
18 struct lber_options ber_int_options = {
19 LBER_UNINITIALIZED, 0, 0 };
27 LDAP_CONST BerElement *ber;
28 LDAP_CONST Sockbuf *sb;
30 ber_int_options.lbo_valid = LBER_INITIALIZED;
32 if(outvalue == NULL) {
33 /* no place to get to */
34 ber_errno = LBER_ERROR_PARAM;
35 return LBER_OPT_ERROR;
39 if(option == LBER_OPT_BER_DEBUG) {
40 * (int *) outvalue = ber_int_debug;
41 return LBER_OPT_SUCCESS;
42 } else if(option == LBER_OPT_MEMORY_INUSE) {
43 /* The memory inuse is a global variable on kernal implementations.
44 * This means that memory debug is shared by all LDAP processes
45 * so for this variable to have much meaning, only one LDAP process
46 * should be running and memory inuse should be initialized to zero
47 * using the lber_set_option() function during startup.
48 * The counter is not accurate for multithreaded ldap applications.
50 #ifdef LDAP_MEMORY_DEBUG
51 * (int *) outvalue = ber_int_options.lbo_meminuse;
52 return LBER_OPT_SUCCESS;
54 return LBER_OPT_ERROR;
56 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
57 *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
58 return LBER_OPT_SUCCESS;
61 ber_errno = LBER_ERROR_PARAM;
62 return LBER_OPT_ERROR;
69 case LBER_OPT_BER_OPTIONS:
70 assert( BER_VALID( ber ) );
71 * (int *) outvalue = ber->ber_options;
72 return LBER_OPT_SUCCESS;
74 case LBER_OPT_BER_DEBUG:
75 assert( BER_VALID( ber ) );
76 * (int *) outvalue = ber->ber_debug;
77 return LBER_OPT_SUCCESS;
79 case LBER_OPT_BER_REMAINING_BYTES:
80 assert( BER_VALID( ber ) );
81 *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
82 return LBER_OPT_SUCCESS;
84 case LBER_OPT_BER_TOTAL_BYTES:
85 assert( BER_VALID( ber ) );
86 *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
87 return LBER_OPT_SUCCESS;
89 case LBER_OPT_BER_BYTES_TO_WRITE:
90 assert( BER_VALID( ber ) );
91 *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
92 return LBER_OPT_SUCCESS;
96 ber_errno = LBER_ERROR_PARAM;
100 return LBER_OPT_ERROR;
107 LDAP_CONST void *invalue)
112 if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
113 && ( ber_int_memory_fns == NULL )
114 && ( option == LBER_OPT_MEMORY_FNS )
115 && ( invalue != NULL ))
117 const BerMemoryFunctions *f =
118 (const BerMemoryFunctions *) invalue;
120 /* make sure all functions are provided */
121 if(!( f->bmf_malloc && f->bmf_calloc
122 && f->bmf_realloc && f->bmf_free ))
124 ber_errno = LBER_ERROR_PARAM;
125 return LBER_OPT_ERROR;
128 ber_int_memory_fns = (BerMemoryFunctions *)
129 (*(f->bmf_malloc))(sizeof(BerMemoryFunctions));
131 if ( ber_int_memory_fns == NULL ) {
132 ber_errno = LBER_ERROR_MEMORY;
133 return LBER_OPT_ERROR;
136 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
138 ber_int_options.lbo_valid = LBER_INITIALIZED;
139 return LBER_OPT_SUCCESS;
142 ber_int_options.lbo_valid = LBER_INITIALIZED;
144 if(invalue == NULL) {
145 /* no place to set from */
146 ber_errno = LBER_ERROR_PARAM;
147 return LBER_OPT_ERROR;
151 if(option == LBER_OPT_BER_DEBUG) {
152 ber_int_debug = * (const int *) invalue;
153 return LBER_OPT_SUCCESS;
155 } else if(option == LBER_OPT_LOG_PRINT_FN) {
156 ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
157 return LBER_OPT_SUCCESS;
158 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
159 ber_pvt_err_file = (void *) invalue;
160 return LBER_OPT_SUCCESS;
161 } else if(option == LBER_OPT_MEMORY_INUSE) {
162 /* The memory inuse is a global variable on kernal implementations.
163 * This means that memory debug is shared by all LDAP processes
164 * so for this variable to have much meaning, only one LDAP process
165 * should be running and memory inuse should be initialized to zero
166 * using the lber_set_option() function during startup.
167 * The counter is not accurate for multithreaded applications.
169 #ifdef LDAP_MEMORY_DEBUG
170 ber_int_options.lbo_meminuse = * (int *) invalue;
171 return LBER_OPT_SUCCESS;
173 return LBER_OPT_ERROR;
175 } else if(option == LBER_OPT_LOG_PROC) {
176 ber_int_log_proc = (BER_LOG_FN)invalue;
179 ber_errno = LBER_ERROR_PARAM;
180 return LBER_OPT_ERROR;
187 case LBER_OPT_BER_OPTIONS:
188 assert( BER_VALID( ber ) );
189 ber->ber_options = * (const int *) invalue;
190 return LBER_OPT_SUCCESS;
192 case LBER_OPT_BER_DEBUG:
193 assert( BER_VALID( ber ) );
194 ber->ber_debug = * (const int *) invalue;
195 return LBER_OPT_SUCCESS;
197 case LBER_OPT_BER_REMAINING_BYTES:
198 assert( BER_VALID( ber ) );
199 ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
200 return LBER_OPT_SUCCESS;
202 case LBER_OPT_BER_TOTAL_BYTES:
203 assert( BER_VALID( ber ) );
204 ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
205 return LBER_OPT_SUCCESS;
207 case LBER_OPT_BER_BYTES_TO_WRITE:
208 assert( BER_VALID( ber ) );
209 ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
210 return LBER_OPT_SUCCESS;
214 ber_errno = LBER_ERROR_PARAM;
218 return LBER_OPT_ERROR;