xref: /illumos-gate/usr/src/lib/libldap5/sources/ldap/common/free.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * The contents of this file are subject to the Netscape Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/NPL/
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * The Original Code is Mozilla Communicator client code, released
13  * March 31, 1998.
14  *
15  * The Initial Developer of the Original Code is Netscape
16  * Communications Corporation. Portions created by Netscape are
17  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
18  * Rights Reserved.
19  *
20  * Contributor(s):
21  */
22 /*
23  *  Copyright (c) 1994 The Regents of the University of Michigan.
24  *  All rights reserved.
25  */
26 /*
27  *  free.c - some free routines are included here to avoid having to
28  *           link in lots of extra code when not using certain features
29  */
30 
31 #if 0
32 #ifndef lint
33 static char copyright[] = "@(#) Copyright (c) 1994 The Regents of the University of Michigan.\nAll rights reserved.\n";
34 #endif
35 #endif
36 
37 #include "ldap-int.h"
38 
39 void
40 LDAP_CALL
41 ldap_getfilter_free( LDAPFiltDesc *lfdp )
42 {
43     LDAPFiltList	*flp, *nextflp;
44     LDAPFiltInfo	*fip, *nextfip;
45 
46     if ( lfdp == NULL ) {
47 	return;
48     }
49 
50     for ( flp = lfdp->lfd_filtlist; flp != NULL; flp = nextflp ) {
51 	for ( fip = flp->lfl_ilist; fip != NULL; fip = nextfip ) {
52 	    nextfip = fip->lfi_next;
53 	    NSLDAPI_FREE( fip->lfi_filter );
54 	    NSLDAPI_FREE( fip->lfi_desc );
55 	    NSLDAPI_FREE( fip );
56 	}
57 	nextflp = flp->lfl_next;
58 	NSLDAPI_FREE( flp->lfl_pattern );
59 	NSLDAPI_FREE( flp->lfl_delims );
60 	NSLDAPI_FREE( flp->lfl_tag );
61 	NSLDAPI_FREE( flp );
62     }
63 
64     if ( lfdp->lfd_curvalcopy != NULL ) {
65 	NSLDAPI_FREE( lfdp->lfd_curvalcopy );
66     }
67     if ( lfdp->lfd_curvalwords != NULL ) {
68 	NSLDAPI_FREE( lfdp->lfd_curvalwords );
69     }
70     if ( lfdp->lfd_filtprefix != NULL ) {
71 	NSLDAPI_FREE( lfdp->lfd_filtprefix );
72     }
73     if ( lfdp->lfd_filtsuffix != NULL ) {
74 	NSLDAPI_FREE( lfdp->lfd_filtsuffix );
75     }
76 
77     NSLDAPI_FREE( lfdp );
78 }
79 
80 
81 /*
82  * free a null-terminated array of pointers to mod structures. the
83  * structures are freed, not the array itself, unless the freemods
84  * flag is set.
85  */
86 void
87 LDAP_CALL
88 ldap_mods_free( LDAPMod **mods, int freemods )
89 {
90 	int	i;
91 
92 	if ( !NSLDAPI_VALID_LDAPMOD_ARRAY( mods )) {
93 		return;
94 	}
95 
96 	for ( i = 0; mods[i] != NULL; i++ ) {
97 		if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
98 			if ( mods[i]->mod_bvalues != NULL ) {
99 				ber_bvecfree( mods[i]->mod_bvalues );
100 			}
101 		} else if ( mods[i]->mod_values != NULL ) {
102 			ldap_value_free( mods[i]->mod_values );
103 		}
104 		if ( mods[i]->mod_type != NULL ) {
105 			NSLDAPI_FREE( mods[i]->mod_type );
106 		}
107 		NSLDAPI_FREE( (char *) mods[i] );
108 	}
109 
110 	if ( freemods )
111 		NSLDAPI_FREE( (char *) mods );
112 }
113 
114 
115 /*
116  * ldap_memfree() is needed to ensure that memory allocated by the C runtime
117  * assocated with libldap is freed by the same runtime code.
118  */
119 void
120 LDAP_CALL
121 ldap_memfree( void *s )
122 {
123 	if ( s != NULL ) {
124 		NSLDAPI_FREE( s );
125 	}
126 }
127 
128 
129 /*
130  * ldap_ber_free() is just a cover for ber_free()
131  * ber_free() checks for ber == NULL, so we don't bother.
132  */
133 void
134 LDAP_CALL
135 ldap_ber_free( BerElement *ber, int freebuf )
136 {
137 	ber_free( ber, freebuf );
138 }
139