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