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 };
28 static BerMemoryFunctions ber_int_memory_fns_datum;
36 const BerElement *ber;
39 ber_int_options.lbo_valid = LBER_INITIALIZED;
41 if(outvalue == NULL) {
42 /* no place to get to */
43 ber_errno = LBER_ERROR_PARAM;
44 return LBER_OPT_ERROR;
49 case LBER_OPT_BER_DEBUG:
50 * (int *) outvalue = ber_int_debug;
51 return LBER_OPT_SUCCESS;
53 case LBER_OPT_MEMORY_INUSE:
54 /* The memory inuse is a global variable on kernal implementations.
55 * This means that memory debug is shared by all LDAP processes
56 * so for this variable to have much meaning, only one LDAP process
57 * should be running and memory inuse should be initialized to zero
58 * using the lber_set_option() function during startup.
59 * The counter is not accurate for multithreaded ldap applications.
61 #ifdef LDAP_MEMORY_DEBUG
62 * (int *) outvalue = ber_int_options.lbo_meminuse;
63 return LBER_OPT_SUCCESS;
65 return LBER_OPT_ERROR;
68 case LBER_OPT_LOG_PRINT_FILE:
69 *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
70 return LBER_OPT_SUCCESS;
73 ber_errno = LBER_ERROR_PARAM;
74 return LBER_OPT_ERROR;
81 case LBER_OPT_BER_OPTIONS:
82 assert( LBER_VALID( ber ) );
83 * (int *) outvalue = ber->ber_options;
84 return LBER_OPT_SUCCESS;
86 case LBER_OPT_BER_DEBUG:
87 assert( LBER_VALID( ber ) );
88 * (int *) outvalue = ber->ber_debug;
89 return LBER_OPT_SUCCESS;
91 case LBER_OPT_BER_REMAINING_BYTES:
92 assert( LBER_VALID( ber ) );
93 *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
94 return LBER_OPT_SUCCESS;
96 case LBER_OPT_BER_TOTAL_BYTES:
97 assert( LBER_VALID( ber ) );
98 *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
99 return LBER_OPT_SUCCESS;
101 case LBER_OPT_BER_BYTES_TO_WRITE:
102 assert( LBER_VALID( ber ) );
103 *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
104 return LBER_OPT_SUCCESS;
106 case LBER_OPT_BER_MEMCTX:
107 assert( LBER_VALID( ber ) );
108 *((void **) outvalue) = ber->ber_memctx;
109 return LBER_OPT_SUCCESS;
113 ber_errno = LBER_ERROR_PARAM;
117 return LBER_OPT_ERROR;
124 LDAP_CONST void *invalue)
129 if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
130 && ( ber_int_memory_fns == NULL )
131 && ( option == LBER_OPT_MEMORY_FNS )
132 && ( invalue != NULL ) )
134 const BerMemoryFunctions *f =
135 (const BerMemoryFunctions *) invalue;
136 /* make sure all functions are provided */
137 if(!( f->bmf_malloc && f->bmf_calloc
138 && f->bmf_realloc && f->bmf_free ))
140 ber_errno = LBER_ERROR_PARAM;
141 return LBER_OPT_ERROR;
144 ber_int_memory_fns = &ber_int_memory_fns_datum;
146 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
148 ber_int_options.lbo_valid = LBER_INITIALIZED;
149 return LBER_OPT_SUCCESS;
152 ber_int_options.lbo_valid = LBER_INITIALIZED;
154 if(invalue == NULL) {
155 /* no place to set from */
156 ber_errno = LBER_ERROR_PARAM;
157 return LBER_OPT_ERROR;
162 case LBER_OPT_BER_DEBUG:
163 ber_int_debug = * (const int *) invalue;
164 return LBER_OPT_SUCCESS;
166 case LBER_OPT_LOG_PRINT_FN:
167 ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
168 return LBER_OPT_SUCCESS;
170 case LBER_OPT_LOG_PRINT_FILE:
171 ber_pvt_err_file = (void *) invalue;
172 return LBER_OPT_SUCCESS;
174 case LBER_OPT_MEMORY_INUSE:
175 /* The memory inuse is a global variable on kernal implementations.
176 * This means that memory debug is shared by all LDAP processes
177 * so for this variable to have much meaning, only one LDAP process
178 * should be running and memory inuse should be initialized to zero
179 * using the lber_set_option() function during startup.
180 * The counter is not accurate for multithreaded applications.
182 #ifdef LDAP_MEMORY_DEBUG
183 ber_int_options.lbo_meminuse = * (int *) invalue;
184 return LBER_OPT_SUCCESS;
186 return LBER_OPT_ERROR;
188 case LBER_OPT_LOG_PROC:
189 ber_int_log_proc = (BER_LOG_FN)invalue;
190 return LBER_OPT_SUCCESS;
193 ber_errno = LBER_ERROR_PARAM;
194 return LBER_OPT_ERROR;
201 case LBER_OPT_BER_OPTIONS:
202 assert( LBER_VALID( ber ) );
203 ber->ber_options = * (const int *) invalue;
204 return LBER_OPT_SUCCESS;
206 case LBER_OPT_BER_DEBUG:
207 assert( LBER_VALID( ber ) );
208 ber->ber_debug = * (const int *) invalue;
209 return LBER_OPT_SUCCESS;
211 case LBER_OPT_BER_REMAINING_BYTES:
212 assert( LBER_VALID( ber ) );
213 ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
214 return LBER_OPT_SUCCESS;
216 case LBER_OPT_BER_TOTAL_BYTES:
217 assert( LBER_VALID( ber ) );
218 ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
219 return LBER_OPT_SUCCESS;
221 case LBER_OPT_BER_BYTES_TO_WRITE:
222 assert( LBER_VALID( ber ) );
223 ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
224 return LBER_OPT_SUCCESS;
226 case LBER_OPT_BER_MEMCTX:
227 assert( LBER_VALID( ber ) );
228 ber->ber_memctx = *(void **)invalue;
229 return LBER_OPT_SUCCESS;
233 ber_errno = LBER_ERROR_PARAM;
237 return LBER_OPT_ERROR;