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