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