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