]> git.sur5r.net Git - openldap/blob - libraries/libldap/controls.c
Updates for MSVC 5.0. Fix libraries names to be ol{ber,dap,..}32.lib.
[openldap] / libraries / libldap / controls.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * LDAP controls
7  */
8
9 #include "portable.h"
10
11 #include <stdlib.h>
12
13 #include <ac/time.h>
14 #include <ac/string.h>
15
16 #include "ldap-int.h"
17
18 /*
19  * Free a LDAPControl
20  */
21 void
22 ldap_control_free( LDAPControl *c )
23 {
24         if ( c != NULL ) {
25                 if( c->ldctl_oid != NULL) {
26                         free( c->ldctl_oid );
27                 }
28
29                 if( c->ldctl_value.bv_val != NULL ) {
30                         free( c->ldctl_value.bv_val );
31                 }
32
33                 free( c );
34         }
35 }
36
37 /*
38  * Free an array of LDAPControl's
39  */
40 void
41 ldap_controls_free( LDAPControl **controls )
42 {
43         if ( controls != NULL ) {
44                 LDAPControl *c;
45
46                 for(c = *controls; c != NULL; c++) {
47                         ldap_control_free( c );
48                 }
49
50                 free( controls );
51         }
52 }
53
54 /*
55  * Duplicate an array of LDAPControl
56  */
57 LDAPControl **ldap_controls_dup( LDAPControl **controls )
58 {
59         LDAPControl **new;
60         int i;
61
62         if ( controls == NULL ) {
63                 return NULL;
64         }
65
66         /* count the controls */
67         for(i=0; controls[i] != NULL; i++) /* empty */ ;
68
69         if( i < 1 ) {
70                 /* no controls to duplicate */
71                 return NULL;
72         }
73
74         new = (LDAPControl **) malloc( i * sizeof(LDAPControl *) );
75
76         if( new == NULL ) {
77                 /* memory allocation failure */
78                 return NULL;
79         }
80
81         /* duplicate the controls */
82         for(i=0; controls[i] != NULL; i++) {
83                 new[i] = ldap_control_dup( controls[i] );
84
85                 if( new[i] == NULL ) {
86                         ldap_controls_free( new );
87                         return NULL;
88                 }
89         }
90
91         new[i] = NULL;
92
93         return new;
94 }
95
96 /*
97  * Duplicate a LDAPControl
98  */
99 LDAPControl *ldap_control_dup( LDAPControl *c )
100 {
101         LDAPControl *new;
102
103         if ( c == NULL ) {
104                 return NULL;
105         }
106
107         new = (LDAPControl *) malloc( sizeof(LDAPControl) );
108
109         if( new == NULL ) {
110                 return NULL;
111         }
112
113         if( c->ldctl_oid != NULL ) {
114                 new->ldctl_oid = strdup( c->ldctl_oid );
115
116                 if(new->ldctl_oid == NULL) {
117                         free( new );
118                         return NULL;
119                 }
120
121         } else {
122                 new->ldctl_oid = NULL;
123         }
124
125         if( c->ldctl_value.bv_len > 0 ) {
126                 new->ldctl_value.bv_val = (char *) malloc( c->ldctl_value.bv_len );
127
128                 if(new->ldctl_value.bv_val == NULL) {
129                         if(new->ldctl_oid != NULL) {
130                                 free( new->ldctl_oid );
131                         }
132                         free( new );
133                         return NULL;
134                 }
135                 
136                 SAFEMEMCPY( new->ldctl_value.bv_val, c->ldctl_value.bv_val, 
137                         c->ldctl_value.bv_len );
138
139                 new->ldctl_value.bv_len = c->ldctl_value.bv_len;
140
141         } else {
142                 new->ldctl_value.bv_len = 0;
143                 new->ldctl_value.bv_val = NULL;
144         }
145
146         new->ldctl_iscritical = c->ldctl_iscritical;
147         return new;
148 }
149
150 /* get the controls from the BerElement */
151 int ldap_get_ber_controls( BerElement *be, LDAPControl ***cp)
152 {
153         return LDAP_NOT_SUPPORTED;
154 }