2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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;
46 if(option == LBER_OPT_BER_DEBUG) {
47 * (int *) outvalue = ber_int_debug;
48 return LBER_OPT_SUCCESS;
49 } else if(option == LBER_OPT_MEMORY_INUSE) {
50 /* The memory inuse is a global variable on kernal implementations.
51 * This means that memory debug is shared by all LDAP processes
52 * so for this variable to have much meaning, only one LDAP process
53 * should be running and memory inuse should be initialized to zero
54 * using the lber_set_option() function during startup.
55 * The counter is not accurate for multithreaded ldap applications.
57 #ifdef LDAP_MEMORY_DEBUG
58 * (int *) outvalue = ber_int_options.lbo_meminuse;
59 return LBER_OPT_SUCCESS;
61 return LBER_OPT_ERROR;
63 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
64 *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
65 return LBER_OPT_SUCCESS;
68 ber_errno = LBER_ERROR_PARAM;
69 return LBER_OPT_ERROR;
76 case LBER_OPT_BER_OPTIONS:
77 assert( LBER_VALID( ber ) );
78 * (int *) outvalue = ber->ber_options;
79 return LBER_OPT_SUCCESS;
81 case LBER_OPT_BER_DEBUG:
82 assert( LBER_VALID( ber ) );
83 * (int *) outvalue = ber->ber_debug;
84 return LBER_OPT_SUCCESS;
86 case LBER_OPT_BER_REMAINING_BYTES:
87 assert( LBER_VALID( ber ) );
88 *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
89 return LBER_OPT_SUCCESS;
91 case LBER_OPT_BER_TOTAL_BYTES:
92 assert( LBER_VALID( ber ) );
93 *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
94 return LBER_OPT_SUCCESS;
96 case LBER_OPT_BER_BYTES_TO_WRITE:
97 assert( LBER_VALID( ber ) );
98 *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
99 return LBER_OPT_SUCCESS;
101 case LBER_OPT_BER_MEMCTX:
102 assert( LBER_VALID( ber ) );
103 *((void **) outvalue) = ber->ber_memctx;
104 return LBER_OPT_SUCCESS;
108 ber_errno = LBER_ERROR_PARAM;
112 return LBER_OPT_ERROR;
119 LDAP_CONST void *invalue)
124 if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
125 && ( ber_int_memory_fns == NULL )
126 && ( option == LBER_OPT_MEMORY_FNS )
127 && ( invalue != NULL ))
129 const BerMemoryFunctions *f =
130 (const BerMemoryFunctions *) invalue;
132 /* make sure all functions are provided */
133 if(!( f->bmf_malloc && f->bmf_calloc
134 && f->bmf_realloc && f->bmf_free ))
136 ber_errno = LBER_ERROR_PARAM;
137 return LBER_OPT_ERROR;
140 ber_int_memory_fns = (BerMemoryFunctions *)
141 (*(f->bmf_malloc))(sizeof(BerMemoryFunctions), NULL);
143 if ( ber_int_memory_fns == NULL ) {
144 ber_errno = LBER_ERROR_MEMORY;
145 return LBER_OPT_ERROR;
148 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
150 ber_int_options.lbo_valid = LBER_INITIALIZED;
151 return LBER_OPT_SUCCESS;
154 ber_int_options.lbo_valid = LBER_INITIALIZED;
156 if(invalue == NULL) {
157 /* no place to set from */
158 ber_errno = LBER_ERROR_PARAM;
159 return LBER_OPT_ERROR;
163 if(option == LBER_OPT_BER_DEBUG) {
164 ber_int_debug = * (const int *) invalue;
165 return LBER_OPT_SUCCESS;
167 } else if(option == LBER_OPT_LOG_PRINT_FN) {
168 ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
169 return LBER_OPT_SUCCESS;
170 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
171 ber_pvt_err_file = (void *) invalue;
172 return LBER_OPT_SUCCESS;
173 } else if(option == LBER_OPT_MEMORY_INUSE) {
174 /* The memory inuse is a global variable on kernal implementations.
175 * This means that memory debug is shared by all LDAP processes
176 * so for this variable to have much meaning, only one LDAP process
177 * should be running and memory inuse should be initialized to zero
178 * using the lber_set_option() function during startup.
179 * The counter is not accurate for multithreaded applications.
181 #ifdef LDAP_MEMORY_DEBUG
182 ber_int_options.lbo_meminuse = * (int *) invalue;
183 return LBER_OPT_SUCCESS;
185 return LBER_OPT_ERROR;
187 } else if(option == LBER_OPT_LOG_PROC) {
188 ber_int_log_proc = (BER_LOG_FN)invalue;
191 ber_errno = LBER_ERROR_PARAM;
192 return LBER_OPT_ERROR;
199 case LBER_OPT_BER_OPTIONS:
200 assert( LBER_VALID( ber ) );
201 ber->ber_options = * (const int *) invalue;
202 return LBER_OPT_SUCCESS;
204 case LBER_OPT_BER_DEBUG:
205 assert( LBER_VALID( ber ) );
206 ber->ber_debug = * (const int *) invalue;
207 return LBER_OPT_SUCCESS;
209 case LBER_OPT_BER_REMAINING_BYTES:
210 assert( LBER_VALID( ber ) );
211 ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
212 return LBER_OPT_SUCCESS;
214 case LBER_OPT_BER_TOTAL_BYTES:
215 assert( LBER_VALID( ber ) );
216 ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
217 return LBER_OPT_SUCCESS;
219 case LBER_OPT_BER_BYTES_TO_WRITE:
220 assert( LBER_VALID( ber ) );
221 ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
222 return LBER_OPT_SUCCESS;
224 case LBER_OPT_BER_MEMCTX:
225 assert( LBER_VALID( ber ) );
226 ber->ber_memctx = *(void **)invalue;
227 return LBER_OPT_SUCCESS;
231 ber_errno = LBER_ERROR_PARAM;
235 return LBER_OPT_ERROR;