1 /* $OpenLDAP: pkg/ldap/libraries/liblber/options.c,v 1.43.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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 };
28 static BerMemoryFunctions ber_int_memory_fns_datum
;
36 const BerElement
*ber
;
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_meminuse
;
61 return LBER_OPT_SUCCESS
;
63 return LBER_OPT_ERROR
;
66 case LBER_OPT_LOG_PRINT_FILE
:
67 *((FILE**)outvalue
) = (FILE*)ber_pvt_err_file
;
68 return LBER_OPT_SUCCESS
;
71 ber_errno
= LBER_ERROR_PARAM
;
72 return LBER_OPT_ERROR
;
79 case LBER_OPT_BER_OPTIONS
:
80 assert( LBER_VALID( ber
) );
81 * (int *) outvalue
= ber
->ber_options
;
82 return LBER_OPT_SUCCESS
;
84 case LBER_OPT_BER_DEBUG
:
85 assert( LBER_VALID( ber
) );
86 * (int *) outvalue
= ber
->ber_debug
;
87 return LBER_OPT_SUCCESS
;
89 case LBER_OPT_BER_REMAINING_BYTES
:
90 assert( LBER_VALID( ber
) );
91 *((ber_len_t
*) outvalue
) = ber_pvt_ber_remaining(ber
);
92 return LBER_OPT_SUCCESS
;
94 case LBER_OPT_BER_TOTAL_BYTES
:
95 assert( LBER_VALID( ber
) );
96 *((ber_len_t
*) outvalue
) = ber_pvt_ber_total(ber
);
97 return LBER_OPT_SUCCESS
;
99 case LBER_OPT_BER_BYTES_TO_WRITE
:
100 assert( LBER_VALID( ber
) );
101 *((ber_len_t
*) outvalue
) = ber_pvt_ber_write(ber
);
102 return LBER_OPT_SUCCESS
;
104 case LBER_OPT_BER_MEMCTX
:
105 assert( LBER_VALID( ber
) );
106 *((void **) outvalue
) = ber
->ber_memctx
;
107 return LBER_OPT_SUCCESS
;
111 ber_errno
= LBER_ERROR_PARAM
;
115 return LBER_OPT_ERROR
;
122 LDAP_CONST
void *invalue
)
127 if(invalue
== NULL
) {
128 /* no place to set from */
129 ber_errno
= LBER_ERROR_PARAM
;
130 return LBER_OPT_ERROR
;
135 case LBER_OPT_BER_DEBUG
:
136 ber_int_debug
= * (const int *) invalue
;
137 return LBER_OPT_SUCCESS
;
139 case LBER_OPT_LOG_PRINT_FN
:
140 ber_pvt_log_print
= (BER_LOG_PRINT_FN
) invalue
;
141 return LBER_OPT_SUCCESS
;
143 case LBER_OPT_LOG_PRINT_FILE
:
144 ber_pvt_err_file
= (void *) invalue
;
145 return LBER_OPT_SUCCESS
;
147 case LBER_OPT_MEMORY_INUSE
:
148 /* The memory inuse is a global variable on kernal implementations.
149 * This means that memory debug is shared by all LDAP processes
150 * so for this variable to have much meaning, only one LDAP process
151 * should be running and memory inuse should be initialized to zero
152 * using the lber_set_option() function during startup.
153 * The counter is not accurate for multithreaded applications.
155 #ifdef LDAP_MEMORY_DEBUG
156 ber_int_meminuse
= * (int *) invalue
;
157 return LBER_OPT_SUCCESS
;
159 return LBER_OPT_ERROR
;
161 case LBER_OPT_MEMORY_FNS
:
162 if ( ber_int_memory_fns
== NULL
)
164 const BerMemoryFunctions
*f
=
165 (const BerMemoryFunctions
*) invalue
;
166 /* make sure all functions are provided */
167 if(!( f
->bmf_malloc
&& f
->bmf_calloc
168 && f
->bmf_realloc
&& f
->bmf_free
))
170 ber_errno
= LBER_ERROR_PARAM
;
171 return LBER_OPT_ERROR
;
174 ber_int_memory_fns
= &ber_int_memory_fns_datum
;
176 AC_MEMCPY(ber_int_memory_fns
, f
,
177 sizeof(BerMemoryFunctions
));
179 return LBER_OPT_SUCCESS
;
183 case LBER_OPT_LOG_PROC
:
184 ber_int_log_proc
= (BER_LOG_FN
)invalue
;
185 return LBER_OPT_SUCCESS
;
188 ber_errno
= LBER_ERROR_PARAM
;
189 return LBER_OPT_ERROR
;
196 case LBER_OPT_BER_OPTIONS
:
197 assert( LBER_VALID( ber
) );
198 ber
->ber_options
= * (const int *) invalue
;
199 return LBER_OPT_SUCCESS
;
201 case LBER_OPT_BER_DEBUG
:
202 assert( LBER_VALID( ber
) );
203 ber
->ber_debug
= * (const int *) invalue
;
204 return LBER_OPT_SUCCESS
;
206 case LBER_OPT_BER_REMAINING_BYTES
:
207 assert( LBER_VALID( ber
) );
208 ber
->ber_end
= &ber
->ber_ptr
[* (const ber_len_t
*) invalue
];
209 return LBER_OPT_SUCCESS
;
211 case LBER_OPT_BER_TOTAL_BYTES
:
212 assert( LBER_VALID( ber
) );
213 ber
->ber_end
= &ber
->ber_buf
[* (const ber_len_t
*) invalue
];
214 return LBER_OPT_SUCCESS
;
216 case LBER_OPT_BER_BYTES_TO_WRITE
:
217 assert( LBER_VALID( ber
) );
218 ber
->ber_ptr
= &ber
->ber_buf
[* (const ber_len_t
*) invalue
];
219 return LBER_OPT_SUCCESS
;
221 case LBER_OPT_BER_MEMCTX
:
222 assert( LBER_VALID( ber
) );
223 ber
->ber_memctx
= *(void **)invalue
;
224 return LBER_OPT_SUCCESS
;
228 ber_errno
= LBER_ERROR_PARAM
;
232 return LBER_OPT_ERROR
;