]> git.sur5r.net Git - openldap/blob - libraries/libldap/getvalues.c
slashpath from HEAD
[openldap] / libraries / libldap / getvalues.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/stdlib.h>
24
25 #include <ac/ctype.h>
26 #include <ac/socket.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "ldap-int.h"
31
32 char **
33 ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
34 {
35         BerElement      ber;
36         char            *attr;
37         int             found = 0;
38         char            **vals;
39
40         assert( ld != NULL );
41         assert( LDAP_VALID( ld ) );
42         assert( entry != NULL );
43         assert( target != NULL );
44
45 #ifdef NEW_LOGGING
46         LDAP_LOG ( OPERATION, ENTRY, "ldap_get_values\n", 0, 0, 0 );
47 #else
48         Debug( LDAP_DEBUG_TRACE, "ldap_get_values\n", 0, 0, 0 );
49 #endif
50
51         ber = *entry->lm_ber;
52
53         /* skip sequence, dn, sequence of, and snag the first attr */
54         if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) {
55                 ld->ld_errno = LDAP_DECODING_ERROR;
56                 return( NULL );
57         }
58
59         if ( strcasecmp( target, attr ) == 0 )
60                 found = 1;
61
62         /* break out on success, return out on error */
63         while ( ! found ) {
64                 LDAP_FREE(attr);
65                 attr = NULL;
66
67                 if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
68                         ld->ld_errno = LDAP_DECODING_ERROR;
69                         return( NULL );
70                 }
71
72                 if ( strcasecmp( target, attr ) == 0 )
73                         break;
74
75         }
76
77         LDAP_FREE(attr);
78         attr = NULL;
79
80         /* 
81          * if we get this far, we've found the attribute and are sitting
82          * just before the set of values.
83          */
84
85         if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
86                 ld->ld_errno = LDAP_DECODING_ERROR;
87                 return( NULL );
88         }
89
90         return( vals );
91 }
92
93 struct berval **
94 ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
95 {
96         BerElement      ber;
97         char            *attr;
98         int             found = 0;
99         struct berval   **vals;
100
101         assert( ld != NULL );
102         assert( LDAP_VALID( ld ) );
103         assert( entry != NULL );
104         assert( target != NULL );
105
106 #ifdef NEW_LOGGING
107         LDAP_LOG ( OPERATION, ENTRY, "ldap_get_values_len\n", 0, 0, 0 );
108 #else
109         Debug( LDAP_DEBUG_TRACE, "ldap_get_values_len\n", 0, 0, 0 );
110 #endif
111
112         ber = *entry->lm_ber;
113
114         /* skip sequence, dn, sequence of, and snag the first attr */
115         if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) {
116                 ld->ld_errno = LDAP_DECODING_ERROR;
117                 return( NULL );
118         }
119
120         if ( strcasecmp( target, attr ) == 0 )
121                 found = 1;
122
123         /* break out on success, return out on error */
124         while ( ! found ) {
125                 LDAP_FREE( attr );
126                 attr = NULL;
127
128                 if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
129                         ld->ld_errno = LDAP_DECODING_ERROR;
130                         return( NULL );
131                 }
132
133                 if ( strcasecmp( target, attr ) == 0 )
134                         break;
135         }
136
137         LDAP_FREE( attr );
138         attr = NULL;
139
140         /* 
141          * if we get this far, we've found the attribute and are sitting
142          * just before the set of values.
143          */
144
145         if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
146                 ld->ld_errno = LDAP_DECODING_ERROR;
147                 return( NULL );
148         }
149
150         return( vals );
151 }
152
153 int
154 ldap_count_values( char **vals )
155 {
156         int     i;
157
158         if ( vals == NULL )
159                 return( 0 );
160
161         for ( i = 0; vals[i] != NULL; i++ )
162                 ;       /* NULL */
163
164         return( i );
165 }
166
167 int
168 ldap_count_values_len( struct berval **vals )
169 {
170         return( ldap_count_values( (char **) vals ) );
171 }
172
173 void
174 ldap_value_free( char **vals )
175 {
176         LDAP_VFREE( vals );
177 }
178
179 void
180 ldap_value_free_len( struct berval **vals )
181 {
182         ber_bvecfree( vals );
183 }
184
185 char **
186 ldap_value_dup( char *const *vals )
187 {
188         char **new;
189         int i;
190
191         if( vals == NULL ) {
192                 return NULL;
193         }
194
195         for( i=0; vals[i]; i++ ) {
196                 ;   /* Count the number of values */
197         }
198
199         if( i == 0 ) {
200                 return NULL;
201         }
202
203         new = LDAP_MALLOC( (i+1)*sizeof(char *) );  /* Alloc array of pointers */
204         if( new == NULL ) {
205                 return NULL;
206         }
207
208         for( i=0; vals[i]; i++ ) {
209                 new[i] = LDAP_STRDUP( vals[i] );   /* Dup each value */
210                 if( new[i] == NULL ) {
211                         LDAP_VFREE( new );
212                         return NULL;
213                 }
214         }
215         new[i] = NULL;
216
217         return new;
218 }
219