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