]> git.sur5r.net Git - openldap/blob - libraries/liblber/options.c
#define *_OPT_ON &ber_pvt_opt_on. The old (void*)1 definition could be == NULL.
[openldap] / libraries / liblber / options.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <ac/stdlib.h>
9 #include <ac/string.h>
10 #include <ac/stdarg.h>
11 #include "lber-int.h"
12
13 char ber_pvt_opt_on;    /* used to get a non-NULL address for *_OPT_ON */
14
15 struct lber_options ber_int_options = {
16         LBER_UNINITIALIZED, 0, 0, 0 };
17
18 int
19 ber_get_option(
20         void    *item,
21         int             option,
22         void    *outvalue)
23 {
24         const BerElement *ber;
25         const Sockbuf *sb;
26
27         ber_int_options.lbo_valid = LBER_INITIALIZED;
28
29         if(outvalue == NULL) {
30                 /* no place to get to */
31                 ber_errno = LBER_ERROR_PARAM;
32                 return LBER_OPT_ERROR;
33         }
34
35         if(item == NULL) {
36                 if(option == LBER_OPT_BER_DEBUG) {
37                         * (int *) outvalue = ber_int_debug;
38                         return LBER_OPT_SUCCESS;
39                 } else if(option == LBER_OPT_MEMORY_INUSE) {
40                         /* The memory inuse is a global variable on kernal implementations.
41                          * This means that memory debug is shared by all LDAP processes
42                          * so for this variable to have much meaning, only one LDAP process
43                          * should be running and memory inuse should be initialized to zero
44                          * using the lber_set_option() function during startup.
45                          * The counter is not accurate for multithreaded ldap applications.
46                          */
47 #ifdef LDAP_MEMORY_DEBUG
48                         * (int *) outvalue = ber_int_options.lbo_meminuse;
49                         return LBER_OPT_SUCCESS;
50 #else
51                         return LBER_OPT_ERROR;
52 #endif
53                 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
54                         *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
55                         return LBER_OPT_SUCCESS;
56                 }
57
58                 ber_errno = LBER_ERROR_PARAM;
59                 return LBER_OPT_ERROR;
60         }
61
62         ber = item;
63         sb = item;
64
65         switch(option) {
66         case LBER_OPT_BER_OPTIONS:
67                 assert( LBER_VALID( ber ) );
68                 * (int *) outvalue = ber->ber_options;
69                 return LBER_OPT_SUCCESS;
70
71         case LBER_OPT_BER_DEBUG:
72                 assert( LBER_VALID( ber ) );
73                 * (int *) outvalue = ber->ber_debug;
74                 return LBER_OPT_SUCCESS;
75
76         case LBER_OPT_BER_REMAINING_BYTES:
77                 assert( LBER_VALID( ber ) );
78                 *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
79                 return LBER_OPT_SUCCESS;
80
81         case LBER_OPT_BER_TOTAL_BYTES:
82                 assert( LBER_VALID( ber ) );
83                 *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
84                 return LBER_OPT_SUCCESS;
85
86         case LBER_OPT_BER_BYTES_TO_WRITE:
87                 assert( LBER_VALID( ber ) );
88                 *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
89                 return LBER_OPT_SUCCESS;
90
91         case LBER_OPT_BER_MEMCTX:
92                 assert( LBER_VALID( ber ) );
93                 *((void **) outvalue) = ber->ber_memctx;
94                 return LBER_OPT_SUCCESS;
95         
96         default:
97                 /* bad param */
98                 ber_errno = LBER_ERROR_PARAM;
99                 break;
100         }
101
102         return LBER_OPT_ERROR;
103 }
104
105 int
106 ber_set_option(
107         void    *item,
108         int             option,
109         LDAP_CONST void *invalue)
110 {
111         BerElement *ber;
112         Sockbuf *sb;
113
114         if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
115                 && ( ber_int_memory_fns == NULL )
116                 && ( option == LBER_OPT_MEMORY_FNS )
117                 && ( invalue != NULL ))
118         {
119                 const BerMemoryFunctions *f =
120                         (const BerMemoryFunctions *) invalue;
121
122                 /* make sure all functions are provided */
123                 if(!( f->bmf_malloc && f->bmf_calloc
124                         && f->bmf_realloc && f->bmf_free ))
125                 {
126                         ber_errno = LBER_ERROR_PARAM;
127                         return LBER_OPT_ERROR;
128                 }
129
130                 ber_int_memory_fns = (BerMemoryFunctions *)
131                         (*(f->bmf_malloc))(sizeof(BerMemoryFunctions), NULL);
132
133                 if ( ber_int_memory_fns == NULL ) {
134                         ber_errno = LBER_ERROR_MEMORY;
135                         return LBER_OPT_ERROR;
136                 }
137
138                 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
139
140                 ber_int_options.lbo_valid = LBER_INITIALIZED;
141                 return LBER_OPT_SUCCESS;
142         }
143
144         ber_int_options.lbo_valid = LBER_INITIALIZED;
145
146         if(invalue == NULL) {
147                 /* no place to set from */
148                 ber_errno = LBER_ERROR_PARAM;
149                 return LBER_OPT_ERROR;
150         }
151
152         if(item == NULL) {
153                 if(option == LBER_OPT_BER_DEBUG) {
154                         ber_int_debug = * (const int *) invalue;
155                         return LBER_OPT_SUCCESS;
156
157                 } else if(option == LBER_OPT_LOG_PRINT_FN) {
158                         ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
159                         return LBER_OPT_SUCCESS;
160                 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
161                         ber_pvt_err_file = (void *) invalue;
162                         return LBER_OPT_SUCCESS;
163                 } else if(option == LBER_OPT_MEMORY_INUSE) {
164                         /* The memory inuse is a global variable on kernal implementations.
165                          * This means that memory debug is shared by all LDAP processes
166                          * so for this variable to have much meaning, only one LDAP process
167                          * should be running and memory inuse should be initialized to zero
168                          * using the lber_set_option() function during startup.
169                          * The counter is not accurate for multithreaded applications.
170                          */
171 #ifdef LDAP_MEMORY_DEBUG
172                         ber_int_options.lbo_meminuse = * (int *) invalue;
173                         return LBER_OPT_SUCCESS;
174 #else
175                         return LBER_OPT_ERROR;
176 #endif
177                 } else if(option == LBER_OPT_LOG_PROC) {
178                         ber_int_log_proc = (BER_LOG_FN)invalue;
179                 }
180
181                 ber_errno = LBER_ERROR_PARAM;
182                 return LBER_OPT_ERROR;
183         }
184
185         ber = item;
186         sb = item;
187
188         switch(option) {
189         case LBER_OPT_BER_OPTIONS:
190                 assert( LBER_VALID( ber ) );
191                 ber->ber_options = * (const int *) invalue;
192                 return LBER_OPT_SUCCESS;
193
194         case LBER_OPT_BER_DEBUG:
195                 assert( LBER_VALID( ber ) );
196                 ber->ber_debug = * (const int *) invalue;
197                 return LBER_OPT_SUCCESS;
198
199         case LBER_OPT_BER_REMAINING_BYTES:
200                 assert( LBER_VALID( ber ) );
201                 ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
202                 return LBER_OPT_SUCCESS;
203
204         case LBER_OPT_BER_TOTAL_BYTES:
205                 assert( LBER_VALID( ber ) );
206                 ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
207                 return LBER_OPT_SUCCESS;
208
209         case LBER_OPT_BER_BYTES_TO_WRITE:
210                 assert( LBER_VALID( ber ) );
211                 ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
212                 return LBER_OPT_SUCCESS;
213
214         case LBER_OPT_BER_MEMCTX:
215                 assert( LBER_VALID( ber ) );
216                 ber->ber_memctx = *(void **)invalue;
217                 return LBER_OPT_SUCCESS;
218
219         default:
220                 /* bad param */
221                 ber_errno = LBER_ERROR_PARAM;
222                 break;
223         }
224
225         return LBER_OPT_ERROR;
226 }