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