2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
18 #include <ac/stdlib.h>
19 #include <ac/string.h>
20 #include <ac/stdarg.h>
23 char ber_pvt_opt_on; /* used to get a non-NULL address for *_OPT_ON */
25 struct lber_options ber_int_options = {
26 LBER_UNINITIALIZED, 0, 0, 0 };
34 const BerElement *ber;
37 ber_int_options.lbo_valid = LBER_INITIALIZED;
39 if(outvalue == NULL) {
40 /* no place to get to */
41 ber_errno = LBER_ERROR_PARAM;
42 return LBER_OPT_ERROR;
47 case LBER_OPT_BER_DEBUG:
48 * (int *) outvalue = ber_int_debug;
49 return LBER_OPT_SUCCESS;
51 case LBER_OPT_MEMORY_INUSE:
52 /* The memory inuse is a global variable on kernal implementations.
53 * This means that memory debug is shared by all LDAP processes
54 * so for this variable to have much meaning, only one LDAP process
55 * should be running and memory inuse should be initialized to zero
56 * using the lber_set_option() function during startup.
57 * The counter is not accurate for multithreaded ldap applications.
59 #ifdef LDAP_MEMORY_DEBUG
60 * (int *) outvalue = ber_int_options.lbo_meminuse;
61 return LBER_OPT_SUCCESS;
63 return LBER_OPT_ERROR;
65 case LBER_OPT_LOG_PRINT_FILE:
66 *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
67 return LBER_OPT_SUCCESS;
70 ber_errno = LBER_ERROR_PARAM;
71 return LBER_OPT_ERROR;
78 case LBER_OPT_BER_OPTIONS:
79 assert( LBER_VALID( ber ) );
80 * (int *) outvalue = ber->ber_options;
81 return LBER_OPT_SUCCESS;
83 case LBER_OPT_BER_DEBUG:
84 assert( LBER_VALID( ber ) );
85 * (int *) outvalue = ber->ber_debug;
86 return LBER_OPT_SUCCESS;
88 case LBER_OPT_BER_REMAINING_BYTES:
89 assert( LBER_VALID( ber ) );
90 *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
91 return LBER_OPT_SUCCESS;
93 case LBER_OPT_BER_TOTAL_BYTES:
94 assert( LBER_VALID( ber ) );
95 *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
96 return LBER_OPT_SUCCESS;
98 case LBER_OPT_BER_BYTES_TO_WRITE:
99 assert( LBER_VALID( ber ) );
100 *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
101 return LBER_OPT_SUCCESS;
103 case LBER_OPT_BER_MEMCTX:
104 assert( LBER_VALID( ber ) );
105 *((void **) outvalue) = ber->ber_memctx;
106 return LBER_OPT_SUCCESS;
110 ber_errno = LBER_ERROR_PARAM;
114 return LBER_OPT_ERROR;
121 LDAP_CONST void *invalue)
126 if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
127 && ( ber_int_memory_fns == NULL )
128 && ( option == LBER_OPT_MEMORY_FNS )
129 && ( invalue != NULL ) )
131 const BerMemoryFunctions *f =
132 (const BerMemoryFunctions *) invalue;
133 /* make sure all functions are provided */
134 if(!( f->bmf_malloc && f->bmf_calloc
135 && f->bmf_realloc && f->bmf_free ))
137 ber_errno = LBER_ERROR_PARAM;
138 return LBER_OPT_ERROR;
141 ber_int_memory_fns = (BerMemoryFunctions *)
142 (*(f->bmf_malloc))(sizeof(BerMemoryFunctions), NULL);
144 if ( ber_int_memory_fns == NULL ) {
145 ber_errno = LBER_ERROR_MEMORY;
146 return LBER_OPT_ERROR;
149 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
151 ber_int_options.lbo_valid = LBER_INITIALIZED;
152 return LBER_OPT_SUCCESS;
155 if ( option == LBER_OPT_MEMORY_FNS ) {
156 if ( ber_int_options.lbo_valid != LBER_INITIALIZED ) {
157 return LBER_OPT_ERROR;
160 if ( invalue != NULL ) {
161 return LBER_OPT_ERROR;
164 if ( ber_int_memory_fns == NULL ) {
165 return LBER_OPT_ERROR;
168 ber_int_memory_fns->bmf_free( ber_int_memory_fns, NULL );
169 ber_int_memory_fns = NULL;
170 ber_int_options.lbo_valid = LBER_UNINITIALIZED;
171 return LBER_OPT_SUCCESS;
174 ber_int_options.lbo_valid = LBER_INITIALIZED;
176 if(invalue == NULL) {
177 /* no place to set from */
178 ber_errno = LBER_ERROR_PARAM;
179 return LBER_OPT_ERROR;
184 case LBER_OPT_BER_DEBUG:
185 ber_int_debug = * (const int *) invalue;
186 return LBER_OPT_SUCCESS;
188 case LBER_OPT_LOG_PRINT_FN:
189 ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
190 return LBER_OPT_SUCCESS;
192 case LBER_OPT_LOG_PRINT_FILE:
193 ber_pvt_err_file = (void *) invalue;
194 return LBER_OPT_SUCCESS;
196 case LBER_OPT_MEMORY_INUSE:
197 /* The memory inuse is a global variable on kernal implementations.
198 * This means that memory debug is shared by all LDAP processes
199 * so for this variable to have much meaning, only one LDAP process
200 * should be running and memory inuse should be initialized to zero
201 * using the lber_set_option() function during startup.
202 * The counter is not accurate for multithreaded applications.
204 #ifdef LDAP_MEMORY_DEBUG
205 ber_int_options.lbo_meminuse = * (int *) invalue;
206 return LBER_OPT_SUCCESS;
208 return LBER_OPT_ERROR;
210 case LBER_OPT_LOG_PROC:
211 ber_int_log_proc = (BER_LOG_FN)invalue;
212 return LBER_OPT_SUCCESS;
215 ber_errno = LBER_ERROR_PARAM;
216 return LBER_OPT_ERROR;
223 case LBER_OPT_BER_OPTIONS:
224 assert( LBER_VALID( ber ) );
225 ber->ber_options = * (const int *) invalue;
226 return LBER_OPT_SUCCESS;
228 case LBER_OPT_BER_DEBUG:
229 assert( LBER_VALID( ber ) );
230 ber->ber_debug = * (const int *) invalue;
231 return LBER_OPT_SUCCESS;
233 case LBER_OPT_BER_REMAINING_BYTES:
234 assert( LBER_VALID( ber ) );
235 ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
236 return LBER_OPT_SUCCESS;
238 case LBER_OPT_BER_TOTAL_BYTES:
239 assert( LBER_VALID( ber ) );
240 ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
241 return LBER_OPT_SUCCESS;
243 case LBER_OPT_BER_BYTES_TO_WRITE:
244 assert( LBER_VALID( ber ) );
245 ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
246 return LBER_OPT_SUCCESS;
248 case LBER_OPT_BER_MEMCTX:
249 assert( LBER_VALID( ber ) );
250 ber->ber_memctx = *(void **)invalue;
251 return LBER_OPT_SUCCESS;
255 ber_errno = LBER_ERROR_PARAM;
259 return LBER_OPT_ERROR;