]> git.sur5r.net Git - openldap/blob - libraries/liblber/options.c
Replace recursive get_stringbvr with iterative get_stringbvl to avoid
[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 };
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         default:
90                 /* bad param */
91                 ber_errno = LBER_ERROR_PARAM;
92                 break;
93         }
94
95         return LBER_OPT_ERROR;
96 }
97
98 int
99 ber_set_option(
100         void    *item,
101         int             option,
102         LDAP_CONST void *invalue)
103 {
104         BerElement *ber;
105         Sockbuf *sb;
106
107         if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
108                 && ( ber_int_memory_fns == NULL )
109                 && ( option == LBER_OPT_MEMORY_FNS )
110                 && ( invalue != NULL ))
111         {
112                 const BerMemoryFunctions *f =
113                         (const BerMemoryFunctions *) invalue;
114
115                 /* make sure all functions are provided */
116                 if(!( f->bmf_malloc && f->bmf_calloc
117                         && f->bmf_realloc && f->bmf_free ))
118                 {
119                         ber_errno = LBER_ERROR_PARAM;
120                         return LBER_OPT_ERROR;
121                 }
122
123                 ber_int_memory_fns = (BerMemoryFunctions *)
124                         (*(f->bmf_malloc))(sizeof(BerMemoryFunctions));
125
126                 if ( ber_int_memory_fns == NULL ) {
127                         ber_errno = LBER_ERROR_MEMORY;
128                         return LBER_OPT_ERROR;
129                 }
130
131                 AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
132
133                 ber_int_options.lbo_valid = LBER_INITIALIZED;
134                 return LBER_OPT_SUCCESS;
135         }
136
137         ber_int_options.lbo_valid = LBER_INITIALIZED;
138
139         if(invalue == NULL) {
140                 /* no place to set from */
141                 ber_errno = LBER_ERROR_PARAM;
142                 return LBER_OPT_ERROR;
143         }
144
145         if(item == NULL) {
146                 if(option == LBER_OPT_BER_DEBUG) {
147                         ber_int_debug = * (const int *) invalue;
148                         return LBER_OPT_SUCCESS;
149
150                 } else if(option == LBER_OPT_LOG_PRINT_FN) {
151                         ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
152                         return LBER_OPT_SUCCESS;
153                 } else if(option == LBER_OPT_LOG_PRINT_FILE) {
154                         ber_pvt_err_file = (void *) invalue;
155                         return LBER_OPT_SUCCESS;
156                 } else if(option == LBER_OPT_MEMORY_INUSE) {
157                         /* The memory inuse is a global variable on kernal implementations.
158                          * This means that memory debug is shared by all LDAP processes
159                          * so for this variable to have much meaning, only one LDAP process
160                          * should be running and memory inuse should be initialized to zero
161                          * using the lber_set_option() function during startup.
162                          * The counter is not accurate for multithreaded applications.
163                          */
164 #ifdef LDAP_MEMORY_DEBUG
165                         ber_int_options.lbo_meminuse = * (int *) invalue;
166                         return LBER_OPT_SUCCESS;
167 #else
168                         return LBER_OPT_ERROR;
169 #endif
170                 } else if(option == LBER_OPT_LOG_PROC) {
171                         ber_int_log_proc = (BER_LOG_FN)invalue;
172                 }
173
174                 ber_errno = LBER_ERROR_PARAM;
175                 return LBER_OPT_ERROR;
176         }
177
178         ber = item;
179         sb = item;
180
181         switch(option) {
182         case LBER_OPT_BER_OPTIONS:
183                 assert( LBER_VALID( ber ) );
184                 ber->ber_options = * (const int *) invalue;
185                 return LBER_OPT_SUCCESS;
186
187         case LBER_OPT_BER_DEBUG:
188                 assert( LBER_VALID( ber ) );
189                 ber->ber_debug = * (const int *) invalue;
190                 return LBER_OPT_SUCCESS;
191
192         case LBER_OPT_BER_REMAINING_BYTES:
193                 assert( LBER_VALID( ber ) );
194                 ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
195                 return LBER_OPT_SUCCESS;
196
197         case LBER_OPT_BER_TOTAL_BYTES:
198                 assert( LBER_VALID( ber ) );
199                 ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
200                 return LBER_OPT_SUCCESS;
201
202         case LBER_OPT_BER_BYTES_TO_WRITE:
203                 assert( LBER_VALID( ber ) );
204                 ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
205                 return LBER_OPT_SUCCESS;
206
207         default:
208                 /* bad param */
209                 ber_errno = LBER_ERROR_PARAM;
210                 break;
211         }
212
213         return LBER_OPT_ERROR;
214 }