]> git.sur5r.net Git - openldap/blob - libraries/libldap/getvalues.c
ee2962b7c34f725797605e021ea1e11e1d5b4304
[openldap] / libraries / libldap / getvalues.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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_get_values_ber( LDAP *ld, LDAPMessage *entry, BerElement *ber, BerVarray *bv )
149 {
150         int             rc = LDAP_SUCCESS;
151
152         assert( ld != NULL );
153         assert( LDAP_VALID( ld ) );
154         assert( entry != NULL );
155         assert( ber != NULL );
156         assert( bv != NULL );
157
158 #ifdef NEW_LOGGING
159         LDAP_LOG ( OPERATION, ENTRY, "ldap_get_values_ber\n", 0, 0, 0 );
160 #else
161         Debug( LDAP_DEBUG_TRACE, "ldap_get_values_ber\n", 0, 0, 0 );
162 #endif
163
164         /* get the array of vals */
165         if ( ber_scanf( ber, "W}" /* }}} */, bv ) == LBER_ERROR ) {
166                 rc = ld->ld_errno = LDAP_DECODING_ERROR;
167         }
168
169         return( rc );
170 }
171 int
172 ldap_count_values( char **vals )
173 {
174         int     i;
175
176         if ( vals == NULL )
177                 return( 0 );
178
179         for ( i = 0; vals[i] != NULL; i++ )
180                 ;       /* NULL */
181
182         return( i );
183 }
184
185 int
186 ldap_count_values_len( struct berval **vals )
187 {
188         return( ldap_count_values( (char **) vals ) );
189 }
190
191 void
192 ldap_value_free( char **vals )
193 {
194         LDAP_VFREE( vals );
195 }
196
197 void
198 ldap_value_free_len( struct berval **vals )
199 {
200         ber_bvecfree( vals );
201 }