]> git.sur5r.net Git - openldap/blob - libraries/liblber/options.c
Deal with sb_trans_needs_read and sb_trans_needs_write
[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                 const BerMemoryFunctions *f =
80                         (const BerMemoryFunctions *) invalue;
81
82                 /* make sure all functions are provided */
83                 if(!( f->bmf_malloc && f->bmf_calloc
84                         && f->bmf_realloc && f->bmf_free ))
85                 {
86                         return LBER_OPT_ERROR;
87                 }
88
89                 ber_int_memory_fns = (BerMemoryFunctions *)
90                         (*(f->bmf_malloc))(sizeof(BerMemoryFunctions));
91
92                 if ( ber_int_memory_fns == NULL ) {
93                         return LBER_OPT_ERROR;
94                 }
95
96                 memcpy(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
97
98                 ber_int_options.lbo_valid = LBER_INITIALIZED;
99                 return LBER_OPT_SUCCESS;
100         }
101
102         ber_int_options.lbo_valid = LBER_INITIALIZED;
103
104         if(invalue == NULL) {
105                 /* no place to set from */
106                 return LBER_OPT_ERROR;
107         }
108
109         if(item == NULL) {
110                 if(option == LBER_OPT_BER_DEBUG) {
111                         ber_int_debug = * (const int *) invalue;
112                         return LBER_OPT_SUCCESS;
113
114                 } else if(option == LBER_OPT_LOG_PRINT_FN) {
115                         ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
116                         return LBER_OPT_SUCCESS;
117                 }
118
119                 return LBER_OPT_ERROR;
120         }
121
122         ber = item;
123         sb = item;
124
125         switch(option) {
126         case LBER_OPT_BER_OPTIONS:
127                 assert( BER_VALID( ber ) );
128                 ber->ber_options = * (const int *) invalue;
129                 return LBER_OPT_SUCCESS;
130
131         case LBER_OPT_BER_DEBUG:
132                 assert( BER_VALID( ber ) );
133                 ber->ber_debug = * (const int *) invalue;
134                 return LBER_OPT_SUCCESS;
135
136         default:
137                 /* bad param */
138                 break;
139         }
140
141         return LBER_OPT_ERROR;
142 }