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