]> git.sur5r.net Git - openldap/blob - libraries/liblber/options.c
Notice updates
[openldap] / libraries / liblber / options.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 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 int
29 ber_get_option(
30         void    *item,
31         int             option,
32         void    *outvalue)
33 {
34         const BerElement *ber;
35         const Sockbuf *sb;
36
37         ber_int_options.lbo_valid = LBER_INITIALIZED;
38
39         if(outvalue == NULL) {
40                 /* no place to get to */
41                 ber_errno = LBER_ERROR_PARAM;
42                 return LBER_OPT_ERROR;
43         }
44
45         if(item == NULL) {
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.
56                          */
57 #ifdef LDAP_MEMORY_DEBUG
58                         * (int *) outvalue = ber_int_options.lbo_meminuse;
59                         return LBER_OPT_SUCCESS;
60 #else
61                         return LBER_OPT_ERROR;
62 #endif
63                 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
64                         *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
65                         return LBER_OPT_SUCCESS;
66                 }
67
68                 ber_errno = LBER_ERROR_PARAM;
69                 return LBER_OPT_ERROR;
70         }
71
72         ber = item;
73         sb = item;
74
75         switch(option) {
76         case LBER_OPT_BER_OPTIONS:
77                 assert( LBER_VALID( ber ) );
78                 * (int *) outvalue = ber->ber_options;
79                 return LBER_OPT_SUCCESS;
80
81         case LBER_OPT_BER_DEBUG:
82                 assert( LBER_VALID( ber ) );
83                 * (int *) outvalue = ber->ber_debug;
84                 return LBER_OPT_SUCCESS;
85
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;
90
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;
95
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;
100
101         case LBER_OPT_BER_MEMCTX:
102                 assert( LBER_VALID( ber ) );
103                 *((void **) outvalue) = ber->ber_memctx;
104                 return LBER_OPT_SUCCESS;
105         
106         default:
107                 /* bad param */
108                 ber_errno = LBER_ERROR_PARAM;
109                 break;
110         }
111
112         return LBER_OPT_ERROR;
113 }
114
115 int
116 ber_set_option(
117         void    *item,
118         int             option,
119         LDAP_CONST void *invalue)
120 {
121         BerElement *ber;
122         Sockbuf *sb;
123
124         if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
125                 && ( ber_int_memory_fns == NULL )
126                 && ( option == LBER_OPT_MEMORY_FNS )
127                 && ( invalue != NULL ))
128         {
129                 const BerMemoryFunctions *f =
130                         (const BerMemoryFunctions *) invalue;
131
132                 /* make sure all functions are provided */
133                 if(!( f->bmf_malloc && f->bmf_calloc
134                         && f->bmf_realloc && f->bmf_free ))
135                 {
136                         ber_errno = LBER_ERROR_PARAM;
137                         return LBER_OPT_ERROR;
138                 }
139
140                 ber_int_memory_fns = (BerMemoryFunctions *)
141                         (*(f->bmf_malloc))(sizeof(BerMemoryFunctions), NULL);
142
143                 if ( ber_int_memory_fns == NULL ) {
144                         ber_errno = LBER_ERROR_MEMORY;
145                         return LBER_OPT_ERROR;
146                 }
147
148                 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
149
150                 ber_int_options.lbo_valid = LBER_INITIALIZED;
151                 return LBER_OPT_SUCCESS;
152         }
153
154         ber_int_options.lbo_valid = LBER_INITIALIZED;
155
156         if(invalue == NULL) {
157                 /* no place to set from */
158                 ber_errno = LBER_ERROR_PARAM;
159                 return LBER_OPT_ERROR;
160         }
161
162         if(item == NULL) {
163                 if(option == LBER_OPT_BER_DEBUG) {
164                         ber_int_debug = * (const int *) invalue;
165                         return LBER_OPT_SUCCESS;
166
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.
180                          */
181 #ifdef LDAP_MEMORY_DEBUG
182                         ber_int_options.lbo_meminuse = * (int *) invalue;
183                         return LBER_OPT_SUCCESS;
184 #else
185                         return LBER_OPT_ERROR;
186 #endif
187                 } else if(option == LBER_OPT_LOG_PROC) {
188                         ber_int_log_proc = (BER_LOG_FN)invalue;
189                 }
190
191                 ber_errno = LBER_ERROR_PARAM;
192                 return LBER_OPT_ERROR;
193         }
194
195         ber = item;
196         sb = item;
197
198         switch(option) {
199         case LBER_OPT_BER_OPTIONS:
200                 assert( LBER_VALID( ber ) );
201                 ber->ber_options = * (const int *) invalue;
202                 return LBER_OPT_SUCCESS;
203
204         case LBER_OPT_BER_DEBUG:
205                 assert( LBER_VALID( ber ) );
206                 ber->ber_debug = * (const int *) invalue;
207                 return LBER_OPT_SUCCESS;
208
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;
213
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;
218
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;
223
224         case LBER_OPT_BER_MEMCTX:
225                 assert( LBER_VALID( ber ) );
226                 ber->ber_memctx = *(void **)invalue;
227                 return LBER_OPT_SUCCESS;
228
229         default:
230                 /* bad param */
231                 ber_errno = LBER_ERROR_PARAM;
232                 break;
233         }
234
235         return LBER_OPT_ERROR;
236 }