]> git.sur5r.net Git - openldap/blob - libraries/liblber/options.c
plug leaks
[openldap] / libraries / liblber / options.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <ac/stdlib.h>
19 #include <ac/string.h>
20 #include <ac/stdarg.h>
21 #include "lber-int.h"
22
23 char ber_pvt_opt_on;    /* used to get a non-NULL address for *_OPT_ON */
24
25 struct lber_options ber_int_options = {
26         LBER_UNINITIALIZED, 0, 0, 0 };
27
28 static BerMemoryFunctions       ber_int_memory_fns_datum;
29
30 int
31 ber_get_option(
32         void    *item,
33         int             option,
34         void    *outvalue)
35 {
36         const BerElement *ber;
37         const Sockbuf *sb;
38
39         ber_int_options.lbo_valid = LBER_INITIALIZED;
40
41         if(outvalue == NULL) {
42                 /* no place to get to */
43                 ber_errno = LBER_ERROR_PARAM;
44                 return LBER_OPT_ERROR;
45         }
46
47         if(item == NULL) {
48                 switch ( option ) {
49                 case LBER_OPT_BER_DEBUG:
50                         * (int *) outvalue = ber_int_debug;
51                         return LBER_OPT_SUCCESS;
52
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.
60                          */
61 #ifdef LDAP_MEMORY_DEBUG
62                         * (int *) outvalue = ber_int_options.lbo_meminuse;
63                         return LBER_OPT_SUCCESS;
64 #else
65                         return LBER_OPT_ERROR;
66 #endif
67
68                 case LBER_OPT_LOG_PRINT_FILE:
69                         *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
70                         return LBER_OPT_SUCCESS;
71                 }
72
73                 ber_errno = LBER_ERROR_PARAM;
74                 return LBER_OPT_ERROR;
75         }
76
77         ber = item;
78         sb = item;
79
80         switch(option) {
81         case LBER_OPT_BER_OPTIONS:
82                 assert( LBER_VALID( ber ) );
83                 * (int *) outvalue = ber->ber_options;
84                 return LBER_OPT_SUCCESS;
85
86         case LBER_OPT_BER_DEBUG:
87                 assert( LBER_VALID( ber ) );
88                 * (int *) outvalue = ber->ber_debug;
89                 return LBER_OPT_SUCCESS;
90
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;
95
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;
100
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;
105
106         case LBER_OPT_BER_MEMCTX:
107                 assert( LBER_VALID( ber ) );
108                 *((void **) outvalue) = ber->ber_memctx;
109                 return LBER_OPT_SUCCESS;
110         
111         default:
112                 /* bad param */
113                 ber_errno = LBER_ERROR_PARAM;
114                 break;
115         }
116
117         return LBER_OPT_ERROR;
118 }
119
120 int
121 ber_set_option(
122         void    *item,
123         int             option,
124         LDAP_CONST void *invalue)
125 {
126         BerElement *ber;
127         Sockbuf *sb;
128
129         if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
130                 && ( ber_int_memory_fns == NULL )
131                 && ( option == LBER_OPT_MEMORY_FNS )
132                 && ( invalue != NULL ) )
133         {
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 ))
139                 {
140                         ber_errno = LBER_ERROR_PARAM;
141                         return LBER_OPT_ERROR;
142                 }
143
144                 ber_int_memory_fns = &ber_int_memory_fns_datum;
145
146                 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
147
148                 ber_int_options.lbo_valid = LBER_INITIALIZED;
149                 return LBER_OPT_SUCCESS;
150         }
151
152         ber_int_options.lbo_valid = LBER_INITIALIZED;
153
154         if(invalue == NULL) {
155                 /* no place to set from */
156                 ber_errno = LBER_ERROR_PARAM;
157                 return LBER_OPT_ERROR;
158         }
159
160         if(item == NULL) {
161                 switch ( option ) {
162                 case LBER_OPT_BER_DEBUG:
163                         ber_int_debug = * (const int *) invalue;
164                         return LBER_OPT_SUCCESS;
165
166                 case LBER_OPT_LOG_PRINT_FN:
167                         ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
168                         return LBER_OPT_SUCCESS;
169
170                 case LBER_OPT_LOG_PRINT_FILE:
171                         ber_pvt_err_file = (void *) invalue;
172                         return LBER_OPT_SUCCESS;
173
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.
181                          */
182 #ifdef LDAP_MEMORY_DEBUG
183                         ber_int_options.lbo_meminuse = * (int *) invalue;
184                         return LBER_OPT_SUCCESS;
185 #else
186                         return LBER_OPT_ERROR;
187 #endif
188                 case LBER_OPT_LOG_PROC:
189                         ber_int_log_proc = (BER_LOG_FN)invalue;
190                         return LBER_OPT_SUCCESS;
191                 }
192
193                 ber_errno = LBER_ERROR_PARAM;
194                 return LBER_OPT_ERROR;
195         }
196
197         ber = item;
198         sb = item;
199
200         switch(option) {
201         case LBER_OPT_BER_OPTIONS:
202                 assert( LBER_VALID( ber ) );
203                 ber->ber_options = * (const int *) invalue;
204                 return LBER_OPT_SUCCESS;
205
206         case LBER_OPT_BER_DEBUG:
207                 assert( LBER_VALID( ber ) );
208                 ber->ber_debug = * (const int *) invalue;
209                 return LBER_OPT_SUCCESS;
210
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;
215
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;
220
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;
225
226         case LBER_OPT_BER_MEMCTX:
227                 assert( LBER_VALID( ber ) );
228                 ber->ber_memctx = *(void **)invalue;
229                 return LBER_OPT_SUCCESS;
230
231         default:
232                 /* bad param */
233                 ber_errno = LBER_ERROR_PARAM;
234                 break;
235         }
236
237         return LBER_OPT_ERROR;
238 }