]> git.sur5r.net Git - openldap/blob - libraries/liblber/options.c
Merge in all devel changes since 2.0-alpha2.
[openldap] / libraries / liblber / options.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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 #undef LDAP_F_PRE
12 #define LDAP_F_PRE LDAP_F_EXPORT
13
14 #include "lber-int.h"
15
16 struct lber_options ber_int_options = {
17         LBER_UNINITIALIZED, 0, 0 };
18
19 int
20 ber_get_option(
21         LDAP_CONST void *item,
22         int             option,
23         void    *outvalue)
24 {
25         LDAP_CONST BerElement *ber;
26         LDAP_CONST Sockbuf *sb;
27
28         ber_int_options.lbo_valid = LBER_INITIALIZED;
29
30         if(outvalue == NULL) {
31                 /* no place to get to */
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                 }
40
41                 return LBER_OPT_ERROR;
42         }
43
44         ber = item;
45         sb = item;
46
47         switch(option) {
48         case LBER_OPT_BER_OPTIONS:
49                 assert( BER_VALID( ber ) );
50                 * (int *) outvalue = ber->ber_options;
51                 return LBER_OPT_SUCCESS;
52
53         case LBER_OPT_BER_DEBUG:
54                 assert( BER_VALID( ber ) );
55                 * (int *) outvalue = ber->ber_debug;
56                 return LBER_OPT_SUCCESS;
57
58         default:
59                 /* bad param */
60                 break;
61         }
62
63         return LBER_OPT_ERROR;
64 }
65
66 int
67 ber_set_option(
68         void    *item,
69         int             option,
70         LDAP_CONST void *invalue)
71 {
72         BerElement *ber;
73         Sockbuf *sb;
74
75         if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
76                 && ( ber_int_memory_fns == NULL )
77                 && ( option == LBER_OPT_MEMORY_FNS )
78                 && ( invalue != NULL ))
79         {
80                 const BerMemoryFunctions *f =
81                         (const BerMemoryFunctions *) invalue;
82
83                 /* make sure all functions are provided */
84                 if(!( f->bmf_malloc && f->bmf_calloc
85                         && f->bmf_realloc && f->bmf_free ))
86                 {
87                         return LBER_OPT_ERROR;
88                 }
89
90                 ber_int_memory_fns = (BerMemoryFunctions *)
91                         (*(f->bmf_malloc))(sizeof(BerMemoryFunctions));
92
93                 if ( ber_int_memory_fns == NULL ) {
94                         return LBER_OPT_ERROR;
95                 }
96
97                 memcpy(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
98
99                 ber_int_options.lbo_valid = LBER_INITIALIZED;
100                 return LBER_OPT_SUCCESS;
101         }
102
103         ber_int_options.lbo_valid = LBER_INITIALIZED;
104
105         if(invalue == NULL) {
106                 /* no place to set from */
107                 return LBER_OPT_ERROR;
108         }
109
110         if(item == NULL) {
111                 if(option == LBER_OPT_BER_DEBUG) {
112                         ber_int_debug = * (const int *) invalue;
113                         return LBER_OPT_SUCCESS;
114
115                 } else if(option == LBER_OPT_LOG_PRINT_FN) {
116                         ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
117                         return LBER_OPT_SUCCESS;
118                 }
119
120                 return LBER_OPT_ERROR;
121         }
122
123         ber = item;
124         sb = item;
125
126         switch(option) {
127         case LBER_OPT_BER_OPTIONS:
128                 assert( BER_VALID( ber ) );
129                 ber->ber_options = * (const int *) invalue;
130                 return LBER_OPT_SUCCESS;
131
132         case LBER_OPT_BER_DEBUG:
133                 assert( BER_VALID( ber ) );
134                 ber->ber_debug = * (const int *) invalue;
135                 return LBER_OPT_SUCCESS;
136
137         default:
138                 /* bad param */
139                 break;
140         }
141
142         return LBER_OPT_ERROR;
143 }