]> git.sur5r.net Git - openldap/blob - libraries/liblber/options.c
Regarding previous commit:
[openldap] / libraries / liblber / options.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <ac/stdlib.h>
8 #include <ac/string.h>
9
10 #undef LDAP_F_PRE
11 #define LDAP_F_PRE LDAP_F_EXPORT
12
13 #include "lber-int.h"
14
15 struct lber_options ber_int_options = {
16         LBER_UNINITIALIZED, 0, 0 };
17
18 int
19 ber_get_option(
20         LDAP_CONST 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                 return LBER_OPT_ERROR;
32         }
33
34         if(item == NULL) {
35                 if(option == LBER_OPT_BER_DEBUG) {
36                         * (int *) outvalue = ber_int_debug;
37                         return LBER_OPT_SUCCESS;
38                 }
39
40                 return LBER_OPT_ERROR;
41         }
42
43         ber = item;
44         sb = item;
45
46         switch(option) {
47         case LBER_OPT_BER_OPTIONS:
48                 assert( BER_VALID( ber ) );
49                 * (int *) outvalue = ber->ber_options;
50                 return LBER_OPT_SUCCESS;
51
52         case LBER_OPT_BER_DEBUG:
53                 assert( BER_VALID( ber ) );
54                 * (int *) outvalue = ber->ber_debug;
55                 return LBER_OPT_SUCCESS;
56
57         default:
58                 /* bad param */
59                 break;
60         }
61
62         return LBER_OPT_ERROR;
63 }
64
65 int
66 ber_set_option(
67         void    *item,
68         int             option,
69         LDAP_CONST void *invalue)
70 {
71         BerElement *ber;
72         Sockbuf *sb;
73
74         if( (ber_int_options.lbo_valid == LBER_UNINITIALIZED)
75                 && ( ber_int_memory_fns == NULL )
76                 && ( option == LBER_OPT_MEMORY_FNS )
77                 && ( invalue != NULL ))
78         {
79                 BerMemoryFunctions *f = (BerMemoryFunctions *) invalue;
80
81                 /* make sure all functions are provided */
82                 if(!( f->bmf_malloc && f->bmf_calloc
83                         && f->bmf_realloc && f->bmf_free ))
84                 {
85                         return LBER_OPT_ERROR;
86                 }
87
88                 ber_int_memory_fns = (BerMemoryFunctions *)
89                         (*(f->bmf_malloc))(sizeof(BerMemoryFunctions));
90
91                 if ( ber_int_memory_fns == NULL ) {
92                         return LBER_OPT_ERROR;
93                 }
94
95                 memcpy(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
96
97                 ber_int_options.lbo_valid = LBER_INITIALIZED;
98                 return LBER_OPT_SUCCESS;
99         }
100
101         ber_int_options.lbo_valid = LBER_INITIALIZED;
102
103         if(invalue == NULL) {
104                 /* no place to set from */
105                 return LBER_OPT_ERROR;
106         }
107
108         if(item == NULL) {
109                 if(option == LBER_OPT_BER_DEBUG) {
110                         ber_int_debug = * (int *) invalue;
111                         return LBER_OPT_SUCCESS;
112
113                 } else if(option == LBER_OPT_LOG_PRINT_FN) {
114                         ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
115                         return LBER_OPT_SUCCESS;
116                 }
117
118                 return LBER_OPT_ERROR;
119         }
120
121         ber = item;
122         sb = item;
123
124         switch(option) {
125         case LBER_OPT_BER_OPTIONS:
126                 assert( BER_VALID( ber ) );
127                 ber->ber_options = * (int *) invalue;
128                 return LBER_OPT_SUCCESS;
129
130         case LBER_OPT_BER_DEBUG:
131                 assert( BER_VALID( ber ) );
132                 ber->ber_debug = * (int *) invalue;
133                 return LBER_OPT_SUCCESS;
134
135         default:
136                 /* bad param */
137                 break;
138         }
139
140         return LBER_OPT_ERROR;
141 }